Kbase 17949: How to Call WIN32 API Function: TerminateProcess
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  19/05/1998 |
|
How to Call WIN32 API Function: TerminateProcess
DISCLAIMER:
===========
The code example in this knowledgebase is for informational purposes
only. If you have specific questions about which API calls would be
best suited to your design goals, please consult your Microsoft
documentation.
INTRODUCTION:
=============
The following sample code shows how to call the Win32 API function
called TerminateProcess. This function ends a process and all of its
threads. TerminateProcess() is usually called by a parent process to
terminate one of the processes it creates.
Please note that although this sample does not show it, you should
do a CloseHandle() call on the process handle you got back from
the OpenProcess() call.
Please note that this API call should be used with care. It is
equivalent to a UNIX "kill -9" command and does not give any DLL's
used by the process a chance to shutdown properly.
In this sample program you are prompted to enter a process id (PID).
In a real world program you would normally get the process id (and
process handle) from the CreateProcess() call.
This code has been tested on Windows NT 4.0 Workstation only.
&GLOBAL-DEFINE PROCESS_ALL_ACCESS 2035711
&GLOBAL-DEFINE PROCESS_CREATE_THREAD 2
&GLOBAL-DEFINE PROCESS_DUP_HANDLE 64
&GLOBAL-DEFINE PROCESS_QUERY_INFORMATION 1024
&GLOBAL-DEFINE PROCESS_SET_INFORMATION 512
&GLOBAL-DEFINE PROCESS_TERMINATE 1
&GLOBAL-DEFINE PROCESS_VM_OPERATION 8
&GLOBAL-DEFINE PROCESS_VM_READ 16
&GLOBAL-DEFINE PROCESS_VM_WRITE 32
&GLOBAL-DEFINE SYNCHRONIZE 1048576
DEFINE VARIABLE intResult AS INTEGER NO-UNDO.
DEFINE VARIABLE intProcessHandle AS INTEGER NO-UNDO.
DEFINE VARIABLE intProcessId AS INTEGER NO-UNDO.
PROCEDURE OpenProcess EXTERNAL "KERNEL32.DLL":
DEFINE INPUT PARAMETER intAccess AS LONG.
DEFINE INPUT PARAMETER intInherit AS LONG.
DEFINE INPUT PARAMETER intProcessId AS LONG.
DEFINE RETURN PARAMETER intProcessHandle AS LONG.
END PROCEDURE.
PROCEDURE TerminateProcess EXTERNAL "KERNEL32.DLL":
DEFINE INPUT PARAMETER intProcessID AS LONG.
DEFINE INPUT PARAMETER intExitCode AS LONG.
DEFINE RETURN PARAMETER intResult AS LONG.
END PROCEDURE.
UPDATE intProcessId.
RUN OpenProcess(INPUT {&PROCESS_TERMINATE},
INPUT 0,
INPUT intProcessId,
OUTPUT intProcessHandle).
IF intProcessHandle <> 0 THEN
DO:
RUN TerminateProcess(intProcessHandle, 0, OUTPUT intResult).
IF intResult = 1 THEN
MESSAGE "Process Terminated" VIEW-AS ALERT-BOX.
ELSE
MESSAGE "Termination Failed" VIEW-AS ALERT-BOX.
END.
ELSE
MESSAGE "Unable to Obtain Process Handle" VIEW-AS ALERT-BOX.