【编者按】网学网C/C++频道为大家收集整理了“c++读取注册表详解“提供大家参考,希望对大家有所帮助!
1.读注册表
// 读取注册表
CString ReadRegisteTable(CString root,CString path, CString key)
{
HKEY hAppKey;
LPCTSTR WINDS_SERVICE_REGISTRY_KEY=path;
LPCTSTR DATA_FILE_SUB_KEY=key;
char szDataFile[80];
if(root=="HKEY_LOCAL_MACHINE")
{
if (ERROR_SUCCESS == RegOpenKeyEx (
HKEY_LOCAL_MACHINE,
WINDS_SERVICE_REGISTRY_KEY,
0,
KEY_READ,
&hAppKey))
{
ULONG cbSize = MAX_PATH*sizeof(TCHAR);
DWORD dwFlag = RegQueryValueEx (
hAppKey,
DATA_FILE_SUB_KEY,
NULL,
NULL,
(LPBYTE)szDataFile,&cbSize);
RegCloseKey (hAppKey);
if (ERROR_SUCCESS == dwFlag)
{
CString strDate=szDataFile; //MessageBox(strDate);
if(strDate.GetLength()>0) return strDate;
else return "";
}
return "";
}
}
if(root=="HKEY_CURRENT_USER")
{
if (ERROR_SUCCESS == RegOpenKeyEx (
HKEY_CURRENT_USER,
WINDS_SERVICE_REGISTRY_KEY,
0,
KEY_READ,
&hAppKey))
{
ULONG cbSize = MAX_PATH*sizeof(TCHAR);
DWORD dwFlag = RegQueryValueEx (
hAppKey,
DATA_FILE_SUB_KEY,
NULL,
NULL,
(LPBYTE)szDataFile,&cbSize);
RegCloseKey (hAppKey);
if (ERROR_SUCCESS == dwFlag)
{
CString strDate=szDataFile; //MessageBox(strDate);
if(strDate.GetLength()>0) return strDate;
else return "";
}
return "";
}
}
return "";
}[nextqage]
2.写注册表
// 修改注册表
BOOL ModifyRegisteTable(CString root,CString path, CString key, CString value)
{
HKEY hAppKey;
DWORD dwDisposition = MAX_PATH*sizeof(TCHAR);
LPCTSTR WINDS_SERVICE_REGISTRY_KEY=path;
LPCTSTR DATA_FILE_SUB_KEY=key;
char szDataFile[80]; strcpy(szDataFile,value);
if(root=="HKEY_LOCAL_MACHINE")
{
if (ERROR_SUCCESS != RegCreateKeyEx (
HKEY_LOCAL_MACHINE,
WINDS_SERVICE_REGISTRY_KEY,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
&hAppKey,
&dwDisposition))
{
return false;
}
else
{
if (ERROR_SUCCESS != RegSetValueEx (hAppKey,
DATA_FILE_SUB_KEY,
0,
REG_SZ,
(LPBYTE)szDataFile,
(lstrlen (szDataFile)+1)*sizeof(TCHAR)))
{
return false;
}
RegCloseKey (hAppKey);
}
}
if(root=="HKEY_CURRENT_USER")
{
if (ERROR_SUCCESS != RegCreateKeyEx (
HKEY_CURRENT_USER,
WINDS_SERVICE_REGISTRY_KEY,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
&hAppKey,
&dwDisposition))
{
return false;
}
else
{
if (ERROR_SUCCESS != RegSetValueEx (hAppKey,
DATA_FILE_SUB_KEY,
0,
REG_SZ,
(LPBYTE)szDataFile,
(lstrlen (szDataFile)+1)*sizeof(TCHAR)))
{
return false;
}
RegCloseKey (hAppKey);
}
}
return true;
}