Search code examples
delphiide

How to detect if code is run from IDE


I have the following function that I use to check if my code is run from the IDE or not What is really frustrating is that from time to time the function returns False even if the code is run from IDE. And the fact that I can't find any common denominator for when it works OK and when it doesn't Anyone got an idea as how to fix this function or a whole other method of doing this check. (I use the function to make a menu with test functions available during development and hide them to end user)

function IDERunning: Bool;
begin
  Result := (FindWindow('TAppBuilder', nil) > 0) and
    (FindWindow('TPropertyInspector', 'Object Inspector') > 0);
end;

I use Delphi XE8


Solution

  • It depends on exactly what you want to test:

    • Use IsDebuggerPresent to test if any user mode debugger is attached to your process.
    • Test if DebugHook is non-zero to determine that the program is running under the Delphi IDE debugger.

    Note that when DebugHook is non-zero, then IsDebuggerPresent will return true, but the reverse is not always the case. There are debuggers other than the IDE debugger.