Recent

Author Topic: kill process with name  (Read 13571 times)

CCdrom

  • Newbie
  • Posts: 2
kill process with name
« on: March 18, 2007, 12:21:10 pm »
Hi,

i would like to kill a specific process in windows and linux. I want to terminate an user-specific process, for example "calc.exe" under windows or "xterm" in linux. Under delphi i used this function, but that doensn't work in lazarus and that isn't platform independent.
Code: [Select]

function KillTask(ExeFileName: string): integer;
const
 PROCESS_TERMINATE=$0001;
var
 ContinueLoop: BOOL;
 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;


How can I do this platform-independent ?

Thanks for your help,
CCdrom

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: kill process with name
« Reply #1 on: March 31, 2007, 10:27:11 am »
I guess FpKill is what you are looking for. See BaseUnix unit FPC documentation.

bonmario

  • Sr. Member
  • ****
  • Posts: 322
Re: RE: kill process with name
« Reply #2 on: May 04, 2008, 09:37:18 am »
Quote from: "antonio"
I guess FpKill is what you are looking for. See BaseUnix unit FPC documentation.


Someone knows how to get the process list? I need it before to call fpkill but i don't understand how obtain it :-(

Thanks, Mario

Zablus

  • New member
  • *
  • Posts: 7
Re: kill process with name
« Reply #3 on: May 14, 2010, 09:16:58 pm »
I know this thread is very old but I see that bonmario is still active in this forum.

@bonmario
Do you have an answer for your question about getting a process list on linux?
I don't want to analyse the "ps aux" response in my code. I think this way of getting a process list isn't really save.


bonmario

  • Sr. Member
  • ****
  • Posts: 322
Re: kill process with name
« Reply #4 on: May 15, 2010, 05:29:49 am »
Sorry, but i've used the command "ps" to obtain the list of all the process and "ps -C" to obtain the list of a known process name.

Hi, Mario


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3998
  • I like bugs.
Re: kill process with name
« Reply #5 on: May 15, 2010, 05:51:50 am »
Source of "ps" and other process related stuff:

http://procps.sourceforge.net/

It shows how it is done using kernel calls and it could be ported to Pascal.
File "readproc.c" is important (found by a google search, I don't know this personally).

If someone decides to port such code and perhaps make a platform independent function, it could be included in FCL.

A function GetProcessIDByName() would be useful and functions to iterate all processes.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Zablus

  • New member
  • *
  • Posts: 7
Re: kill process with name
« Reply #6 on: May 15, 2010, 08:13:31 am »
Thanks for your answers!

It would be very nice if anybody woul port the C-code to Pascal.
My C (and Linux) experience isn't high enough to do it by myself.

Probably I found a way to kill a process by name. I tried the killall command.
It seems to work in my case. But I don't know whether it works in a really save way.

Stamp

  • Newbie
  • Posts: 2
Re: kill process with name
« Reply #7 on: July 27, 2010, 06:07:54 pm »
Sorry, gotta rez this old thread :P

Did anyone actually make a class in fpc that does this? 

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9175
  • FPC developer.
Re: kill process with name
« Reply #8 on: July 27, 2010, 11:43:02 pm »
Source of "ps" and other process related stuff:

http://procps.sourceforge.net/

It shows how it is done using kernel calls and it could be ported to Pascal.
File "readproc.c" is important (found by a google search, I don't know this personally).

If someone decides to port such code and perhaps make a platform independent function, it could be included in FCL.

On most other *nixes, /proc is hardly filled, so this is very Linux specific (and might not even work on embedded linuxes)

The problem is that this functionality is not standarized unixwide, except for the "ps" command

 

TinyPortal © 2005-2018