OpenProcessToken function (processthreadsapi.h)

The OpenProcessToken function opens the access token associated with a process.

Syntax

BOOL OpenProcessToken(
  [in]  HANDLE  ProcessHandle,
  [in]  DWORD   DesiredAccess,
  [out] PHANDLE TokenHandle
);

Parameters

[in] ProcessHandle

A handle to the process whose access token is opened. The process must have the PROCESS_QUERY_LIMITED_INFORMATION access permission. See Process Security and Access Rights for more info.

[in] DesiredAccess

Specifies an access mask that specifies the requested types of access to the access token. These requested access types are compared with the discretionary access control list (DACL) of the token to determine which accesses are granted or denied.

For a list of access rights for access tokens, see Access Rights for Access-Token Objects.

[out] TokenHandle

A pointer to a handle that identifies the newly opened access token when the function returns.

Return value

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

To get a handle to an elevated process from within a non-elevated process, both processes must be started from the same account.

If the process being checked was started by a different account, the checking process needs to have the SE_DEBUG_NAME privilege enabled. See Privilege Constants (Authorization) for more info.

To close the access token handle returned through the TokenHandle parameter, call CloseHandle.

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header processthreadsapi.h (include Windows.h)
Library Advapi32.lib
DLL Advapi32.dll

See also

Access Control

Basic Access Control Functions

AccessCheck

AdjustTokenGroups

AdjustTokenPrivileges

CloseHandle

GetCurrentProcessToken

GetCurrentThreadEffectiveToken

GetCurrentThreadToken

GetTokenInformation

OpenThreadToken

SetTokenInformation