SetThreadToken function (processthreadsapi.h)

The SetThreadToken function assigns an impersonation token to a thread. The function can also cause a thread to stop using an impersonation token.


BOOL SetThreadToken(
  [in, optional] PHANDLE Thread,
  [in, optional] HANDLE  Token


[in, optional] Thread

A pointer to a handle to the thread to which the function assigns the impersonation token.

If Thread is NULL, the function assigns the impersonation token to the calling thread.

[in, optional] Token

A handle to the impersonation token to assign to the thread. This handle must have been opened with TOKEN_IMPERSONATE access rights. For more information, see Access Rights for Access-Token Objects.

If Token is NULL, the function causes the thread to stop using an impersonation 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.


When using the SetThreadToken function to impersonate, you must have the impersonate privileges and make sure that the SetThreadToken function succeeds before calling the RevertToSelf function.


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

Basic Access Control Functions