Link to home
Start Free TrialLog in
Avatar of trausti
trausti

asked on

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
Avatar of inthe
inthe

hi , have a look here:

http://nettrash.com/users/madshi/
Avatar of trausti

ASKER

I can't find anything, can you let me have complete code for a button.click?
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?
Avatar of trausti

ASKER

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

Can you give me complete source for the terminating?
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.
Avatar of trausti

ASKER

I use Delphi 4, thanks
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trausti

ASKER

You are genius!!!

Thanks
Avatar of trausti

ASKER

I got this error when i click the button

Error creating toolhelp32snapshot and error finding program
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

Avatar of trausti

ASKER

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

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.

Avatar of trausti

ASKER

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
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
Avatar of trausti

ASKER

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?
Avatar of trausti

ASKER

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
ok good :-)
now is everything solved in this question so i can get points :-))
Avatar of trausti

ASKER

Of course!!!

you anwser this question wery well 
then you have to accept my proposed answer,you should have an option on the page to accept/reject etc .
Avatar of trausti

ASKER

Great!!!
enumstuff.pas , i want to download it

Please attach websit address

shakil
Where is
enumstuff.pas on that site.

Shakil

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.