Board index » delphi » Killing a running process

Killing a running process

I've managed to list running processes with this code

uses
  Tlhelp32;

procedure LookForExe;
var
  fnd    : Boolean;
  PHndle : THandle;
  Proc   : TProcessEntry32;
  Snap   : THandle;
begin
  Snap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
  Proc.dwSize := SizeOf(TProcessEntry32);
  Process32First(Snap,Proc);
  repeat
    fnd := (UpperCase(String(ExtractFileName(Proc.szExeFile))) =
'DONKEY.EXE');
  until (not Process32Next(Snap,Proc)) or (fnd);

  If fnd then
    begin
      {Code to kill DONKEY.EXE}
    end;
end;

How do I kill the process?? (Donkey.exe)
Can i append something to this code, or do i have to rewrite it?
If you know how, send source code or instructions to my email, do not answer
to the newgroup. Please.....

J...@LinuxX.nu <- remove the big X:s, (spam protect)

 

Re:Killing a running process


On Fri, 19 Nov 1999 11:05:46 +0100, "Jim Persson" <J...@LinuxX.Nu>
wrote:

Quote
>I've managed to list running processes with this code

>uses
>  Tlhelp32;

>procedure LookForExe;
>var
>  fnd    : Boolean;
>  PHndle : THandle;
>  Proc   : TProcessEntry32;
>  Snap   : THandle;
>begin
>  Snap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
>  Proc.dwSize := SizeOf(TProcessEntry32);
>  Process32First(Snap,Proc);
>  repeat
>    fnd := (UpperCase(String(ExtractFileName(Proc.szExeFile))) =
>'DONKEY.EXE');
>  until (not Process32Next(Snap,Proc)) or (fnd);

>  If fnd then
>    begin

**************************************************

TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.th32ProcessID));
**************************************************

Quote
>    end;
>end;

>How do I kill the process?? (Donkey.exe)
>Can i append something to this code, or do i have to rewrite it?
>If you know how, send source code or instructions to my email, do not answer
>to the newgroup. Please.....

The source code is between the if fnd then begin and end.

--
Regards,
Richard Speelmans
15-year-old boy from holland.

Re:Killing a running process


This doesn't work
It says, "Not enought actual parameters"
So i tried to change to
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.th32ProcessID));
Then nothing happened at all!
How do i do?

Quote
> On Fri, 19 Nov 1999 11:05:46 +0100, "Jim Persson" <J...@LinuxX.Nu>
> wrote:

> >I've managed to list running processes with this code

> >uses
> >  Tlhelp32;

> >procedure LookForExe;
> >var
> >  fnd    : Boolean;
> >  PHndle : THandle;
> >  Proc   : TProcessEntry32;
> >  Snap   : THandle;
> >begin
> >  Snap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
> >  Proc.dwSize := SizeOf(TProcessEntry32);
> >  Process32First(Snap,Proc);
> >  repeat
> >    fnd := (UpperCase(String(ExtractFileName(Proc.szExeFile))) =
> >'DONKEY.EXE');
> >  until (not Process32Next(Snap,Proc)) or (fnd);

> >  If fnd then
> >    begin
> **************************************************

> TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.th32ProcessID));
> **************************************************
> >    end;
> >end;

> >How do I kill the process?? (Donkey.exe)
> >Can i append something to this code, or do i have to rewrite it?
> >If you know how, send source code or instructions to my email, do not
answer
> >to the newgroup. Please.....

> The source code is between the if fnd then begin and end.

> --
> Regards,
> Richard Speelmans
> 15-year-old boy from holland.

Re:Killing a running process


On Fri, 19 Nov 1999 19:11:44 +0100, "Jim Persson" <J...@LinuxX.Nu>
wrote:

Quote
>This doesn't work
>It says, "Not enought actual parameters"
>So i tried to change to
>TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.th32ProcessID));

sorry my mistake :
TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,proc.th32ProcessID),1);

If it still doesn't work check the Help file
\Borland\Borland Shared\MSHelp\win32.hlp. You can find there
TerminateProcess and OpenProcess.

--
Regards,
Richard

Re:Killing a running process


eeh... it worked! But it didn't close DONKEY.EXE, it closed Delphi
instead.....not what i wanted.....

/Jim

Robin <robinh...@hotpop.com> skrev i
diskussionsgruppsmeddelandet:3835af38.305...@news.quicknet.nl...

Quote
> On Fri, 19 Nov 1999 19:11:44 +0100, "Jim Persson" <J...@LinuxX.Nu>
> wrote:

> >This doesn't work
> >It says, "Not enought actual parameters"
> >So i tried to change to

>TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,Proc.th32ProcessID));

> sorry my mistake :

TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,proc.th32ProcessID),1);
Quote

> If it still doesn't work check the Help file
> \Borland\Borland Shared\MSHelp\win32.hlp. You can find there
> TerminateProcess and OpenProcess.

> --
> Regards,
> Richard

Re:Killing a running process


Quote
On Sat, 20 Nov 1999 13:16:07 +0100, "Jim Persson" <J...@LinuxX.Nu> wrote:
>eeh... it worked! But it didn't close DONKEY.EXE, it closed Delphi
>instead.....not what i wanted.....

Ok here is the complete code :

uses tlhelp32;

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('donkey.exe') then ShowMessage('Error finding program') else ShowMessage('Program closed');
  end;

It is tested with Delphi 5 trial, but I know it works with Delphi 4 Client/Server too.

--
Regards,
Richard

Re:Killing a running process


Thank you! Now it works perfect!

/Jim

Robin <robinh...@hotpop.com> skrev i
diskussionsgruppsmeddelandet:3836c8e0.677...@news.quicknet.nl...

Quote
> On Sat, 20 Nov 1999 13:16:07 +0100, "Jim Persson" <J...@LinuxX.Nu> wrote:

> >eeh... it worked! But it didn't close DONKEY.EXE, it closed Delphi
> >instead.....not what i wanted.....

> Ok here is the complete code :

> uses tlhelp32;

> 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.szE
xeFile))))) > 0 then begin

- Show quoted text -

Quote
>       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('donkey.exe') then ShowMessage('Error finding program')

else ShowMessage('Program closed');

- Show quoted text -

Quote
>   end;

> It is tested with Delphi 5 trial, but I know it works with Delphi 4
Client/Server too.

> --
> Regards,
> Richard

Other Threads