众所周知,注册表的重要性就不需要在这里阐述了,但是我们如何用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;
}
以上只是理论,未经实际操作证实,如有错误操作,敬请原谅1