GetCurrentThread

Header: <windows.h>

Library: Kernel32.lib

DLL: Kernel32.dll

Function

The GetCurrentThread function retrieves a pseudo handle for the current thread.

A pseudo handle is a context-specific handle for the current object of the type. This handle is guaranteed to be valid only within the process or thread that created it. It is not a global handle that refers to the object throughout the system.

Pseudo handles are useful in situations where you need a handle to an object but do not have a handle readily available. For example, you can use the pseudo handle returned by GetCurrentThread to specify the current thread in a call to the SetThreadPriority function.

Syntax

HANDLE GetCurrentThread();

Parameters

This function does not take any parameters.

Return Value

The return value is a pseudo handle to the current thread. This handle is a pseudo handle, not a real handle, and can be used only by the calling process.

Remarks

The pseudo handle returned by GetCurrentThread is a special value that refers to the current thread. It is not a handle that needs to be closed using CloseHandle. The system automatically handles the lifetime of this pseudo handle.

When you need to pass a handle to the current thread to a function that expects a thread handle, and you do not have an open handle to the thread, you can use the pseudo handle returned by GetCurrentThread.

For example, to change the priority of the current thread, you would use:

BOOL SetThreadPriority(
              HANDLE hThread,
              int nPriority
          );

        HANDLE hThread = GetCurrentThread();
        if (SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST)) {
            // Priority set successfully
        }

Requirements

Component Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Target Platform Windows
Header file windows.h
Library Kernel32.lib
DLL Kernel32.dll

See Also