Microsoft Docs

OpenProcess

The OpenProcess function opens an existing local process object.

HANDLE WINAPI OpenProcess(
    DWORD dwDesiredAccess,
    BOOL  bInheritHandle,
    DWORD dwProcessId
);
Note: You must have appropriate access rights. Some rights require the caller to have the SeDebugPrivilege.

Parameters

Parameter Description
dwDesiredAccess Access rights requested for the process object. Combining flags with a logical OR is allowed.
bInheritHandle If non‑zero, the returned handle can be inherited by child processes.
dwProcessId The identifier of the process to be opened.

Common Access Rights

FlagMeaning
PROCESS_TERMINATETerminate the process.
PROCESS_CREATE_THREADCreate a thread in the process.
PROCESS_VM_OPERATIONPerform memory operations (e.g., VirtualAllocEx).
PROCESS_VM_READRead the process's memory.
PROCESS_VM_WRITEWrite to the process's memory.
PROCESS_DUP_HANDLEDuplicate handles in the process.
PROCESS_QUERY_INFORMATIONQuery certain information about the process.
PROCESS_SET_INFORMATIONSet certain information about the process.
SYNCHRONIZEWait on the process handle.

Return Value

If the function succeeds, the return value is a handle to the specified process. If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int main(void)
{
    DWORD pid = 1234; // Replace with target PID
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                 FALSE, pid);
    if (hProcess == NULL) {
        printf("OpenProcess failed. Error: %lu\\n", GetLastError());
        return 1;
    }

    // Example: retrieve the process's executable name
    wchar_t path[MAX_PATH];
    if (GetModuleFileNameExW(hProcess, NULL, path, MAX_PATH))
        wprintf(L"Process path: %s\\n", path);
    else
        printf("GetModuleFileNameEx failed. Error: %lu\\n", GetLastError());

    CloseHandle(hProcess);
    return 0;
}