Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Calling MSI.DLL functions through VB.NET

1.75/5 (7 votes)
2 Mar 2007CPOL 2  
Useful code for calling MSI.DLL functions through VB.NET.

Introduction

We have plenty of apps in my company that are updated on an almost a daily basis. We wanted to add a module to a VB.NET project that would open an MSI file and determine if the exe in the latest MSI on the network was different than the version currently running. If the .exe file in the install package is different than the one that is running, then we needed to re-install.

There were some VBScript examples for querying an MSI file using MSI.DLL on the Web, but no .NET examples. So after blowing an afternoon figuring this out, we figured we'd share it.

To use the code, just include a reference to MSI.DLL (usually found in system32) in your project.

VB
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)