StudentX
StudentX

Reputation: 2323

RegSetValueEx() not changing the key value

In my application, when I first set the key-value using RegSetValueEx() it works, but when I try to change the value using the same function it doesn't work, the value remains same. What am I doing wrong ?

Here is the code :

SetSZValue( "MyAppData", "versionInfo", "1.0.0" );


    HKEY CreateKey( string regkey )
    {
         HKEY hKey ;
         DWORD disValue ;

         char msg[512] ;

         string _key = "HKEY_LOCAL_MACHINE\\" ;
                _key += regkey ;

         LONG retValue = RegCreateKeyEx( HKEY_LOCAL_MACHINE, regkey.c_str(), 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, &disValue ) ;
         if (retValue != ERROR_SUCCESS)
         {
             FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, &msg[0], 512, 0 ) ;
             MessageBox( 0, &msg[0], "CreateKey", MB_OK | MB_ICONEXCLAMATION );
         }

         return hKey ;
    }



    void SetSZValue( string regkey, string keyName, string keyValue )
    {
         HKEY hKey;
         DWORD disValue;

         char msg[512];

         hKey = CreateKey(regkey);
         if (hKey)
         {
            LONG retValue = RegSetValueEx( hKey, keyName.c_str(), 0, REG_SZ, ( const BYTE* )( keyValue.c_str() ), keyValue.size()+1 );
            if ( retValue != ERROR_SUCCESS )
            {
                FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, &msg[0], 512, 0 );
                  MessageBox( 0, &msg[0], "SetSZValue", MB_OK | MB_ICONEXCLAMATION );
            }

                RegCloseKey ( hKey );
         }
    }

Upvotes: 1

Views: 2439

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597101

Is your app a 32-bit process running on a 64-bit Windows version? If so, does your app have a UAC manifest with a "requestedExecutionLevel" value in it? If not, your key may be getting virtualized to another section of the Registry and you are simply not looking in the right place. Registry Virtualization is a feature of WOW64 so legacy 32-bit and 64-bit processes do not step over each other in the Registry. You should install SysInternals Process Monitor, it will show you which keys and values your app is actually accessing.

Upvotes: 1

Billy ONeal
Billy ONeal

Reputation: 106589

RegSetValueEx accepts the name of the value inside the key to change; not the name of the key. Supply the value name instead; the key name comes from the HKEY itself.

Upvotes: 1

Related Questions