2010-03-18 105 views
3

嗨,我想知道什麼程序安裝在我的系統上,我使用MsiEnumProducts和MsiGetProductInfo函數,但不是所有應用程序,我可以在「添加/刪除程序」中看到我通過此功能。C++檢查已安裝的程序

回答

7

枚舉註冊表項:

HKEY_LOCAL_MACHINE \ SOFTWARE \微軟\的Windows \ CurrentVersion \卸載

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

我想補充的人看到這一點,並遇到錯誤,您需要啓用Unicode的在你的項目屬性:在「配置屬性」和「常規」下,將參數字符集更改爲使用Unicode字符集。 – ComethTheNerd 2014-06-23 21:58:53

+0

這很有趣,因爲我從來沒有開發過Unicode C++。我爲這個例子做過一次這樣的事情,因爲我認爲如果我不這樣做,我會遭到批評。 :) – NTDLS 2014-06-27 18:24:56

+0

安裝日期如何? – 2014-12-07 20:28:50