> I would like to kill a process from my program. I know the name of the
application (for example cmd.exe).      Does anybody know how can i do
this ?

I'm not sure how to do this in a platform independent way, I'm also not
aware of a library that encapsulates this functionality. On Windows
there are several options:

1.  Find window handle associated with main window in process and send a
WM_CLOSE message.  This is similar to clicking on the x button of a
window to close it.  Of course the process can ignore this message, or
pop up a dialog to ask whether you are certain etc.  Finding the window
handle from just the exe name may be difficult, since you have to
enumerate all top level windows using EnumWindows, then retrieve the
process ID of the process that created that window using
GetWindowThreadProcessId, then check that the file name of the
executable of that process matches your application name using
GetModuleFileNameEx.

2.  Enumerate all processes using EnumProcesses, then check exe name for
each process ID using the GetModuleFileNameEx function.  Once you have
identified the correct process ID, you can either call TerminateProcess
which would kill the process and all threads unconditionally, or attach
a thread to this process that calls ExitProcess, which allows a more
elegant termination of the process but involves more work.

3.  You can also use the Tool Help functions to enumerate the processes
by calling CreateToolhelp32Snapshot and iterate through this list with
Process32First/Process32Next.  The Process32First/Next functions return
information on the process including the exe name and process ID.  Once
the process is identified, terminate as in 2 above.

The functions mentioned above are windows specific and can be found in
various units in the packages\base\winunits folder.  Obviously this is
only a start, you can search for code snippets on Google using the
functions I have mentioned (or maybe even discover an aesier way to do
it).

Regards,
Christo

        

----------------------------------------------------------------------------
NOTICE: Please note that this eMail, and the contents thereof, 
is subject to the standard Sasol eMail legal notice which may be found at: 
http://www.sasol.com/legalnotices                                               
                                                           

If you cannot access the legal notice through the URL attached and you wish 
to receive a copy thereof please send an eMail to 
[EMAIL PROTECTED]
----------------------------------------------------------------------------

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to