的确出现这种情况了。。
function GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
HWND: THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not Result then begin EI.HWND := WND;
end;
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EI.ProcessID := PID;
EI.HWND := 0;
j:=inttostr(PID);
EnumWindows(@EnumWindowsProc, Integer(@EI)); //用来列举屏幕上所有顶层窗口。
//;showmessage(j);
Result := EI.HWND;
end;
begin
if hPID<>0 then begin
Result:=FindMainWindow(hPID);
end
else
Result:=0;
end;
function KillTask(ExeFileName: string): integer;{Uses Tlhelp32}
{const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
ProcessHwd:THandle;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
ProcessHwd := Integer(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
result:=GetHWndByPID(ProcessHwd);
CloseHandle(FSnapshotHandle); }
var
ProcessName : string; //进程名
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
ContinueLoop:BOOL;
MyHwnd:THandle;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32.szExeFile;
if(ProcessName = ExeFileName) then begin
result:=GetHWndByPID(FProcessEntry32.th32ProcessID);
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle); // 释放快照句柄
end;
delphi 的代码。。不同的编译器返回的结果不同!!!!! |