Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How do I know if my program is running in a debugger

Title: How do I know if my program is running in a debugger Question: If your program is being run in a debugger, and you want to see ShowMessage etc messages or you don't want it debugged, use this code. Answer: I came across this on a non Delphi site where it was written by a Mr Tim Chew and I give him full credit for it. It was too good not to copy into Delphi. The original C/C++ code and article can be found on CodeProject at http://www.rntsoft.com/useritems/debugger.asp#xx72109xx I have converted it to Delphi and tested it under Win 2000 Pro and it appears to work. Running in the Delphi IDE it reveals it is being debugged. Just add a button to a form, create the event handler for the button and add this code below. function IsDebuggerAttached: boolean; var dw: integer; begin asm push eax // Preserve the registers push ecx mov eax, fs:[$18] // Get the TIB's linear address mov eax, dword ptr [eax + $30] mov ecx, dword ptr [eax] // Get the whole DWORD mov [dw], ecx // Save it pop ecx // Restore the registers pop eax end; // The 3rd byte is the byte we really need to check for the // presence of a debugger. // Check the 3rd byte result := (dw and $00010000 ) 0; end; procedure TForm1.Button1Click(Sender: TObject); begin if IsDebuggerAttached then ShowMessage('Debugger') else ShowMessage('No Debugger'); end; I haven't tested it on Windows 95/98. Anyone care to try it. If you run it by double clicking you shoudl see 'No Debugger'. Inside the IDE or another debugger and you should see 'Debugger'.