得到所有在运行的程序的列表!type TProcessInfo=record FileName: string; Caption: string; Visible: boolean; Handle: DWord; PClass: string; ThreadID: DWord; PID: DWord; end;procedure GetProcessList; var i,Laenge: integer; begin DateiList.Clear; HandleList.Clear; ClassList.Clear; CaptionList.Clear; VisibleList.Clear; ThreadIdList.Clear; PIDList.Clear; EnumWindows(@EnumWindowsProc, 0); Laenge:=DateiList.Count; SetLength(ProcessInfo,Laenge); for i:=0 to Laenge-1 do begin DateiList[i]:=UpperCase(DateiList[i]); with ProcessInfo[i] do begin FileName:=DateiList[i]; Caption:=CaptionList[i]; Visible:=VisibleList[i]='1'; Handle:=StrToInt64(HandleList[i]); PClass:=ClassList[i]; ThreadID:=StrToInt64(ThreadIdList[i]); PID:=StrToInt64(PIDList[i]); end; end; end;procedure TfrmForm.GetProcessInfo; //Get the Process info var i:integer; begin with lvProcess.Items do begin BeginUpdate; Clear; EndUpdate; end; GetProcessList; for i:=0 to Length(ProcessInfo)-1 do begin with lvProcess.Items.Add do begin ImageIndex := 4; Caption := inttostr(ProcessInfo[i].PID); SubItems.Add(inttostr(ProcessInfo[i].ThreadID)); SubItems.Add(ProcessInfo[i].FileName); SubItems.Add(ProcessInfo[i].Caption); SubItems.Add(inttostr(ProcessInfo[i].Handle)); SubItems.Add(ProcessInfo[i].PClass); if (ProcessInfo[i].Visible) then SubItems.Add('YES') else SubItems.Add('NO'); end; end; sbStatus.Panels[0].Text := M_PROCESS; end;
需要一个ListView来显示列表!
你在A中用findwindow或findwindowEX就可以了。
procedure TMainForm.BuildProcessList(Rebuild: Boolean = False); var SnapProcHandle, ProcessHandle: THandle; ProcessEntry: TProcessEntry32; Next: Boolean; FileInfo: TSHFileInfo; ProcessVersion: DWORD; FindItem: TListItem; I: Integer; ProcList: TList; Added, Changed: Boolean; procedure CheckChanged; begin if ProcessListView.ItemFocused = FindItem then Changed := True; end;begin if FDisableUpdate then Exit; ProcList := TList.Create; Added := False; Changed := False; with ProcessListView do try FDisableUpdate := True; try if Rebuild then begin Screen.Cursor := crHourGlass; Items.BeginUpdate; Items.Clear; FProcess_Cnt := 0; FThreads_Cnt := 0; end else SendMessage(Handle, WM_SETREDRAW, 0, 0); SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if SnapProcHandle <> THandle(-1) then begin ProcessEntry.dwSize := Sizeof(ProcessEntry); Next := Process32First(SnapProcHandle, ProcessEntry); while Next do begin ProcList.Add(Pointer(ProcessEntry.th32ProcessID)); FindItem := FindData(0, Pointer(ProcessEntry.th32ProcessID), True, False); with ProcessEntry do if FindItem = nil then begin // New Process Added := True; if IsWin2k then begin ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, th32ProcessID); if Handle <> 0 then begin if GetModuleFileNameEx(ProcessHandle, 0, szExeFile, SizeOf(szExeFile)) = 0 then StrPCopy(szExeFile, '[Idle]'); CloseHandle(ProcessHandle); end; end; ProcessVersion := SHGetFileInfo(szExeFile, 0, FileInfo, Sizeof(FileInfo), SHGFI_EXETYPE); SHGetFileInfo(szExeFile, 0, FileInfo, Sizeof(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON); with Items.Add, ProcessEntry do begin Caption := AnsiLowerCase(ExtractFileName(szExeFile)); Data := Pointer(th32ProcessID); ImageIndex := FileInfo.iIcon; StateIndex := GetPriorityIconIndex(pcPriClassBase); SubItems.AddObject(Format('%.8x', [th32ProcessID]), Pointer(th32ProcessID)); SubItems.AddObject(Format('%d', [pcPriClassBase]), Pointer(pcPriClassBase)); SubItems.AddObject(Format('%d', [cntThreads]), Pointer(cntThreads)); SubItems.AddObject(GetProcessVersion(ProcessVersion), Pointer(ProcessVersion)); SubItems.Add(szExeFile); SubItems.AddObject(Format('(%.8x)', [th32ParentProcessID]), Pointer(th32ParentProcessID)); Inc(FProcess_Cnt); Inc(FThreads_Cnt, cntThreads); end; end else with FindItem do begin // Any changes in existing process ? if SubItems.Objects[1] <> Pointer(pcPriClassBase) then begin SubItems.Objects[1] := Pointer(pcPriClassBase); SubItems.Strings[1] := Format('%d', [pcPriClassBase]); StateIndex := GetPriorityIconIndex(pcPriClassBase); end; if SubItems.Objects[2] <> Pointer(cntThreads) then begin Inc(FThreads_Cnt, cntThreads - DWORD(SubItems.Objects[2])); SubItems.Objects[2] := Pointer(cntThreads); SubItems.Strings[2] := Format('%d', [cntThreads]); CheckChanged; end; end; Next := Process32Next(SnapProcHandle, ProcessEntry); end; CloseHandle(SnapProcHandle); end; if Added then // find the names of parent processes begin for I := 0 to Items.Count - 1 do begin FindItem := FindData(0, Items[I].SubItems.Objects[5], True, False); if FindItem <> nil then Items[I].SubItems[5] := FindItem.Caption; end; AlphaSort; end; for I := Items.Count - 1 downto 0 do // delete non-existing processes if ProcList.IndexOf(Items[I].Data) = -1 then begin Dec(FProcess_Cnt); Dec(FThreads_Cnt, DWORD(Items[I].SubItems.Objects[2])); Items.Delete(I); end; if GetNextItem(nil, sdAll, [isSelected]) = nil then begin if ItemFocused = nil then ItemFocused := Items[0]; ItemFocused.Selected := True; end else if Changed then BuildThreadsList(DWORD(ItemFocused.Data)); UpdateStatusLine(True); finally if Rebuild then Items.EndUpdate else SendMessage(Handle, WM_SETREDRAW, 1, 0); end; finally FDisableUpdate := False; ProcList.Free; Screen.Cursor := crDefault; end; end;
如果你用的是2000,找找有关psapi的有关介绍就知道了。
利用getwndow和getwindowtext来获取 var CurWindow:HWND; szText:array[0..254] of char; begin CurWindow:=GetWindow(Handle,GW_HWNDFIRST); While CurWindow<>0 do begin if GetWindowText(CurWindow,@szText,255)>0 then memo1.Lines.Add(Strpas(@szText)); CurWindow:=GetWindow(CurWindow,GW_HWNDNEXT); end; end;
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsProc, 0);
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laenge);
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(DateiList[i]);
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]='1';
Handle:=StrToInt64(HandleList[i]);
PClass:=ClassList[i];
ThreadID:=StrToInt64(ThreadIdList[i]);
PID:=StrToInt64(PIDList[i]);
end;
end;
end;procedure TfrmForm.GetProcessInfo; //Get the Process info
var
i:integer;
begin
with lvProcess.Items do
begin
BeginUpdate;
Clear;
EndUpdate;
end;
GetProcessList;
for i:=0 to Length(ProcessInfo)-1 do
begin
with lvProcess.Items.Add do
begin
ImageIndex := 4;
Caption := inttostr(ProcessInfo[i].PID);
SubItems.Add(inttostr(ProcessInfo[i].ThreadID));
SubItems.Add(ProcessInfo[i].FileName);
SubItems.Add(ProcessInfo[i].Caption);
SubItems.Add(inttostr(ProcessInfo[i].Handle));
SubItems.Add(ProcessInfo[i].PClass);
if (ProcessInfo[i].Visible) then
SubItems.Add('YES')
else SubItems.Add('NO');
end;
end;
sbStatus.Panels[0].Text := M_PROCESS;
end;
var
SnapProcHandle, ProcessHandle: THandle;
ProcessEntry: TProcessEntry32;
Next: Boolean;
FileInfo: TSHFileInfo;
ProcessVersion: DWORD;
FindItem: TListItem;
I: Integer;
ProcList: TList;
Added, Changed: Boolean; procedure CheckChanged;
begin
if ProcessListView.ItemFocused = FindItem then Changed := True;
end;begin
if FDisableUpdate then Exit;
ProcList := TList.Create;
Added := False;
Changed := False;
with ProcessListView do
try
FDisableUpdate := True;
try
if Rebuild then
begin
Screen.Cursor := crHourGlass;
Items.BeginUpdate;
Items.Clear;
FProcess_Cnt := 0;
FThreads_Cnt := 0;
end else
SendMessage(Handle, WM_SETREDRAW, 0, 0);
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if SnapProcHandle <> THandle(-1) then
begin
ProcessEntry.dwSize := Sizeof(ProcessEntry);
Next := Process32First(SnapProcHandle, ProcessEntry);
while Next do
begin
ProcList.Add(Pointer(ProcessEntry.th32ProcessID));
FindItem := FindData(0, Pointer(ProcessEntry.th32ProcessID), True, False);
with ProcessEntry do if FindItem = nil then
begin // New Process
Added := True;
if IsWin2k then
begin
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, th32ProcessID);
if Handle <> 0 then
begin
if GetModuleFileNameEx(ProcessHandle, 0, szExeFile, SizeOf(szExeFile)) = 0 then
StrPCopy(szExeFile, '[Idle]');
CloseHandle(ProcessHandle);
end;
end;
ProcessVersion := SHGetFileInfo(szExeFile, 0, FileInfo, Sizeof(FileInfo), SHGFI_EXETYPE);
SHGetFileInfo(szExeFile, 0, FileInfo, Sizeof(FileInfo), SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
with Items.Add, ProcessEntry do
begin
Caption := AnsiLowerCase(ExtractFileName(szExeFile));
Data := Pointer(th32ProcessID);
ImageIndex := FileInfo.iIcon;
StateIndex := GetPriorityIconIndex(pcPriClassBase);
SubItems.AddObject(Format('%.8x', [th32ProcessID]), Pointer(th32ProcessID));
SubItems.AddObject(Format('%d', [pcPriClassBase]), Pointer(pcPriClassBase));
SubItems.AddObject(Format('%d', [cntThreads]), Pointer(cntThreads));
SubItems.AddObject(GetProcessVersion(ProcessVersion), Pointer(ProcessVersion));
SubItems.Add(szExeFile);
SubItems.AddObject(Format('(%.8x)', [th32ParentProcessID]), Pointer(th32ParentProcessID));
Inc(FProcess_Cnt);
Inc(FThreads_Cnt, cntThreads);
end;
end else
with FindItem do
begin // Any changes in existing process ?
if SubItems.Objects[1] <> Pointer(pcPriClassBase) then
begin
SubItems.Objects[1] := Pointer(pcPriClassBase);
SubItems.Strings[1] := Format('%d', [pcPriClassBase]);
StateIndex := GetPriorityIconIndex(pcPriClassBase);
end;
if SubItems.Objects[2] <> Pointer(cntThreads) then
begin
Inc(FThreads_Cnt, cntThreads - DWORD(SubItems.Objects[2]));
SubItems.Objects[2] := Pointer(cntThreads);
SubItems.Strings[2] := Format('%d', [cntThreads]);
CheckChanged;
end;
end;
Next := Process32Next(SnapProcHandle, ProcessEntry);
end;
CloseHandle(SnapProcHandle);
end;
if Added then // find the names of parent processes
begin
for I := 0 to Items.Count - 1 do
begin
FindItem := FindData(0, Items[I].SubItems.Objects[5], True, False);
if FindItem <> nil then Items[I].SubItems[5] := FindItem.Caption;
end;
AlphaSort;
end;
for I := Items.Count - 1 downto 0 do // delete non-existing processes
if ProcList.IndexOf(Items[I].Data) = -1 then
begin
Dec(FProcess_Cnt);
Dec(FThreads_Cnt, DWORD(Items[I].SubItems.Objects[2]));
Items.Delete(I);
end;
if GetNextItem(nil, sdAll, [isSelected]) = nil then
begin
if ItemFocused = nil then ItemFocused := Items[0];
ItemFocused.Selected := True;
end else
if Changed then BuildThreadsList(DWORD(ItemFocused.Data));
UpdateStatusLine(True);
finally
if Rebuild then
Items.EndUpdate
else
SendMessage(Handle, WM_SETREDRAW, 1, 0);
end;
finally
FDisableUpdate := False;
ProcList.Free;
Screen.Cursor := crDefault;
end;
end;
var
CurWindow:HWND;
szText:array[0..254] of char;
begin
CurWindow:=GetWindow(Handle,GW_HWNDFIRST);
While CurWindow<>0 do
begin
if GetWindowText(CurWindow,@szText,255)>0 then
memo1.Lines.Add(Strpas(@szText));
CurWindow:=GetWindow(CurWindow,GW_HWNDNEXT);
end;
end;