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
topRequirements
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 topCreate a Windows Installer File
To create a Windows Installer file, create and then build a setup project. To do this, follow these steps:
- Start Visual Studio .NET.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click
Setup and Deployment Projects.
- Under Templates, click
Setup Project.
- In the Name text box, type
MySetup.
- Click OK.
- On the Build menu, click Build
MySetup.
back to the topCreate a Console Application
To create a console application that you will call Windows
Installer functions from, follow these steps:
- On the File menu
in Visual Studio .NET, point to New, and then click
Project.
- Under Project Types, click
Visual Basic Projects.
- Under Templates, click
Console Application.
- Click Add to Solution.
- Click OK. By default, the Module1.vb file
is created.
back to the topDeclare 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 "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 topAdd 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 "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 topCall 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 = "C:\MySetup\Debug\MySetup.msi"
- 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 topSample 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 topVerify 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 - Press ENTER to quit your
application.
back to the
top