网上查了一下“c++ 注册表”,一搜一大片,但基本上都不能用,我就自己花时间,整合了网上N多个文章的代码,写了一个真正能用的,费话不说了,直接上代码:
#include "atlstr.h" 注册表操作中需要引用的头文件及库文件
#include "shlwapi.h"
#pragma comment(lib, "shlwapi.lib")
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, "");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = wcstombs(NULL, _Source, 0) + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
};
std::wstring s2ws(const std::string& s)//可移植版本 string => wstring
{
std::string curLocale = setlocale(LC_ALL, "");
const char* _Source = s.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
std::wstring result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
};
std::string GetRegValue(HKEY hKey, LPCTSTR data_Set, const std::string& strKey)
{
std::string strValue("");
HKEY hKeyResult = NULL;
DWORD dwSize = 0;
DWORD dwDataType = 0;
std::wstring wstrKey = s2ws(strKey);
if (ERROR_SUCCESS == ::RegOpenKeyEx(hKey, data_Set, 0, KEY_QUERY_VALUE, &hKeyResult))
{
::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, NULL, &dwSize);// 获取缓存的长度dwSize及类型dwDataType
switch (dwDataType)
{
case REG_MULTI_SZ:
{
BYTE* lpValue = new BYTE[dwSize];//分配内存大小
LONG lRet = ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, lpValue, &dwSize);//获取注册表中指定的键所对应的值
delete[] lpValue;
break;
}
case REG_SZ:
{
wchar_t* lpValue = new wchar_t[dwSize];//分配内存大小
memset(lpValue, 0, dwSize * sizeof(wchar_t));
if (ERROR_SUCCESS == ::RegQueryValueEx(hKeyResult, wstrKey.c_str(), 0, &dwDataType, (LPBYTE)lpValue, &dwSize))
{
std::wstring wstrValue(lpValue);
strValue = ws2s(wstrValue);
}
delete[] lpValue;
break;
}
default:break;
}
}
::RegCloseKey(hKeyResult);//关闭注册表
return strValue;
};
int WriteRegValue(HKEY hKey, LPCTSTR data_Set, LPCTSTR MyNewSubKeyValue, LPCTSTR MyKeyValue)
{
HKEY hKeyResult = NULL;
DWORD length = _tcslen(MyKeyValue) * sizeof(TCHAR);;//定义数据长度
if (ERROR_SUCCESS == ::RegOpenKeyEx(hKey, data_Set, 0, KEY_QUERY_VALUE, &hKeyResult))
{
if (ERROR_SUCCESS == ::RegCreateKey(hKeyResult, MyNewSubKeyValue, &hKeyResult))
{
if (ERROR_SUCCESS != ::RegSetValueEx(hKeyResult, _T("Name"), 0, REG_SZ, (LPBYTE)(LPCTSTR)MyKeyValue, length)) //创建子键下键值
{
std::cout << "RegSetValueEx error::" << std::endl;
}
else
{
std::cout << "RegSetValueEx ok::" << std::endl;
}
}
else
{
std::cout << "RegCreateKey error::" << std::endl;
}
}
else
{
std::cout << "RegOpenKeyEx error::" << std::endl;
}
::RegCloseKey(hKeyResult);//关闭注册表
return 0;
};
int main()
{
//=========================================================
LPCTSTR data_Set = _T("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows");
std::string strValue = GetRegValue(HKEY_CURRENT_USER, data_Set, "Device");
std::cout << "strValue::" << strValue.c_str() << std::endl;
data_Set = _T("SOFTWARE");
LPCTSTR vMykeyValue = _T("ChinaChinaChinaChinaChinaChina1");
LPCTSTR vMySubKey = _T("TopView_Qht\China");
WriteRegValue(HKEY_CURRENT_USER, data_Set, vMySubKey, vMykeyValue);
//=========================================================
return 0;
}



