Reputation: 3457
I need to programmatically disable Windows Error Reporting in my C++ application. To do this, I want to edit the Windows Registry and write two values to it.
The following code works perfectly on my 32 bit Windows 7 machine:
#include <stdio.h>
#include <Windows.h>
void DisableWER()
{
HKEY key;
printf("%d\n", RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\Windows Error Reporting\\"), &key));
DWORD val = 1;
printf("%d\n", RegSetValueEx(key, TEXT("Disable"), 0, REG_DWORD, (const BYTE*)&val, sizeof(val)));
RegCloseKey(key);
}
int main()
{
DisableWER();
printf("%d\n", GetLastError());
getchar();
}
Both functions succeed (return ERROR_SUCCESS), GetLastError() prints 0, and the required value is set in the registry.
On my VPS the program output is the same, but the registry isn't actually modified - simply nothing happens. I can set the value manually using regedit, so I presume it's not a privilege related problem. The VPS runs Windows Server 2008 R2, 64-bit.
What could be the reason? Is it possible that the VPS host's configuration is interfering with the Windows API?
Upvotes: 0
Views: 80
Reputation: 942518
If you get ERROR_SUCCESS then you know that the function succeeded and that the registry was in fact updated. Do not use GetLastError() at all, the registry api doesn't use it.
You are simply looking in the wrong place for the change, a 32-bit process gets redirected on a 64-bit operating system. So first place to look is under the HKLM\SOFTWARE\Wow6432Node registry key.
Build your program to target x64 to avoid this redirection or use the KEY_WOW64_64KEY option in RegOpenKeyEx(). Your program must run elevated to be able to modify this key, use the appropriate manifest entry (requireAdministrator) to activate the UAC prompt. The SysInternals' Process Monitor utility is excellent to troubleshoot registry access problems.
Upvotes: 1