Can I get a "__try"-clause to compile with /EHsc?

1

The Microsoft-approved way of setting a thread name doesn't compile with /EHsc enabled. The compiler tells me

C2712: Cannot use __try in functions that require object unwinding

http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx

//
// Usage: SetThreadName (-1, "MainThread");
//
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( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info );
   }
   __except(EXCEPTION_CONTINUE_EXECUTION)
   {
   }
}

Any idea on how to fix this without changing the compiler settings?

Using Visual Studio 2008 on WinXP

c++
visual-studio
asked on Stack Overflow Feb 28, 2011 by Pieter • edited Feb 28, 2011 by Pieter

1 Answer

1

The usual solution is to separate it into two functions, one calling the other. One sets up the SEH __try/__except block and the other has all the stuff related to C++ exceptions and destructor calls for local variables.

But I don't see any types that need a destructor call.

Maybe it's just a typo (except) vs (__except)?

answered on Stack Overflow Feb 28, 2011 by Ben Voigt

User contributions licensed under CC BY-SA 3.0