我怎样才能收到转储堆?

时间:2014-05-13 15:14:37

标签: c++ winapi

提示请问如何创建转储堆? 我试试,但没有写入文件

case IDC_BUTTON2:
hFile = CreateFile( TEXT("minidump.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );

  if( hFile!=NULL &&  hFile!=INVALID_HANDLE_VALUE )
  {

  MINIDUMP_EXCEPTION_INFORMATION eInfo;
  eInfo.ThreadId = GetCurrentThreadId();
  eInfo.ExceptionPointers = NULL;
  eInfo.ClientPointers = FALSE;

  MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), hFile, 
      MiniDumpNormal, &eInfo, NULL, NULL);

MessageBox(hDlg,L"MiniDumpCreate",L"Minidump",MB_OK);

CloseHandle( hFile ); }

2 个答案:

答案 0 :(得分:1)

  1. 如果您没有提供任何SEH指针,那么您应该为第5个参数传递NULL。
  2. 您应该从MiniDumpWriteDump检查错误HRESULT,这可能表明错误是什么。
  3. 以下是我msdn blog

    的工作示例代码
    HANDLE hFile = CreateFileA(path,
        GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
    if(hFile == INVALID_HANDLE_VALUE)
        {
        error = GetLastError();
        error = HRESULT_FROM_WIN32(error);
        return error;
        }
    
    // get the process information
    HANDLE hProc = GetCurrentProcess();
    DWORD procID = GetProcessId(hProc);
    
    // generate the crash dump
    BOOL result = MiniDumpWriteDump(hProc, procID, hFile,
        flags, NULL, NULL, NULL);
    
    if(!result)
        {
        error = (HRESULT)GetLastError(); // already an HRESULT
        }
    
    // close the file
    CloseHandle(hFile);
    

答案 1 :(得分:1)

如果您有例外,可以使用以下代码编写minidump。如果您想拥有“堆”信息,则需要使用MiniDumpWithFullMemory

    typedef BOOL (__stdcall *tMDWD)(
        IN HANDLE hProcess,
        IN DWORD ProcessId,
        IN HANDLE hFile,
        IN MINIDUMP_TYPE DumpType,
        IN CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, OPTIONAL
        //IN CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam OPTIONAL
        IN CONST PVOID UserStreamParam OPTIONAL,
        //IN CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam OPTIONAL
        IN CONST PVOID CallbackParam OPTIONAL
        );

    static int WriteMiniDump(LPCTSTR szFN, struct _EXCEPTION_POINTERS *ep, MINIDUMP_TYPE miniDumpType = MiniDumpWithFullMemory)
    {
      int ret = EXCEPTION_EXECUTE_HANDLER;
      HANDLE hFile;
      static tMDWD pMDWD = NULL;

      if (pMDWD == NULL)
      {
        HMODULE hdbghelpmod = LoadLibrary(_T("dbghelp.dll"));
        if (hdbghelpmod == NULL)
          return ret;
        pMDWD = (tMDWD) GetProcAddress(hdbghelpmod, "MiniDumpWriteDump");
      }
      if (pMDWD == NULL)
        return ret;

      hFile = CreateFile(szFN,
        GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      if (hFile != INVALID_HANDLE_VALUE)
      {
        MINIDUMP_EXCEPTION_INFORMATION stMDEI;
        stMDEI.ThreadId = GetCurrentThreadId();
        stMDEI.ExceptionPointers = ep;
        stMDEI.ClientPointers = TRUE;
        // try to create an miniDump:
        if (pMDWD(
          GetCurrentProcess(),
          GetCurrentProcessId(),
          hFile,
          miniDumpType,
          &stMDEI,
          NULL,
          NULL
          ) == FALSE)
        {
          printf("Minidump failed! 0x8.8X\n", GetLastError());
          OutputDebugString(_T("Minidump failed!"));
        }
        else
        {  //ret = 0;  // suceeded }
        CloseHandle(hFile);
      }
      return ret;
    }

然后你可以像这样使用它:

__try
{
    fread((char*)0x12, 0x10, 1, (FILE*)0x12);
}
__except (WriteMiniDump(_T("c:\\Temp\\MyDump.dmp"), GetExceptionInformation())
{
    printf("Handled");
}