• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 807
  • Last Modified:

Kill Application with process id e.g notepad.exe

How can i kill application like notepad not with findwindow, i will search for the processID like notepad.exe and send WM_CLOSE or WM_DESTROY. Can i check notepad is running and the kill the application?

Can you write complete code, i'm not so good in this

Regards
Trausti
0
trausti
Asked:
trausti
  • 12
  • 10
  • 2
1 Solution
 
intheCommented:
hi , have a look here:

http://nettrash.com/users/madshi/
0
 
traustiAuthor Commented:
I can't find anything, can you let me have complete code for a button.click?
0
 
intheCommented:
if you have D4 you download the enumStuff unit from there.
look at the function at he bottom of first page:

function IsApplicationRunning (exeFile: string) : boolean;
var pl : TProcessList;
    i1 : integer;
begin
  result:=false;
  pl:=GetProcessList;
  for i1:=0 to high(pl) do
    if CompareText(ExtractFileName(pl[i1].exeFile),ExtractFileName(exeFile))=0 then begin
      result:=true;
      exit;
    end;
end;

Then you can call GetProcessList to get a dynamic array with the processID and the exefile path of all running processes.
To end a process you should call OpenProcess to get a process handle to that process, then TerminateProcess(processHandle).

what version of delphi do you have?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
traustiAuthor Commented:
I get an error on "var pl : TProcessList;" Undeclare Indentifier

Can you give me complete source for the terminating?
0
 
intheCommented:
I need to know what version of delphi you have as this makes a difference to the code, also need to know this for the other question about the mapping drives.(again the code will be different depending on what veriosn)
im trying to get that working so it works when clicking on a node in the treeview it will mapp the drive.i think it is possible to make it work.
well i hope certainly so.
0
 
traustiAuthor Commented:
I use Delphi 4, thanks
0
 
intheCommented:
hi,
first im sorry this is late i've been quite sick so just sleeping alot for the last few days :-(

note:
for this solution you do not need the enumstuff unit .

make a new project and put 1 button on the form and double-click it,then paste in this code over the code that you have
or just use the code but add "tlhelp32" to the uses section.:

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function CloseApp(FileName : string) : Boolean;
var
  Handle : HWND;
  pInfo  : ProcessEntry32;
  Found  : Boolean;
  ProcessID : Cardinal;
begin
  handle :=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
  pInfo.dwSize :=sizeOf(ProcessEntry32);
  found :=Process32First(Handle,pInfo);
  Result :=false;
  ProcessID :=0;
  if found = false then begin
    ShowMessage('Error creating Toolhelp32SnapShot');
    exit;
  end;
  While found do begin
    if pos(String(StrUpper(PChar(FileName))),String(StrUpper(PChar(String(pInfo.szExeFile))))) > 0 then begin       ProcessID :=pInfo.th32ProcessID;
      Found :=false;
    end else
      Found :=Process32Next(handle,pInfo);
  end;
  CloseHandle(Handle);
  if ProcessID <> 0 then begin
    TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);
     Result :=true;
  end;
end;
 
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if not CloseApp('notepad.exe') then
   ShowMessage('Error finding program') else
    ShowMessage('Program closed');
   end;

end.


extra infos:

as i said before you dont need this enumstuff anymore but so you know what to do in the future when you get  the error you were getting when you get an error like this:

"var pl : TProcessList;" Undeclare Indentifier

it is because delphi cant find the path to the pas file that declares TProcessList (in this case it was enumstuff.pas from madshis web site)
you would have needed to add the path where you installed/unzipped the file  to delphi's library path.
for instance mine is in d:\delphi\delphi4\lib\add\winobj
directory ,so i would open delphi ,go to tools menu,enviroment options,library tab,click the button beside the library path combobox and type the line:
$(DELPHI)\lib\add\Winobj
click "add" , click "ok" now when i
need a function from one o these files in this winobj director i add the units name(enumstuff)to the uses section and when i compile a project that uses any file in winobj directory delphi knows where to find them.

another note :
you may get that error the first time you use tlhelp32 if you've never used it before ,so if you do get error ,do a tlhelp32.* search (using start-find-files or folders)in your delphi directory for it and add the path you get to library path.


Regards Barry

0
 
traustiAuthor Commented:
You are genius!!!

Thanks
0
 
traustiAuthor Commented:
I got this error when i click the button

Error creating toolhelp32snapshot and error finding program
0
 
intheCommented:
hi
give me exact errors.
do you add toolhelp32 to the uses section?
can you "build all" without errors(if it does it means that the library path is correct so we look elsewhere for faults)
has it run at all without errors?
give plenty of details as i think this must be annoying you now as you been waiting all week for it :-(

"error finding program " is the custom error to say there was no exe running
so are you sure you typed in the correct exe name?what is the exe name you typed?

"Error creating toolhelp32snapshot "
this error probably caused the error above to happen if the snapshot never worked but i never heard of this eror happening so i have to dig into it a bit.
if you want i can send you a demo of the sample i made where you can select the exe to look for using a opendialog.
leave you email address and i send a demo project.
Regards Barry

0
 
traustiAuthor Commented:
I put "Tlhelp32" in the uses clause

I can build the program with no errors

The program runs with no errors

Here is my email address

tr@vis.is, and thanks

0
 
intheCommented:
ok i wish i had known you were using nt.
CreateToolhelp32Snapshot only works in 9* and NT5, for other nt (nt3,nt4 etc) you have to use EnumProcesses() (from PSAPI.dll).

but...

with the code i was giving originally (the stuff from madshi site) will work on nt so we have to start again and get rid of the errors you were getting
remember the comment you wrote:
I get an error on "var pl : TProcessList;" Undeclare Indentifier

well see the stuff i wrote about that error a few comments below and see if you can get rid of it.
have you still got the enumstuff.pas file?
if not go back and download it and put it in your lib directory.
start a new project ,add Enumstuff to the uses section and try the code for
IsApplicationRunning () function i pasted here.

0
 
traustiAuthor Commented:
Do you have a complete example for me?

i use ISApplicationRunning ans it works fine but i want another button that can kill the process e.g notepad.exe
0
 
intheCommented:
ok think i got it working properly.


uses
     Enumstuff;

Procedure Killapp(exeFile: string);
var pl : TProcessList;
    i1 : integer;
     h : thandle;
begin
  pl:=GetProcessList;
  for i1:=0 to high(pl) do
    if CompareText(ExtractFileName(pl[i1].name),ExtractFileName(exeFile))=0 then begin
 h := openprocess(PROCESS_TERMINATE,false,pl[i1].pid);
   TerminateProcess(h,0);
   closehandle(h);
      exit;
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
killapp('notepad.exe')
end;

end.



think that should work fine
Regards Barry
0
 
traustiAuthor Commented:
Hello Barry, this example works fine with parameter notepad.exe. But if i want to kill another process e.g delphi32.exe i can't

What is wrong?
0
 
traustiAuthor Commented:
Sorry, i thing i was giving comment to quick.

I compile the application and try to kill delphi32.exe and it works,
maby Delphi set any sharing on the process, thanks
0
 
intheCommented:
ok good :-)
now is everything solved in this question so i can get points :-))
0
 
traustiAuthor Commented:
Of course!!!

you anwser this question wery well 
0
 
intheCommented:
then you have to accept my proposed answer,you should have an option on the page to accept/reject etc .
0
 
traustiAuthor Commented:
Great!!!
0
 
ShakilCommented:
enumstuff.pas , i want to download it

Please attach websit address

shakil
0
 
traustiAuthor Commented:
0
 
ShakilCommented:
Where is
enumstuff.pas on that site.

Shakil

0
 
intheCommented:
it is not available anymore the components have recently been sorted into those mad* packages on the site,some shareware some freeware.
you could ask a question titled " to madshi" or something and he may still have a copy he could send you.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 12
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now