|
-
June 30th, 2011, 10:00 AM
#1
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.
-
July 1st, 2011, 06:23 AM
#2
Re: A problem with RegSetValueEx
Check whether you have proper admin priviliges to modify registry.
-
July 1st, 2011, 06:51 AM
#3
Re: A problem with RegSetValueEx
 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?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 1st, 2011, 09:27 AM
#4
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!
-
July 1st, 2011, 09:41 AM
#5
Re: A problem with RegSetValueEx
 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
-
July 1st, 2011, 09:55 AM
#6
Re: A problem with RegSetValueEx
 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.
-
July 1st, 2011, 11:01 AM
#7
Re: A problem with RegSetValueEx
 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?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 1st, 2011, 11:06 AM
#8
Re: A problem with RegSetValueEx
 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
Last edited by Paul McKenzie; July 1st, 2011 at 11:19 AM.
-
July 7th, 2011, 12:23 PM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|