Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
Testing for 'design time' or 'run time' in VB 6
Question: How can I test whether a program is running at design time or run time using VB6?
 Join the Discussion
Add to the FAQ!
Discuss this at the
About.Com Forum
 
 Related Articles
• Using VB with Win32 and other API's
• A Guide to the Win32 API
A review of the Dan Appleman classic book

 

Design Time vs Run Time

The first problem to solve in answering this question is to decide what the question means. To one set of programmers, running a program at design time means clicking the Start icon on the Visual Studio tool bar, selecting the Run > Start menu item, or just clicking F5 in Visual Studio. Run time means that you have compiled your program into an EXE or DLL file and that is what is being executed.

An alternative interpretation of the question is whether Visual Studio itself is in "Design Time" mode versus "Run Time" mode. See this note at MSDN for an explanation of this alternative definition.

This second interpretation is important in allowing VB 6 programmers to create controls where properties may only be available at Visual Studio's design time or run time. For example, the Name property can only be changed at design time and is read only at run time. If this is what you want to do, then Visual Studio and MSDN both have a lot of documentation available. Search on the terms Ambient and UserMode for complete instructions on coding Property Let and Property Get blocks correctly to create properties that behave the way you want them to in VB 6. (VB .NET no longer uses these blocks.)

Design Time vs Run Time

If you're trying to figure out how to implement the first interpretation ... that is, to be able to add code to your program that detects whether the program is running under the control of an IDE like Visual Studio, or whether it's being executed 'stand alone' using an EXE or DLL, you have an entirely different problem. An example reason why someone might want to do this is to determine whether a "run time only" version of a program is being executed as part of an IDE.

To the best of my knowledge, VB 6 doesn't provide a standard way to determine whether it's executing under the control of an IDE or not. (If you know of one, please let me know!) But there are ways to determine it. All of them depend on testing some "uniqueness" or "difference" between the IDE and the runtime environment.

One way is to test to see if the Module that is actually executing is one of the known runtimes for VB. The GetModuleHandle Win32 API handles this chore. (See the review of A Guide to the Win32 API, the Dan Appleman classic book.) Here's some sample code showing how that works. (To run this code, build a project with one form and one command button. Add this code to the code window.)

Private Declare Function GetModuleHandle _
	Lib "kernel32" Alias _
	"GetModuleHandleA" _
	(ByVal lpModuleName As String) As Long
Public Function TestEnvironment() As Boolean
Dim ModuleHandle As String
Dim EnvFileName As String
Dim EnvVal As Variant
Dim ReturnVal As Long
Dim i As Long
EnvVal = _
	Array("vb.exe", _
	"vb32.exe", _
	"vb5.exe", _
	"vb6.exe")
For i = LBound(EnvVal) To UBound(EnvVal)
   ModuleHandle = EnvVal(i)
   ReturnVal = GetModuleHandle(ModuleHandle)
   If ReturnVal <> 0 Then
      EnvFileName = ModuleHandle
      TestEnvironment = True
      Exit For
   End If
Next
End Function
 
Private Sub Command1_Click()
If TestEnvironment() = True Then
    MsgBox ("Running under IDE")
Else
    MsgBox ("Running as EXE")
End If
End Sub

Another way to do it is to use a different API and look for the Visual Studio window. This approach detects the Visual Basic IDE whenever it is open on a particular machine.

Private Declare Function FindWindow _
	Lib "user32" Alias _
	"FindWindowA" (ByVal lpClassName As String, _
	ByVal lpWindowName As String) As Long
Public Function TestWindow() As Boolean
Dim ReturnVal As Long
If FindWindow("IDEOwner", vbNullString) > 0 Then
      TestWindow = True
Else
      TestWindow = False
End If
End Function
 
Private Sub Command1_Click()
If TestWindow() = True Then
    MsgBox ("Running under IDE")
Else
    MsgBox ("Running as EXE")
End If
End Sub

Finally, we discovered two ways to avoid the use of API calls entirely by taking advantage of the fact that Debug doesn't get compiled into executables in VB 6! The first way traps an error when Debug.Print deliberately triggers an error (which doesn't happen if it's not there) and the second way uses the value of a boolean that is changed by Debug.Assert (which doesn't change it if it's not there). The second idea was suggested by About Visual Basic Reader Brad Buchanan who notes that "There is no error, and therefore no monkeying with error trapping. Single-stepping a program using F8 also seems more intuitive this way (at least to me)."

Way 1:
Private Function TestIDE() As Boolean
     On Error GoTo IDEInUse
     Debug.Print 1 \ 0 'division by zero error
     TestIDE = False
     Exit Function
IDEInUse:
     TestIDE = True
End Function

Private Sub Command1_Click()
If TestIDE() = True Then
    MsgBox ("Running under IDE")
Else
    MsgBox ("Running as EXE")
End If
End Sub
Way 2 (Contributed by Brad Buchanan):
'You can check this flag anywhere in your code
Public IsIDE As Boolean
Private Sub Form_Load()
    IsIDE = False
    'This line is only executed if
    'running in the IDE and then
    'returns True
    Debug.Assert CheckIDE
    'Use the IsIDE flag anywhere
    If IsIDE Then
        MsgBox ("Running under IDE")
    Else
        MsgBox ("Running as EXE")
    End If
End Sub
 
Private Function CheckIDE() As Boolean
    'This function will never be executed in an EXE
    IsIDE = True        'set global flag
    'Set CheckIDE or the Debug.Assert will Break
    CheckIDE = True
End Function

Suggestion: To get some insight into the way Debug.Assert works, try commenting out the statement CheckIDE = True in the function CheckIDE and run the example.

There are probably other ways to do this as well. This topic was suggested by a question from the About Delphi guide. He has a tip that shows you how to do it in the Delphi language at this link. Let us know what your favorite technique is!

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.