Search code examples
c++windowsmultithreadingwinapiunmanaged

How to get the name of a Win32 Thread?


I know of the non-intuitive process to set the name of a thread under Windows (see "How to set name to a Win32 Thread?"). Is there a way to get the name of the thread? I don't see any Windows API that lets me do this (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs.85).aspx).


Solution

  • Beginning with Windows 10, version 1607, you can now get the name of a thread using GetThreadDescription(), assuming SetThreadDescription() was used to set the name of the thread.

    Here's an example:

    PWSTR data;
    HRESULT hr = GetThreadDescription(ThreadHandle, &data);
    if (SUCCEEDED(hr))
    {   
        wprintf(L"%ls\n", data);
        LocalFree(data);
    }
    

    Here's the documentation:

    https://msdn.microsoft.com/en-us/library/windows/desktop/mt774972(v=vs.85).aspx