使用c ++创建一个新的Windows注册表项 [英] Create a new windows registry key using c++

查看:84
本文介绍了使用c ++创建一个新的Windows注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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\0";

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

我可以使用非常类似的代码片段(基本上用RegOpenKeyEx替换RegCreateKeyEx)。我想象一个或多个参数我传递到RegCreateKeyEx是造成麻烦。我真的不知道哪里的东西可能被污染,因为所有的错误代码,我被困住显示成功。作为参考,这里是RegCreateKeyEx的函数签名:

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:

/*
 * 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
	);
 */

任何想法都很棒!

感谢,
brian

thanks, brian

推荐答案

我已经编译了我自己的个人函数库。其中的一部分完全涉及注册表访问,请参阅CreateRegistryKey函数注册表.Cpp 文件。

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.Cpp file.

如果您有兴趣,可以选择整个图书馆

If your interested, You can grab the entire library here.

这篇关于使用c ++创建一个新的Windows注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆