在C ++中将字符串(REG_SZ)值写入注册表 [英] Writing string (REG_SZ) values to the registry in C++

查看:318
本文介绍了在C ++中将字符串(REG_SZ)值写入注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大部分的代码写入一个值到Windows注册表,但是当我将路径更改为一个虚拟键和值,我设置为测试失败。我的代码如下:

  HKEY hKey; 
LPCTSTR sk = TEXT(SOFTWARE\TestSoftware);

LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,sk,0,KEY_ALL_ACCESS,& hKey);

if(openRes == ERROR_SUCCESS){
printf(Success opening key。);
} else {
printf(Error opening key。);
}

LPCTSTR值= TEXT(TestSoftwareKey);
LPCTSTR data =TestData\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(Write to Registry。);
}

LONG closeOut = RegCloseKey(hKey);

if(closeOut == ERROR_SUCCESS){
printf(Success closing key。);
} else {
printf(Error closing key。);
}

这三个测试都会产生错误状态。



令我困惑的部分是,我能够在将其指向注册表的其他部分时运行此代码。任何想法?



感谢,
brian

解决方案

我觉得很蠢。解决方案是,需要正确转义字符串中的斜杠,如下所示:

  LPCTSTR sk = TEXT(SOFTWARE\\\ \\ TESTSoftware);希望有人发现这有用... 

I've got most of the code for writing a value to the windows registry, however when I change the path to a dummy key and value that I've set up for testing it fails. My code is below:

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

    LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey);

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

    LPCTSTR value = TEXT("TestSoftwareKey");
    LPCTSTR data = "TestData\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.");
    }

    LONG closeOut = RegCloseKey(hKey);

    if (closeOut == ERROR_SUCCESS) {
    	printf("Success closing key.");
    } else {
    	printf("Error closing key.");
    }

All three tests yield error statuses.

The part that confuses me is that I was able to run this code when pointing it at other portions of the registry. Any ideas?

thanks, brian

解决方案

I feel silly. The solution is that need to properly escape the slash in the string as follows:

LPCTSTR sk = TEXT("SOFTWARE\\TestSoftware");

Hopefully someone finds this useful...

这篇关于在C ++中将字符串(REG_SZ)值写入注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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