Microsoft KB Archive/83350

= How To Call LoadModule API Function from Visual Basic =

Article ID: 83350

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q83350



SUMMARY
This article demonstrates how to call the Windows LoadModule API function from a Visual Basic program. The LoadModule API function loads and executes a Windows program or creates a new instance of an existing Windows program. The code example below shows an example of calling WINVER.EXE with the LoadModule function call, but you can change it to any executable file.

NOTE: The Shell function provided in Visual Basic provides a functionality similar to and simpler than the technique explained in this article.

NOTE: Only 16-bit implementations of VBA support the sample code in this article.



MORE INFORMATION
The LoadModule API function call has only two parameters, but the second parameter is a pointer to a structure with an embedded structure in it.

The two parameters are as follows: lpModuleName     Points to a null terminated string that contains the filename of the application to be run.

lpParameterBlock Points to a data structure consisting of four fields that define a parameter block. The data structure consists of the following fields:

wEnvSeg:   Specifies the segment address of the environment under which the module is to run; 0 indicates that the Windows environment is to be copied.

lpCmdLine:  Points to a NULL terminated character string that contains a correctly formed command line. This string must not exceed 120 bytes in length.

lpCmdShow:  Points to a data structure containing two WORD length values. The first value must be set to 2, and the second value in this example will be set to 5.

dwReserved: Reserved and must be NULL.

Steps to Reproduce Behavior
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.  Add the following code to the GLOBAL.BAS file (or any module in Visual basic version 2.0): Type CmdShow fp As Integer  ' first parameter sp As Integer  ' second parameter End Type

Type lpParameterBlock wEnvSeg As Integer lpCmdLine As Long     ' This line modified 6/25/93 lpCmdShow As Long     ' This line modified 5/27/92 dwReserved As Long End Type

Declare Function lstrcpy Lib &quot;Kernel&quot; (lp1 As Any, lp2 As Any) As Long ' Enter the following Declare statement on one, single line Declare Function LoadModule% Lib &quot;kernel&quot; (ByVal lpModuleName As String,     lpParameterBlock As Any)   Add a CommandButton to Form1, and add the following code to the Command1_Click procedure: Sub Command1_Click

Dim cs As CmdShow Dim pb As lpParameterBlock ' assign values to the CmdShow structure pb.lpCmdShow = lstrcpy(cs, cs)          ' Line added 5/27/92 cs.fp = 2 cs.sp = 5 ' assign values to the lpParameterBlock structure pb.wEnvSeg = 0 ' append null to end of path ' Following two lines added 6/25/93 replacing previous line: lpCmdLine$ = &quot;c:\windows\winver.exe&quot; + Chr$(0) pb.lpCmdLine = lstrcpy(ByVal lpCmdLine$, ByVal lpCmdLine$) pb.dwReserved = 0& ' make sure to append null to end of .EXE name m% = LoadModule%(&quot;winver.exe&quot; + Chr$(0), pb)

End Sub  Save the program and run it.

When you run the program and press the command button, the WinVer program will run as it would with the Run command on the Windows Program Manager File menu.

Keywords: kbhowto kbprogramming KB83350

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.