Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27355

Help with Some Windows Installer Code

$
0
0
Hi all,

I'm trying to get some code together to check if a patch (.msp) has been applied to a product.

I hope I'm getting pretty close with this...

Code:

Const MinMsiVersion = "3.0" 'Minimum version to support functionality
          Const MSIPATCHSTATE_APPLIED = 1 'Patch is applied to this product instance.
          Const msiInstallContextMachine = 4 'Enumerate products that are under the machine account.

        Dim iInstaller As WindowsInstaller.Installer
        Dim pPatch 'As WindowsInstaller.Patch
        '= CreateObject("WindowsInstaller.Installer")

        Dim strPatchPath, _
            strPatchCode, _
            strProdCode As String
        Dim strPatchXML As String
        Dim xmlMsiPatch As XmlDocument = New XmlDocument()

        Try
            iInstaller = CType(CreateObject("WindowsInstaller.Installer"),  _
                          WindowsInstaller.Installer)
           
            If MinMsiVersion > iInstaller.Version Then
                MsgBox("Minimum Windows Installer version " & MinMsiVersion & " required.  Current version is " & iInstaller.Version, MsgBoxStyle.OkOnly, "Windows Installer version problem...")
            End If

            strPatchPath = "C:\Program Files (x86)\Synergis\Adept80\Client\Install\AdeptPatch.msp"
            strPatchXML = iInstaller.ExtractPatchXMLData(strPatchPath)

            xmlMsiPatch.LoadXml(strPatchXML)

            strPatchCode = xmlMsiPatch.DocumentElement.Attributes("PatchGUID").Value
            strProdCode = xmlMsiPatch.GetElementsByTagName("TargetProductCode").Item(0).InnerText

            pPatch = iInstaller.Patch(strPatchCode, strProdCode, "", msiInstallContextMachine)

            If pPatch.State = MSIPATCHSTATE_APPLIED Then 'already applied
                MsgBox("Write to log... Patch has already been applied (" & pPatch.State & ")!")
            Else
                MsgBox("I'm installing the patch")
            End If

        Catch ex As Exception
            MsgBox(Err.Number)
            MsgBox(Err.Description)
            'MsgBox(ex.Message)
            MsgBox(ex.ToString)

            'ERROR_PATCH_NOT_APPLIED
        End Try

The code is failing on...

Code:

pPatch = iInstaller.Patch(strpatchCode, strProdCode, "", msiInstallContextMachine)
The Error Number is 13 - Return Arguement Has An Invalid Type.

Can anyone help me correct this?

Since it may have to be ported to C++ I'm wondering if it would be better to use this code instead...

Code:

Const MinMsiVersion = "3.0" 'Minimum version to support functionality
          Const MSIPATCHSTATE_APPLIED = 1 'Patch is applied to this product instance.
          Const msiInstallContextMachine = 4 'Enumerate products that are under the machine account.

        Dim iInstaller = CreateObject("WindowsInstaller.Installer")
   
        Dim strPatchPath, _
            strPatchCode, _
            strProdCode As String
        Dim strPatchXML As String
        Dim xmlMsiPatch As XmlDocument = New XmlDocument()

        Try           
            If MinMsiVersion > iInstaller.Version Then
                MsgBox("Minimum Windows Installer version " & MinMsiVersion & " required.  Current version is " & iInstaller.Version, MsgBoxStyle.OkOnly, "Windows Installer version problem...")
            End If

            strPatchPath = "C:\Program Files (x86)\Synergis\Adept80\Client\Install\AdeptPatch.msp"
            strPatchXML = iInstaller.ExtractPatchXMLData(strPatchPath)

            xmlMsiPatch.LoadXml(strPatchXML)

            strPatchCode = xmlMsiPatch.DocumentElement.Attributes("PatchGUID").Value
            strProdCode = xmlMsiPatch.GetElementsByTagName("TargetProductCode").Item(0).InnerText

            Dim pPatch = iInstaller.Patch(strPatchCode, strProdCode, "", msiInstallContextMachine)

            If pPatch.State = MSIPATCHSTATE_APPLIED Then 'already applied
                MsgBox("Write to log... Patch has already been applied (" & pPatch.State & ")!")
            Else
                MsgBox("I'm installing the patch")
            End If

        Catch ex As Exception
            MsgBox(Err.Number)
            MsgBox(Err.Description)
            'MsgBox(ex.Message)
            MsgBox(ex.ToString)

            'ERROR_PATCH_NOT_APPLIED
        End Try

However this code snippet was failing on...

Code:

If pPatch.State = MSIPATCHSTATE_APPLIED Then
The error number I eventually was able to get from it was -21470223249 which after digging a bit I found was ERROR_PATCH_NOT_APPLIED.

I'm not currently able to handle if the patch has not been applied with this code. If the patch is installed, the return, 1, is fine and the log write would occur (in this case msgbox). If not installed or applied, BOOM!

In the first code snipped, these were the imports...

Code:

Imports WindowsInstaller
Imports System
Imports System.Xml

In the second code segment, Windowsinstaller was commented out.

I hope someone can help me as I think/hope I am very close to having some workable code to detect if a patch has been applied!

Thanks in advance for any help!!!

Viewing all articles
Browse latest Browse all 27355


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>