Question

I am trying to write a DWORD value to the registry programmatically in C++.

I've done a bit of searching and I have found that this question has been asked before. I've tried to follow their solution but have come up with a really frustrating issue which, as far as I know, have not been addressed by their solution.

This is my code:

HKEY hKey;
LPCWSTR sKeyPath;
int iResult;

sKeyPath = L"Software\\ABI\\";
iResult = RegOpenKeyEx(HKEY_CURRENT_USER, sKeyPath, NULL, KEY_ALL_ACCESS, &hKey);
DWORD value = 0x00000003;
iResult = RegSetValueEx(hKey, L"Test", NULL, REG_DWORD, (const BYTE*)value, sizeof(value));
RegCloseKey(hKey);

I've done some basic debugging and found that the value of iResult is 998 after I call RegSetValueEx. I am sure that this key is present in the windows registry because I created it manually with regedit.exe for testing purposes. The value of the DWORD "Test" is initially 0x00000009 and is unchanged after I run my program.

I am not sure where I am wrong.

Any help would be appreciated.

P.S. I've not managed to find any helpful site on the net for error 998. The only reference I found mentions that that's the worst error you can get when handling registry.

P.P.S. By the way, I'm running this program on Windows 8. I don't think that changes anything but I've had experiences with Windows 8 having some weird security issues before.

Was it helpful?

Solution

You need to pass the address of value:

iResult = RegSetValueEx(hKey,
                        L"Test",
                        NULL,
                        REG_DWORD,
                        (const BYTE*)&value, // Change made here.
                        sizeof(value));

The error code 998 means:

Invalid access to memory location.

When the address of value is not passed its actual value (3) is being used as a memory address, causing the failure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top