获取已安装的软件列表

发布于 2024-11-06 01:14:01 字数 1203 浏览 1 评论 0原文

可能的重复:
获取已安装的软件列表

如何使用C获取已安装的应用程序软件列表?我有一个 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 代码?

Possible Duplicate:
Get Installed software list

How to get installed application software list using C? I have a solution in C#. This is the C# code:

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; 
}

How can I convert the C# code into C code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-11-13 01:14:01

您需要将此处执行的操作映射到其相应的 Windows API 注册表函数和其他字符串操作函数,如果您希望进行翻译。

否则,您可能可以使用 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;
}

ps,您需要链接到 msi.lib/dll 库。

You need to map the operations performed here to their corresponding Windows API registry functions and other string manipulation functions if you wish to perform the translation.

Otherwise you could probably use the Windows Installer API to do this directly.

Here's an example that dumps the products using the installer 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;
}

p.s., you'll need to link to the msi.lib/dll library.

我很坚强 2024-11-13 01:14:01

您可以例如:

  1. 使用 .NET Reflector 来查看注册表功能的实际实现方式,然后
  2. 看一下Microsoft 注册表函数来使用它们在 C/C++ 中。

当然你也可以不进行步骤1,直接进行步骤2。

You could e.g.:

  1. Use .NET Reflector to see how the registry functions are actually implemented, then
  2. Take a look at the Microsoft Registry Functions to use them in C/C++.

Of course you can also directly move to step 2 without step 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文