SULTAN
asked on
How to use!
Ned a Demo Project to understand this code!
-------------------------- ---------- ---------- ---------- --
unit ProcessViewer;
interface
uses
Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms;
const
SleepForReCheck=5000;
type TProcessInfo=record
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;
var
DateiList,CaptionList,Visi bleList,Ha ndleList,C lassList,T hreadIdLis t,PIDList: TStringList;
ProcessInfo: array of TProcessInfo;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(File Name: string; KillAll: boolean): boolean;
procedure GetProcessList;
function GetFileNameFromHandle(Hand le: hwnd):string;
function IsFileActive(FileName: String): boolean;
implementation
procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsPr oc, 0);
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laen ge);
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(Da teiList[i] );
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]='1 ';
Handle:=StrToInt64(HandleL ist[i]);
PClass:=ClassList[i];
ThreadID:=StrToInt64(Threa dIdList[i] );
PID:=StrToInt64(PIDList[i] );
end;
end;
end;
function IsFileActive(FileName: String): boolean;
var
i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(Extrac tFileName( FileName)) ;
for i:=0 to Length(ProcessInfo)-1 do
begin
if Pos(FileName,ProcessInfo[i ].FileName )>0 then
begin
result:=true;
break;
end;
end;
end;
function GetFileNameFromHandle(Hand le: hwnd):string;
var
PID: DWord;
aSnapShotHandle: THandle;
ContinueLoop: Boolean;
aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(H andle, @PID);
aSnapShotHandle := CreateToolHelp32SnapShot(T H32CS_SNAP PROCESS, 0);
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHa ndle, aProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if aProcessEntry32.th32Proces sID = PID then
begin
result:=aProcessEntry32.sz ExeFile;
break;
end;
ContinueLoop := Process32Next(aSnapShotHan dle, aProcessEntry32);
end;
CloseHandle(aSnapShotHandl e);
end;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
Capt,Cla: array[0..255] of char;
Datei: string;
ident: dword;
begin
GetWindowText(hWnd, Capt, 255);
GetClassName(hwnd,Cla,255) ;
ThreadIdList.Add(IntToStr( GetWindowT hreadProce ssId(hwnd, nil)));
Datei:=GetFileNameFromhand le(hwnd);
DateiList.Add(Datei);
HandleList.Add(IntToStr(HW nd));
if IsWindowVisible(HWnd) then VisibleList.Add('1') else VisibleList.Add('0');
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(S trToInt(Ha ndleList[H andleList. Count-1]), @ident);
PIDList.Add(IntToStr(ident ));
Result:=true;
end;
function KillProcessByPID(PID : DWord): boolean;
var
myhandle : THandle;
i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMIN ATE, False, PID);
TerminateProcess(myhandle, 0);
for i:=0 to SleepForReCheck do Application.ProcessMessage s; //Genug Zeit geben
GetProcessList;
Result:=PIDList.IndexOf(In tToStr(PID ))=-1;
end;
function KillProcessByFileName(File Name: string; KillAll: boolean): boolean;
var
i: integer;
FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(Extrac tFileName( FileName)) ;
result:=true;
GetProcessList;
if KillAll then
begin
//Kill all
FileFound:=false;
repeat
GetProcessList;
FileFound:=false;
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i]) >0 then
begin
FileFound:=true;
break;
end;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6 4(PIDList[ i])) then
begin
result:=false;
exit;
end;
end;
until not FileFound;
end else
begin
//Kill one
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i]) >0 then break;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6 4(PIDList[ i])) then
begin
result:=false;
exit;
end;
end;
end;
end;
initialization
DateiList:=TStringList.Cre ate;
HandleList:=TStringList.Cr eate;
ClassList:=TStringList.Cre ate;
CaptionList:=TStringList.C reate;
VisibleList:=TStringList.C reate;
ThreadIdList:=TStringList. Create;
PIDList:=TStringList.Creat e;
finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;
end.
--------------------------
unit ProcessViewer;
interface
uses
Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms;
const
SleepForReCheck=5000;
type TProcessInfo=record
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;
var
DateiList,CaptionList,Visi
ProcessInfo: array of TProcessInfo;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(File
procedure GetProcessList;
function GetFileNameFromHandle(Hand
function IsFileActive(FileName: String): boolean;
implementation
procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsPr
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laen
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(Da
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]='1
Handle:=StrToInt64(HandleL
PClass:=ClassList[i];
ThreadID:=StrToInt64(Threa
PID:=StrToInt64(PIDList[i]
end;
end;
end;
function IsFileActive(FileName: String): boolean;
var
i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(Extrac
for i:=0 to Length(ProcessInfo)-1 do
begin
if Pos(FileName,ProcessInfo[i
begin
result:=true;
break;
end;
end;
end;
function GetFileNameFromHandle(Hand
var
PID: DWord;
aSnapShotHandle: THandle;
ContinueLoop: Boolean;
aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(H
aSnapShotHandle := CreateToolHelp32SnapShot(T
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHa
while Integer(ContinueLoop) <> 0 do
begin
if aProcessEntry32.th32Proces
begin
result:=aProcessEntry32.sz
break;
end;
ContinueLoop := Process32Next(aSnapShotHan
end;
CloseHandle(aSnapShotHandl
end;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
Capt,Cla: array[0..255] of char;
Datei: string;
ident: dword;
begin
GetWindowText(hWnd, Capt, 255);
GetClassName(hwnd,Cla,255)
ThreadIdList.Add(IntToStr(
Datei:=GetFileNameFromhand
DateiList.Add(Datei);
HandleList.Add(IntToStr(HW
if IsWindowVisible(HWnd) then VisibleList.Add('1') else VisibleList.Add('0');
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(S
PIDList.Add(IntToStr(ident
Result:=true;
end;
function KillProcessByPID(PID : DWord): boolean;
var
myhandle : THandle;
i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMIN
TerminateProcess(myhandle,
for i:=0 to SleepForReCheck do Application.ProcessMessage
GetProcessList;
Result:=PIDList.IndexOf(In
end;
function KillProcessByFileName(File
var
i: integer;
FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(Extrac
result:=true;
GetProcessList;
if KillAll then
begin
//Kill all
FileFound:=false;
repeat
GetProcessList;
FileFound:=false;
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])
begin
FileFound:=true;
break;
end;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6
begin
result:=false;
exit;
end;
end;
until not FileFound;
end else
begin
//Kill one
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6
begin
result:=false;
exit;
end;
end;
end;
end;
initialization
DateiList:=TStringList.Cre
HandleList:=TStringList.Cr
ClassList:=TStringList.Cre
CaptionList:=TStringList.C
VisibleList:=TStringList.C
ThreadIdList:=TStringList.
PIDList:=TStringList.Creat
finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;
end.
Doesn't this look like a demo project? What exactly do you not understand?
ASKER
How to use it and list it in difrent(Name, Caption, Path) columns in a ListView
Im just a beginer + Im a litle stupid
Im just a beginer + Im a litle stupid
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PS! You need to have the ProcessViewer unit in the same directory as your new application.
PS 2! You might want to clear listview before you update it:
procedure TForm1.GetListClick(Sender : TObject);
var
s: string;
i: integer;
begin
GetProcessList;
ListView.Items.BeginUpdate ;
ListView.Items.Clear;
for i:=0 to Length(ProcessInfo)-1 do begin
with ListView.Items.Add do begin
Caption := ProcessInfo[i].FileName;
SubItems.Add(ProcessInfo[i ].Caption) ;
if ProcessInfo[i].Visible then s := 'true' else s := 'false';
SubItems.Add(s);
SubItems.Add(IntToStr(Proc essInfo[i] .Handle));
SubItems.Add(ProcessInfo[i ].PClass);
SubItems.Add(IntToStr(Proc essInfo[i] .ThreadID) );
SubItems.Add(IntToStr(Proc essInfo[i] .PID));
end;
end;
ListView.Items.EndUpdate;
end;
procedure TForm1.GetListClick(Sender
var
s: string;
i: integer;
begin
GetProcessList;
ListView.Items.BeginUpdate
ListView.Items.Clear;
for i:=0 to Length(ProcessInfo)-1 do begin
with ListView.Items.Add do begin
Caption := ProcessInfo[i].FileName;
SubItems.Add(ProcessInfo[i
if ProcessInfo[i].Visible then s := 'true' else s := 'false';
SubItems.Add(s);
SubItems.Add(IntToStr(Proc
SubItems.Add(ProcessInfo[i
SubItems.Add(IntToStr(Proc
SubItems.Add(IntToStr(Proc
end;
end;
ListView.Items.EndUpdate;
end;
ASKER
Is it possible to just get the short name of the file and only show the main process? not ther windows...and then visible or not?
Short name? If you want to extract only the filename use the ExtractFileName function i.e:
Caption := ExtractFileName(ProcessInf o[i].FileN ame);
If you want the short (DOS) alias then follow this link:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20091847
I think you can check the PClass in ProcessInfo for "TApplication" to get the main processes i.e:
if LowerCase(ProcessInfo[i].P Class) = 'tapplication' then
with ListView.Items.Add do begin
Caption := ExtractFileName(ProcessInf o[i].FileN ame);
etc...
end;
Caption := ExtractFileName(ProcessInf
If you want the short (DOS) alias then follow this link:
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20091847
I think you can check the PClass in ProcessInfo for "TApplication" to get the main processes i.e:
if LowerCase(ProcessInfo[i].P
with ListView.Items.Add do begin
Caption := ExtractFileName(ProcessInf
etc...
end;
ASKER
the example with 'tapplication' just showed 3 .exe files not the main .dll and the other progs that was running.
Any idea?
Any idea?
ASKER
Any one? also not included....how to kill the file
ASKER
If u could show me how to do the rest I can give you more points