unit EnumTask;
interface
uses Windows, Messages, Classes, SysUtils, TLHelp32;
type
TEnumTaskRecord = record
WindowTitle: PChar;
ProcessID, ThreadID: THandle;
WindowHandle: HWND;
end;
PEnumTaskRecord = ^TEnumTaskRecord;
TEnumTask = class
private
FList: TList;
procedure Clear;
function GetCount: Integer;
function GetItem(Index: Integer): PEnumTaskRecord;
public
constructor Create;
destructor Destroy; override;
procedure Open;
function IndexOf(ATitle: string): integer;
function Close(Index: integer):boolean;
property Count: Integer read GetCount;
property Items[Index: Integer]: PEnumTaskRecord read GetItem;
end;
type
TEnumProcessInfo = Record
ExeName : String;
ProcessID : DWORD;
end;
pEnumProcessInfo = ^TEnumProcessInfo;
TEnumProcess = class
private
FList: TList;
procedure Clear;
function GetCount: Integer;
function GetItem(Index: Integer): pEnumProcessInfo;
public
constructor Create;
destructor Destroy; override;
procedure Open;
function IndexOf(AExeName, AID: string): integer;
function Close(Index: integer):boolean;
property Count: Integer read GetCount;
property Items[Index: Integer]: pEnumProcessInfo read GetItem;
end;
implementation
//TEnumTask
constructor TEnumTask.Create;
begin
inherited;
FList := TList.Create;
end;
destructor TEnumTask.Destroy;
begin
Clear;
FList.Free;
inherited;
end;
procedure TEnumTask.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
begin
StrDispose(PEnumTaskRecord(FList.Items)^.WindowTitle);
FreeMem(FList.Items);
end;
FList.Clear;
end;
function TEnumTask.GetCount: Integer;
begin
Result := FList.Count;
end;
function TEnumTask.GetItem(Index: Integer): PEnumTaskRecord;
begin
Result := PEnumTaskRecord(FList.Items[Index]);
end;
procedure TEnumTask.Open;
var
DesktopWindow, CurWindow: HWND;
TitleLength, I: Integer;
Node: PEnumTaskRecord;
Style: LongInt;
ProcessID, ThreadID: THandle;
begin
Clear;
DesktopWindow := GetDesktopWindow;
CurWindow := GetWindow(DesktopWindow, GW_CHILD);
while CurWindow <> 0 do
begin
if IsWindowVisible(CurWindow) then
begin
TitleLength := GetWindowTextLength(CurWindow);
if TitleLength > 0 then
begin
Style := GetWindowLong(CurWindow, GWL_EXSTYLE);
if (Style and WS_EX_TOOLWINDOW = 0) then
begin
ThreadID := GetWindowThreadProcessID(CurWindow, @ProcessID);
Node := nil;
for I := 0 to FList.Count - 1 do
if ThreadID = PEnumTaskRecord(FList.Items)^.ThreadID then
begin
Node := PEnumTaskRecord(FList.Items);
break;
end;
if Node = nil then
begin
New(Node);
Flist.Add(Node);
Node^.ThreadId := ThreadID;
Node^.ProcessID := ProcessID;
end;
Node^.WindowHandle := CurWindow;
Node^.WindowTitle := StrAlloc(TitleLength + 1);
GetWindowText(CurWindow, Node^.WindowTitle, TitleLength + 1);
end;
end;
end;
CurWindow := GetWindow(CurWindow, GW_HWNDNEXT);
end;
end;
function TEnumTask.IndexOf(ATitle: string): integer;
var
i:integer;
begin
result:=-1;
for i:=0 to FList.Count-1 do
if PEnumTaskRecord(FList.Items)^.WindowTitle = ATitle then
result:=i;
end;
function TEnumTask.Close(Index: integer):boolean;
begin
result:=false;
if TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, DWORD(GetItem(Index).ProcessID)),
$FFFFFFFF) then
result:=true;
end;
//TEnumProcess
constructor TEnumProcess.Create;
begin
inherited;
FList := TList.Create;
end;
destructor TEnumProcess.Destroy;
begin
Clear;
FList.Free;
inherited;
end;
procedure TEnumProcess.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
begin
FreeMem(FList.Items);
end;
FList.Clear;
end;
function TEnumProcess.GetCount: Integer;
begin
Result := FList.Count;
end;
function TEnumProcess.GetItem(Index: Integer): pEnumProcessInfo;
begin
Result := pEnumProcessInfo(FList.Items[Index]);
end;
procedure TEnumProcess.Open;
var p : pEnumProcessInfo;
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeName := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
FList.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
function TEnumProcess.IndexOf(AExeName, AID: string): integer;
var
i:integer;
begin
result:=-1;
for i:=0 to FList.Count-1 do
if (pEnumProcessInfo(FList.Items)^.ExeName = AExeName) and
(pEnumProcessInfo(FList.Items)^.ProcessID = strtoint(AID)) then
result:=i;
end;
function TEnumProcess.Close(Index: integer):boolean;
begin
result:=false;
if TerminateProcess(OpenProcess(PROCESS_TERMINATE,
False,
GetItem(Index).ProcessID),
$FFFFFFFF) then
result:=true;
end;
end.