Solved

How to use!

Posted on 2001-09-16
10
653 Views
Last Modified: 2012-05-04
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,VisibleList,HandleList,ClassList,ThreadIdList,PIDList: TStringList;
      ProcessInfo: array of TProcessInfo;

function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
procedure GetProcessList;
function GetFileNameFromHandle(Handle: 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(@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;

function IsFileActive(FileName: String): boolean;
var
      i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(ExtractFileName(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(Handle: hwnd):string;
var
      PID: DWord;
      aSnapShotHandle: THandle;
      ContinueLoop: Boolean;
      aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(Handle, @PID);
aSnapShotHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHandle, aProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
      if aProcessEntry32.th32ProcessID = PID then
      begin
            result:=aProcessEntry32.szExeFile;
            break;
      end;
      ContinueLoop := Process32Next(aSnapShotHandle, aProcessEntry32);
end;
CloseHandle(aSnapShotHandle);
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(GetWindowThreadProcessId(hwnd,nil)));
Datei:=GetFileNameFromhandle(hwnd);
DateiList.Add(Datei);
HandleList.Add(IntToStr(HWnd));
if IsWindowVisible(HWnd) then VisibleList.Add('1') else VisibleList.Add('0');
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(StrToInt(HandleList[HandleList.Count-1]),@ident);
PIDList.Add(IntToStr(ident));
Result:=true;
end;

function KillProcessByPID(PID : DWord): boolean;
var
      myhandle : THandle;
      i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMINATE, False, PID);
TerminateProcess(myhandle, 0);
for i:=0 to SleepForReCheck do Application.ProcessMessages; //Genug Zeit geben
GetProcessList;
Result:=PIDList.IndexOf(IntToStr(PID))=-1;
end;

function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
var
      i: integer;
      FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(ExtractFileName(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(StrToInt64(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(StrToInt64(PIDList[i])) then
            begin
                  result:=false;
                  exit;
            end;
      end;
end;
end;

initialization
DateiList:=TStringList.Create;
HandleList:=TStringList.Create;
ClassList:=TStringList.Create;
CaptionList:=TStringList.Create;
VisibleList:=TStringList.Create;
ThreadIdList:=TStringList.Create;
PIDList:=TStringList.Create;

finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;

end.
0
Comment
Question by:SULTAN
  • 5
  • 5
10 Comments
 
LVL 1

Expert Comment

by:Fraction
ID: 6486229
Doesn't this look like a demo project? What exactly do you not understand?
0
 

Author Comment

by:SULTAN
ID: 6486246
How to use it and list it in difrent(Name, Caption, Path) columns in a ListView

Im just a beginer + Im a litle stupid
0
 
LVL 1

Accepted Solution

by:
Fraction earned 50 total points
ID: 6487453
Hi Sultan!
You don't need to be stupid just because your a beginner ;-)

Anyway, try to create a new application and paste this code, it might help you getting started:

// -- (Cut from here!) -------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ProcessViewer,
  StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    Panel: TPanel;
    ListView: TListView;
    Button: TButton;
    procedure GetListClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Panel := TPanel.Create(Form1);
  Panel.Parent := Form1;
  Panel.Align := alBottom;
  Panel.Height := 23;

  ListView := TListView.Create(Form1);
  ListView.Parent := Form1;
  ListView.Align := alClient;
  ListView.ViewStyle := vsReport;

  with ListView.Columns.Add do begin
    Caption := 'FileName';
    AutoSize := true;
  end;
  with ListView.Columns.Add do begin
    Caption := 'Caption';
    AutoSize := true;
  end;
  with ListView.Columns.Add do begin
    Caption := 'Visible';
    AutoSize := true;
  end;
  with ListView.Columns.Add do begin
    Caption := 'Handle';
    AutoSize := true;
  end;
  with ListView.Columns.Add do begin
    Caption := 'Class';
    AutoSize := true;
  end;
  with ListView.Columns.Add do begin
    Caption := 'ThreadID';
    AutoSize := true;
  end;

  Button := TButton.Create(Panel);
  Button.Parent := Panel;
  Button.Align := alLeft;
  Button.Width := 100;
  Button.Caption := 'Get Process List';
  Button.OnClick := GetListClick;
end;

procedure TForm1.GetListClick(Sender: TObject);
var
  s: string;
  i: integer;
begin
  GetProcessList;
  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(ProcessInfo[i].Handle));
      SubItems.Add(ProcessInfo[i].PClass);
      SubItems.Add(IntToStr(ProcessInfo[i].ThreadID));
      SubItems.Add(IntToStr(ProcessInfo[i].PID));
    end;
  end;
end;

end.
// -- (Cut to here!) -------------------

//GL Fraction!
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Expert Comment

by:Fraction
ID: 6487462
PS! You need to have the ProcessViewer unit in the same directory as your new application.
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6488279
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(ProcessInfo[i].Handle));
      SubItems.Add(ProcessInfo[i].PClass);
      SubItems.Add(IntToStr(ProcessInfo[i].ThreadID));
      SubItems.Add(IntToStr(ProcessInfo[i].PID));
    end;
  end;
  ListView.Items.EndUpdate;
end;
0
 

Author Comment

by:SULTAN
ID: 6489140
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?
0
 
LVL 1

Expert Comment

by:Fraction
ID: 6489432
Short name? If you want to extract only the filename use the ExtractFileName function i.e:
Caption := ExtractFileName(ProcessInfo[i].FileName);

If you want the short (DOS) alias then follow this link:
http://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].PClass) = 'tapplication' then
  with ListView.Items.Add do begin
    Caption := ExtractFileName(ProcessInfo[i].FileName);
    etc...
  end;
0
 

Author Comment

by:SULTAN
ID: 6498498
the example with 'tapplication' just showed 3 .exe files not the main .dll and the other progs that was running.

Any idea?
0
 

Author Comment

by:SULTAN
ID: 6510746
Any one? also not included....how to kill the file
0
 

Author Comment

by:SULTAN
ID: 6512286
If u could show me how to do the rest I can give you more points
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
code issue 8 131
Print Graphic and Text to Epson TM-T88v 12 260
Convert GUI app into console app for Win32 Env 5 100
firemonkey keyboard covers the controls 1 26
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question