Hi all,

This is kind of related to another thread I posted in C++, but I thought I'd go the VB.NET route and have a co-worker help with the C++ conversion if needed.

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

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       
    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

    iInstaller = CType(CreateObject("WindowsInstaller.Installer"),  _
                   WindowsInstaller.Installer)

    strPatchPath = "C:\Program Files (x86)\MyComp\Adept80\MyApp\Install\MyAppPatch.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
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...

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)\MyComp\MyApp\Client\Install\MyAppPatch.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...

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...

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!!!

Here's the error description that I get from code snippet 1

9509677dacc6c1acae4970cd0c78f310

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.