C++ 将字符串(REG#u SZ)值写入C+中的注册表+;
我已经有了将值写入windows注册表的大部分代码,但是当我将路径更改为一个伪键时,我为测试它而设置的值失败了。我的代码如下:C++ 将字符串(REG#u SZ)值写入C+中的注册表+;,c++,registry,C++,Registry,我已经有了将值写入windows注册表的大部分代码,但是当我将路径更改为一个伪键时,我为测试它而设置的值失败了。我的代码如下: HKEY hKey; LPCTSTR sk = TEXT("SOFTWARE\TestSoftware"); LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey); if (openRes==ERROR_SUCCESS) {
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.");
}
所有三个测试都会产生错误状态
让我困惑的是,当我将代码指向注册表的其他部分时,我能够运行这段代码。有什么想法吗
谢谢,
布莱恩,我觉得自己很傻。解决方案是需要按如下方式正确转义字符串中的斜杠:
LPCTSTR sk = TEXT("SOFTWARE\\TestSoftware");
希望有人会觉得这很有用……HKEY OpenKey(HKEY hRootKey,char*strKey)
HKEY OpenKey(HKEY hRootKey, char* strKey)
{
HKEY hKey;
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
if (nError==ERROR_FILE_NOT_FOUND)
{
cout << "Creating registry key: " << strKey << endl;
nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
}
if (nError)
cout << "Error: " << nError << " Could not find or create " << strKey << endl;
return hKey;
}
void SetintVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
if (nError)
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
DWORD GetintVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data;
DWORD size = sizeof(data);
DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
if (nError==ERROR_FILE_NOT_FOUND)
data = 0;
SetVal() is called.
else if (nError)
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
return data;
}
BOOL SetcharVal(HKEY Key,char* subkey,char* StringName,char* Stringdata)
{
HKEY hKey = OpenKey(Key,subkey);
LONG openRes = RegOpenKeyEx(Key, subkey, 0, KEY_ALL_ACCESS , &hKey);
if (openRes==ERROR_SUCCESS) {
} else {
printf("Error opening key.");
}
LONG setRes = RegSetValueEx (hKey, StringName, 0, REG_SZ, (LPBYTE)Stringdata, strlen(Stringdata)+1);
if (setRes == ERROR_SUCCESS) {
} else {
printf("Error writing to Registry.");
}
LONG closeOut = RegCloseKey(hKey);
if (closeOut == ERROR_SUCCESS) {
} else {
printf("Error closing key.");
}
}
char* GetCharVal(HKEY Key,char* subkey,char* StringName)
{
DWORD dwType = REG_SZ;
HKEY hKey = 0;
char value[1024];
DWORD value_length = 1024;
RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey);
RegQueryValueEx(hKey, StringName, NULL, &dwType, (LPBYTE)&value, &value_length);
RegCloseKey(hKey);
return value;
}
{
HKEY HKEY;
LONG nError=RegOpenKeyEx(hRootKey、strKey、NULL、KEY\u ALL\u ACCESS和hKey);
如果(错误==找不到错误文件)
{
cout对于UNICODE
环境:
//Writing to registry
HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows");
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("Sample");
WCHAR path[80] = TEXT("C:\\samples\\app.exe");
LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)path, sizeof(path));
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.");
}
请注意,RegSetValueEx
函数以字节(而非字符)为单位获取字符串长度因此,您最好使用<代码> StutcCbLimeS/<代码>,而不是<代码> StLeN<代码>。请直接编辑您以前的代码,而不是发布第二个问题的答案。请考虑编辑您的答案,包括解释您的代码是如何解决手头的问题的。或者,从VC++ 2013开始:< COD。e> LR“(“软件\测试软件”)”
这些被称为原始字符串文字。虽然sizeof(path)
肯定足够大,但我相信您想要的不是sizeof(path)
而是wcslen(path)*sizeof(WCHAR)
。这也足以容纳整个字符串。在我的代码中,我将LPCWSTR I\u lpStringValue
传递给我的函数,并使用wcslen(I\u lpStringValue)*sizeof(wchar\u t)
用于RegSetValueEx
函数的cbData
参数。它似乎保存了所有字符。如果我有误解,请随时更正。