Consultor Eletrônico



Kbase 17947: How to Call WIN32 API Function: OpenProcess
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   19/05/1998
How to Call WIN32 API Function: OpenProcess

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 OpenProcess. This function obtains a handle for an existing
process object (i.e. running program). This handle can then be used
to manipulate the running process (i.e. terminate it, increase or
decrease its priority, etc).

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.

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 via
code instead of prompting the user for it.

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 intProcessId AS INTEGER NO-UNDO.
DEFINE VARIABLE intProcessHandle 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.

UPDATE intProcessId. /* User Enters a Process ID (PID) */

RUN OpenProcess(INPUT {&PROCESS_ALL_ACCESS},
INPUT 0,
INPUT intProcessId,
OUTPUT intProcessHandle).

IF intProcessHandle <> 0 THEN
MESSAGE "Process Handle Obtained" VIEW-AS ALERT-BOX.
ELSE
MESSAGE "Unable to Obtain Process Handle" VIEW-AS ALERT-BOX.