1

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

这是完整的代码:

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);
    }
}
4

2 回答 2

1

HKEY_LOCAL_MACHINE是系统位置;作为“普通”用户,您无权在那里写信。您需要管理员权限。

于 2011-08-11T12:20:48.263 回答
1

尝试按照 MSDN中的说明在对toReserved的调用中设置参数,而不是在代码中使用的参数。RegSetValueEx()02

于 2011-08-11T13:49:08.227 回答