HOWTO: Call LoadModule() API Function from Visual Basic

Last reviewed: September 29, 1997
Article ID: Q83350
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

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

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

  2. 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 "Kernel" (lp1 As Any, lp2 As Any) As Long
       ' Enter the following Declare statement on one, single line
       Declare Function LoadModule% Lib "kernel" (ByVal lpModuleName As String,
          lpParameterBlock As Any)
    
    

  3. 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$ = "c:\windows\winver.exe" + Chr$(0)
          pb.lpCmdLine = lstrcpy(ByVal lpCmdLine$, ByVal lpCmdLine$)
          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.

Keywords          : kbprg kbfasttip
Version           : WINDOWS:2.0 3.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.