使用windows api读取应用程序的当前安装版本

本文关键字:安装 版本 应用程序 windows api 读取 使用 | 更新日期: 2023-09-27 18:08:17

我正在尝试使用windows api查找已安装应用程序的版本信息。

我使用升级代码来查找使用MsiEnumRelatedProducts api的产品代码,但是当我尝试使用MsiGetProductInfo使用产品代码时,版本信息返回为垃圾。

这是我的MsiGetProductInfo api:

[DllImport("msi.dll", CharSet = CharSet.Unicode)]
private static extern Int32 MsiGetProductInfo(
    string product, string property, [Out] StringBuilder valueBuf,
    ref Int32 len);
MsiGetProductInfo(sbProductCode, "INSTALLPROPERTY_INSTALLVERSION", builder, ref len);

你知道我做错了什么吗?

使用windows api读取应用程序的当前安装版本

在对@JoshHetland的响应中,要传递的字符串是INSTALLPROPERTY_VERSIONSTRING的CamelCase后缀-记住MSI是区分大小写的。

:

INSTALLPROPERTY_VERSIONSTRING变为VersionString

INSTALLPROPERTY_INSTALLDATE变为InstallDate

等等

可用属性的完整列表在MSDN页面上的MsiGetProductInfo函数。

我是这样解决我的问题的

        Int32 m_len = 11512;
        StringBuilder m_versionInfo = new StringBuilder(m_len);
        StringBuilder m_sbProductCode = GetProductCodeFromMsiUpgradeCode();
        MsiGetProductInfo(m_sbProductCode.ToString(), "**VersionString**", m_versionInfo, ref m_len);
        return m_versionInfo.ToString();

这确实返回了版本字符串,并且还从十进制转换为字符串格式,如1.4.3。

应用。ProductVersion为我工作,不需要手动调用WinAPI(虽然我仍然在。net 1.1)