-2

prompt please how can i create dump heap? I try, but nothing is written to the file

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 ); }
DEMONICK
  • 1
  • 1

2 Answers2

1
  1. If you aren't providing any SEH pointers, then you should pass NULL for the 5th parameter.
  2. You should check the error HRESULT from MiniDumpWriteDump, that might indicate what the error is.

Here is a working sample code from my 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);
josh poley
  • 7,236
  • 1
  • 25
  • 25
1

You can write a minidump with the following code, if you have a exception. If you want to have "heap" informations, you need to use 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;
    }

Then you can use it like this:

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