unit Unit1;interfaceuses PsAPI,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}function EnumWindowsProc(hwnd:HWND;lParam:DWORD ):boolean; stdcall; var arr,buf: array[0..256] of Char; pid,pHandle:Integer; s:string; begin if (IsWindowVisible(hwnd)=True) and (GetParent(hwnd)=0) THEN begin GetWindowThreadProcessId(hwnd,@pid); pHandle := OpenProcess(PROCESS_ALL_ACCESS, False, pid); GetModuleFileNameEx(pHandle, 0, buf, Length(buf)); s:=buf; s:= Copy(s,LastDelimiter('\',s)+1,Length(s)) ; if s='TheWorld.exe' then begin GetClassName(hwnd, arr, Length(arr)); s:='进程:'+s+' '; s:=s+'句柄:'+inttostr(hwnd)+' '; s:=s+'类名:'+arr+' '; form1.Memo1.Lines.Add(s); end; end;result:= TRUE; end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Clear; EnumWindows(@EnumWindowsProc, 0); end;end.
if s='TheWorld.exe' then 改成 if s='qq.exe' then
XE2,测试可以执行,QQ给查出来,自己写的程序就无显示,64位系统
uses TlHelp32;var pidList: TList;function EnumWindowsProc( hwnd: HWND; lParam: LPARAM ): BOOL; stdcall; var I: INteger; pid: Cardinal; c, w: array [0..255] of Char; begin GetWindowThreadProcessId(hwnd, pid); for I := 0 to pidList.Count - 1 do if Cardinal(pidList.Items[I]) = pid then begin GetClassName(hwnd, c, 256); GetWindowText(hwnd, w, 256); Form1.Memo1.Lines.Add(Format('pid: %d, hwnd: %d, class: %s, caption: %s', [ pid, hwnd, c, w])) end; end;procedure TForm1.FormCreate(Sender: TObject); var h: Cardinal; p: tagPROCESSENTRY32; I: Integer; begin {根据进程名取进程pid} h := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if h = INVALID_HANDLE_VALUE then raise Exception.Create('创建进程快照失败!'); p.dwSize := SizeOf(tagPROCESSENTRY32); if not Process32First(h, p) then begin CloseHandle(h); raise Exception.Create('枚举进程失败!'); end; pidList := TList.Create; repeat if SameText(p.szExeFile, 'qq.exe') then pidList.Add(Pointer(p.th32ProcessID)); until not Process32Next(h, p); {列举pid对应的窗口} Memo1.Clear; Memo1.ScrollBars := ssBoth; EnumWindows(@EnumWindowsProc, 0); pidList.Free; CloseHandle(h); end;
PsAPI,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function EnumWindowsProc(hwnd:HWND;lParam:DWORD ):boolean; stdcall;
var
arr,buf: array[0..256] of Char;
pid,pHandle:Integer;
s:string;
begin
if (IsWindowVisible(hwnd)=True) and (GetParent(hwnd)=0) THEN
begin
GetWindowThreadProcessId(hwnd,@pid);
pHandle := OpenProcess(PROCESS_ALL_ACCESS, False, pid);
GetModuleFileNameEx(pHandle, 0, buf, Length(buf));
s:=buf;
s:= Copy(s,LastDelimiter('\',s)+1,Length(s)) ;
if s='TheWorld.exe' then
begin
GetClassName(hwnd, arr, Length(arr));
s:='进程:'+s+' ';
s:=s+'句柄:'+inttostr(hwnd)+' ';
s:=s+'类名:'+arr+' ';
form1.Memo1.Lines.Add(s);
end;
end;result:= TRUE;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
EnumWindows(@EnumWindowsProc, 0);
end;end.
改成
if s='qq.exe' then
pidList: TList;function EnumWindowsProc( hwnd: HWND;
lParam: LPARAM
): BOOL; stdcall;
var
I: INteger;
pid: Cardinal;
c, w: array [0..255] of Char;
begin
GetWindowThreadProcessId(hwnd, pid);
for I := 0 to pidList.Count - 1 do
if Cardinal(pidList.Items[I]) = pid then
begin
GetClassName(hwnd, c, 256);
GetWindowText(hwnd, w, 256);
Form1.Memo1.Lines.Add(Format('pid: %d, hwnd: %d, class: %s, caption: %s', [
pid, hwnd, c, w]))
end;
end;procedure TForm1.FormCreate(Sender: TObject);
var
h: Cardinal;
p: tagPROCESSENTRY32;
I: Integer;
begin
{根据进程名取进程pid}
h := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if h = INVALID_HANDLE_VALUE then
raise Exception.Create('创建进程快照失败!'); p.dwSize := SizeOf(tagPROCESSENTRY32);
if not Process32First(h, p) then
begin
CloseHandle(h);
raise Exception.Create('枚举进程失败!');
end; pidList := TList.Create;
repeat
if SameText(p.szExeFile, 'qq.exe') then
pidList.Add(Pointer(p.th32ProcessID)); until not Process32Next(h, p); {列举pid对应的窗口}
Memo1.Clear;
Memo1.ScrollBars := ssBoth;
EnumWindows(@EnumWindowsProc, 0);
pidList.Free;
CloseHandle(h);
end;