subscribe to the RSS Feed

Saturday, May 19, 2012

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

By TechSupport


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

Applications Supported:

COPYRIGHT NOTICE: (c) 2007 Microsoft Corporation. All rights reserved.

SUPPORT SOLUTION:
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..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 2003Microsoft Visual Studio .NETThis
article assumes that you are familiar with the following topics:
Visual Studio .NET Setup ProjectsWindows Installer FunctionsProgramming by using Visual Basic .NETPlatform invoke services in Visual Basic .NET.Create 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..Create 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..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 Module1Paste 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.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 “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..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 = “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().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.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 succeededThe call to
MsiCloseHandle succeededPress ENTER to
quitPress ENTER to quit your
application..For more information, visit the following Microsoft
Developer Network (MSDN) Web sites:Setup Projectshttp://msdn2.microsoft.com/en-us/library/996a3fxs(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/996a3fxs(vs.71).aspx)
Installer
Functionshttp://msdn2.microsoft.com/en-us/library/aa369425.aspx
(http://msdn2.microsoft.com/en-us/library/aa369425.aspx)
MsiCloseHandlehttp://msdn2.microsoft.com/en-us/library/aa370067.aspx
(http://msdn2.microsoft.com/en-us/library/aa370067.aspx)
Database
Functionshttp://msdn2.microsoft.com/en-us/library/aa368250.aspx
(http://msdn2.microsoft.com/en-us/library/aa368250.aspx)
MsiOpenDatabasehttp://msdn2.microsoft.com/en-us/library/aa370338.aspx
(http://msdn2.microsoft.com/en-us/library/aa370338.aspx)
Interoperating
with Unmanaged Codehttp://msdn2.microsoft.com/en-us/library/sd10k43k(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/sd10k43k(vs.71).aspx)
.

For File Repair and Data Recovery, visit File Repair / Data Recovery