Windows 系統中,安裝程序都可以在注冊表 HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Windows\CurrentVersion\Uninstall獲取,並且xp、vista 、win7、win8都一樣
打開注冊表命令:regedit,依次展開上述注冊表路徑,安裝程序列表如下圖:
獲取系統安裝程序列表:
// 獲取系統安裝程序列表 BOOL CMyListCtrlTestDlg::AllRegKey(HKEY hKey, LPCTSTR szDesKeyItem) { HKEY hSubKey; DWORD dwIndex = 0; long lResult; int i = 1; lResult = RegOpenKeyEx(hKey, szDesKeyItem, 0, KEY_READ, &hSubKey); // 打開注 冊表 if(ERROR_SUCCESS == lResult) { TCHAR szKeyName[256] = { 0 }; DWORD cbName = 256*sizeof(TCHAR); lResult = RegEnumKeyEx(hSubKey, dwIndex, szKeyName, &cbName, 0, NULL, NULL, NULL); // 獲取字段 while (lResult == ERROR_SUCCESS && lResult != ERROR_MORE_DATA){ OutputDebugString(szKeyName); CString str; str.Format(_T("%d"), i); i = m_MyList.InsertItem(i, str); // 插入到List控件顯示 m_MyList.SetItemText(i,1,szKeyName); dwIndex = dwIndex + 1; // 改變dwIndex,循環獲取下移字段 cbName = 256*sizeof(TCHAR); lResult = RegEnumKeyEx(hSubKey, dwIndex, szKeyName, &cbName, 0, NULL, NULL, NULL); i++; } RegCloseKey(hSubKey); // 關閉注冊表 return TRUE; } RegCloseKey(hSubKey); return FALSE; }
獲取安裝程序屬性信息(二次讀取注冊表):
BOOL SoftList::AllRegKey(HKEY hKey, LPCTSTR szDesKeyItem) { HKEY hSubKey; DWORD dwIndex = 0; long lResult; lResult = RegOpenKeyEx(hKey, szDesKeyItem, 0, KEY_READ, &hSubKey); // 打開注 冊表 if(ERROR_SUCCESS == lResult) { int id = 1; // 序號 TCHAR s_name[256] = {0}; // 軟件名 TCHAR s_DisplayName[256] = {0}; // 顯示名稱 TCHAR s_Publisher[256] = {0}; // 發布者 TCHAR s_InstallDate[12] = {0}; // 安裝日期 DWORD s_EstimatedSize; // 大小 TCHAR s_DisplayVersion[256] = { 0 }; // 版本 DWORD cbName = 256*sizeof(TCHAR); DWORD cbName2 = sizeof(TCHAR); DWORD typeSZ = REG_SZ; // SZ 注冊表類型 DWORD typeDWORD = REG_DWORD; // DWORD 注冊表類型 lResult = RegEnumKeyEx(hSubKey, dwIndex, s_name, &cbName, 0, NULL, NULL, NULL); // 獲取字段 while (lResult == ERROR_SUCCESS && lResult != ERROR_MORE_DATA){ OutputDebugString(s_name); CString str; str.Format(_T("%d"), id); id = m_SoftList.InsertItem(id, str); // 插入到List控件顯示 m_SoftList.SetItemText(id, 1, s_name); HKEY hItem; // 下一級注冊表句柄 if(::RegOpenKeyEx(hSubKey, s_name, 0, KEY_READ, &hItem) == ERROR_SUCCESS){ // 提取下一級注冊表程序的屬性信息 if (::RegQueryValueEx(hItem, _T("DisplayName"), 0, &typeSZ, (LPBYTE) s_DisplayName, &cbName) == ERROR_SUCCESS) { m_SoftList.SetItemText(id, 1, s_DisplayName); } if (::RegQueryValueEx(hItem, _T("Publisher"), 0, &typeSZ, (LPBYTE) s_Publisher, &cbName) == ERROR_SUCCESS) { m_SoftList.SetItemText(id, 2, s_Publisher); } if (::RegQueryValueEx(hItem, _T("InstallDate"), 0, &typeSZ, (LPBYTE) s_InstallDate, &cbName) == ERROR_SUCCESS) { if (s_InstallDate != NULL) { // 判斷是否為空 m_SoftList.SetItemText(id, 3, s_InstallDate); } } if (::RegQueryValueEx(hItem, _T("EstimatedSize"), 0, &typeDWORD, (LPBYTE)&s_EstimatedSize, &cbName2) == ERROR_SUCCESS) { float f_Size = s_EstimatedSize/1024.0; // DWORD 轉換為 CString 類型 CString p_Size; p_Size.Format(_T("%.2f M"), f_Size); m_SoftList.SetItemText(id, 4, p_Size); } if (::RegQueryValueEx(hItem, _T("DisplayVersion"), 0, &typeSZ, (LPBYTE)s_DisplayVersion, &cbName) == ERROR_SUCCESS) { m_SoftList.SetItemText(id, 5, s_DisplayVersion); } ::RegCloseKey(hItem); // 關閉二級注冊表(程序屬性信息) } dwIndex = dwIndex + 1; // 改變dwIndex,循環獲取下移字段 cbName = 256*sizeof(TCHAR); lResult = RegEnumKeyEx(hSubKey, dwIndex, s_name, &cbName, 0, NULL, NULL, NULL); id++; } RegCloseKey(hSubKey); // 關閉一級注冊表(讀取程序信息) return TRUE; } RegCloseKey(hSubKey); return FALSE; }
通過注冊表路徑獲取調用:
AllRegKey(HKEY_LOCAL_MACHINE, TEXT ("SoftWare\\Microsoft\\Windows\\CurrentVersion\\Uninstall"));
運行結果: