OpenProcess function
Overview
The OpenProcess function opens an existing local process object.
Syntax
#include <windows.h>
HANDLE OpenProcess(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwProcessId
);
Parameters
| Name | Type | Description |
|---|---|---|
| dwDesiredAccess | DWORD | Access rights requested for the process object. For a list of access rights, see Process Security and Access Rights. |
| bInheritHandle | BOOL | TRUE to allow the handle to be inherited by child processes; otherwise FALSE. |
| dwProcessId | DWORD | The identifier of the local process to be opened. |
Return value
If the function succeeds, the return value is an open handle to the specified process.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
The handle returned by OpenProcess can be used by the calling process to manipulate the target process based on the access rights requested.
Do not close a handle twice. Closing an invalid handle results in undefined behavior.
Example
#include <windows.h>
#include <stdio.h>
int main(void) {
DWORD pid = 1234; // Replace with a valid process ID
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess == NULL) {
printf("OpenProcess failed. Error: %lu\\n", GetLastError());
return 1;
}
// Use the handle ...
CloseHandle(hProcess);
return 0;
}