6 Replies - 2288 Views - Last Post: 29 June 2012 - 09:30 AM Rate Topic: -----

#1 sogilvie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Can't open MSI database for a query using VB 2010

Posted 28 June 2012 - 06:46 AM

Hi,

I am trying to query a MSI to get the ProductCode from the MSI so I can later populate a script with the proper ProductCode.

During debugging it "looks" like the database is open for read, however if I look at the oDB object I see this "Error = No further information on this object could be discovered" When it tries to do the OpenView on the SQL statement it throws an exception "OpenView, Sql" I believe that is because the MSI really isn't open for read?

I have added the reference to the Microsoft Windows Installer Object Library version 1.0.0.0

Any idea what is up, I have "googled" and can't find any other useful code :(


Here is the code: (I pass the fully qualified path + filename of the msi)
Private Sub GetMsiProperty(ByVal sMSIFilePath As String)
        Const sReadMSIDataBaseErrorStr As String = "ERROR:  Could not read MSI database!"
        Const msiOpenDatabaseModeReadOnly = 0
        Dim oInstaller As Installer
        Dim oDb As Database
        Dim oView As View = Nothing
        Dim oRecord As Record
        Try
            oInstaller = CType(CreateObject("WindowsInstaller.Installer"), Installer)
            oDb = oInstaller.OpenDatabase(sMSIFilePath, msiOpenDatabaseModeReadOnly)

            '"SELECT 'ProductCode' FROM 'Property'"
            '"SELECT FROM 'Property' WHERE `Property`='ProductCode'"
            '"SELECT `ProductCode` FROM `Property` WHERE `Property` = ProductCode"
            oView = oDb.OpenView("SELECT * FROM 'Property'")
            oView.Execute(oRecord)
            oRecord = oView.Fetch()
            While Not oRecord Is Nothing
                _sGUID = oRecord.StringData(1)
            End While
            MessageBox.Show("This is the MSI file: " + sMSIFilePath, "")
            MessageBox.Show("This is the GUID: " + _sGUID, "")
            Return

        Catch ex As Exception
            MessageBox.Show(sReadMSIDataBaseErrorStr + ": " + ex.Message, CreatePackageDirectoryWindows_OkButtonclick_CreatePackageDir_Win_Exception___, MessageBoxButtons.OK)
            Return
        Finally
            If Not (oView Is Nothing) Then
                Marshal.FinalReleaseComObject(oView)
                oView.Close()
            End If
        End Try
    End Sub



thanks,

Steve

Is This A Good Question/Topic? 0
  • +

Replies To: Can't open MSI database for a query using VB 2010

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,682
  • Joined: 02-July 08

Re: Can't open MSI database for a query using VB 2010

Posted 28 June 2012 - 08:54 AM

It may also help to explore your msi with Orka, if you haven't already.

This is the only thing I have done with msi properties.
Public Sub  GetRevisionFromMSI(ByVal sMSIFilePath 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
    Try
      oInstaller = CType(CreateObject("WindowsInstaller.Installer"), 
                         WindowsInstaller.Installer)
      oDb = oInstaller.OpenDatabase(sMSIFilePath, 0)
      sSQL = "Select Property, Value From Property"
      oView = oDb.OpenView(sSQL)
      oView.Execute()
      oRecord = oView.Fetch()
      While Not oRecord Is Nothing
        lsb.Items.Add(oRecord.StringData(1) & " :  " & oRecord.StringData(2))
        oRecord = oView.Fetch()
      End While
      Catch ex As Exception
      MessageBox.Show(ex.Message)
    Finally
      oRecord = Nothing
      If Not (oView Is Nothing) Then
        Marshal.FinalReleaseComObject(oView)
        oView = Nothing
      End If
      oDb = Nothing
      oInstaller = Nothing
    End Try
  End Sub


Was This Post Helpful? 0
  • +
  • -

#3 sogilvie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Can't open MSI database for a query using VB 2010

Posted 28 June 2012 - 09:28 AM

Must be my SQL strings that are causing the issue :(
"SELECT 'ProductCode' FROM 'Property'"
"SELECT FROM 'Property' WHERE `Property`='ProductCode'"
"SELECT `ProductCode` FROM `Property` WHERE `Property` = ProductCode"

crap :(

I am doing this program way...

Steve
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,682
  • Joined: 02-July 08

Re: Can't open MSI database for a query using VB 2010

Posted 28 June 2012 - 10:11 AM

That's the cool thing about Orca it shows you exactly what is in there to query.
Was This Post Helpful? -1
  • +
  • -

#5 sogilvie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Can't open MSI database for a query using VB 2010

Posted 28 June 2012 - 11:02 AM

got it to work using a work around, I check the record and see if it ProductCode, if it is, grab the value and stuff it into my variable... (I think I "borrowed" your code :))

sSQL = "Select Property, Value From Property"
            oView = oDb.OpenView(sSQL)
            oView.Execute()
            oRecord = oView.Fetch()
            While Not oRecord Is Nothing
                sRecord1 = oRecord.StringData(1)
                sRecord2 = oRecord.StringData(2)
                If sRecord1 = "ProductCode" Then
                    _sGUID = sRecord2
                End If
                oRecord = oView.Fetch()
            End While



Steve
Was This Post Helpful? 0
  • +
  • -

#6 sogilvie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Can't open MSI database for a query using VB 2010

Posted 29 June 2012 - 08:51 AM

grrr... single quotes...

Public Sub GetMsiProperty(ByVal sMSIFilePath As String)
        Const sReadMSIDataBaseErrorStr As String = "ERROR:  Could not read MSI database!"
        Dim oInstaller As Installer
        Dim oDb As Database
        Dim oView As View = Nothing
        Dim oRecord As Record
        Dim sSQL As String

        Try

            oInstaller = CType(CreateObject("WindowsInstaller.Installer"), Installer)
            oDb = oInstaller.OpenDatabase(sMSIFilePath, 0)
            sSQL = "Select Value from Property where Property='ProductCode'"
            oView = oDb.OpenView(sSQL)
            oView.Execute()
            oRecord = oView.Fetch()
            If Not (oRecord Is Nothing) Then
                _sGUID = oRecord.StringData(1)
            End If
            'MessageBox.Show("This is the GUID: " + _sGUID, "")
            Return

        Catch ex As Exception
            MessageBox.Show(sReadMSIDataBaseErrorStr + ": " + ex.Message, CreatePackageDirectoryWindows_OkButtonclick_CreatePackageDir_Win_Exception___, MessageBoxButtons.OK)
            Return
        Finally
            If Not (oView Is Nothing) Then
                Marshal.FinalReleaseComObject(oView)
            End If
        End Try
    End Sub



works like a charm...
Was This Post Helpful? 1
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 957
  • View blog
  • Posts: 3,682
  • Joined: 02-July 08

Re: Can't open MSI database for a query using VB 2010

Posted 29 June 2012 - 09:30 AM

Thanks for posting the solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1