Processes List

Hello,

I have a PopupMenu,

I need to know how to at runtime make this like have a menu of all running exe's
e.g

My Menu > Running Processes > explorer.exe
                                                notepad.exe
                                                someapp.exe

But also when I click one one lets say 'notepad.exe' it will kill that task.

Needs to work on Windows XP
LVL 2
-Karamja-Asked:
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.

Mohammed NasmanSoftware DeveloperCommented:
Hello

Download JCL library, and look at jclsysinfo unit, there's function called RunningProcessesList, which will return all the Processes running, its work on win 9x to win XP, and there are some samples included in with JCL which give you current process and ability to kill any process

you can download it from here
http://sourceforge.net/projects/jcl

Also look at Madshi Kernal
www.madshi.net
0
HypoviaxCommented:
I'm coding one right now for you...
0
HypoviaxCommented:
Ok here is a complete working version i coded and tested in XP:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus,Tlhelp32,ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    PopupMenu1: TPopupMenu;
    sdfsd1: TMenuItem;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);


  private
    procedure findprocesses ;
    procedure MenuClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



function KillTask(ExeFileName: string): integer;
 const  //Kills a process by exename
 PROCESS_TERMINATE=$0001;
 var
 ContinueLoop: BOOLEAN;
 FSnapshotHandle: THandle;
 FProcessEntry32: TProcessEntry32;
             begin
              //      result := 0;
                   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
                   ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);

 while integer(ContinueLoop) <> 0 do
 begin
             if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =UpperCase(ExeFileName))
             or (UpperCase(FProcessEntry32.szExeFile) =
             UpperCase(ExeFileName))) then
             Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID), 0));
                ContinueLoop := Process32Next(FSnapshotHandle,
              FProcessEntry32);
 end;
 CloseHandle(FSnapshotHandle);
end;



function StripChars(const Value: string; SkipChars: string): string;
var
  X,y: Integer;
  Max,maxskips: Integer;
  CurrentChr,CurrentSkip:String;
  foundskipchar:boolean;
begin
max:=length(value);
maxskips:=length(SkipChars);
   For X:=1 to max do
   begin
   foundskipchar:=false;
   CurrentChr:=copy(value,x,1);
    For Y:=0 to maxskips do
      begin
         CurrentSkip:=copy(skipchars,y,1);
        if CurrentChr=CurrentSkip then
          begin
              foundskipchar:=true;
          end;
      end;
      if foundskipchar<>true then
        begin
          result:=result+CurrentChr
        end;
   end;
end;

procedure TForm1.MenuClick(Sender: TObject); //this occurs when the click the item on the popupmenu
var
  tmpMenuItem : TMenuItem;
  process:string;
begin
  tmpMenuItem := Sender As TMenuItem;
  //for some reason we need to get rid of the & symbol so:
  process:=stripchars(tmpmenuitem.Caption,'&');
  killtask(process);     //kill it
end;

procedure tform1.findprocesses ; //adds to the popupmenu
const
 PROCESS_TERMINATE=$0001;
 var
 ContinueLoop: BOOLEAN;
 FSnapshotHandle: THandle;
 FProcessEntry32: TProcessEntry32;
 menuitem:Tmenuitem;
             begin

     popupmenu1.Items.Clear;     //clear previous list
                   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
                   ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);

 while integer(ContinueLoop) <> 0 do
 begin    //add to list
 popupmenu1.items.Add(NewItem(FProcessEntry32.szExeFile, TextToShortCut(''), False, True, MenuClick, 0, 'Item1'));
  ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
 CloseHandle(FSnapshotHandle);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin //this can be whereever you want it to be.
if button=mbright then //right click on form
begin
findprocesses; //create and update list
popupmenu1.Popup(form1.left+100,form1.Top-100); //popup someplace
end;
 end;



end.

Best Regards,

Hypoviax
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
HypoviaxCommented:
Thanks,

I hope it is what you wanted

Regards,

Hypoviax
0
-Karamja-Author Commented:
It is thanks ;)
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.