A problem with RegSetValueEx
I called RegSetValueEx to modify a value. RegSetValueEx returns ERROR_SUCCESS but it isn't able to modify the value. I don't have such a problem in my machine. It happens when I tried to run it in another machine. I wonder how I can find out why it happens? Thanks.
Re: A problem with RegSetValueEx
Check whether you have proper admin priviliges to modify registry.
Re: A problem with RegSetValueEx
Quote:
Originally Posted by
resumurof
Check whether you have proper admin priviliges to modify registry.
If he didn't, RegSetValueEx should have failed...
Can we see the code?
And how do you check for that new value?
Re: A problem with RegSetValueEx
Here is the code snippet,
Code:
// A.cpp
HKEY hKey;
DWORD dwShowLocation = 0;
DWORD dwSize;
if(RegCreateKeyEx(
HKEY_CURRENT_USER,
_T("Software\\Location"),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL) == ERROR_SUCCESS)
{
dwSize = sizeof(dwShowLocation);
RegSetValueEx(hKey, _T("ShowLocation"), 0, REG_DWORD, (LPBYTE)&dwShowLocation, dwSize);
RegCloseKey(hKey);
}
Code:
// B.cpp
HKEY hKey;
DWORD dwShowLocation = 1;
DWORD dwSize;
dwSize = sizeof(dwShowLocation);
if(RegOpenKeyEx(
HKEY_CURRENT_USER,
_T("Software\\Location"),
0,
KEY_SET_VALUE,
&hKey) == ERROR_SUCCESS)
{
RegSetValueEx(hKey, _T("ShowLocation"), 0, REG_DWORD, (LPBYTE)&dwShowLocation, dwSize);
RegCloseKey(hKey);
}
Actually, it is success to create the key and set the value in A.cpp and then when B.cpp is executed, RegSetValueEx returns ERROR_SUCCESS but it wasn't able to modify the value. Any thing wrong? Thanks for your help!
Re: A problem with RegSetValueEx
Quote:
Originally Posted by
LarryChen
RegSetValueEx returns ERROR_SUCCESS
So why doesn't your code show that you're checking for this value? You should be convincing us that you're really checking these return values by posting code that shows this.
Regards,
Paul McKenzie
Re: A problem with RegSetValueEx
Quote:
Originally Posted by
Paul McKenzie
So why doesn't your code show that you're checking for this value? You should be convincing us that you're really checking these return values by posting code that shows this.
Regards,
Paul McKenzie
Actually, B.cpp is like this,
Code:
// B.cpp
HKEY hKey;
DWORD dwShowLocation = 1;
DWORD dwSize;
dwSize = sizeof(dwShowLocation);
if(RegOpenKeyEx(
HKEY_CURRENT_USER,
_T("Software\\Location"),
0,
KEY_SET_VALUE,
&hKey) == ERROR_SUCCESS)
{
if(RegSetValueEx(hKey, _T("ShowLocation"), 0, REG_DWORD, (LPBYTE)&dwShowLocation, dwSize) == ERROR_SUCCESS)
MessageBox(_T("suc"));
else
MessageBox(_T("fail"));
RegCloseKey(hKey);
}
The message box displaying "suc" shows up when B.cpp is executed. So I am sure RegSetValueEx returns ERROR_SCUCESS. But if I check the registry, the value "ShowLocation" is still 0 instead of 1.
Re: A problem with RegSetValueEx
Quote:
Originally Posted by
LarryChen
The message box displaying "suc" shows up when B.cpp is executed. So I am sure RegSetValueEx returns ERROR_SCUCESS. But if I check the registry, the value "ShowLocation" is still 0 instead of 1.
When exactly do you check your registry?
After the call to RegCloseKey()? And before the next time you set it to 0 in A.cpp?
Re: A problem with RegSetValueEx
Quote:
Originally Posted by
LarryChen
But if I check the registry, the value "ShowLocation" is still 0 instead of 1.
Instead of manually checking the registry which is prone to human error, why not write the code to check if the key has been set? Right after you write the key, open the key again using the registry functions and check the value that way.
Since you have code to prove what your doing is actually happening, it is far more reliable to write the code to do these checks so that (again from my previous post) it will convince us and yourself that you're doing things correctly. If the registry functions returns that the key has been set, then the next step is to figure out why when you look at the registry, you claim that the key is not set.
Regards,
Paul McKenzie
Re: A problem with RegSetValueEx
It turns out that after I call RegSetValueEx to set ShowLocation to 1, RegSetValueEx is called again to set ShowLocation to 0. That is what happened so it looks like RegSetValueEx failed to set value. Thanks.