Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Change Thread Name in MFC

Change Thread Name in MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pixinger77
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • P Pixinger77

      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

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      //- ----------------------------------------------------------------- //- 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 *

      B 1 Reply Last reply
      0
      • B Blake Miller

        //- ----------------------------------------------------------------- //- 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 *

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        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[^]

        P 1 Reply Last reply
        0
        • B Blake Miller

          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[^]

          P Offline
          P Offline
          Pixinger77
          wrote on last edited by
          #4

          Thanks thats what i wanted ;-)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups