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);
   }
}

 

posted on 2015-02-05 17:57  齐文宣  阅读(363)  评论(0编辑  收藏  举报

导航