OpenThreadToken function (processthreadsapi.h)

The OpenThreadToken function opens the access token associated with a thread.

Syntax

BOOL OpenThreadToken(
  [in]  HANDLE  ThreadHandle,
  [in]  DWORD   DesiredAccess,
  [in]  BOOL    OpenAsSelf,
  [out] PHANDLE TokenHandle
);

Parameters

[in] ThreadHandle

A handle to the thread whose access token is opened.

[in] DesiredAccess

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

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

[in] OpenAsSelf

TRUE if the access check is to be made against the process-level security context.

FALSE if the access check is to be made against the current security context of the thread calling the OpenThreadToken function.

The OpenAsSelf parameter allows the caller of this function to open the access token of a specified thread when the caller is impersonating a token at SecurityIdentification level. Without this parameter, the calling thread cannot open the access token on the specified thread because it is impossible to open executive-level objects by using the SecurityIdentification impersonation level.

[out] TokenHandle

A pointer to a variable that receives the handle to the newly opened access token.

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. If the token has the anonymous impersonation level, the token will not be opened and OpenThreadToken sets ERROR_CANT_OPEN_ANONYMOUS as the error.

Remarks

Tokens with the anonymous impersonation level cannot be opened.

Close the access token handle returned through the TokenHandle parameter by calling 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 Overview

AccessCheck

AdjustTokenGroups

AdjustTokenPrivileges

Basic Access Control Functions

CloseHandle

GetCurrentThreadToken

GetTokenInformation

OpenProcessToken

SECURITY_IMPERSONATION_LEVEL

SetThreadToken

SetTokenInformation