MiniDump类笔记

h文件 :

#pragma once
#include <windows.h>
#include <Tchar.h>
#include <stdio.h>
#include <stdlib.h>

class CMiniDump
{
public:
    static BOOL Begin(VOID);                //声明成静态函数是想直接调用而不必new一个实例
    static BOOL End(VOID);
};


cpp文件 :

#include "CMiniDump.hpp"
#include <DbgHelp.h>

typedef BOOL (WINAPI *MINIDUMPWRITEDUMP)(
    HANDLE hProcess, 
    DWORD dwPid, 
    HANDLE hFile, 
    MINIDUMP_TYPE DumpType,
    CONST PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
    CONST PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
    CONST PMINIDUMP_CALLBACK_INFORMATION CallbackParam);

LPTOP_LEVEL_EXCEPTION_FILTER PreviousExceptionFilter = NULL;

LONG WINAPI UnHandledExceptionFilter(struct _EXCEPTION_POINTERS *exceptionInfo)
{
    HMODULE    DllHandle        = NULL;

    // Windows 2000
    DllHandle                = LoadLibrary(_T("DBGHELP.DLL"));

    if (DllHandle)
    {
        MINIDUMPWRITEDUMP Dump = (MINIDUMPWRITEDUMP) GetProcAddress(DllHandle, "MiniDumpWriteDump");
     // GetProcAddress : Processes explicitly linking to a DLL call GetProcAddress to obtain the
address of an exported function in the DLL. You use the returned function
pointer to call the DLL function.
GetProcAddress takes as parameters the DLL module handle (returned by
either LoadLibrary, AfxLoadLibrary, or GetModuleHandle) and
takes either the name of the function you want to call or the function's export ordinal.

Because you are calling the DLL function through a pointer and there is no
compile-time type checking, make sure that the parameters to the function are
correct so that you do not overstep the memory allocated on the stack and cause
an access violation. One way to help provide type-safety is to look at the
function prototypes of the exported functions and create matching typedefs for
the function pointers. For example:
if (Dump) { TCHAR DumpPath[MAX_PATH] = {0,}; SYSTEMTIME SystemTime; GetLocalTime(&SystemTime); _sntprintf(DumpPath, MAX_PATH, _T("%d-%d-%d %d_%d_%d.dmp"), SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond); HANDLE FileHandle = CreateFile( DumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (FileHandle != INVALID_HANDLE_VALUE) { _MINIDUMP_EXCEPTION_INFORMATION MiniDumpExceptionInfo; MiniDumpExceptionInfo.ThreadId = GetCurrentThreadId(); MiniDumpExceptionInfo.ExceptionPointers = exceptionInfo; MiniDumpExceptionInfo.ClientPointers = NULL; BOOL Success = Dump( GetCurrentProcess(), GetCurrentProcessId(), FileHandle, MiniDumpNormal, &MiniDumpExceptionInfo, NULL, NULL); if (Success) { CloseHandle(FileHandle); return EXCEPTION_EXECUTE_HANDLER; } } CloseHandle(FileHandle); } } return EXCEPTION_CONTINUE_SEARCH; } BOOL CMiniDump::Begin(VOID) { SetErrorMode(SEM_FAILCRITICALERRORS); //The system does not display the critical-error-handler message box. //Instead, the system sends the error to the calling process.
   //加这句以后 , 出错时系统不再提示,也就是不出现出错窗口, 而是把错误处理交给程序自己控制 PreviousExceptionFilter = SetUnhandledExceptionFilter(UnHandledExceptionFilter); // 设置callback 函数 return true; } BOOL CMiniDump::End(VOID) { SetUnhandledExceptionFilter(PreviousExceptionFilter); return true; }

 

GetProcAddress的例子 :

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...

HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}

 

转载于:https://www.cnblogs.com/lthxk-yl/p/4275236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值