View & Kill Processes

How to view all windows' running processes (like in windows task manager), and kill any process?
Like place all processes in ListBox and kill the one the user choose...
Thank you
peritoXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tobjectpascalCommented:
Alright then, New Application. Stick a Button and a Listbox on there

unit ProKiller;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  WindowList: TList;


implementation

{$R *.DFM}


function EnumWindowsProc(wHandle: HWND; lb: TListBox): Bool; stdcall; export;
var
  Title, ClassName: array[0..255] of char;
  ProcessID,CPid: DWord;
begin
  Result := True;
  GetWindowText(wHandle, Title, 255);
  GetClassName(wHandle, ClassName, 255);
  if IsWindowVisible(wHandle) then
   Begin
     GetWindowThreadProcessID(wHandle,@cpid);
     lb.Items.AddObject(string(Title) + '-' + string(ClassName)+' PID '+IntToStr(CPID),Tobject(CPID));
   End;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
 ListBox1.Clear;
 EnumWindows(@EnumWindowsProc, Integer(Listbox1));
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
Var
 N: Integer;
 CPID: Dword;
begin
 N:=Form1.ListBox1.ItemIndex;
 CPID:=DWord(ListBox1.Items.Objects[N]);
 TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,CPID),1);
end;

end.

Make sure you assign the double click event for the ListBox as i've made it kill the process of whatever you clicked on.
0
peritoXAuthor Commented:
mmm
it didnt show the processes, it showed the applications
I mean it didnt show
msnmsgr.exe
ctfmon.exe
explorer.exe
...

it showed
Form1
Delphi Proramming: View & Kill Processes - Microsoft Internet Explorer

...

one more thing, how to create a new process
Ill add points
0
tobjectpascalCommented:
 if IsWindowVisible(wHandle) then
   Begin
     GetWindowThreadProcessID(wHandle,@cpid);
     lb.Items.AddObject(string(Title) + '-' + string(ClassName)+' PID '+IntToStr(CPID),Tobject(CPID));
   End;

replace for

     GetWindowThreadProcessID(wHandle,@cpid);
     lb.Items.AddObject(string(Title) + '-' + string(ClassName)+' PID '+IntToStr(CPID),Tobject(CPID));

and you'll see what i mean.


new process, look up CreateProcess.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

tobjectpascalCommented:
in other words remove

>> if IsWindowVisible(wHandle) then
   >>Begin
>> End;

remove them 3 lines.

0
peritoXAuthor Commented:
okey....
now I got many many more things, something like

Shell Embedding PID 3784
ComboLBox PID 4004
tooltips_class32 PID 4004

and many more things

I only want exactly what is found in the process window
exact name
explorer.exe
bla bla

can it be done?
0
2266180Commented:
hi,

you can use WMI to get the list of processes/services with pid.:
http://www.ciuly.com/delphi/wmi/runningProcesses/index.html
and
http://www.ciuly.com/delphi/wmi/runningServices/index.html
and terminate the process by pid (for example using the solution from here:http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20651898.html)
0
peritoXAuthor Commented:
its not working
a unit is missing
I guess thats the WMI
where can I download the unit???
0
2266180Commented:
:D sorry. forgot that not everybody is setting up their delphi for WMI. here is the "how to":
http://www.ciuly.com/delphi/wmi/settingUpWmi/index.html
0
tammozCommented:
hi
you can use the jv components you can download theme from
www.Jedi-project.org
there is an example that shoots your goal exactly
best regards
0
peritoXAuthor Commented:
jedi-project.org is not working (The link)
site down?
0
2266180Commented:
google sayd http://www.delphi-jedi.org/ maybe they changed they domain.
0
saravananvgCommented:
Hello Sir,

   Try the following code snippet.


Place a Listbox and a button on a form.

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 601
  Height = 389
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object Button2: TButton
    Left = 224
    Top = 296
    Width = 75
    Height = 25
    Caption = '&Delete'
    TabOrder = 0
    OnClick = Button2Click
  end
  object ListBox1: TListBox
    Left = 56
    Top = 8
    Width = 449
    Height = 249
    ItemHeight = 13
    TabOrder = 1
  end
end






unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Tlhelp32, StdCtrls;
const
  MAX_PATH           = 260;
  TH32CS_SNAPPROCESS = 2;
type
  TProcessEntry32 = packed record
    iSize,
    iUsage,
    iProcessID,
    iDefaultHeapId,
    iModuleId,
    iThreads,
    iParentProcessId,
    iPriClassBase,
    iFlags  : Integer;
    aExeFile: array[0..MAX_PATH] of Char;
  end;
  function CreateToolHelpSnapShot(lFlags, lProcessId: Longint): Longint; stdcall;
  function ProcessFirst(hSnapshot: longint; var uProcess: TProcessEntry32): Longint; stdcall;
  function ProcessNext(hSnapshot: Longint; var uProcess: TProcessEntry32): Longint; stdcall;
  procedure CloseHandle(hPass: Longint); StdCall;
  function IsProcess(sExe: string): Boolean;

type
  TForm1 = class(TForm)
    Button2: TButton;
    ListBox1: TListBox;
    function KillTask(sExe: string): Integer;
    procedure FillList;
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  ExitCode: longword;
implementation

  function CreateToolHelpSnapShot; external 'kernel32.dll' name 'CreateToolhelp32Snapshot';
  function ProcessFirst; external 'kernel32.dll' name 'Process32First';
  function ProcessNext; external 'kernel32.dll' name 'Process32Next';
  procedure CloseHandle; external 'kernel32.dll';
  procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
  external user32 Name 'SwitchToThisWindow';
         {x = false: Size unchanged, x = true: normal size}
{$R *.dfm}

function IsProcess(sExe: string): Boolean;
(*
** This routine examines Windows processes currently running to see if a
** certain program is running.
**
** sExe  : The executable name of the program to check.
** Result: True if the program is running, False otherwise.
*)
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
  EXEName : String;
begin
  Result := False;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then begin
        Result := True;
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

function TForm1.KillTask(sExe: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
  EXEName : String;
begin
  Result := 0;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then
      begin
        GetExitCodeProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), ExitCode);  // Optional
        GetExitCodeThread(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads), ExitCode);
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID));
        CloseHandle(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iThreads));
        Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),rProcess.iProcessID), 0));
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

procedure TForm1.FillList;
(*
** This routine examines Windows processes currently running to see if a
** certain program is running.
**
** sExe  : The executable name of the program to check.
** Result: True if the program is running, False otherwise.
*)
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
begin
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      ListBox1.AddItem(UpperCase(rProcess.aExeFile),Self);
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  KillTask(ListBox1.Items.Strings[ListBox1.ItemIndex]);
  ListBox1.Clear;
  FillList;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  FillList;
end;

end.

with regards,
padmaja.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scay7Commented:
MR Padmaja

Thanks for that one i was looking for something similar to that, and that wonderful example is what i needed exactly. Shot!

Scay7

 *** peritoX use This example above my post is what you were looking for, POINTS to saravananvg !!! ***
0
peritoXAuthor Commented:
@saravananvg
exactly as I wanted
you've got the points

before I accept can u give me the code for creating New Porcesses
I'll increase 30 points
Thank you
0
saravananvgCommented:
Hello Sir,

  You can use the ShellExecute Function to create  new process.

  check the following example to create a new process.

uses ShellApi;
...
ShellExecute(Handle, 'open', 'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL) ;

with regards,
padmaja.
0
peritoXAuthor Commented:
Thank You
***Points Increased***
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.