Windows下C编程获取软件安装列表信息
代码如下:
#include <windows.h> #include <stdio.h> #include <iostream> #include <vector> using namespace std; #ifndef MSVCR #define _T #define _tcscpy strcpy #define _stprintf sprintf #define _tcscmp strcmp #endif typedef struct SetupSoftInfo { string displayName; //软件名称 string publisher; //发行商 string displayVersion; //版本 string installDate; //安装日期 string uninstallString; //卸载命令及参数 void show() { cout<<"\n=======================================================\n软件名称:" <<displayName<<"\n发行商:"<<publisher<<"\n版本:"<<displayVersion <<"\n安装日期:"<<installDate<<"\n卸载命令:"<<uninstallString<<endl; } }*PSetupSoftInfo; BOOL GetSetupSofts(vector<SetupSoftInfo>&stupInfo) { const int MAX_LEG = 256 * sizeof(TCHAR); HKEY hKey; DWORD dwRegNum = MAX_LEG; TCHAR regBufferName[MAX_LEG] = {0}; if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS ) { if( RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwRegNum, NULL, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ){ for( int i = 0; i < (int)dwRegNum; i++ ) { DWORD dwRegSize = MAX_LEG; RegEnumKeyEx(hKey, i, regBufferName, &dwRegSize, NULL, NULL, NULL, NULL); DWORD dwType; HKEY hSubKey; if( RegOpenKeyEx(hKey, regBufferName, NULL, KEY_READ, &hSubKey) == ERROR_SUCCESS) { SetupSoftInfo info; TCHAR regBufferValue[MAX_LEG] = {0}; dwRegSize = MAX_LEG; RegQueryValueEx(hSubKey, _T("DisplayName"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.displayName = regBufferValue; //软件名称 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("Publisher"), 0, &dwType,(LPBYTE)regBufferValue, &dwRegSize); info.publisher = regBufferValue; //发行商 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("DisplayVersion"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.displayVersion = regBufferValue; //版本 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); // 判断是否能在注册表中获取到安装时间, 否取子项创建时间 if(RegQueryValueEx(hSubKey, _T("InstallDate"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize) == ERROR_SUCCESS ) { info.installDate = regBufferValue; } else { FILETIME fileLastTime; RegQueryInfoKey(hSubKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &fileLastTime); SYSTEMTIME sTime, stLocal ; FileTimeToSystemTime(&fileLastTime,&sTime); SystemTimeToTzSpecificLocalTime(NULL, &sTime, &stLocal); TCHAR tchTime[MAX_LEG] = {0}; _stprintf(tchTime,_T("%d%02d%02d"),stLocal.wYear,stLocal.wMonth,stLocal.wDay); info.installDate = tchTime; } dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); RegQueryValueEx(hSubKey, _T("UninstallString"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); info.uninstallString = regBufferValue;//卸载命令及参数 dwRegSize = MAX_LEG; _tcscpy(regBufferValue,_T("")); // 取ParentKeyName键值,判断是否是补丁信息, 是补丁信息键值为"OperatingSystem" RegQueryValueEx(hSubKey, _T("ParentKeyName"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); TCHAR tchLocal[MAX_LEG] = {0}; _tcscpy(tchLocal,regBufferValue); _tcscpy(regBufferValue,_T("")); dwRegSize = MAX_LEG; RegQueryValueEx(hSubKey, _T("QuietUninstallString"), 0, &dwType, (LPBYTE)regBufferValue, &dwRegSize); if( _tcscmp(regBufferValue,_T("") ) ) { info.displayName = regBufferName; info.uninstallString = regBufferValue; } if( (info.displayName != _T("") ) &&( _tcscmp( tchLocal,_T("OperatingSystem") ) )) //判断是否是补丁信息 { stupInfo.push_back(info); } } }//end for(; ;) } }else { return FALSE; //打开键失败 } RegCloseKey(hKey); return TRUE; } int main() { cout << "开始输出!" << endl; vector<SetupSoftInfo> stupInfo; GetSetupSofts(stupInfo); for(auto& x:stupInfo){ x.show(); } return 0; }
分类:
C/C++
, windows编程开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理