vc++高級班之註冊表篇[3]---註冊表中鍵值的相關操作

①、鍵值信息的獲取: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 -------------------------------------------
發佈了4 篇原創文章 · 獲贊 21 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章