获取安装软件列表

问题描述:

可能重复:用C
Get Installed software list获取安装软件列表

如何获得安装的应用软件列表?我在C#中有一个解决方案。这是C#代码:

System Microsoft.Win32 
private string Getinstalledsoftware() { 
    string Software = null; 
    string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey)) { 
     foreach (string skName in rk.GetSubKeyNames()) { 
      using (RegistryKey sk = rk.OpenSubKey(skName)) { 
       try { 
        if (!(sk.GetValue("DisplayName") == null)) { 
         if (sk.GetValue("InstallLocation") == null) 
          Software += sk.GetValue("DisplayName") + " - Install path not known\n"; 
         else 
          Software += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; 
        } 
       } 
       catch (Exception ex) { 
       } 
      } 
     } 
    } 
    return Software; 
} 

如何将C#代码转换为C代码?

+3

如果你在C#中有一个工作解决方案,为什么要将它转换为C? – 2011-05-09 05:07:22

+1

我们不在项目中使用c#代码,请您帮忙。 – user735936 2011-05-09 05:12:37

你可以例如为:

  1. 使用.NET Reflector看到注册表的功能是如何实际执行,然后
  2. 看看微软Registry Functions在C/C使用它们++。

当然,你也可以直接跳转到步骤2,而不步骤1

您需要映射的操作上执行了自己相应的Windows API registry functionsstring manipulation功能,如果你想进行翻译。

否则,您可以使用Windows Installer API直接执行此操作。

下面是使用安装程序API倾销产品的一个示例:

#include <stdio.h> 
#include <Windows.h> 
#include <Msi.h> 

static UINT msierrno; 

#define PRODUCT_NAME_SIZE 512 
#define INSTALL_LOCATION_SIZE 512 

int main(void) 
{ 
    DWORD index; 
    TCHAR productCode[39]; 
    for (index = 0; (msierrno = MsiEnumProducts(index, productCode)) == ERROR_SUCCESS; index++) 
    { 
     TCHAR productName[PRODUCT_NAME_SIZE]; 
     TCHAR installLocation[INSTALL_LOCATION_SIZE]; 
     DWORD productNameSize = PRODUCT_NAME_SIZE; 
     DWORD installLocationSize = INSTALL_LOCATION_SIZE; 

     wprintf(L"product code: %s\n", productCode); 
     if ((msierrno = MsiGetProductInfo(productCode, INSTALLPROPERTY_INSTALLEDPRODUCTNAME, productName, &productNameSize)) != ERROR_SUCCESS) 
     { 
      /* handle error */ 
     } 
     else wprintf(L"\tproduct name: %s\n", productName); 
     if ((msierrno = MsiGetProductInfo(productCode, INSTALLPROPERTY_INSTALLLOCATION, installLocation, &installLocationSize)) != ERROR_SUCCESS) 
     { 
      /* handle error */ 
     } 
     else wprintf(L"\tinstall location: %s\n", installLocation); 
    } 
    return 0; 
} 

附注,你需要链接到msi.lib/dll库。