枚举进程,主要代码
procedure TProcessInfoList.EnumProcess;
var
p : TProcessInfo;
ContinueLoop : BOOL;
FSnapshotHandle : THandle;
FProcessEntry32 : TProcessEntry32;
sUser : string;
sDomain : string;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
try
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
p := TProcessInfo.Create;
p.FHandle := FProcessEntry32.th32ProcessID;
p.FName := FProcessEntry32.szExeFile;
p.FFileName := GetExePathByProcessID(p.Handle);
if GetUserAndDomainFromPID(p.Handle, sUser, sDomain)>0 then
begin
p.FUserName := sUser;
p.FDomain := sDomain;
end;
if p.FFileName = '' then
p.FFileName := FProcessEntry32.szExeFile;
p.FFileName := AnalyzeFileName(p.FFileName);
GetFileIcon(p.FileName, p.Icon);
FItems.Add(p);
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
finally
CloseHandle(FSnapshotHandle);
end;
end;
判断进程名后,枚举进程的窗口
function EnumThreadProc(AHandle:HWND; lParam:LPARAM): Boolean; stdcall;
procedure GetWindowInfo(
const AHandle : HWND;
const AList : TStrings
);
var
szText : array[0..MAXBYTE] of char;
begin
AList.Add(IntToStr(AHandle));//句柄
GetWindowText(AHandle, szText, MAXBYTE);
AList.Add(StrPas(szText));//标题
GetClassName(AHandle, szText, MAXBYTE);
AList.Add(StrPas(szText));//类名
end;
begin
GetWindowInfo(AHandle, TStrings(Pointer(lParam)));
Result:=True;
end;
procedure TProcessInfo.EnumWindows(const AList: TStrings);
var
snap : THandle;
ThreadStruct: TThreadEntry32;
begin
if FHandle = 0 then
Exit;
snap := CreateToolHelp32Snapshot(TH32CS_SNAPTHREAD, FHandle);
if snap <> 0 then
try
ThreadStruct.dwSize := sizeOf(TThreadEntry32);
if Thread32First(snap, ThreadStruct) then
repeat
if ThreadStruct.th32OwnerProcessID = FHandle then
EnumThreadWindows(ThreadStruct.th32ThreadID, @EnumThreadProc, Integer(AList));
until not Thread32Next(snap, ThreadStruct);
finally
CloseHandle(snap);
end;
end;
然后从中找出你感兴趣的窗口