示例程序一:讀取注冊表獲取計算機上已安裝程序的信息
Windows 系統中,安裝程序都可以在注冊表 HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Windows\CurrentVersion\Uninstall 獲取,並且xp、vista、win7、win8都一樣
以下示例程序中:
結構體ApplicationInfoA用於記錄每個安裝程序的具體信息,至於為何在名稱後面加A,主要是為了表明其下的信息全是用string記錄的。
函數GetAllInstalledAppInfoA用於獲取計算機上已安裝程序的全部信息,它接受vector<ApplicationInfoA>引用類型的參數,並將獲取的全部信息存放在該vector中。該程序執行成功返回0,執行失敗則返回-1。
main()函數中演示了怎麼使用:
vector<ApplicationInfoA> vAppInfo;
GetAllInstalledAppInfoA(vAppInfo);
在獲取了安裝程序的信息後,輸出到D盤下的InstalledAppInfo.txt文件中。
[cpp]
#include <windows.h>
#include <iostream>
#include <TCHAR.H>
#include <vector>
using namespace std;
//
// 用於記錄安裝軟件信息的結構體
//
struct ApplicationInfoA
{
string strName; // 軟件名
string strDisplayName; // 顯示的軟件名
string strPublisher; // 發布者
string strVersion; // 版本
string strDisplayVersion; // 顯示的版本
string strInstallLocation; // 安裝的位置
};
//
// 獲取具體的程序的鍵值Data
// hKey [in]
// --- 指向HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall的子鍵句柄
// lpszKeyValueName [in]
// --- hKey下面的子鍵名稱
// lpszKeyValueName [in]
// --- 正如其名,鍵值的名稱
// strKeyValue [out]
// --- 鍵值的Data
//
int _GetAppInfoA_(HKEY hKey, LPCSTR lpszAppName, LPCSTR lpszKeyValueName, string& strKeyValue)
{
int ret;
// 打開已安裝軟件的注冊表鍵------------------------------------------------
HKEY hInstallAppKey;
ret = RegOpenKeyEx(hKey, lpszAppName, 0, KEY_ALL_ACCESS, &hInstallAppKey);
if (ret != ERROR_SUCCESS)
{
return -1;
}
// 獲取已安裝軟件的注冊表鍵的鍵值------------------------------------------
// 1.獲取字符串大小(默認為字符串即REG_SZ)
DWORD dwKeyValueType = REG_SZ;
DWORD dwKeyValueDataSize = 0;
ret = RegQueryValueExA(
hInstallAppKey,
lpszKeyValueName,
NULL,
&dwKeyValueType,
NULL,
&dwKeyValueDataSize);
if (ret == ERROR_FILE_NOT_FOUND)
{
RegCloseKey(hInstallAppKey);
return 0;
}
else if (ret != ERROR_SUCCESS)
{
RegCloseKey(hInstallAppKey);
return -1;
}
// 2.獲取字符串值
if (dwKeyValueType != REG_SZ) // 如果不是字符串類型則返回,有的安裝程序此項不為字符串而為其他類型,忽略
{
RegCloseKey(hInstallAppKey);
return 0;
}
LPSTR lpszKeyValueData = new char[dwKeyValueDataSize + 1];
memset(lpszKeyValueData, 0, dwKeyValueDataSize + 1);
ret = RegQueryValueExA(
hInstallAppKey,
lpszKeyValueName,
NULL,
&dwKeyValueType,
(LPBYTE)lpszKeyValueData,
&dwKeyValueDataSize);
if (ret != ERROR_SUCCESS)
{
delete[] lpszKeyValueData;
RegCloseKey(hInstallAppKey);
return -1;
}
strKeyValue = lpszKeyValueData;
delete[] lpszKeyValueData;
// 關閉注冊表鍵------------------------------------------------------------
RegCloseKey(hInstallAppKey);
return 0;
}
//
// 獲取系統安裝的程序信息並存儲於參數vector中
// 成功執行返回0
// 執行失敗則返回-1
//
int GetAllInstalledAppInfoA(vector<ApplicationInfoA>& vAppInfo)
{
int ret;
// 打開注冊表鍵------------------------------------------------------------
HKEY hKey;
LPCSTR lpszSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (ret != ERROR_SUCCESS)
{
return -1;
}
// 獲取子鍵&鍵值信息-------------------------------------------------------
DWORD dwSubKeysCnt; // 子鍵數量
DWORD dwMaxSubKeyNameLen; // 子鍵名字的最大長度(not including the terminating null character)
DWORD dwKeyValueCnt; // 鍵值的數量
DWORD dwMaxKeyValueNameLen; // 鍵值名字的最大長度(not including the terminating null character)
DWORD dwMaxKeyValueDataLen; // 鍵值數據的最大長度(in Bytes)
ret = RegQueryInfoKey(
hKey,
NULL,
NULL,
NULL,
&dwSubKeysCnt,
&dwMaxSubKeyNameLen,
NULL,
&dwKeyValueCnt,
&dwMaxKeyValueNameLen,
&dwMaxKeyValueDataLen,
NULL,
NULL);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return -1;
}
// 枚舉子鍵信息------------------------------------------------------------
DWORD dwIndex;
LPSTR lpszSubKeyName = new char[dwMaxSubKeyNameLen + 1];
DWORD dwNameLen = dwMaxSubKeyNameLen + 1;
for (dwIndex = 0; dwIndex < dwSubKeysCnt; ++dwIndex)
{
dwNameLen = dwMaxSubKeyNameLen + 1;
memset(lpszSubKeyName, 0, dwMaxSubKeyNameLen + 1);
ret = RegEnumKeyEx(
hKey,
dwIndex,
lpszSubKeyName,
&dwNameLen,
NULL,
NULL,
NULL,
NULL);
if (ret != ERROR_SUCCESS)
{
RegCloseKey(hKey);
delete[] lpszSubKeyName;
return -1;
}
//************獲取具體的程序的安裝信息BEG*************
ApplicationInfoA appInfo;
appInfo.strName = lpszSubKeyName;
_GetAppInfoA_(hKey, lpszSubKeyName, "DisplayName", appInfo.strDisplayName);
_GetAppInfoA_(hKey, lpszSubKeyName, "Publisher", appInfo.strPublisher);
_GetAppInfoA_(hKey, lpszSubKeyName, "Version", appInfo.strVersion);
_GetAppInfoA_(hKey, lpszSubKeyName, "DisplayVersion", appInfo.strDisplayVersion);
_GetAppInfoA_(hKey, lpszSubKeyName, "InstallLocation", appInfo.strInstallLocation);
vAppInfo.push_back(appInfo);
//************獲取具體的程序的安裝信息END*************
}
delete[] lpszSubKeyName;
// 關閉注冊表鍵------------------------------------------------------------
RegCloseKey(hKey);
return 0;
}
int main()
{
cout << "Reg Demo Test" << endl;
vector<ApplicationInfoA> vAppInfo;
cout << GetAllInstalledAppInfoA(vAppInfo) << endl;
//輸出到文件
vector<ApplicationInfoA>::iterator iter = vAppInfo.begin();
FILE *fp = fopen("D:\\InstalledAppInfo.txt", "a");
while (iter != vAppInfo.end())
{
fprintf(fp, "----------------\n");
fprintf(fp, "Name: %s\n DisplayName: %s\n Publisher: %s\n Version: %s\n DisplayVersion: %s\n InstallLocation: %s\n",
iter->strName.c_str(), iter->strDisplayName.c_str(),
iter->strPublisher.c_str(), iter->strVersion.c_str(),
iter->strDisplayVersion.c_str(), iter->strInstallLocation.c_str());
fprintf(fp, "----------------\n\n");
++iter;
}
fclose(fp);
return 0;
}