c++ - RegSetValueEx() 不改变键值

标签 c++ winapi

在我的应用程序中,当我第一次使用 RegSetValueEx() 设置键值时它有效,但是当我尝试使用相同的函数更改值时它不起作用,值保持不变。我做错了什么?

代码如下:

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 );
         }
    }

最佳答案

RegSetValueEx 接受要更改的键内的值的名称;不是 key 的名称。改为提供值名称;键名来自 HKEY 本身。

关于c++ - RegSetValueEx() 不改变键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770498/

相关文章:

.net - 长按电源键如何拒绝关机?

c++ - 是否可以检测指向成员函数的指针?

c++ - 对 map::erase() 的重载决议感到困惑

c++ - 自定义 malloc 实现

c++ - 如何在 Qt 中使用 Windows 网络 API?

windows - 我怎么知道下一个 SHGetFileInfo (SHGFI_ICON) 调用是否会很慢(对于 exe 文件等)?

c++ - 找出一个点是否在 voronoi 单元内

具有预定义容量的 C++ vector 性能

c++ - 如何检查 HANDLE 是否有效?

perl - 如何在 Windows 上为 Perl 脚本的输出文本着色?