Detect if debugger is connected to VB6

I am doing some maintenance work on one of our old applications written in Visual Basic 6, and for various reasons we have some code that needs to be executed only if we run the program through the VB6 IDE (i.e., the debugger is connected )

In VB.NET, you can do this using a property System.Diagnostics.Debugger.IsAttached(), but I cannot find anything like it in VB6 on Google.

Is there an easy way to understand this information?

+5
source share
5 answers

Here is the function I used:

Private Function RunningInIde() As Boolean
    On Error GoTo ErrHandler
    Debug.Print 1 / 0
ErrHandler:
    RunningInIde = (Err.Number <> 0)
End Function            ' RunningInIde
+7
source

Here is what we use, which has no 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
+9
source

- , . , , :

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

.

Err.

.

1: "" "bFlag" , bFlag "IsRunningInIde". , , , .

3: "Debug.Assert" , IDE. , IDE "IsrunningInIde" .

2: , bFlag false true. ( IDE), false.

3: "IsRunningInIde", , , bFlag .

4: True, , Assert. , "Not bFlag", bFlag "False", IsRunningInIde , bFlag "True", . , Not bFlag "True", IDE.

5: bFlag , "False" .

, .

, .

, , .

+2

That my function, like Josh, is one, but easier to read (see comments inside).

I used it for so long that I forgot where I borrowed ...

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
+1
source
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
0
source

All Articles