How to get the name of a Win32 Thread?

11,174

Solution 1

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:

HRESULT hr = GetThreadDescription(ThreadHandle, &data);
if (SUCCEEDED(hr))
{   
    wprintf(“%ls\m”, data);
    LocalFree(data);
}

Here's the documentation:

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

Solution 2

Threads don't actually have names in Win32. The process via RaiseException is just a "Secret Handshake" with the VS Debugger, who actually stores the TID => Name mapping. Windows itself has no notion of a thread "Name".

Solution 3

There is no such WinAPI call since there exists no such thing as thread names.

If you set a thread name then the debugger of your IDE will store it for you, which makes it easier to debug. However the name is never really attached to the thread by a windows API call.

If you run your application without a debugger then setting a thread name has no effect, therefore you can't retrieve the name.

Even if it would be accessible - I wouldn't write code that works only with a debugger attached. Better store the name for yourself together with the handle.

Share:
11,174
Michael Kelley
Author by

Michael Kelley

Updated on June 12, 2022

Comments