崩溃写DMP文件:
http://blog.csdn.net/lys07962000/article/details/41556439
再重启
#include <stdio.h>
#include <windows.h>
#include <dbghelp.h>
#include "tlhelp32.h"
//#pragma comment(lib, "Dbghelp.lib")
#ifdef _WIN64
#pragma comment(lib, "DbgHelp64.Lib")
#else
#pragma comment(lib, "DbgHelp32.Lib")
#endif
// 关闭进程
HANDLE GetProcessHandle(LPCTSTR pName)//通过进程名获取进程句柄
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)) {
if (!_tcscmp(pe.szExeFile, pName)) {
CloseHandle(hSnapshot);
//return GetProcessHandle(pe.th32ProcessID);
}
}
return NULL;
}
// 自动重启功能
void ReBoot()
{
GetProcessHandle(_T("xxx.exe"));
//清空结构
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb=sizeof(sInfo);
sInfo.dwFlags=STARTF_USESHOWWINDOW;
sInfo.wShowWindow=SW_SHOWNORMAL;
ZeroMemory(&pInfo,sizeof(pInfo));
//创建一个进程
if(!::CreateProcess(_T("xxx.exe"),NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&sInfo,
&pInfo))
{
//输出出错信息
const int nBufSize=512;
TCHAR chBuf[nBufSize];
ZeroMemory(chBuf,nBufSize);
wsprintf(chBuf,_T("CreateProcess failed(%d).\n"),GetLastError());
::OutputDebugString(chBuf);
return;
}
}
LONG WINAPI MyUnhandledFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
LONG ret = EXCEPTION_EXECUTE_HANDLER;
TCHAR szFileName[64];
SYSTEMTIME st;
::GetLocalTime(&st);
wsprintf(szFileName, TEXT("%04d-%02d-%02d-%02d-%02d-%02d-%02d-%02d.dmp"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, rand()%100);
HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = ::GetCurrentThreadId();
ExInfo.ExceptionPointers = lpExceptionInfo;
ExInfo.ClientPointers = false;
// write the dump
BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );
if (bOK)
{
printf("Create Dump File Success!\n");
}
else
{
printf("MiniDumpWriteDump Failed: %d\n", GetLastError());
}
::CloseHandle(hFile);
}
else
{
printf("Create File %s Failed %d\n", szFileName, GetLastError());
}
ReBoot();
return ret;
}