2018 05 21 22 09 [c] 讓程式自動產生mini dump file.

基本上,是利用 SetUnhandledExceptionFilter 去攔截沒有處理的exception.
然後寫入 mini dump file.


#include <Windows.h>
#include <Minidumpapiset.h>
#include <stdio.h>

LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *excpInfo)
{
    DWORD rc;
    HANDLE hFile = CreateFileA("mini.dmp", GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile) {
        MINIDUMP_EXCEPTION_INFORMATION eInfo;
        eInfo.ThreadId = GetCurrentThreadId();
        eInfo.ExceptionPointers = excpInfo;
        eInfo.ClientPointers = FALSE;

        MiniDumpWriteDump((HANDLE)GetCurrentProcess(),
            GetCurrentProcessId(),
            hFile,
            MiniDumpNormal,
            excpInfo ? &eInfo : NULL,
            NULL,
            NULL);

        CloseHandle(hFile);
    }
    return   EXCEPTION_CONTINUE_SEARCH;
}

int main(int argc, const char *argv[])
{
    int i = 0;
    SetUnhandledExceptionFilter(TopLevelExceptionFilter);
    i = argc / i;
    return 0;
}