How to rename a C++ thread in Visual Studio 2008 debugger?
January 10th, 2010Sometimes it’s useful to rename your threads so you can find them easily in your Visual Studio debugger. In .NET-land it’s easy: you have to set the System.Threading.Thread.Name property of the thread you wish to rename and you’re done. It’s not that simple with native code, though.
For native code, when Visual Studio (and WinDbg) is attached, it listens for a specific exception being raised, with specific information in it.
struct VS_THREADNAME_INFO
{
DWORD type;
LPCSTR name;
DWORD threadId;
DWORD flags;
};
void SetThreadName(DWORD threadId, LPCSTR name)
{
if (IsDebuggerPresent())
{
VS_THREADNAME_INFO tni = { 0x1000, name, threadId, 0 };
__try
{
size_t s = sizeof(VS_THREADNAME_INFO) / sizeof(DWORD);
RaiseException(0x406D1388, 0, s, (DWORD*)&tni);
}
__except (EXCEPTION_CONTINUE_EXECUTION) { /* do nothing */ }
}
}
The code above should be pretty self-explanatory. Source: Setting a Thread Name (Unmanaged)








