gpt4 book ai didi

c++ - 将注册表项值读取到 std::string 的最简单方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:01 24 4
gpt4 key购买 nike

将注册表项值读取到 std::String 的最简单方法是什么?

说我有:

HKEY_LOCAL_MACHINE / SOFTWARE / MyApp / value1 = "some text"
HKEY_LOCAL_MACHINE / SOFTWARE / MyApp / value2 = "some more text"

如何快速将这些值获取到 std::string?

最佳答案

我有一些非常古老的代码,但它应该能给你一个好主意:

/**
* @param location The location of the registry key. For example "Software\\Bethesda Softworks\\Morrowind"
* @param name the name of the registry key, for example "Installed Path"
* @return the value of the key or an empty string if an error occured.
*/
std::string getRegKey(const std::string& location, const std::string& name){
HKEY key;
TCHAR value[1024];
DWORD bufLen = 1024*sizeof(TCHAR);
long ret;
ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, location.c_str(), 0, KEY_QUERY_VALUE, &key);
if( ret != ERROR_SUCCESS ){
return std::string();
}
ret = RegQueryValueExA(key, name.c_str(), 0, 0, (LPBYTE) value, &bufLen);
RegCloseKey(key);
if ( (ret != ERROR_SUCCESS) || (bufLen > 1024*sizeof(TCHAR)) ){
return std::string();
}
std::string stringValue = std::string(value, (size_t)bufLen - 1);
size_t i = stringValue.length();
while( i > 0 && stringValue[i-1] == '\0' ){
--i;
}
return stringValue.substr(0,i);
}

关于c++ - 将注册表项值读取到 std::string 的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1813647/

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