Can a thread be called CWinThread?

I have a C ++ application (VS2008), and I am running topics such as:

CWinThread *myThread= AfxBeginThread(myOp,0); 

Now all I wanted to do was name this thread, so I can identify it during debugging.

This sounds like a simple task, but I could not find a way to do this. Is this possible, and if so, how?

+7
source share
1 answer

This can be done relatively easily, as described in MSDN: how to set the stream name in native code .

Essentially, you send the debugger a magic exception containing the name and identifier of the stream, and then the debugger monitors and displays the name that you sent to it.

Sample code from an MSDN article is given below:

 // // Usage: SetThreadName (-1, "MainThread"); // #include <windows.h> const DWORD MS_VC_EXCEPTION=0x406D1388; #pragma pack(push,8) 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; #pragma pack(pop) void SetThreadName( DWORD dwThreadID, char* threadName) { THREADNAME_INFO info; info.dwType = 0x1000; info.szName = threadName; info.dwThreadID = dwThreadID; info.dwFlags = 0; __try { RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); } __except(EXCEPTION_EXECUTE_HANDLER) { } } 
+11
source

All Articles