HOW TO: Call Windows Installer Functions by Using Visual Basic .NET (827024)



The information in this article applies to:

  • Microsoft Windows Installer
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SUMMARY

This step-by-step article describes how to call Microsoft Windows Installer functions by using Microsoft Visual Basic .NET.

This article describes how to call the MsiOpenDatabase function. You can use the MsiOpenDatabase function to open a Windows Installer file for data access. This article also describes how to call the MsiCloseHandle function. You can use the MsiCloseHandle function to close an open installation handle.

This article also contains sample code that illustrates the concepts that this article discusses. You can use the guidelines that this article discusses to call other Windows Installer functions in a similar way.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET Setup Projects
  • Windows Installer Functions
  • Programming by using Visual Basic .NET
  • Platform invoke services in Visual Basic .NET
back to the top

Create a Windows Installer File

To create a Windows Installer file, create and then build a setup project. To do this, follow these steps:
  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Setup and Deployment Projects.
  4. Under Templates, click Setup Project.
  5. In the Name text box, type MySetup.
  6. Click OK.
  7. On the Build menu, click Build MySetup.
back to the top

Create a Console Application

To create a console application that you will call Windows Installer functions from, follow these steps:
  1. On the File menu in Visual Studio .NET, point to New, and then click Project.
  2. Under Project Types, click Visual Basic Projects.
  3. Under Templates, click Console Application.
  4. Click Add to Solution.
  5. Click OK. By default, the Module1.vb file is created.
back to the top

Declare References to External Procedures That Are in Msi.dll

To call unmanaged functions from your managed Visual Basic .NET application, use the Declare keyword to declare references to the unmanaged functions. To do this, follow these steps:
  1. In the Module1.vb file, locate the following code:
    Module Module1
  2. Paste the following code after the code that you located in step 1:
    Private Declare Auto Function MsiOpenDatabase Lib "msi.dll" _
       (ByVal szDatabasePath As String, ByVal szPersist As IntPtr, _
          ByRef phDatabase As IntPtr) As Integer
    
    Private Declare Auto Function MsiCloseHandle Lib "msi.dll" _
       (ByVal hAny As Integer) As Integer
back to the top

Add Initialization Code

To declare local variables that you can use in your code, and to then initialize these local variables, follow these steps:
  1. In the Module1.vb file, locate the following code:
    Sub Main()
  2. Paste the following code after the code that you located in step 1:
    Dim szDatabasePath As String
    ' Initialize the szPersist variable with the integral value "1".
    Dim szPersist As New IntPtr(1)
    Dim phDatabase As IntPtr
    Dim Result As Integer
    
    ' Set the szDatabasePath variable to the path of your MySetup.msi file.
    szDatabasePath = "C:\MySetup\Debug\MySetup.msi"
    Note Replace "C:\MySetup\Debug\MySetup.msi" with the path of the MySetup.msi file that you created in step 7 of the "Create a Windows Installer File" section of this article.
back to the top

Call Windows Installer Functions

To call Windows Installer functions by using Visual Basic .NET, follow these steps:
  1. In the Module1.vb file, locate the code that corresponds to the following code:
    szDatabasePath = "C:\MySetup\Debug\MySetup.msi"
  2. Paste the following code after the code that you located in step 1:
    ' Open the MySetup.msi file in transaction mode.
    Result = MsiOpenDatabase(szDatabasePath, szPersist, phDatabase)
    If Result = 0 Then
       Console.WriteLine("The call to MsiOpenDatabase succeeded")
    Else
       Console.WriteLine("The call to MsiOpenDatabase failed")
    End If
    ' Close the phDatabase handle.
    Result = MsiCloseHandle(phDatabase.ToInt32())
    If Result = 0 Then
       Console.WriteLine("The call to MsiCloseHandle succeeded")
    Else
       Console.WriteLine("The call to MsiCloseHandle failed")
    End If
    
    Console.WriteLine("Press ENTER to quit")
    Console.ReadLine()
back to the top

Sample Code Listing (Module1.vb)

Note In this code, replace "C:\MySetup\Debug\MySetup.msi" with the path of the MySetup.msi file that you created in step 7 of the "Create a Windows Installer File" section of this article.
Option Strict On

Module Module1

   Private Declare Auto Function MsiOpenDatabase Lib "msi.dll" _
      (ByVal szDatabasePath As String, ByVal szPersist As IntPtr, _
         ByRef phDatabase As IntPtr) As Integer

   Private Declare Auto Function MsiCloseHandle Lib "msi.dll" _
      (ByVal hAny As Integer) As Integer

   Sub Main()

      Dim szDatabasePath As String
      ' Initialize the szPersist variable with the integral value "1".
      Dim szPersist As New IntPtr(1)
      Dim phDatabase As IntPtr
      Dim Result As Integer

      ' Set the szDatabasePath variable to the path of your MySetup.msi file.
      szDatabasePath = "C:\MySetup\Debug\MySetup.msi"

      ' Open the MySetup.msi file in transaction mode.
      Result = MsiOpenDatabase(szDatabasePath, szPersist, phDatabase)
      If Result = 0 Then
         Console.WriteLine("The call to MsiOpenDatabase succeeded")
      Else
         Console.WriteLine("The call to MsiOpenDatabase failed")
      End If
      ' Close the phDatabase handle.
      Result = MsiCloseHandle(phDatabase.ToInt32())
      If Result = 0 Then
         Console.WriteLine("The call to MsiCloseHandle succeeded")
      Else
         Console.WriteLine("The call to MsiCloseHandle failed")
      End If

      Console.WriteLine("Press ENTER to quit")
      Console.ReadLine()

   End Sub

End Module
back to the top

Verify That Your Code Works

To verify that your code works, follow these steps:
  1. On the Debug menu, click Start to run your application. A console window appears that contains the following text:The call to MsiOpenDatabase succeeded
    The call to MsiCloseHandle succeeded
    Press ENTER to quit
  2. Press ENTER to quit your application.
back to the top

Modification Type:MinorLast Reviewed:5/23/2005
Keywords:kbSDK kbDLL kbAPI kbPDWizard kbIDEProject kbinterop kbcode kbSample kbProgramming kbsetup kbMarshal kbDeployment kbAppSetup kbHOWTOmaster KB827024 kbAudDeveloper