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

时间:2011-08-11 12:07:22

标签: c++ winapi

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

这是完整的代码:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

尝试将您调用ReservedRegSetValueEx()的{​​{1}}参数设置为instructed in MSDN,而不是您在代码中使用的0