Reputation: 15450
I'm doing some maintenance work on one of our old applications that is written in Visual Basic 6, and for various reasons, we have a part of the code that only needs to be run if we are running the program through the VB6 IDE (i.e., the debugger is attached).
In VB.NET, you can do this by using the System.Diagnostics.Debugger.IsAttached()
property, but I can't find anything similar in VB6 on Google.
Is there some easy way to figure this information out?
Upvotes: 6
Views: 3493
Reputation: 676
Public Function InIDE(Optional ByRef bool As Boolean = True) As Boolean
If bool Then Debug.Assert Not InIDE(InIDE) Else bool = True
End Function
Usage:
If InIDE() Then
'- Yes
End If
Upvotes: 0
Reputation: 11399
Public Function InIDE() As Boolean
On Error GoTo ErrHandler
Debug.Print 1 \ 0 'If compiled, this line will not be present, so we immediately get into the next line without any speed loss
InIDE = False
Exit Function
ErrHandler:
InIDE = True 'We'll get here if we're in the IDE because the IDE will try to output "Debug.Print 1 \ 0" (which of course raises an error).
End Function
Upvotes: 0
Reputation: 31
That's my function, similar to Josh's one, but simpler to read (see comments inside).
I used it for so long that I forgot where I borrowed from...
Public Function InDesign() As Boolean
' Returns TRUE if in VB6 IDE
Static CallCount As Integer
Static Res As Boolean
CallCount = CallCount + 1
Select Case CallCount
Case 1 ' Called for the 1st time
Debug.Assert InDesign()
Case 2 ' Called for the 2nd time: that means Debug.Assert
' has been executed, so we're in the IDE
Res = True
End Select
' When Debug.Assert has been called, the function returns True
' in order to prevent the code execution from breaking
InDesign = Res
' Reset for further calls
CallCount = 0
End Function
Upvotes: 2
Reputation: 2672
I wrote something like this awhile back and can't find it, and needed it again. So I just wrote it again and I think I got it right:
Public Function IsRunningInIde() As Boolean
Static bFlag As Boolean
bFlag = Not bFlag
If bFlag Then Debug.Assert IsRunningInIde()
IsRunningInIde = Not bFlag
bFlag = False
End Function
No errors getting raised.
No resetting of Err.
Just one function.
Line 1: The "Static" declaration of "bFlag" causes the value of bFlag to stick across multiple calls to "IsRunningInIde". We want this because I call this function within itself, and I didn't want to litter the function with input parameters that aren't needed by the user.
Line 3: The "Debug.Assert" doesn't get called when not running in the IDE. So only when in the IDE does "IsrunningInIde" get called recursively.
Line 2: If not in the recursive call, bFlag starts out false, and gets set to true. If in the recursive call (only happens when running in the IDE), it starts out as true, and gets set back to false.
Line 3: Only call "IsRunningInIde" if it isn't already in this function recursively, by checking if bFlag is true.
Line 4: If in recursive call, always returns True, which doesn't really matter, but doesn't cause the Assert to fail. If not in recursive call, then returns "Not bFlag", which bFlag is now "False" if IsRunningInIde was called recursively, and bFlag is "True" if not called recursively. So basically, Not bFlag returns "True" if it is running in the IDE.
Line 5: Clears the bFlag so that it is always "False" at the beginning of the next call to this function.
It's hard to explain, it is better to step through it in your mind, in both scenarios.
If you want simpler to understand code, don't use it.
If there is a problem with this code, I apologize and let me know so I can fix it.
Upvotes: 2
Reputation: 11991
Here is what we are using that does not have any side effects
Public Property Get InIde() As Boolean
Debug.Assert pvSetTrue(InIde)
End Property
Private Function pvSetTrue(bValue As Boolean) As Boolean
bValue = True
pvSetTrue = True
End Function
Upvotes: 11
Reputation: 53593
Here's a function I've been using:
Private Function RunningInIde() As Boolean
On Error GoTo ErrHandler
Debug.Print 1 / 0
ErrHandler:
RunningInIde = (Err.Number <> 0)
End Function ' RunningInIde
Upvotes: 7