Microsoft KB Archive/827024

= HOW TO: Call Windows Installer Functions by Using Visual Basic .NET =

Article ID: 827024

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Windows Installer 2.0
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Windows Installer File
 * Create a Console Application
 * Declare References to External Procedures That Are in Msi.dll
 * Add Initialization Code
 * Call Windows Installer Functions
 * Sample Code Listing (Module1.vb)
 * Verify That Your Code Works
 * REFERENCES



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:   In the Module1.vb file, locate the following code: Module Module1   Paste the following code after the code that you located in step 1: Private Declare Auto Function MsiOpenDatabase Lib &quot;msi.dll&quot; _ (ByVal szDatabasePath As String, ByVal szPersist As IntPtr, _     ByRef phDatabase As IntPtr) As Integer

Private Declare Auto Function MsiCloseHandle Lib &quot;msi.dll&quot; _ (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:   In the Module1.vb file, locate the following code: Sub Main   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 &quot;1&quot;. 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 = &quot;C:\MySetup\Debug\MySetup.msi&quot; Note Replace &quot;C:\MySetup\Debug\MySetup.msi&quot; with the path of the MySetup.msi file that you created in step 7 of the &quot;Create a Windows Installer File&quot; 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:   In the Module1.vb file, locate the code that corresponds to the following code: szDatabasePath = &quot;C:\MySetup\Debug\MySetup.msi&quot; </li>  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(&quot;The call to MsiOpenDatabase succeeded&quot;) Else Console.WriteLine(&quot;The call to MsiOpenDatabase failed&quot;) End If ' Close the phDatabase handle. Result = MsiCloseHandle(phDatabase.ToInt32) If Result = 0 Then Console.WriteLine(&quot;The call to MsiCloseHandle succeeded&quot;) Else Console.WriteLine(&quot;The call to MsiCloseHandle failed&quot;) End If

Console.WriteLine(&quot;Press ENTER to quit&quot;) Console.ReadLine </li></ol>

back to the top

Sample Code Listing (Module1.vb)
Note In this code, replace &quot;C:\MySetup\Debug\MySetup.msi&quot; with the path of the MySetup.msi file that you created in step 7 of the &quot;Create a Windows Installer File&quot; section of this article. Option Strict On

Module Module1

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

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

Sub Main

Dim szDatabasePath As String ' Initialize the szPersist variable with the integral value &quot;1&quot;. 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 = &quot;C:\MySetup\Debug\MySetup.msi&quot;

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

Console.WriteLine(&quot;Press ENTER to quit&quot;) Console.ReadLine

End Sub

End Module back to the top

Verify That Your Code Works
To verify that your code works, follow these steps:  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

</li> Press ENTER to quit your application.</li></ol>

back to the top

<div class="references_section">