Using ruby1.7.3 (2002-11-17) [i386-mswin32]
-
Under Windows (XP), is there a way to kill a process which is created
using the system(" …") call ? -
How does one use the Windows API: TerminateProcess?
Here is my attempt, assuming pid = 1234 (not sure if I am doing it right):
require ‘Win32API’
tp = Win32API.new(“kernel32”,“TerminateProcess”,[‘i’,‘i’],‘i’)
tp.call(1234,1)
=begin
The TerminateProcess function terminates the specified process and all of
its threads.
BOOL TerminateProcess(
HANDLE hProcess,
UINT uExitCode
);
Parameters
hProcess
[in] Handle to the process to terminate.
Windows NT/2000/XP: The handle must have the PROCESS_TERMINATE access
right. For more information, see Process Security and Access Rights.
uExitCode
[in] Exit code to be used by the process and threads terminated as a
result of this call. Use the GetExitCodeProcess function to retrieve a
process’s exit value. Use the GetExitCodeThread function to retrieve a
thread’s exit value.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError.
Remarks
The TerminateProcess function is used to unconditionally cause a process to
exit. Use it only in extreme circumstances. The state of global data
maintained by dynamic-link libraries (DLLs) may be compromised if
TerminateProcess is used rather than ExitProcess.
TerminateProcess initiates termination and returns immediately. This stops
execution of all threads within the process and requests cancellation of all
pending I/O. The terminated process cannot exit until all pending I/O has
been completed or canceled.
Terminating a process causes the following:
1… All object handles opened by the process are closed.
2… All threads in the process terminate their execution. Each thread
exits when all its pending I/O has been completed or canceled. The process
exits after all its threads exit.
3… The state of the process object becomes signaled, satisfying any
threads that had been waiting for the process to terminate. The process
object is deleted when the last handle to the process is closed.
4… The states of all threads of the process become signaled, satisfying
any threads that had been waiting for the threads to terminate.
5… The termination status of the process changes from STILL_ACTIVE to the
exit value of the process.
Terminating a process does not cause child processes to be terminated.
DLLs attached to the process are not notified that the process is
terminating.
Terminating a process does not generate notifications for WH_CBT hook
procedures.
Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT
Workstation, Windows Me, Windows 98, Windows 95.
Server: Included in Windows Server 2003¬ Windows 2000 Server, Windows NT
Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
See Also
Processes and Threads Overview, Process and Thread Functions, ExitProcess,
OpenProcess, GetExitCodeProcess, GetExitCodeThread
=end