Cas*_*ady 10 delphi winapi delphi-7
我正在使用此Delphi 7代码来检测Internet Explorer是否正在运行:
function IERunning: Boolean;
begin
Result := FindWindow('IEFrame', NIL) > 0;
end;
Run Code Online (Sandbox Code Playgroud)
这适用于使用IE 8,9和10的99%的系统.
但是有一些系统(不幸的是我没有,但我有两个beta测试人员都有这样的系统,都是Win7 x64 SP1),其中FindWindow()为IEFrame返回0,即使IE在内存中也是如此.
所以我编写了另一种方法来查找窗口:
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;
Run Code Online (Sandbox Code Playgroud)
替代方法适用于所有系统的100%.
我的问题 - 为什么FindWindow()在某些系统上不可靠?
我猜测FindWindow
声明返回一个 WinHandle,它是一个 THandle,它是一个已签名的 Integer。(至少,我认为很多年前我用 Delphi 编程时就是这种情况。)
如果 IE 的窗口句柄设置了最高位,那么它将为负数,因此您的测试将返回 False:
Result := FindWindow('IEFrame', NIL) > 0;
Run Code Online (Sandbox Code Playgroud)
窗把手通常不会设置顶部位,但我不知道这是不可能的。