说明:每安装一个软件,那么该软件的安装程序会向注册表当中“ SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall ”的注册表项下面创建一个注册表子项,键值存储卸载数据信息。
实现:
1、定义存储程序卸载信息的结构体SOFTWARE
1 struct SOFTWARE 2 { 3 CString DisplayName; //名称 4 CString Publisher; //发行人 5 CString InstallDate; //安装日期 6 CString InstallLocation; //安装途径 7 CString UninstStr; //卸载程序路径 8 CString DisplayVersion; //版本 9 BOOL is64; //是否是64位程序 10 };
2、获取所有程序卸载信息函数GetSoftListAll中定义动态数组,并调用获取程序卸载信息函数GetSoftList。
判断系统是否是64位,若是64位系统取消重定向:64bit系统的注册表分32 位注册表项和64位注册表项两部分。SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 会重定向至 SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall。
1 const vector<SOFTWARE>& GetSoftListAll() 2 { 3 static vector<SOFTWARE> s_lst; 4 5 if (0 == s_lst.size()) 6 { 7 GetSoftList(s_lst, FALSE); 8 BOOL is64 = IsWow64(); 9 if(is64) 10 { 11 DisableRedirect(); //取消重定向 12 GetSoftList(s_lst, TRUE); 13 RestoreRedirect(); //恢复重定向 14 } 15 } 16 17 return s_lst; 18 }
3、GetSoftList函数:打开注册表,遍历获取子键信息
1 void GetSoftList( std::vector<SOFTWARE> &lst, BOOL is64 ) 2 { 3 REGSAM samDesired = KEY_READ; 4 if(is64) 5 samDesired |= KEY_WOW64_64KEY; 6 7 HKEY hKey = NULL; 8 CString root_key = _T("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); 9 //打开指定的注册表键 10 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, root_key, 0, samDesired, &hKey) != ERROR_SUCCESS ) 11 { 12 13 return; 14 } 15 16 DWORD cSubKeys = 0; //子键数量 17 DWORD cbMaxSubKey = 0; //子键名称的最大长度 18 DWORD retCode = RegQueryInfoKey(hKey, 0, 0, 0, &cSubKeys, &cbMaxSubKey, 0, 0, 0, 0, 0, 0); 19 20 for (int i=0; i<(int)cSubKeys; i++) 21 { 22 TCHAR achKey[MAX_PATH+1] = {0}; //子键名称 23 DWORD cbName = MAX_PATH; //子键名称的大小 24 25 //枚举子键信息 26 retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, NULL); 27 if (retCode == ERROR_SUCCESS) 28 { 29 CString subkey = root_key + L"\"; 30 subkey += achKey; 31 //给SOFTEARE结构体成员赋值,加入动态数组 32 QuerySoft(HKEY_LOCAL_MACHINE, subkey, is64, lst); 33 } 34 } 35 36 RegCloseKey(hKey); 37 }
4、QuerySoft函数:给SOFTWARE结构体成员赋值,加入动态数组
1 void _QuerySoft(HKEY hKey, LPCTSTR lpSubKey, BOOL is64, std::vector<SOFTWARE> &lst) 2 { 3 CpzRegistry reg; 4 5 pzRegAccessRights pDesired = pzRAR_Read; 6 if(is64) 7 pDesired = pzRAR_Read64; 8 9 reg.OpenKey(hKey, lpSubKey, FALSE, pDesired); 10 11 SOFTWARE soft; 12 //给SOFTEARE结构体成员赋值 13 soft.DisplayName = reg.ReadString(_T("DisplayName")); 14 soft.DisplayName.Trim(); //去掉字符序列左边和右边的空格 15 soft.DisplayName.Replace(L"=",L""); 16 17 soft.UninstStr = reg.ReadString(_T("UninstallString")); 18 soft.UninstStr.Trim(); 19 20 soft.Publisher = reg.ReadString(_T("Publisher")); 21 soft.Publisher.Trim(); 22 23 soft.DisplayVersion = reg.ReadString(_T("DisplayVersion")); 24 soft.DisplayVersion.Trim(); 25 26 soft.InstallDate = reg.ReadString(_T("InstallDate")); 27 soft.InstallDate.Trim(); 28 29 soft.InstallLocation = reg.ReadString(_T("InstallLocation")); 30 soft.InstallLocation.Trim(); 31 32 soft.is64 = is64; 33 34 CString parent_name = reg.ReadString(_T("ParentDisplayName")); 35 CString parent_key = reg.ReadString(_T("ParentKeyName")); 36 37 int SystemComponent = 0; //系统组件 38 reg.ReadInt(_T("SystemComponent"), SystemComponent); 39 40 41 if(soft.DisplayName.GetLength() == 0) 42 return; 43 44 if(parent_name.GetLength() != 0 || parent_key.GetLength() != 0) 45 return; 46 47 if(SystemComponent) 48 return; 49 50 // 查找重名; 51 for(int i=0;i<(int)lst.size();i++) 52 { 53 if(lst[i].DisplayName == soft.DisplayName) 54 return; 55 } 56 57 //加入数组 58 lst.push_back(soft); 59 }