c++ check installed programms

前端 未结 1 1429
臣服心动
臣服心动 2021-01-06 09:54

How do I list all programs installed on my computer? I\'ve tried using the MsiEnumProducts and MsiGetProductInfo functions, but they do not return

相关标签:
1条回答
  • 2021-01-06 10:42

    Enumerate the registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

    bool EnumInstalledSoftware(void)
    {
        HKEY hUninstKey = NULL;
        HKEY hAppKey = NULL;
        WCHAR sAppKeyName[1024];
        WCHAR sSubKey[1024];
        WCHAR sDisplayName[1024];
        WCHAR *sRoot = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
        long lResult = ERROR_SUCCESS;
        DWORD dwType = KEY_ALL_ACCESS;
        DWORD dwBufferSize = 0;
    
        //Open the "Uninstall" key.
        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sRoot, 0, KEY_READ, &hUninstKey) != ERROR_SUCCESS)
        {
            return false;
        }
    
        for(DWORD dwIndex = 0; lResult == ERROR_SUCCESS; dwIndex++)
        {
            //Enumerate all sub keys...
            dwBufferSize = sizeof(sAppKeyName);
            if((lResult = RegEnumKeyEx(hUninstKey, dwIndex, sAppKeyName,
                &dwBufferSize, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
            {
                //Open the sub key.
                wsprintf(sSubKey, L"%s\\%s", sRoot, sAppKeyName);
                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sSubKey, 0, KEY_READ, &hAppKey) != ERROR_SUCCESS)
                {
                    RegCloseKey(hAppKey);
                    RegCloseKey(hUninstKey);
                    return false;
                }
    
                //Get the display name value from the application's sub key.
                dwBufferSize = sizeof(sDisplayName);
                if(RegQueryValueEx(hAppKey, L"DisplayName", NULL,
                    &dwType, (unsigned char*)sDisplayName, &dwBufferSize) == ERROR_SUCCESS)
                {
                    wprintf(L"%s\n", sDisplayName);
                }
                else{
                    //Display name value doe not exist, this application was probably uninstalled.
                }
    
                RegCloseKey(hAppKey);
            }
        }
    
        RegCloseKey(hUninstKey);
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题