How to Find Out If Executable Is Running in VB Develop Shell

ID: Q113680


The information in this article applies to:
  • Microsoft Visual Basic programming system for Windows, version 3.0


SUMMARY

If a Visual Basic application needs to know if it is running in the Visual Basic development shell, the application can call the Windows API ModuleFileName function to find out what its module name is. If the ModuleFileName function returns VB.EXE, it is running in the Visual Basic development shell.


MORE INFORMATION

The following example shows how to find out if a program is running interpreted within the Visual Basic development shell as opposed to running as a compiled executable:

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add the following code to the general declarations section of Form1:
    
       ' Enter each of the following Declare statements as one, single line:
       Declare Function GetModuleFileName Lib "Kernel"
          (ByVal hModule As Integer, ByVal lpFilename As String,
          ByVal nSize As Integer) As Integer
       Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer,
          ByVal nIndex As Integer) As Integer
    
       Const GWW_HINSTANCE = (-6)
     


  3. Add the following code to the Form_Load event of Form1:
    
       Sub Form_Load ()
          Dim ModuleName As String
          Dim FileName As String
          Dim hInst, ret As Integer
          ModuleName = String$(128, Chr$(0))
          ' Get the hInstance application:
          hInst = GetWindowWord(Me.hWnd, GWW_HINSTANCE)
          ' Get the ModuleFileName:
          ' Enter the following two lines as one, single line:
          ModuleName = Left$(ModuleName,
             GetModuleFileName(hInst, ModuleName, Len(ModuleName)))
          If (Len(ModuleName)) > 0 Then
          ' Get the "." in the file name. Then go back three characters.
          ' FileName should = \VB.EXE, so check for the backslash (\)
          ' because FileName could be GVB.EXE, which isn't the
          ' VB executable name:
             FileName = Mid$(ModuleName, InStr(ModuleName, ".") - 3)
             If FileName = "\VB.EXE" Then
                MsgBox "In VB development Shell"
             Else
                MsgBox "Not in VB development Shell"
             End If
          End If
       End Sub
     


  4. Run the program.


If you are running within the Visual Basic environment, you will get the "In VB development Shell" message box. If you are running a compiled executable, you will get the "Not in VB development Shell" message.

Additional query words: 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: June 14, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.