C++ access to the registry to get a list of installed software information sample code

  • 2020-04-02 02:00:49
  • OfStack




// ---------------------------------------------------------------
// FlieNmae: 
//   SofInfo.h
// Remark:
//& have spent & have spent The registry is read to obtain information about the installed software on the machine.
// ---------------------------------------------------------------
#pragma once
#include <vector>
struct SoftInfo
{
    //The software name
    CString m_strSoftName;
    //Software version number
    CString m_strSoftVersion;
    //Software installation directory
    CString m_strInstallLocation;
    //Software vendor
    CString m_strPublisher;
    //The full path of the main program
    CString m_strMainProPath;
    //Uninstall exe on the full path
    CString m_strUninstallPth;
};
class CSoftInfo
{
private:
    //Save installed common software installation information
    std::vector<SoftInfo> m_SoftInfoArr;
    //Save the system patch information
    std::vector<SoftInfo> m_SystemPatchesArr;
public:
    CSoftInfo();
    ~CSoftInfo(){}

    //Gets a Vector that contains information about common software installations
    std::vector<SoftInfo> GetSoftInfo (void) const;
    //Gets all installed common software names
    void GetSoftName (std::vector<LPCTSTR>& lpszSoftName);
    //Gets the version Numbers of all installed commonly used software
    void GetSoftVersion (std::vector<LPCTSTR>& lpszSoftVersion);
    //Gets the directory of all installed common software installations
    void GetInstallLocation (std::vector<LPCTSTR>& lpszInstallLocation);
    //Gets all installed vendors of commonly used software
    void GetPublisher (std::vector<LPCTSTR>& lpszPublisher);
    //Gets the path of all installed common software main programs
    void GetMainProPath (std::vector<LPCTSTR>& lpszMainProPath);
    //Gets the path to which all common software uninstaller programs have been installed
    void GetUninstallPth (std::vector<LPCTSTR>& lpszSoftName);
    //Gets a Vector containing system patch information
    std::vector<SoftInfo> GetSystemPatchesInfo (void) const;
    //Gets the patch names for all installed systems
    void GetSystemPatchesName (std::vector<LPCTSTR>& lpszSoftName);
};


// FlieNmae: Softinfo.cpp
#include "stdafx.h"
#include "SoftInfo.h"
CSoftInfo::CSoftInfo()
{
    struct SoftInfo softinfo;
    HKEY RootKey;            //A primary key
    LPCTSTR lpSubKey;        //Name of children
    HKEY hkResult;            //Handle to the key to be opened
    HKEY hkRKey;
    LONG lReturn;            //Record whether the registry was read successfully
    CString strBuffer;
    CString strMidReg;
    DWORD index = 0;
    TCHAR szKeyName[255] = {0};        //Registry key name
    TCHAR szBuffer[255] = {0};
    DWORD dwKeyLen = 255;
    DWORD dwNameLen = 255;
    DWORD dwType = REG_BINARY|REG_DWORD|REG_EXPAND_SZ|REG_MULTI_SZ|REG_NONE|REG_SZ;
    RootKey = HKEY_LOCAL_MACHINE;
    lpSubKey = _T("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    lReturn = RegOpenKeyEx(RootKey, lpSubKey, 0, KEY_ALL_ACCESS, &hkResult);

    if (lReturn == ERROR_SUCCESS)
    {

        while (ERROR_NO_MORE_ITEMS !=RegEnumKeyEx(hkResult, index, szKeyName, &dwKeyLen, 0, NULL, NULL, NULL))
        {
            index++;
            strBuffer.Format(_T("%s"), szKeyName);
            if (!strBuffer.IsEmpty())
            {
                strMidReg = (CString)lpSubKey +_T("\") + strBuffer;
                if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, strMidReg, 0, KEY_ALL_ACCESS, &hkRKey)==ERROR_SUCCESS) 
                { 
                    RegQueryValueEx(hkRKey, _T("DisplayName"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strSoftName = szBuffer;
                    dwNameLen = 255;
                    memset(szBuffer, 0, 255);
                    RegQueryValueEx(hkRKey, _T("DisplayVersion"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strSoftVersion = szBuffer;
                    dwNameLen = 255;
                    memset(szBuffer, 0, 255);
                    RegQueryValueEx(hkRKey, _T("InstallLocation"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strInstallLocation = szBuffer;
                    dwNameLen = 255;
                    memset(szBuffer, 0, 255);
                    RegQueryValueEx(hkRKey, _T("Publisher"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strPublisher = szBuffer;
                    dwNameLen = 255;
                    RegQueryValueEx(hkRKey, _T("InstallLocation"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strMainProPath = szBuffer;
                    dwNameLen = 255;
                    memset(szBuffer, 0, 255);
                    RegQueryValueEx(hkRKey, _T("UninstallString"), 0, &dwType, (LPBYTE)szBuffer,&dwNameLen); 
                    softinfo.m_strUninstallPth = szBuffer;
                    dwNameLen = 255;
                    memset(szBuffer, 0, 255);

                    if(!softinfo.m_strSoftName.IsEmpty())
                    {
                        if(strBuffer.GetAt(0) == 'K' && strBuffer.GetAt(1) == 'B')
                        {
                            m_SystemPatchesArr.push_back(softinfo);
                        }
                        else
                        {
                            m_SoftInfoArr.push_back(softinfo);
                        }
                    }
                }

                dwKeyLen = 255;
                memset(szKeyName,0, 255);
            }
        }
        RegCloseKey(hkResult);
    }
    else
    {
        ::MessageBox(NULL, _T(" Failed to open registry !"), NULL, MB_ICONWARNING);
    }
}
std::vector<SoftInfo> CSoftInfo::GetSoftInfo (void) const
{
    return m_SoftInfoArr;
}
void CSoftInfo::GetSoftName (std::vector<LPCTSTR>& lpszSoftName)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        lpszSoftName.push_back(iter->m_strSoftName);
    }
}
void CSoftInfo::GetSoftVersion (std::vector<LPCTSTR>& lpszSoftVersion)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        if (!(iter->m_strSoftVersion).IsEmpty())
        {
            lpszSoftVersion.push_back(iter->m_strSoftVersion);
        }
    }
}
void CSoftInfo::GetInstallLocation (std::vector<LPCTSTR>& lpszInstallLocation)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        if (!(iter->m_strInstallLocation).IsEmpty())
        {
            lpszInstallLocation.push_back(iter->m_strInstallLocation);
        }
    }
}
void CSoftInfo::GetPublisher (std::vector<LPCTSTR>& lpszPublisher)
{
    std::vector<SoftInfo>::iterator iter;

    bool bSign;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        bSign = true;
        //Start with a repeat vendor
        std::vector<LPCTSTR>::iterator itr;
        for (itr = lpszPublisher.begin(); itr != lpszPublisher.end(); itr++)
        {
            if (iter->m_strPublisher == (CString)*itr)
            {
                bSign = false;
            }
        }

        if (bSign)
        {
            lpszPublisher.push_back(iter->m_strPublisher);
        }
    }
}
void CSoftInfo::GetMainProPath (std::vector<LPCTSTR>& lpszMainProPath)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        if (!(iter->m_strMainProPath).IsEmpty())
        {
            lpszMainProPath.push_back(iter->m_strMainProPath);
        }
    }
}
void CSoftInfo::GetUninstallPth (std::vector<LPCTSTR>& lpszSoftName)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SoftInfoArr.begin(); iter != m_SoftInfoArr.end(); iter++)
    {
        if (!(iter->m_strUninstallPth).IsEmpty())
        {
            lpszSoftName.push_back(iter->m_strUninstallPth);
        }
    }
}
std::vector<SoftInfo> CSoftInfo::GetSystemPatchesInfo (void) const
{
    return m_SystemPatchesArr;
}
void CSoftInfo::GetSystemPatchesName (std::vector<LPCTSTR>& lpszSoftName)
{
    std::vector<SoftInfo>::iterator iter;
    for (iter = m_SystemPatchesArr.begin(); iter != m_SystemPatchesArr.end(); iter++)
    {
        lpszSoftName.push_back(iter->m_strSoftName);
    }
}


Related articles: