Article ID: 118819
Article Last Modified on 7/13/2004
APPLIES TO
- Microsoft Visual Basic 3.0 Professional Edition
- Microsoft Visual Basic 3.0 Professional Edition
This article was previously published under Q118819
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:
- Start a new project (Form1 is created by default).
- Add a new module to the program (MODULE1.BAS by default).
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
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().
Additional query words: 3.00 .EXE VB.EXE debug run-time IDE
Keywords: kbhowto KB118819