6

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?

bhamby
  • 15,112
  • 1
  • 45
  • 66
  • See also the question [debug mode in vb6](http://stackoverflow.com/questions/9052024/debug-mode-in-vb-6) – MarkJ Feb 29 '12 at 20:21
  • Running in the IDE is not the same as having a debugger attached, which can also be done of course but it's a different thing. – Bob77 Mar 01 '12 at 12:51

6 Answers6

11

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
wqw
  • 11,771
  • 1
  • 33
  • 41
  • 2
    By no side effects the poster means that if you have "Break On All Errors" turned on, this will not stop execution of the program like the accepted answer from @Jay Riggs does (which can be very annoying if you use InIde all over the place. – Kris Erickson Mar 02 '12 at 16:17
  • @Kris: That, and not resetting `Err` object if using `On Error Resume Next` + `Err.Number <> 0` style of error checking. Btw, never seen anyone using anything but `Break On Unhandled Errors` (unless being clueless of these IDE settings). – wqw Mar 03 '12 at 09:40
  • 1
    @Kris: Two years after your original comment I'm convinced that "Break On All Errors" is the way to go on any remotely professional project. It cost me one hand and a leg to refactor all my "codes" but it was worth it. Thanks for the eye-opener! – wqw Jul 09 '14 at 12:25
7

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
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • That looks great! One question, though. If we have other `On Error GoTo` statements outside of this function, will I need to reset those outside of this function? Or is the error handler scoped inside the function? – bhamby Feb 29 '12 at 20:17
  • @galador All errors thrown in this function will be handled inside this function and you won't need to reset. – Jay Riggs Feb 29 '12 at 23:55
  • 2
    This functions resets `Err` object as a side effect – wqw Mar 01 '12 at 12:51
  • There are better solutions mentioned here, so this shouldn't be marked as the answer. – tmighty Jun 28 '17 at 17:11
2

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.

Mafu Josh
  • 2,523
  • 1
  • 23
  • 25
2

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
Rafunk
  • 31
  • 3
0
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
tmighty
  • 10,734
  • 21
  • 104
  • 218
0
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
Auto
  • 667
  • 6
  • 9