views:

383

answers:

3

A very simple situation. I'm working on an application in Delphi 2007 which is often compiled as 'Release' but still runs under a debugger. And occasionally it will run under SilkTest too, for regression testing. While this is quite fun I want to do something special...

I want to detect if my application is running within a debugger/regression-tester and if that's the case, I want the application to know which tool is used! (Thus, when the application crashes, I could report this information in it's error report.)

Any suggestions, solutions?

+2  A: 

You're probably looking for the IsDebuggerPresent function.

Greg Hewgill
But will it also detect SilkTest and other testing software? Or remote debuggers?
Workshop Alex
There is also the `CheckRemoteDebuggerPresent` function, which sounds like it might identify remote debuggers. I'm unfamiliar with SilkTest so I don't know whether it acts as a debugger or not. If it does, then the above will work. If not, then you will need to find another technique.
Greg Hewgill
A: 

You can also do

if DebugHook <> 0 then ...
gabr
That only works from within the Delphi IDE, not other debuggers. Also, keep in mind the application is debugged in "Release" mode, not "Debug" mode.
Workshop Alex
+7  A: 

You can check the parent process that started your application. With CreateToolhelp32Snapshot/Process32First/Process32Next get the parent PID (PROCESSENTRY32.th32ParentProcessID or TProcessEntry32.th32ParentProcessID) for your application PID. Then get the filename for the parent PID to compare with the applications you want to check for, like SilkTest.

Check this article for code usage.

In addition to IsDebuggerPresent and CheckRemoteDebuggerPresent, you can also query PEB.BeingDebugged (PEB is Process Environment Block, to get PEB you must query TEB, which is the Thread Enviroment Block).

pani