HOWTO: Tell Whether an App Runs in VB Design Environment

Last reviewed: February 26, 1997
Article ID: Q118819
3.00 WINDOWS kbenv kbprg kbcode

The information in this article applies to:

  • Standard and Professional Editions of Microsoft Visual Basic Programming System for Windows, version 3.0

SUMMARY

This article describes how a Visual Basic application can determine whether it is running in the design environment or as an executable file. Two ways that Visual Basic does this are explained.

MORE INFORMATION

Visual Basic provides the APP object, which has the property EXENAME. APP.EXENAME reports the name of the executable file when it runs as an executable file. However, in the design environment, APP.EXENAME reports the name of the project. If you use different names for the project and the executable file, then you can use APP.EXENAME to determine whether an application is running in the Visual Basic design environment or as an executable file.

You can also use the Windows API to determine whether an application is running in the Visual Basic Design Environment or as an executable file. When running in the design environment, the application's module name is "VB." However, as an executable file the module name matches the executable file name that is chosen when compiling the application to an executable file from Visual Basic.

Example

You can determine the module name by using the functions GetCurrentTask() and TaskFindHandle() from the Windows API. The following example illustrates how to use the functions to determine whether the application is running in the Visual Basic design environment:

  1. Start a new project (Form1 is created by default).

  2. Add a new module to the program (MODULE1.BAS by default).

  3. Place the following code in the module:

          Type TASKENTRY
    
             dwSize As Long
             hTask As Integer
             hTaskParent As Integer
             hInst As Integer
             hModule As Integer
             wSS As Integer
             wSP As Integer
             wStackTop As Integer
             wStackMinimum As Integer
             wStackBottom As Integer
             wcEvents As Integer
             hQueue As Integer
             szModule As String * 10
             wPSPOffset As Integer
             hNext As Integer
          End Type
       
          ' The following declare must be entered on a single line
          Declare Function TaskFindHandle Lib "Toolhelp" (lpte As TASKENTRY,
             ByVal hTask As Integer) As Integer
          Declare Function GetCurrentTask Lib "Kernel" () As Integer
       
          Function VBDesignEnvironment () As Integer
             Dim TE As TASKENTRY
             Dim ModuleName As String
             Dim hTask As Integer
             Dim r
       
             hTask = GetCurrentTask()
             TE.dwSize = Len(TE)
             r = TaskFindHandle(TE, hTask)
             ModuleName = Left(TE.szModule, InStr(TE.szModule, Chr(0)) - 1)
       
             If ModuleName = "VB" Then
                VBDesignEnvironment = True
             Else
                VBDesignEnvironment = False
             End If
          End Function
       
    
4. Add the following code to the load event of the form:
   
      Sub Form_Load ()
         Me.Show
         If VBDesignEnvironment() Then
            Print "Design Environment"
         Else
            Print "Executable"
         End If
      End Sub

  • Save the project.

  • Run the application in the design environment. The form should display "Design Environment".

  • Make an executable file from the project.

  • Run the executable file from File Manager. The form should display "Executable".

    Notes

    • The function TaskFindHandle() is in the TOOLHELP.DLL file. This dynamic- link library does not come with Windows version 3.0.
    • You may want to add code to the VBDesignEnvironment() function that checks for errors when returning from GetCurrentTask() and TaskFindHandle().


  • KBCategory: kbenv kbprg kbcode
    KBSubcategory: EnvtRun
    Additional reference words: 3.00 .EXE VB.EXE debug run-time IDE
    Keywords : EnvtRun kbcode kbenv kbprg
    Version : 3.00
    Platform : WINDOWS


    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: February 26, 1997
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.