使用 C++ 创建一个新的 Windows 注册表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/508614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:43:47  来源:igfitidea点击:

Create a new windows registry key using c++

c++registry

提问by Brian Sweeney

I'm trying to create a new registry key in the windows registry using C++. Here is the code I have so far:

我正在尝试使用 C++ 在 Windows 注册表中创建一个新的注册表项。这是我到目前为止的代码:

HKEY hKey;
    LPCTSTR sk = TEXT("SOFTWARE\OtherTestSoftware");

    LONG openRes = RegCreateKeyEx(
            HKEY_LOCAL_MACHINE,
            sk,
            0,
            NULL,
            REG_OPTION_BACKUP_RESTORE,
            KEY_ALL_ACCESS,
            NULL,
            &hKey,
            NULL);

    if (openRes==ERROR_SUCCESS) {
        printf("Success creating key.");
    } else {
        printf("Error creating key.");
    }

    LPCTSTR value = TEXT("OtherTestSoftwareKey");
    LPCTSTR data = "OtherTestData
/*
 * LONG WINAPI RegCreateKeyEx(
      __in        HKEY hKey,
      __in        LPCTSTR lpSubKey,
      __reserved  DWORD Reserved,
      __in_opt    LPTSTR lpClass,
      __in        DWORD dwOptions,
      __in        REGSAM samDesired,
      __in_opt    LPSECURITY_ATTRIBUTES lpSecurityAttributes,
      __out       PHKEY phkResult,
      __out_opt   LPDWORD lpdwDisposition
    );
 */
"; LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1); if (setRes == ERROR_SUCCESS) { printf("Success writing to Registry."); } else { printf("Error writing to Registry."); } //RegDeleteKey(hKey, sk); LONG closeOut = RegCloseKey(hKey); if (closeOut == ERROR_SUCCESS) { printf("Success closing key."); } else { printf("Error closing key."); }

I'm able to successfully open an existing key using a very similar code snippet (basically replace RegCreateKeyEx with RegOpenKeyEx). I would imagine that one or more of the arguments I'm passing into RegCreateKeyEx is causing the trouble. I'm honestly not sure where things might be getting fouled up since all of the error codes i've trapped show success. For reference, here is the function signature for RegCreateKeyEx:

我能够使用非常相似的代码片段成功打开现有密钥(基本上用 RegOpenKeyEx 替换 RegCreateKeyEx)。我想我传递给 RegCreateKeyEx 的一个或多个参数会导致问题。老实说,我不确定事情可能在哪里出错,因为我捕获的所有错误代码都显示成功。作为参考,这里是 RegCreateKeyEx 的函数签名:

LPCTSTR data = "OtherTestData
LPCTSTR data = TEXT("OtherTestData
LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ,
    (LPBYTE)data, _tcslen(data)+1);
");
";

Any thoughts would be great!

任何想法都会很棒!

thanks, brian

谢谢,布赖恩

回答by NTDLS

I've been compiling my own personal Function Library for years. One part of this deals entirely with registry access, see the CreateRegistryKey function the Registry.Cppfile.

多年来,我一直在编译自己的个人函数库。其中一部分完全涉及注册表访问,请参阅Registry.Cpp文件中的 CreateRegistryKey 函数。

If you are interested, you can grab the entire library here.

如果你有兴趣,你可以在这里获取整个图书馆

回答by ChrisN

As already mentioned, you've specified the REG_OPTION_BACKUP_RESTOREoption in the call to RegCreateKeyEx, which means that you're opening the key in order to perform a backup or restore. Ordinarily, you would use REG_OPTION_NON_VOLATILEinstead.

如前所述,您已REG_OPTION_BACKUP_RESTORE在对 的调用中指定了选项RegCreateKeyEx,这意味着您正在打开密钥以执行备份或恢复。通常,您会使用它REG_OPTION_NON_VOLATILE来代替。

What operating system are you running? In Windows 2000/XP, the HKEY_LOCAL_MACHINEregistry hive is not writeable by non-administrator users, so RegCreateKeyExwill fail with an access denied error (error 5). This also applies to Vista, if your application has a requestedExecutionLevelentry in its manifest. If you're running Vista, and your application doesn't specify a requestedExecutionLevel(or if it doesn't have a manifest at all), access to HKEY_LOCAL_MACHINEwill be virtualised, so RegCreateKeyExshould succeed. See Registry Virtualization in Windows Vistain MSDN for more details.

你运行的是什么操作系统?在 Windows 2000/XP 中,HKEY_LOCAL_MACHINE非管理员用户无法写入注册表配置单元,因此RegCreateKeyEx会因访问被拒绝错误(错误 5)而失败。这也适用于 Vista,如果您的应用程序requestedExecutionLevel在其清单中有一个条目。如果您运行的是 Vista,并且您的应用程序没有指定 a requestedExecutionLevel(或者根本没有清单),则访问HKEY_LOCAL_MACHINE将被虚拟化,因此RegCreateKeyEx应该会成功。有关更多详细信息,请参阅MSDN中的 Windows Vista中的注册表虚拟化

There are some more problems with the code you've posted, which will only become apparent if you compile your project with UNICODEdefined. This line:

您发布的代码还有一些问题,只有在使用UNICODE定义的方式编译项目时才会变得明显。这一行:

LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ,
    (LPBYTE)data, (_tcslen(data)+1) * sizeof(TCHAR));

should be

应该

##代码##

and this line:

和这一行:

##代码##

should be:

应该:

##代码##

because the cbDataparameter in RegSetValueExis the length of the data in bytes, not characters.

因为cbData参数 inRegSetValueEx是以字节为单位的数据长度,而不是字符。

I hope this helps!

我希望这有帮助!

回答by Greg Hewgill

The first clue is your use of REG_OPTION_BACKUP_RESTORE. You probably don't want to use that flag, as I believe it requires a special "backup" privilege that you need to enable beforehand. Normal applications won't want to do that.

第一个线索是您对REG_OPTION_BACKUP_RESTORE. 您可能不想使用该标志,因为我相信它需要您需要事先启用的特殊“备份”权限。正常的应用程序不会想要这样做。

回答by Sujay Ghosh

Probably this is the reason why you can cannot create a new key, with your code.

可能这就是您无法使用代码创建新密钥原因

These links might be of help.

这些链接可能会有所帮助。

http://www.codeguru.com/forum/archive/index.php/t-378884.html
http://www.codeguru.com/forum/archive/index.php/t-275250.html

http://www.codeguru.com/forum/archive/index.php/t-378884.html
http://www.codeguru.com/forum/archive/index.php/t-275250.html

As a sidenote, always try GetLastError() to get the error message.

作为旁注,请始终尝试 GetLastError() 以获取错误消息。

I have not tested either of them.

我没有测试过它们中的任何一个。