一、获取安装程序信息
1 #include <windows.h> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 6 using namespace std; 7 8 //记录安装软件信息的结构体 9 struct ApplicationInfoA 10 { 11 string strName; //软件名 12 string strDisplayName; //显示的软件名 13 string strPublisher; //发布者 14 string strVersion; //版本 15 string strDisPlayVersion; //显示的版本 16 string strInstallLocation; //安装的位置 17 }; 18 19 int GetAppInfoA(HKEY hKey, LPCSTR lpszAppName, LPCSTR lpszKeyValueName, string &strKeyValue) 20 { 21 int iRet; 22 HKEY hInstallAppKey; 23 24 iRet = RegOpenKeyExA(hKey, lpszAppName, 0, KEY_ALL_ACCESS, &hInstallAppKey); 25 if (iRet != ERROR_SUCCESS) 26 { 27 return -1; 28 } 29 30 DWORD dwKeyValueType = REG_SZ; 31 DWORD dwKeyValueDataSize = 0; 32 33 iRet = RegQueryValueExA(hInstallAppKey, lpszKeyValueName, NULL, &dwKeyValueType, NULL, &dwKeyValueDataSize); 34 if (iRet == ERROR_FILE_NOT_FOUND) 35 { 36 RegCloseKey(hInstallAppKey); 37 return 0; 38 } 39 else if (iRet != ERROR_SUCCESS) 40 { 41 RegCloseKey(hInstallAppKey); 42 return -1; 43 } 44 45 if (dwKeyValueType != REG_SZ) 46 { 47 RegCloseKey(hInstallAppKey); 48 return 0; 49 } 50 51 LPSTR lpszKeyValueData = new char[dwKeyValueDataSize + 1]; 52 memset(lpszKeyValueData, 0, dwKeyValueDataSize + 1); 53 54 iRet = RegQueryValueExA(hInstallAppKey, lpszKeyValueName, NULL, &dwKeyValueType, (LPBYTE)lpszKeyValueData, &dwKeyValueDataSize); 55 if (iRet != ERROR_SUCCESS) 56 { 57 delete []lpszKeyValueData; 58 RegCloseKey(hInstallAppKey); 59 return -1; 60 } 61 62 strKeyValue = lpszKeyValueData; 63 delete []lpszKeyValueData; 64 RegCloseKey(hInstallAppKey); 65 66 return 0; 67 } 68 69 int GetAllInstalledAppInfoA(vector<ApplicationInfoA> &vecAppInfo) 70 { 71 int iRet; 72 HKEY hKey; 73 LPCSTR lpszSubKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 74 iRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey); 75 if (iRet != ERROR_SUCCESS) 76 { 77 return -1; 78 } 79 80 DWORD dwSubKeysCnt; 81 DWORD dwMaxSubKeyNameLen; 82 DWORD dwKeyValueCnt; 83 DWORD dwMaxKeyValueNameLen; 84 DWORD dwMaxKeyValueDataLen; 85 86 iRet = RegQueryInfoKeyA( 87 hKey, 88 NULL, 89 NULL, 90 NULL, 91 &dwSubKeysCnt, 92 &dwMaxSubKeyNameLen, 93 NULL, 94 &dwKeyValueCnt, 95 &dwMaxKeyValueNameLen, 96 &dwMaxKeyValueDataLen, 97 NULL, 98 NULL); 99 if (iRet != ERROR_SUCCESS) 100 { 101 RegCloseKey(hKey); 102 return -1; 103 } 104 105 DWORD dwIndex; 106 DWORD dwNameLen = dwMaxSubKeyNameLen + 1; 107 LPSTR lpszSubKeyName = new char[dwNameLen]; 108 109 for (dwIndex = 0; dwIndex < dwSubKeysCnt; ++dwIndex) 110 { 111 dwNameLen = dwMaxSubKeyNameLen + 1; 112 memset(lpszSubKeyName, 0, dwNameLen); 113 114 iRet = RegEnumKeyExA(hKey, dwIndex, lpszSubKeyName, &dwNameLen, NULL, NULL, NULL, NULL); 115 if (iRet != ERROR_SUCCESS) 116 { 117 RegCloseKey(hKey); 118 delete []lpszSubKeyName; 119 return -1; 120 } 121 122 ApplicationInfoA appInfo; 123 appInfo.strName = lpszSubKeyName; 124 GetAppInfoA(hKey, lpszSubKeyName, "DisplayName", appInfo.strDisplayName); 125 GetAppInfoA(hKey, lpszSubKeyName, "Publisher", appInfo.strPublisher); 126 GetAppInfoA(hKey, lpszSubKeyName, "Version", appInfo.strVersion); 127 GetAppInfoA(hKey, lpszSubKeyName, "DisplayVersion", appInfo.strDisPlayVersion); 128 GetAppInfoA(hKey, lpszSubKeyName, "InstallLocation", appInfo.strInstallLocation); 129 vecAppInfo.push_back(appInfo); 130 } 131 132 delete []lpszSubKeyName; 133 RegCloseKey(hKey); 134 135 return 0; 136 } 137 138 int main(int argc, char **argv) 139 { 140 cout << "Application Information" << endl; 141 142 vector<ApplicationInfoA> vecAppInfo; 143 cout << GetAllInstalledAppInfoA(vecAppInfo) << endl; 144 145 for (vector<ApplicationInfoA>::const_iterator iter = vecAppInfo.begin(); iter != vecAppInfo.end(); ++iter) 146 { 147 cout << "-------------------------------------------------------------------------------" << endl; 148 cout << "Name:" << iter->strName << endl; 149 cout << "DisplayName:" << iter->strDisplayName << endl; 150 cout << "Publisher:" << iter->strPublisher << endl; 151 cout << "Version:" << iter->strVersion << endl; 152 cout << "DisplayVersion:" << iter->strDisPlayVersion << endl; 153 cout << "InstallLocation:" << iter->strInstallLocation << endl; 154 cout << "-------------------------------------------------------------------------------" << endl; 155 cout << endl; 156 cout << endl; 157 } 158 159 return 0; 160 }
二、添加环境变量
1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include <windows.h> 4 #include <iostream> 5 #include <string> 6 7 using namespace std; 8 9 int AddPathEnvValue(LPCSTR lpszPathValue) 10 { 11 int iRet; 12 HKEY hKey; 13 LPCSTR lpszSubKey = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; 14 15 iRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey); 16 if (ERROR_SUCCESS != iRet) 17 { 18 cout << "RegOpenKeyExA Error!" << endl; 19 return -1; 20 } 21 22 LPCSTR lpszKeyValueName = "Path"; 23 DWORD dwKeyValueType = REG_EXPAND_SZ; 24 DWORD dwKeyValueDataSize = 0; 25 26 iRet = RegQueryValueExA(hKey, lpszKeyValueName, NULL, &dwKeyValueType, NULL, &dwKeyValueDataSize); 27 if (ERROR_FILE_NOT_FOUND != iRet) 28 { 29 iRet = RegSetValueExA(hKey, lpszKeyValueName, 0, REG_EXPAND_SZ, (const BYTE*)"", 1); 30 if (ERROR_SUCCESS != iRet) 31 { 32 cout << "RegSetValueExA Error!" << endl; 33 RegCloseKey(hKey); 34 return -1; 35 } 36 } 37 else if (ERROR_SUCCESS != iRet) 38 { 39 cout << "RegSetValueExA Error!" << endl; 40 RegCloseKey(hKey); 41 return -1; 42 } 43 else if (REG_EXPAND_SZ != dwKeyValueType) 44 { 45 cout << "It is impossible!" << endl; 46 cout << dwKeyValueType << endl; 47 RegCloseKey(hKey); 48 return -1; 49 } 50 51 char *lpszKeyValueData = new char[dwKeyValueDataSize + 1]; 52 memset(lpszKeyValueData, 0, dwKeyValueDataSize + 1); 53 54 iRet = RegQueryValueExA(hKey, lpszKeyValueName, NULL, &dwKeyValueType, (LPBYTE)lpszKeyValueData, &dwKeyValueDataSize); 55 if (ERROR_SUCCESS != iRet) 56 { 57 cout << "RegQueryValueExA Error!" << endl; 58 RegCloseKey(hKey); 59 delete []lpszKeyValueData; 60 return -1; 61 } 62 63 unsigned int iLen = strlen(lpszPathValue); 64 iLen += strlen(lpszKeyValueData); 65 char *lpszKeyValueData_New = new char[iLen + 1]; 66 memset(lpszKeyValueData_New, 0, iLen + 1); 67 sprintf(lpszKeyValueData_New, "%s%s", lpszKeyValueData, lpszPathValue); 68 iRet = RegSetValueExA(hKey, lpszKeyValueName, 0, REG_EXPAND_SZ, (const BYTE*)lpszKeyValueData_New, iLen + 1); 69 if (ERROR_SUCCESS != iRet) 70 { 71 cout << "RegSetValueExA Error!" << endl; 72 RegCloseKey(hKey); 73 delete []lpszKeyValueData; 74 delete []lpszKeyValueData_New; 75 return -1; 76 } 77 78 delete []lpszKeyValueData_New; 79 delete []lpszKeyValueData; 80 81 RegCloseKey(hKey); 82 83 return 0; 84 } 85 86 int main(int argc, char *argv) 87 { 88 cout << AddPathEnvValue(";HelloKitty") << endl; 89 return 0; 90 }