Walking the callstack

REF : http://www.codeproject.com/Articles/11132/Walking-the-callstack


使用:

class MiStatckWaler : public StackWalker
{
public:
    MiStatckWaler()
    {
        TCHAR szFile[MAX_PATH] = {0};
        _stprintf_s(szFile, _T("Log\\StackWalk_%x"), ::GetCurrentProcessId());
        m_hFile = ::CreateFile(szFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    }


    virtual ~MiStatckWaler()
    {
        if(m_hFile != NULL)
        {
            CloseHandle(m_hFile);
            m_hFile = NULL;
        }
    }


    virtual void OnOutput(LPCSTR szText)
    {
        if(m_hFile != NULL && szText != NULL)
        {
            DWORD dwTrans = 0;
            ::WriteFile(m_hFile, szText, strlen(szText), &dwTrans, NULL);
        }
        StackWalker::OnOutput(szText);
    }


    static LONG WINAPI MiExceptionFilter(
        __in struct _EXCEPTION_POINTERS *ExceptionInfo
        )
    {
        MiStatckWaler oStatckWalker;
        oStatckWalker.ShowCallstack(::GetCurrentThread(), ExceptionInfo->ContextRecord);
        return EXCEPTION_EXECUTE_HANDLER;
    }


protected:
    HANDLE m_hFile;
};


程序入口处:

::SetUnhandledExceptionFilter(MiStatckWaler::MiExceptionFilter);



要求

1.编译release版本程序时候,Project Property->C/C++->Debug Information Format : Pragram Database(/Zi)

2. Project Property->Linker->Debugging->Generate Debug Info : YES(/DEBUG)

3.把生成的pdb和exe放在一个目录发布。


你可能感兴趣的:(exception,File,null,database,winapi,Pointers)