Fog Creek Software
Discussion Board




How to detect if VB6 APP is running within the IDE

Is there a way to detect at run time if my VB6 application is running within the VB6 IDE/debugger?

Heston Holtmann
Monday, December 16, 2002

May not be the most elegant way, but it works:

  On Error Resume Next
  Debug.Assert "TESTDEBUG"
  If Err = 0 Then
    ' Runtime variant
  Else
    ' Debug variant
  End If

Why they have not provided the respective flag among conditional compilation constants, I cannot imagine.

Michael Skulsky
Monday, December 16, 2002

I agree with Michael's method but I always used div by zero instead, it makes it more explicit:

Private Function InDbg as Boolean
  On Error Resume Next
  Debug.Assert 1/0
  InDbg = (Err <> 0)
End Function

Billy Boy
Monday, December 16, 2002

Troy,

<quote>
Is there a way to detect at run time if my VB6 application is running within the VB6 IDE/debugger?
</quote>

The question is actually more complicated than you think. Particularly if (for example) you build a VB ActiveX DLL and want to detect whether it is being used by another VB program running in the IDE...

Anyway, this kind of stuff is routinely discussed at http://peach.ease.lsoft.com/scripts/wa.exe?A0=visbas-l which it may be worthwhile you joining...

Matthew Wills
Monday, December 16, 2002

Matthew, I don't code in VB any more, but thanks anyway. I was on that list years ago, though. I had a large programmer's mailing list of my own at the time, too.

Troy King
Monday, December 16, 2002

How about finding out the window class of the IDE (hint: it's "wndclass_desked_gsk") and then esting the result of the FindWindow API call?

John Topley
Tuesday, December 17, 2002

I don't think that would always work. FindWindow finds any window that matches the class name and window name specified. Therefore it would match if you were running the project outside the IDE (eg: it's an ActiveX DLL being run as part of another app) whilst looking at the project's source code, giving you a false positive.

However, you could perhaps then call GetWindowThreadProcessId and check to see if the process matches the process of the VB IDE. (I haven't tried this, so don't hold me to it...)

Ritchie Swann
Tuesday, December 17, 2002

I take your point. It's probably simpler and easier just to use the conditional define. I know that in Delphi the recommended approach is the FindWindow one which was why I suggested it.

John Topley
Tuesday, December 17, 2002

Thanks for all the answers, humour, and critism!  The debug.assert err test method satisfies my needs for now.

Heston Holtmann
Tuesday, December 17, 2002

The Debug.Assert is the mentioned in most VB FAQs, and is what I'd probably use if I ever needed it. If I can't run a VB project in the IDE I just compile it with symbolic debug info turned on and debug it in C++ instead.

Ritchie Swann
Tuesday, December 17, 2002

Not sure it is the best but i use.

Public Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

Function Is_Debug() As Boolean
If GetModuleHandle("App.EXE") = 0 Then Is_Debug = True
End Function

Ths way even my in process dlls can run the debug code.

Jason Hawryluk
Saturday, January 04, 2003

*  Recent Topics

*  Fog Creek Home