1. 程式人生 > >windows下生成core dump檔案

windows下生成core dump檔案

http://blog.csdn.net/xiarendeniao/article/details/7306282

下面是從pandion裡面摘取的兩個檔案

MiniDumper.h

  1. #ifndef MINIDUMPER_H
  2. #define MINIDUMPER_H
  3. #include <windows.h>
  4. class CMiniDumper  
  5. {  
  6. public:  
  7.     CMiniDumper(bool bPromptUserForMiniDump);  
  8.     ~CMiniDumper(void);  
  9. private
    :  
  10.     staticLONG WINAPI unhandledExceptionHandler(struct_EXCEPTION_POINTERS *pExceptionInfo);  
  11.     void setMiniDumpFileName(void);  
  12.     bool getImpersonationToken(HANDLE* phToken);  
  13.     BOOL enablePrivilege(LPCTSTR pszPriv, HANDLE hToken, TOKEN_PRIVILEGES* ptpOld);  
  14.     BOOL restorePrivilege(
    HANDLE hToken, TOKEN_PRIVILEGES* ptpOld);  
  15.     LONG writeMiniDump(_EXCEPTION_POINTERS *pExceptionInfo );  
  16.     _EXCEPTION_POINTERS *m_pExceptionInfo;  
  17.     TCHAR m_szMiniDumpPath[MAX_PATH];  
  18.     TCHAR m_szAppPath[MAX_PATH];  
  19.     TCHAR m_szAppBaseName[MAX_PATH];  
  20.     bool m_bPromptUserForMiniDump;  
  21.     static CMiniDumper* s_pMiniDumper;  
  22.     static LPCRITICAL_SECTION s_pCriticalSection;  
  23. };  
  24. #endif // MINIDUMPER_H

MiniDumper.cpp

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <strsafe.h>
  7. //#include <tchar.h>
  8. #include <dbghelp.h>
  9. #include "miniDumper.h"
  10. #ifdef UNICODE
  11.     #define _tcssprintf wsprintf
  12.     #define tcsplitpath _wsplitpath
  13. #else
  14.     #define _tcssprintf sprintf
  15.     #define tcsplitpath _splitpath
  16. #endif
  17. constint USER_DATA_BUFFER_SIZE = 4096;  
  18. //-----------------------------------------------------------------------------
  19. // GLOBALS
  20. //-----------------------------------------------------------------------------
  21. CMiniDumper* CMiniDumper::s_pMiniDumper = NULL;  
  22. LPCRITICAL_SECTION CMiniDumper::s_pCriticalSection = NULL;  
  23. // Based on dbghelp.h
  24. typedefBOOL (WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess,  
  25.                                          DWORD dwPid,  
  26.                                          HANDLE hFile,  
  27.                                          MINIDUMP_TYPE DumpType,  
  28.                                          CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,  
  29.                                          CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,  
  30.                                          CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);  
  31. //-----------------------------------------------------------------------------
  32. // Name: CMiniDumper()
  33. // Desc: Constructor
  34. //-----------------------------------------------------------------------------
  35. CMiniDumper::CMiniDumper( bool bPromptUserForMiniDump )  
  36. {  
  37.     // Our CMiniDumper should act alone as a singleton.
  38.     assert( !s_pMiniDumper );  
  39.     s_pMiniDumper = this;  
  40.     m_bPromptUserForMiniDump = bPromptUserForMiniDump;  
  41.     // The SetUnhandledExceptionFilter function enables an application to 
  42.     // supersede the top-level exception handler of each thread and process.
  43.     // After calling this function, if an exception occurs in a process 
  44.     // that is not being debugged, and the exception makes it to the 
  45.     // unhandled exception filter, that filter will call the exception 
  46.     // filter function specified by the lpTopLevelExceptionFilter parameter.
  47.     ::SetUnhandledExceptionFilter( unhandledExceptionHandler );  
  48.     // Since DBGHELP.dll is not inherently thread-safe, making calls into it 
  49.     // from more than one thread simultaneously may yield undefined behavior. 
  50.     // This means that if your application has multiple threads, or is 
  51.     // called by multiple threads in a non-synchronized manner, you need to  
  52.     // make sure that all calls into DBGHELP.dll are isolated via a global
  53.     // critical section.
  54.     s_pCriticalSection = new CRITICAL_SECTION;  
  55.     if( s_pCriticalSection )  
  56.         InitializeCriticalSection( s_pCriticalSection );  
  57. }  
  58. //-----------------------------------------------------------------------------
  59. // Name: ~CMiniDumper()
  60. // Desc: Destructor
  61. //-----------------------------------------------------------------------------
  62. CMiniDumper::~CMiniDumper( void )  
  63. {  
  64.     if( s_pCriticalSection )  
  65.     {  
  66.         DeleteCriticalSection( s_pCriticalSection );  
  67.         delete s_pCriticalSection;  
  68.     }  
  69. }  
  70. //-----------------------------------------------------------------------------
  71. // Name: unhandledExceptionHandler()
  72. // Desc: Call-back filter function for unhandled exceptions
  73. //-----------------------------------------------------------------------------
  74. LONG CMiniDumper::unhandledExceptionHandler( _EXCEPTION_POINTERS *pExceptionInfo )  
  75. {  
  76.     if( !s_pMiniDumper )  
  77.         return EXCEPTION_CONTINUE_SEARCH;  
  78.     return s_pMiniDumper->writeMiniDump( pExceptionInfo );  
  79. }  
  80. //-----------------------------------------------------------------------------
  81. // Name: setMiniDumpFileName()
  82. // Desc: 
  83. //-----------------------------------------------------------------------------
  84. void CMiniDumper::setMiniDumpFileName( void )  
  85. {  
  86.     time_t currentTime;  
  87.     time( ¤tTime );  
  88.     wsprintf( m_szMiniDumpPath,  
  89.                  L"%s%s.%ld.dmp",  
  90.                  m_szAppPath,  
  91.                  m_szAppBase