How to Call LoadModule API Function from Visual Basic

ID Number: Q83350

1.00

WINDOWS

Summary:

This article shows 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 this can be changed

to any executable file.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

(Note that the Shell function provided in Visual Basic provides

a functionality similar to and simpler than the technique explained in

this article.)

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 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

---------------------------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Add the following code to the GLOBAL.BAS file:

Type CmdShow

fp As Integer ' first parameter

sp As Integer ' second parameter

End Type

Type lpParameterBlock

wEnvSeg As Integer

lpCmdLine As String

lpCmdShow As Long ' This line was modified 5/27/92

dwReserved As Long

End Type

Declare Function lstrcpy Lib "Kernel" (lp1 As Any, lp2 As Any) As Long

Declare Function LoadModule% Lib "kernel"

(ByVal lpModuleName As String, lpParameterBlock As Any)

'Note: the above Declare statement must be on one line

3. Add a command button 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

pb.lpCmdLine = "c:\windows\winver.exe" + Chr$(0)

pb.dwReserved = 0&

' make sure to append null to end of .EXE name

m% = LoadModule%("winver.exe" + Chr$(0), pb)

End Sub

4. 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.

Additional reference words: 1.00