Click here to Skip to main content
15,886,742 members
Articles / Programming Languages / Visual Basic

Calling MSI.DLL functions through VB.NET

Rate me:
Please Sign up or sign in to vote.
1.75/5 (7 votes)
2 Mar 2007CPOL 50.6K   12   9
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
minitech4-Dec-11 6:44
minitech4-Dec-11 6:44 
GeneralWriteTraceLog Pin
Kevin Warner19-Sep-09 3:50
professionalKevin Warner19-Sep-09 3:50 
Generalmsi locked after opendatabase Pin
greg chu14-Apr-09 6:57
greg chu14-Apr-09 6:57 
GeneralRe: msi locked after opendatabase Pin
Nibbs14-Apr-09 7:08
Nibbs14-Apr-09 7:08 
GeneralRe: msi locked after opendatabase Pin
a_elyoussoufi2-Aug-10 17:48
a_elyoussoufi2-Aug-10 17:48 
GeneralMy vote of 1 Pin
Dave Kreskowiak17-Feb-09 10:14
mveDave Kreskowiak17-Feb-09 10:14 
GeneralHaving trouble adding the reference Pin
ToshRa23-Jul-08 11:22
ToshRa23-Jul-08 11:22 
QuestionMSP files [modified] Pin
svoskamp11-Mar-07 10:30
svoskamp11-Mar-07 10:30 
AnswerRe: MSP files Pin
Nibbs12-Mar-07 11:15
Nibbs12-Mar-07 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.