c++ Windows获取软件安装列表信息

链接

cpp 复制代码
#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
 
class SetupSoftInfo
{
public:
    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;
    }
};

//获取所有程序
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;
}
 
bool CheckSentinelRunTime(const vector<SetupSoftInfo>& stupInfo)
{
	for(int i=0; i<stupInfo.size();i++){
		if(stupInfo[i].displayName == "xxxxxxx")//xxxxxxx为软件名
			return true;
	}
	return false;
}
int main()
{
    vector<SetupSoftInfo> stupInfo;
    GetSetupSofts(stupInfo);
	bool bl = CheckSentinelRunTime(stupInfo);
	
	cout << ":"<<bl;
	while(1){}
    return 0;
}
相关推荐
dialect_1 天前
KMP算法
开发语言·c++·算法
总要学点什么1 天前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
ShineWinsu1 天前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
傲世仙尊1 天前
多态-虚表才活到运行期符号表早已消失
c++
开开心心_Every1 天前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
chenbingjie_c1 天前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
是main不是漫1 天前
【双指针——移动零】数组划分类题目的入门基石
c++·算法
吴卫斌1 天前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
·present·1 天前
CSAPP(Bomb lab)
c语言·c++
王老师青少年编程1 天前
2026年信奥赛c++普及组csp-j第二轮复赛真题
c++·真题·csp·信奥赛