1

My friend and I have written this code to modify the registry, but when we run it it doesn't seem to write the values to the registry but only check if they are set up correctly. If they where set correctly before the program doesn't give any pop ups. But if the registry keys are not matching then the program is supposed to write the correct registry values, but it doesn't right now and we have no clue why its not working...

here is the full code:

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
  • Can you please post a shorter snippet that points us to the problem you think you are having? Commented Aug 11, 2011 at 12:13
  • Stop doing this: char* buff = new char[1024];Allocate 1 buffer at the head of the function. char buffer[1024]; Then you don't need to de-allocate it. Commented Aug 11, 2011 at 12:21
  • 1
    in the first 30 lines of code, that's the part where it reads from the registry and should write to the registry and if something fails it gives the user a message box. The same code is then being reused in the for the DontShowUI registry key. Commented Aug 11, 2011 at 12:22
  • string literals that are ajacent in the code will autoatically be concatenated by the compiler. So use this to format your error messages. ie DoStuff("line1\n" /*Comment*/ "line2\n" /*Comment*/ "line3\n"); is the same as DoStuff("line1\nline2\nline3\n"); Commented Aug 11, 2011 at 12:30

2 Answers 2

1

Try setting the Reserved parameter in your call to RegSetValueEx() to 0, as instructed in MSDN, rather than the 2 you’ve used in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

that worked, thank you:) but on windows vista and server 2008 we got this error: api-ms-win-core-localregistry-l1-1-0.dll Maybe im missing a header? This is my header list: #include "stdafx.h" #include "ServerCheckerGUI.h" #include "Control.h" #include "ServerDoc.h" #include <mmsystem.h>
1

HKEY_LOCAL_MACHINE is a system location; as a "normal" user you don't have permission to write there. You'll need Administrator rights.

2 Comments

UAC was turned off, and we also tried running in compatibility mode with admin and that didn't help.
I suspect your writes are being virtualized to a non-HKLM location because of the Admin issue. I'm not sure compatibility mode really runs at app as admin or merely simulates it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.