How to: Set a Thread Name (Unmanaged) 

This topic applies to:

Visual Studio Edition

Visual Basic

C#

C++

J#

Web Dev

Express Edition

No

No

Native only

No

No

Standard Edition

No

No

Native only

No

No

Pro/Team Edition

No

No

Native only

No

No

To set a thread name in your program, use the SetThreadName function, as shown in the following code example.

Example

//
// Usage: SetThreadName (-1, "MainThread");
//
#define MS_VC_EXCEPTION 0x406D1388

typedef struct tagTHREADNAME_INFO
{
   DWORD dwType; // Must be 0x1000.
   LPCSTR szName; // Pointer to name (in user addr space).
   DWORD dwThreadID; // Thread ID (-1=caller thread).
   DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;

void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName)
{
   THREADNAME_INFO info;
   info.dwType = 0x1000;
   info.szName = szThreadName;
   info.dwThreadID = dwThreadID;
   info.dwFlags = 0;

   __try
   {
      RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
   }
   __except(EXCEPTION_CONTINUE_EXECUTION)
   {
   }
}

See Also

Tasks

How to: Use the Threads Window
How to: Set a Thread Name (Managed)

Concepts

Viewing Data in the Debugger