[ACCEPTED]-How to get the name of a Win32 Thread?-unmanaged

Accepted answer
Score: 18

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

Score: 18

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

Here's 1 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

Score: 12

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

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

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

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

More Related questions