gpt4 book ai didi

C++ 写入注册表在我的代码中不起作用?

转载 作者:行者123 更新时间:2023-11-30 04:33:13 25 4
gpt4 key购买 nike

我和我的 friend 已经编写了这段代码来修改注册表,但是当我们运行它时,它似乎并没有将值写入注册表,而是只检查它们是否设置正确。如果他们在程序不提供任何弹出窗口之前设置正确。但是,如果注册表项不匹配,那么程序应该会写入正确的注册表值,但现在并没有,我们不知道为什么它不起作用......

完整代码如下:

if (version.dwMajorVersion >= 6)
{
HKEY ErrMode;
LONG res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\ControlSet001\\Control\\Windows", 0, KEY_READ | KEY_WRITE, &ErrMode);
if (res == ERROR_SUCCESS)
{
DWORD ErrorMode;
DWORD DRlen = sizeof(DWORD);
DWORD SUlen = sizeof(DWORD);
if (RegQueryValueEx(ErrMode, "ErrorMode", 0, NULL, (LPBYTE)&ErrorMode, &DRlen) == ERROR_SUCCESS)
{
if (ErrorMode != 2) // any of errormode is non-2
{
if (MessageBox(NULL, "Windows Error Reporting is still turned on.\r\nThis program may not work correctly with reporting turned on. Shall I disable it?\r\nNote:The readme explains how you can disable and enable Error Reporting under Windows)","Windows Error Reporting:",
MB_YESNO | MB_ICONQUESTION | MB_APPLMODAL) == IDYES)
{
ErrorMode = 2; // change local
DRlen = sizeof(DWORD);
SUlen = sizeof(DWORD);
if (RegSetValueEx(ErrMode,"ErrorMode",2, REG_DWORD, (LPBYTE)&ErrorMode, DRlen) != ERROR_SUCCESS)
{
char* buff = new char[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0);
MessageBox(NULL, buff,"Error...", MB_OK | MB_ICONEXCLAMATION);
delete[] buff;
ok = false;
}
}
else
ok = false;
}
}
if (RegCloseKey(ErrMode) != ERROR_SUCCESS)
MessageBox(NULL, "Could not close Registry Key handle.","Error...",MB_OK | MB_ICONEXCLAMATION);
}
else
{
char* buff = new char[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0);
CString message;
message.Format("Could not read from registry. Make sure you have either turned off error reporting\r\nor that you run this program from a privileged account.\r\nError: %s",buff);
MessageBox(NULL, message.GetBuffer(),"Error...",MB_OK | MB_ICONEXCLAMATION);
delete[] buff;
ok = false;
}
HKEY DontSUI;
res = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\Windows Error Reporting", 0, KEY_READ | KEY_WRITE, &DontSUI);
if (res == ERROR_SUCCESS)
{
DWORD DontShowUI;
DWORD DRlen = sizeof(DWORD);
DWORD SUlen = sizeof(DWORD);
if (RegQueryValueEx(DontSUI, "DontShowUI", 0, NULL, (LPBYTE)&DontShowUI, &DRlen) == ERROR_SUCCESS)
{
if (DontShowUI != 1) // any of DontShowUI is non 1
{
if (MessageBox(NULL, "Windows Error Reporting is still turned on.\r\nThis program may not work correctly with reporting turned on. Shall I disable it?\r\nNote:The readme explains how you can disable and enable Error Reporting under Windows)","Windows Error Reporting:",
MB_YESNO | MB_ICONQUESTION | MB_APPLMODAL) == IDYES)
{
DontShowUI = 1; // change local
DRlen = sizeof(DWORD);
SUlen = sizeof(DWORD);
if (RegSetValueEx(DontSUI,"DontShowUI",1, REG_DWORD, (LPBYTE)&DontShowUI, DRlen) != ERROR_SUCCESS)
{
char* buff = new char[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0);
MessageBox(NULL, buff,"Error...", MB_OK | MB_ICONEXCLAMATION);
delete[] buff;
ok = false;
}
}
else
ok = false;
}
}
if (RegCloseKey(DontSUI) != ERROR_SUCCESS)
MessageBox(NULL, "Could not close Registry Key handle.","Error...",MB_OK | MB_ICONEXCLAMATION);
}
else
{
char* buff = new char[1024];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,res,0,buff,1024,0);
CString message;
message.Format("Could not read from registry. Make sure you have either turned off error reporting\r\nor that you run this program from a privileged account.\r\nError: %s",buff);
MessageBox(NULL, message.GetBuffer(),"Error...",MB_OK | MB_ICONEXCLAMATION);
delete[] buff;
ok = false;
}
if (!ok)
{
MessageBox(NULL,"Windows Error Reporting may still be active.\r\nThis program may not function correctly.","Warning",MB_ICONWARNING |MB_OK);
}
}

最佳答案

尝试将 RegSetValueEx() 调用中的 Reserved 参数设置为 0,如 instructed in MSDN ,而不是您在代码中使用的 2

关于C++ 写入注册表在我的代码中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7025556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com