获取安装软件列表
问题描述:
如何获得安装的应用软件列表?我在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代码?
答
答
您需要映射的操作上执行了自己相应的Windows API registry functions等string 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库。
如果你在C#中有一个工作解决方案,为什么要将它转换为C? – 2011-05-09 05:07:22
我们不在项目中使用c#代码,请您帮忙。 – user735936 2011-05-09 05:12:37