vc++高级班之注册表篇[3]---注册表中键值的相关操作

来源:互联网 发布:巴西足球知乎 编辑:程序博客网 时间:2024/05/17 22:27

①、键值信息的获取:RegQueryValueEx
方式一:

HKEY hKey = NULL;

TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
DWORD dwType = 0;
BYTE *lpData = new BYTE[maxValueDataLen+1];
ZeroMemory(lpData, maxValueDataLen+1);
lRet = RegQueryValueEx(hKey, _T("Edifier.EasyVOL"), NULL, &dwType, lpData, &maxValueDataLen);


CString strValue;
strValue.Format(_T("%s"), lpData);
MessageBox(strValue);
delete [] lpData;
}
RegCloseKey(hKey);
}


方式二:
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwType = 0, dwDataLen = 0;
BYTE *lpData = NULL;
lRet = RegQueryValueEx(hKey, _T("Edifier.EasyVOL"), NULL, &dwType, NULL, &dwDataLen);
lpData = new BYTE[dwDataLen+1];
ZeroMemory(lpData, dwDataLen+1);
lRet = RegQueryValueEx(hKey, _T("Edifier.EasyVOL"), NULL, &dwType, lpData, &dwDataLen);
RegCloseKey(hKey);


CString strValue;
strValue.Format(_T("%s"), lpData);
MessageBox(strValue);
delete [] lpData;
}
===================================================
②、键值信息的设置:RegSetValueEx
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
CString strPath = _T("D:\\Program Files\\SoftWare\\123.exe");
RegSetValueEx(hKey, _T("Edifier.EasyVOL"), 0, REG_SZ, (LPBYTE)strPath.GetBuffer(), strPath.GetLength()*sizeof(TCHAR));


strPath = _T("G:\\VMWare\\CentOS\\abc.exe");
RegSetValueEx(hKey, _T("TestRun"), 0, REG_SZ, (LPBYTE)strPath.GetBuffer(), strPath.GetLength()*sizeof(TCHAR));
RegCloseKey(hKey);
}
===================================================
③、键值的删除:RegDeleteValue
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
RegDeleteValue(hKey, _T("TestRun"));
RegCloseKey(hKey);
}
===================================================
④、键值的枚举操作:RegEnumValue
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
DWORD dwNameLen = maxValueNameLen+1;
TCHAR *pszName = new TCHAR[dwNameLen];


DWORD dwType = 0;
DWORD dwValueDataLen = maxValueDataLen+1;
BYTE *lpValueData = new BYTE[dwValueDataLen];


for (DWORD dwIndex = 0; dwIndex < dwValueCount; ++dwIndex) {
dwNameLen = maxValueNameLen+1;
ZeroMemory(pszName, dwNameLen);


dwValueDataLen = maxValueDataLen+1;
ZeroMemory(lpValueData, dwValueDataLen);


lRet = RegEnumValue(hKey, dwIndex, pszName, &dwNameLen, NULL, &dwType, lpValueData, &dwValueDataLen);
//Other operations
CString strValueData;
strValueData.Format(_T("%s"), lpValueData);
}
delete [] pszName;
delete [] lpValueData;
RegCloseKey(hKey);
}
}
===================================================
※※※ 小作业:自己试着做一款开机启动项查询工具
------------------------------------- End -------------------------------------------