CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    Join Date
    Jan 2007
    Posts
    143

    Re: A problem with RegSetValueEx

    Check whether you have proper admin priviliges to modify registry.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: A problem with RegSetValueEx

    Quote Originally Posted by resumurof View Post
    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...

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    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!

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A problem with RegSetValueEx

    Quote Originally Posted by LarryChen View Post
    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

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A problem with RegSetValueEx

    Quote Originally Posted by Paul McKenzie View Post
    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.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: A problem with RegSetValueEx

    Quote Originally Posted by LarryChen View Post
    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...

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A problem with RegSetValueEx

    Quote Originally Posted by LarryChen View Post
    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.

  9. #9
    Join Date
    Jul 2005
    Posts
    1,030

    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
  •  





Click Here to Expand Forum to Full Width

Featured