How to get a program to catch a dump file, MiniDumpWriteDump

Source: Internet
Author: User
Tags win32 windows 5

WinDbg This tool can manually catch dump file, if you want your program smart some, when you encounter the development of the program crash, you want the program to catch their own dump files, and then you can analyze. Recently I happened to encounter such a thing, so I looked for, with the help of Network and forum friends, to complete such a thing. MiniDumpWriteDump This Win32 API can accomplish such a thing. Because I am using C # Now, I have encapsulated it and I am not very familiar with calling those win APIs on managed code. So with some friends to help.

I just paste the code out, run it, but to the current position I have not been caught by this test program analysis of the dump. After analyzing it, now there is no environment on the machine.



using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace MiniDump
... {
classProgram
...{
Static voidMain (string[] args)
...{
Try
...{
stringa= "";
A= NULL;
if(a.tostring ()== "1")
Console.WriteLine ("A is 1");


}
Catch
...{
Minidump.trydump ("c:minidmp.dmp", MiniDump.MiniDumpType.WithFullMemory);
}

Console.readkey ();
}
}



/**//// <summary>
/// This class to use after Windows 5.1 version, if your Windows is very old, the WinDbg inside the DLL copy over, generally no problem
/// DbgHelp.dll is a DLL file with Windows.
/// </summary>
     Public Static classMiniDump
...{
/**//*
* Import DbgHelp.dll
*/
[DllImport ("DbgHelp.dll")]
Private Static externBoolean MiniDumpWriteDump (
IntPtr hprocess,
Int32 ProcessID,
IntPtr FileHandle,
Minidumptype Dumptype,
refMinidumpexceptioninfo Excepinfo,
IntPtr UserInfo,
IntPtr extinfo);

/**//*
* Minidump_exception_information information on this macro
*/
structMinidumpexceptioninfo
...{
public Int32 ThreadId;
public IntPtr exceptionpointers;
public Boolean clientpointers;
}

/**//*
* A function of your own packing
*/
Public StaticBoolean trydump (String dmppath, Minidumptype dmptype)
...{

//use file flow to create a health. dmp File
            using(FileStream stream= NewFileStream (Dmppath, FileMode.Create))
...{
//Get process Information
Process Process=process.getcurrentprocess ();

//Initialization of minidump_exception_information information
Minidumpexceptioninfo Mei= NewMinidumpexceptioninfo ();

Mei. ThreadId=Thread.CurrentThread.ManagedThreadId;
Mei. Exceptionpointers=marshal.getexceptionpointers ();
Mei. Clientpointers= true;


//the Win32 API called here
Boolean Res=MiniDumpWriteDump (
Process. Handle,
Process. Id
Stream. Safefilehandle.dangerousgethandle (),
Dmptype,
refMei
IntPtr.Zero,
IntPtr.Zero);

//Empty Stream
Stream. Flush ();
Stream. Close ();

returnRes;
}
}

Public enumMinidumptype
...{
None= 0x00010000,
Normal= 0x00000000,
Withdatasegs= 0x00000001,
Withfullmemory= 0x00000002,
Withhandledata= 0x00000004,
Filtermemory= 0x00000008,
Scanmemory= 0x00000010,
Withunloadedmodules= 0x00000020,
Withindirectlyreferencedmemory= 0x00000040,
Filtermodulepaths= 0x00000080,
Withprocessthreaddata= 0x00000100,
Withprivatereadwritememory= 0x00000200,
Withoutoptionaldata= 0x00000400,
Withfullmemoryinfo= 0x00000800,
Withthreadinfo= 0x00001000,
Withcodesegs= 0x00002000
}
}


}

Http://www.sula.cn/101.shtml



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.