Change Thread Name in MFC
-
Hi, is it possible to change the Threadname of a Thread that was created with "AfxBeginThread()"? In C# it is quite easy, but i couldn't find out a method for C++/MFC. It would be nice to read the correct threadname in the debuggerwindow. Right now it always tells me: "The Thread 'Win32 Thread' finished with code 0 (0x0).". Thanks, Snow
-
Hi, is it possible to change the Threadname of a Thread that was created with "AfxBeginThread()"? In C# it is quite easy, but i couldn't find out a method for C++/MFC. It would be nice to read the correct threadname in the debuggerwindow. Right now it always tells me: "The Thread 'Win32 Thread' finished with code 0 (0x0).". Thanks, Snow
//- ----------------------------------------------------------------- //- SetThreadName.H Header File //- ----------------------------------------------------------------- #pragma once extern "C" void WINAPI SetCurrentThreadName( LPCTSTR szThreadName ); extern "C" void WINAPI SetThreadName( DWORD dwThreadID, LPCTSTR szThreadName ); //- ----------------------------------------------------------------- //- END SetThreadName.H Header File //- ----------------------------------------------------------------- //- ----------------------------------------------------------------- //- SetThreadName.Cpp Source File //- ----------------------------------------------------------------- #include "stdafx.h" #include "SetThreadName.H" #define MS_VC_STN_EXCEPTION 0x406d1388 #pragma pack( push, PPSetThreadName) #pragma pack( 4 ) typedef struct _SETTHREADNAMEINFO { DWORD dwType; // must be 0x1000 LPCSTR szName; // pointer to name in same address space // thread name should be limited to 9 characters DWORD dwThreadId; // thread Id, -1 = caller thread DWORD dwFlags; // reserved for future use, must be zero } SETTHREADNAMEINFO; #pragma pack( pop, PPSetThreadName ) extern "C" void WINAPI SetCurrentThreadName( LPCTSTR szThreadName ){ DWORD dwThreadId = GetCurrentThreadId(); SetThreadName(dwThreadId, szThreadName); } extern "C" void WINAPI SetThreadName( DWORD dwThreadId, LPCTSTR szThreadName ){ // only change names if debugger is loaded if( IsDebuggerPresent() ){ // thread name should be limited to 9 characters SETTHREADNAMEINFO STNInfo; TCHAR szTempName[10]; ZeroMemory(&STNInfo, sizeof(SETTHREADNAMEINFO)); // force name to length of 9 characters ZeroMemory(szTempName, sizeof(szTempName)); lstrcpyn(szTempName, szThreadName, 9); STNInfo.dwType = 0x1000; STNInfo.szName = szTempName; STNInfo.dwThreadId = dwThreadId; STNInfo.dwFlags = 0; TCHAR szMessage[512]; wsprintf(szMessage, "Setting thread 0x%08lx name to '%s'", dwThreadId, szTempName); OutputDebugString(szMessage); __try { RaiseException( MS_VC_STN_EXCEPTION, 0, sizeof(STNInfo) / sizeof(DWORD), (DWORD *
-
//- ----------------------------------------------------------------- //- SetThreadName.H Header File //- ----------------------------------------------------------------- #pragma once extern "C" void WINAPI SetCurrentThreadName( LPCTSTR szThreadName ); extern "C" void WINAPI SetThreadName( DWORD dwThreadID, LPCTSTR szThreadName ); //- ----------------------------------------------------------------- //- END SetThreadName.H Header File //- ----------------------------------------------------------------- //- ----------------------------------------------------------------- //- SetThreadName.Cpp Source File //- ----------------------------------------------------------------- #include "stdafx.h" #include "SetThreadName.H" #define MS_VC_STN_EXCEPTION 0x406d1388 #pragma pack( push, PPSetThreadName) #pragma pack( 4 ) typedef struct _SETTHREADNAMEINFO { DWORD dwType; // must be 0x1000 LPCSTR szName; // pointer to name in same address space // thread name should be limited to 9 characters DWORD dwThreadId; // thread Id, -1 = caller thread DWORD dwFlags; // reserved for future use, must be zero } SETTHREADNAMEINFO; #pragma pack( pop, PPSetThreadName ) extern "C" void WINAPI SetCurrentThreadName( LPCTSTR szThreadName ){ DWORD dwThreadId = GetCurrentThreadId(); SetThreadName(dwThreadId, szThreadName); } extern "C" void WINAPI SetThreadName( DWORD dwThreadId, LPCTSTR szThreadName ){ // only change names if debugger is loaded if( IsDebuggerPresent() ){ // thread name should be limited to 9 characters SETTHREADNAMEINFO STNInfo; TCHAR szTempName[10]; ZeroMemory(&STNInfo, sizeof(SETTHREADNAMEINFO)); // force name to length of 9 characters ZeroMemory(szTempName, sizeof(szTempName)); lstrcpyn(szTempName, szThreadName, 9); STNInfo.dwType = 0x1000; STNInfo.szName = szTempName; STNInfo.dwThreadId = dwThreadId; STNInfo.dwFlags = 0; TCHAR szMessage[512]; wsprintf(szMessage, "Setting thread 0x%08lx name to '%s'", dwThreadId, szTempName); OutputDebugString(szMessage); __try { RaiseException( MS_VC_STN_EXCEPTION, 0, sizeof(STNInfo) / sizeof(DWORD), (DWORD *
Oh yeah, and go here.... http://www.codeproject.com/threads/Name_threads_in_debugger.asp[^] I should have checked first... :doh: -- modified at 12:33 Tuesday 29th November, 2005 Also check this link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp[^]
-
Oh yeah, and go here.... http://www.codeproject.com/threads/Name_threads_in_debugger.asp[^] I should have checked first... :doh: -- modified at 12:33 Tuesday 29th November, 2005 Also check this link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp[^]
Thanks thats what i wanted ;-)