<强> c++读取注册表强>
GetRegValue.h:
的ifndef __GETREGVALUE_H__ #定义__GETREGVALUE_H__ # include & lt; string>//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//功能://GetRegValue获取注册表中指定键的值//访问://公共//参数://[在]int nKeyType -注册表项的类型,传入的参数只可能是以下数值://0:HKEY_CLASSES_ROOT//1:HKEY_CURRENT_USER//2:HKEY_LOCAL_MACHINE//3:HKEY_USERS//4:HKEY_PERFORMANCE_DATA//5:HKEY_CURRENT_CONFIG//6:HKEY_DYN_DATA//7:HKEY_CURRENT_USER_LOCAL_SETTINGS//8:HKEY_PERFORMANCE_TEXT//9:HKEY_PERFORMANCE_NLSTEXT//[在]const std:: string,strUrl——要查找的键的路径//[在]const std:: string,strKey——指定的键//返回://std:: string -指定键的值//备注://?/作者:luoweifu//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - std:: string GetRegValue (int nKeyType, const std:: string&strUrl, const std:: string&strKey);//可移植版本wstring=比;字符串 std:: string二硫化钨(const std:: wstring&ws);//可移植版本字符串=比;wstring std:: wstring s2ws (const std:: string&年代); # endif//__GETREGVALUE_H__ >之前<强> GetRegValue.cpp 强>
# include“stdafx.h” # include & lt; Windows.h> # include“GetRegValue.h”//可移植版本wstring=比;字符串 std:: string二硫化钨(const std:: wstring&ws) { std:: string curLocale=setlocale (LC_ALL " "); const wchar_t * _Source=ws.c_str (); size_t _Dsize=wcstombs(零_Source 0) + 1; char * _Dest=new char [_Dsize]; memset (_Dest 0 _Dsize); wcstombs (_Dest _Source _Dsize);=_Dest std:: string结果; 删除[]_Dest; setlocale (LC_ALL curLocale.c_str ()); 返回结果; }//可移植版本字符串=比;wstring std:: wstring s2ws (const std:: string&s) { std:: string curLocale=setlocale (LC_ALL " "); const char * _Source=s.c_str (); size_t _Dsize=mbstowcs(零_Source 0) + 1; wchar_t * _Dest=new wchar_t [_Dsize]; wmemset (_Dest 0 _Dsize); mbstowcs (_Dest _Source _Dsize); std::=_Dest wstring结果; 删除[]_Dest; setlocale (LC_ALL curLocale.c_str ()); 返回结果; } std:: string GetRegValue (int nKeyType, const std:: string&strUrl, const std:: string&strKey) { std:: string strValue (" "); 根键根键=零; 根键hKeyResult=零; DWORD dwSize=0; DWORD dwDataType=0; std:: wstring wstrUrl=s2ws (strUrl); std:: wstring wstrKey=s2ws (strKey); 开关(nKeyType) { 例0: { 根键=HKEY_CLASSES_ROOT; 打破; } 案例1: { 根键=HKEY_CURRENT_USER; 打破; } 案例2: { 根键=HKEY_LOCAL_MACHINE; 打破; } 案例3: { 根键=HKEY_USERS; 打破; } 案例4: { 根键=HKEY_PERFORMANCE_DATA; 打破; } 例5: { 根键=HKEY_CURRENT_CONFIG; 打破; } 例6: { 根键=HKEY_DYN_DATA; 打破; } 例7: { 根键=HKEY_CURRENT_USER_LOCAL_SETTINGS; 打破; } 例8: { 根键=HKEY_PERFORMANCE_TEXT; 打破; } 例9: { 根键=HKEY_PERFORMANCE_NLSTEXT; 打破; } 默认值: { 返回strValue; } }//打开注册表 如果(ERROR_SUCCESS==:: RegOpenKeyEx(根键,wstrUrl.c_str (), 0, KEY_QUERY_VALUE,, hKeyResult)) {//获取缓存的长度dwSize及类型dwDataType ::RegQueryValueEx (hKeyResult wstrKey.c_str (), 0,, dwDataType, NULL,, dwSize); 开关(dwDataType) { 案例REG_MULTI_SZ: {//分配内存大小 字节* lpValue=https://www.yisu.com/zixun/new字节(dwSize);//获取注册表中指定的键所对应的值 长lRet=: RegQueryValueEx (hKeyResult wstrKey.c_str (), 0, &dwDataType, lpValue, &dwSize); 删除[]lpValue; 打破; } 案例REG_SZ: {//分配内存大小 wchar_t * lpValue=new wchar_t [dwSize]; memset (lpValue 0 dwSize * sizeof (wchar_t));//获取注册表中指定的键所对应的值 如果(ERROR_SUCCESS==:: RegQueryValueEx (hKeyResult, wstrKey.c_str (), 0, &dwDataType, (LPBYTE) lpValue &dwSize)) { std:: wstring wstrValue (lpValue); strValue=二硫化钨(wstrValue); } 删除[]lpValue; 打破; } 默认值: 打破; } }//关闭注册表 ::RegCloseKey (hKeyResult); 返回strValue; }c++读取注册表的实现方法