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

Last reviewed: June 21, 1995
Article 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 reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.