如何判断我运行的Microsoft C ++代码是否使用/ EHa开关编译?

时间:2017-07-29 04:08:58

标签: c++ exception-handling structured-exception

我需要确保我使用的标头是使用/ EHa编译器开关编译的?

我该怎么做?

1 个答案:

答案 0 :(得分:0)

inline bool CodeHasEHaSwitch()
{
    bool dtorCalled = false;

    struct CCheckEHaSwitch
    {
        CCheckEHaSwitch( bool& dtorCalled) : dtorCalled( dtorCalled ) {}
        ~CCheckEHaSwitch() {  dtorCalled = true; }
        bool& dtorCalled;

        static void Win32ExceptionTranslator( unsigned int nExceptionCode,
        EXCEPTION_POINTERS *pExceptionInfo )
        {  throw nExceptionCode; }
    };

    _se_translator_function pfnPrevSeTranslator =
        _set_se_translator( CCheckEHaSwitch::Win32ExceptionTranslator );
    try
    {
        CCheckEHaSwitch test( dtorCalled );

        *((int*)0) = 0;  // generate access violation
    }
    catch (unsigned int)
    {
    }

    _set_se_translator( pfnPrevSeTranslator );

    return dtorCalled;
}