I’m trying to set a DWORD value in the registry. I made it work with a text value, but now I want to set another value with a numeric one(0). But it doesnt write it.
This is my code:
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\pager", 0, KEY_ALL_ACCESS, &hKey);
RegSetValueEx(hKey, TEXT("Save"), 0, REG_SZ, (const BYTE*)0x00, sizeof(DWORD));
RegCloseKey(hKey);
PS: the key already exist with the value 1 so I’m trying to overide it with the value 0(I’m not creating a new one).
The biggest error is in
(const BYTE*)0x00: you are casting 0x00 to aBYTE *, which means that basically you are passing aNULLpointer. Instead, you should create aDWORDvariable, put the value you want to store in the registry in it and pass a pointer to it instead of that0x00.Also, you must change
REG_SZtoREG_DWORDif you want to store aDWORDvalue, otherwise theDWORDwill be interpreted as a (somewhat strange) string.But, most importantly, you should really check the return values of these functions: now you’re just “hoping” they work, ignoring any failure and continuing with the instruction flow, which can lead to unexpected situations.
If you checked the error codes you would have noticed immediately that it is the
RegSetValueExfunction that fails, and the error code may have been something like “invalid parameter”, that would have pointed you in the right direction.