Click here to Skip to main content
Licence CPOL
First Posted 2 Mar 2007
Views 29,607
Bookmarked 10 times

Calling MSI.DLL functions through VB.NET

By | 2 Mar 2007 | Article
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.

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)

About the Author

Nibbs



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberminitech6:44 4 Dec '11  
GeneralWriteTraceLog PinmemberKevin Warner3:50 19 Sep '09  
Generalmsi locked after opendatabase Pinmembergreg chu6:57 14 Apr '09  
GeneralRe: msi locked after opendatabase PinmemberNibbs7:08 14 Apr '09  
GeneralRe: msi locked after opendatabase Pinmembera_elyoussoufi17:48 2 Aug '10  
GeneralMy vote of 1 PinmvpDave Kreskowiak10:14 17 Feb '09  
GeneralHaving trouble adding the reference PinmemberToshRa11:22 23 Jul '08  
QuestionMSP files [modified] Pinmembersvoskamp10:30 11 Mar '07  
AnswerRe: MSP files PinmemberNibbs11:15 12 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 2 Mar 2007
Article Copyright 2007 by Nibbs
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid