65.9K
CodeProject 正在变化。 阅读更多。
Home

通过 VB.NET 调用 MSI.DLL 函数

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.75/5 (7投票s)

2007年3月2日

CPOL
viewsIcon

51851

有用的代码,用于通过 VB.NET 调用 MSI.DLL 函数。

引言

我们公司有很多应用程序几乎每天都在更新。我们希望向 VB.NET 项目添加一个模块,该模块可以打开一个 MSI 文件,并确定网络上最新 MSI 中的 exe 与当前运行的版本是否不同。如果安装包中的 .exe 文件与正在运行的文件不同,那么我们就需要重新安装。

在网上有一些 VBScript 示例,用于使用 MSI.DLL 查询 MSI 文件,但没有 .NET 示例。因此,在花了一个下午的时间弄清楚之后,我们决定分享一下。

要使用此代码,只需在项目中添加对 MSI.DLL (通常位于 system32 中) 的引用即可。

Public Function GetRevisionFromMSI(ByVal sMSIFilePath As String, _
                                   ByVal sEXEFileName As String) As String
    Dim oInstaller As WindowsInstaller.Installer
    Dim oDb As WindowsInstaller.Database
    Dim oView As WindowsInstaller.View = Nothing
    Dim oRecord As WindowsInstaller.Record
    Dim sSQL As String
    Dim sRevision As String = "Not Found"
    Try
        oInstaller = CType(CreateObject("WindowsInstaller.Installer"), _
                           WindowsInstaller.Installer)
        oDb = oInstaller.OpenDatabase(sMSIFilePath, 0)
        sSQL = "SELECT `Component`.`ComponentId`,`File`.`FileName`," &_
               `File`.`Version`" _
            & ",`Component`.`Condition`" _
            & " FROM `Component`,`File` WHERE `Component`.`Component` = " &_
              "`File`.`Component_`"
        oView = oDb.OpenView(sSQL)
        oView.Execute()
        Do
            oRecord = oView.Fetch
            If oRecord Is Nothing Then Exit Do
            If oRecord.StringData(2).ToUpper.Contains(sEXEFileName.ToUpper)_
            And (oRecord.StringData(2).ToUpper.Contains("CONFIG") = False) _
            Then
                oTrace.WriteTraceLog("GetRevisionFromMSI", _
                      "Version Located " & oRecord.StringData(3), False, 1)
                sRevision = oRecord.StringData(3)
            End If
        Loop
        Return sRevision
    Catch ex As Exception
        Return sRevision
    Finally
        oRecord = Nothing
        If Not (oView Is Nothing) Then
            oView.Close()
        End If
        oView = Nothing
        oDb = Nothing
        oInstaller = Nothing
    End Try
End Function
© . All rights reserved.