代码如下
#include <Windows.h>
#include <Dbghelp.h>
#include <excpt.h>
#include <iostream>
#include <thread>
#pragma comment(lib, "Dbghelp.lib")
int test()
{
std::cout << "ni hao" << std::endl;
throw "abc";
return 0;
}
int main()
{
std::cout << "Hello World!\n";
try
{
std::cout << test() << std::endl;
}
catch (const char* sz)
{
std::cout << sz << std::endl;
}
DWORD id = ::GetCurrentThreadId();
//h = ::GetCurrentThread(); //获得的是伪句柄
static bool b = false;
HANDLE h = OpenThread(
THREAD_ALL_ACCESS,
TRUE,
id
); //获得真实句柄
std::thread _th ([=] {
::SuspendThread(h);
CONTEXT ctx = { 0 };
::GetThreadContext(h, &ctx);
MINIDUMP_EXCEPTION_INFORMATION eInfo;
EXCEPTION_POINTERS excpInfo;
excpInfo.ExceptionRecord = NULL;
excpInfo.ContextRecord = &ctx;
eInfo.ThreadId = GetCurrentThreadId(); //把需要的信息添进去
eInfo.ExceptionPointers = &excpInfo;
eInfo.ClientPointers = FALSE;
HANDLE hFile = CreateFile(L"MiniDump.dmp", GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
::MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpNormal,
&eInfo,
NULL,
NULL);
::CloseHandle(hFile);
::ResumeThread(h);
b = true;
});
_th.detach();
while (!b)
::Sleep(200);
return 0;
}