Why is FindWindow() not 100% reliable?

Casady picture Casady · Mar 18, 2013 · Viewed 14.1k times · Source

I'm using this Delphi 7 code to detect if Internet Explorer is running:

function IERunning: Boolean;
begin
  Result := FindWindow('IEFrame', NIL) > 0;
end;

This works on 99% of the systems with IE 8,9 and 10.

But there are some systems (unfortunately none of mine, but I have two beta testers which have such systems, both Win7 x64 SP1) where FindWindow() returns 0 for IEFrame, even if IE is in memory.

So I've coded an alternate method to find the window:

function IERunningEx: Boolean;
var WinHandle : HWND;
    Name: array[0..255] of Char;
begin
  Result := False; // assume no IE window is present

  WinHandle := GetTopWindow(GetDesktopWindow);

  while WinHandle <> 0 do // go thru the window list
  begin
      GetClassName(WinHandle, @Name[0], 255);
      if (CompareText(string(Name), 'IEFrame') = 0) then
      begin // IEFrame found
          Result := True;
          Exit;             
      end;
      WinHandle := GetNextWindow(WinHandle, GW_HWNDNEXT);
  end;      
end;

The alternate method works on 100% of all systems.

My question - why is FindWindow() not reliable on some of the systems?

Answer

arx picture arx · Mar 19, 2013

I'm guessing that FindWindow is declared to return a WinHandle, which is a THandle, which is an Integer, which is signed. (At least, I think this was the case many years ago when I programmed in Delphi.)

If IE has a window handle with the top bit set then it will be negative so your test will return False:

Result := FindWindow('IEFrame', NIL) > 0;

Window handles don't usually have the top bit set, but I don't know that it's impossible.