gpt4 book ai didi

c++ - RegQueryValueEx - 将什么代码添加到此函数以显示 ERROR_SUCCESS

转载 作者:行者123 更新时间:2023-11-30 02:12:41 25 4
gpt4 key购买 nike

向这个函数添加什么代码才能正常工作? (ERROR_SUCCESS)

我有代码,可以检查注册表中的值。

在函数 RegQueryValueEx 中是错误。当 oldValuenewValue 长几个字母时,函数显示 ERROR_MORE_DATA,但我想要 ERROR_SUCCESS

将什么代码添加到此函数来执行此操作?

void function(string newValue, string key, string name)

{

// string key - key in registry, ie Myapp\\Options
// string name - name in registry
// string newValue - data in REG_SZ


string oldValue;
DWORD keytype = REG_SZ;
HKEY keyHandle;
DWORD size = sizeof(string);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(),0L,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
{


LONG isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE)&oldValue, &size);
if(isgood == ERROR_MORE_DATA)
{
cout << "Error more data\n";
}
if(isgood == ERROR_SUCCESS)
{
cout << "Old data is " << oldValue.c_str() << endl;
cout << "New data is " << newValue.c_str() << endl;
if(strcmp(newValue.c_str(), oldValue.c_str()) != 0) // compare 2 strings, if
{
cout << "String 1 and string 2 are different";

}
else
{
cout << "String 1 and string 2 are the same";
}
}
if(isgood == ERROR_FILE_NOT_FOUND)
{
cout << "Name in registry not found!";
}
}

}

最佳答案

ERROR_MORE_DATA 表示需要传入更大的字符串缓冲区。您需要使用的典型模式是调用一次以获取大小,然后分配一个适当大小的缓冲区,然后再次调用。或者,您可以猜测一个大小,传入该大小的缓冲区,并在返回 ERROR_MORE_DATA 时增加大小。

顺便说一句,您还错误地计算了大小。而且您没有关闭注册表项。而且您不准备支持在 unicode 或非 unicode 模式下进行编译。

这里有一些修改后的代码可以解决这些问题。

#include <string>
#include <vector>
#include <iostream>

#include <windows.h>
using namespace std;

namespace std
{
#ifdef _UNICODE
#define tcout wcout
#define tcin wcin
typedef wstring tstring;
#else
#define tcout cout
#define tcin cin
typedef string tstring;
#endif
};

void function(tstring newValue, tstring key, tstring name)
{
// string key - key in registry, ie Myapp\\Options
// string name - name in registry
// string newValue - data in REG_SZ
HKEY keyHandle;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(),0L,KEY_ALL_ACCESS,&keyHandle) == ERROR_SUCCESS)
{
DWORD size = 500; // initial size
vector<TCHAR> buf(size);
tstring oldValue;
DWORD keytype = REG_SZ;

LONG isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE) &buf[0], &size);
if(isgood == ERROR_SUCCESS)
{
oldValue.assign (&buf[0], size);
}
else if(isgood == ERROR_MORE_DATA)
{
buf.reserve (size); // expand to however large we need
isgood = RegQueryValueEx(keyHandle, name.c_str(), 0, &keytype, (LPBYTE)&buf[0], &size);
if(isgood == ERROR_SUCCESS)
oldValue.assign (&buf[0], size);
}
RegCloseKey (keyHandle); // remember to close this!
if(isgood == ERROR_SUCCESS)
{
tcout << _T("Old data is ") << oldValue << endl;
tcout << _T("New data is ") << newValue << endl;
if(newValue.compare(oldValue) != 0) // compare 2 strings, if
{
tcout << _T("String 1 and string 2 are different");

}
else
{
tcout << _T("String 1 and string 2 are the same");
}
}
if(isgood == ERROR_FILE_NOT_FOUND)
{
tcout << _T("Name in registry not found!");
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
tstring val;
function (val, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"), _T("CommonFilesDir"));
return 0;
}

关于c++ - RegQueryValueEx - 将什么代码添加到此函数以显示 ERROR_SUCCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1514241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com