1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| class DumpWriter { public enum MiniDumpType { 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 }
[DllImport("DbgHelp.dll")] private static extern bool MiniDumpWriteDump( IntPtr hProcess, Int32 processId, IntPtr fileHandle, MiniDumpType dumpType, ref MiniDumpExceptionInformation excepInfo, IntPtr userInfo, IntPtr extInfo);
[DllImport("DbgHelp.dll")] private static extern bool MiniDumpWriteDump( IntPtr hProcess, Int32 processId, IntPtr fileHandle, MiniDumpType dumpType, IntPtr excepParam, IntPtr userInfo, IntPtr extInfo);
[StructLayout(LayoutKind.Sequential, Pack = 4)] private struct MiniDumpExceptionInformation { public uint ThreadId; public IntPtr ExceptionPointers; [MarshalAs(UnmanagedType.Bool)] public bool ClientPointers; }
[DllImport("kernel32.dll")] private static extern uint GetCurrentThreadId();
private bool WriteDump(String dmpPath, MiniDumpType dmpType) { using (FileStream stream = new FileStream(dmpPath, FileMode.Create)) { Process process = Process.GetCurrentProcess();
MiniDumpExceptionInformation mei = new MiniDumpExceptionInformation(); mei.ThreadId = GetCurrentThreadId(); mei.ExceptionPointers = Marshal.GetExceptionPointers(); mei.ClientPointers = true;
bool res = false;
if (mei.ExceptionPointers == IntPtr.Zero) { res = MiniDumpWriteDump( process.Handle, process.Id, stream.SafeFileHandle.DangerousGetHandle(), dmpType, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } else { res = MiniDumpWriteDump( process.Handle, process.Id, stream.SafeFileHandle.DangerousGetHandle(), dmpType, ref mei, IntPtr.Zero, IntPtr.Zero); } return res; } }
public DumpWriter() { FilePath = Environment.CurrentDirectory + @"\Dump"; if (!Directory.Exists(FilePath)) Directory.CreateDirectory(FilePath); }
public string FilePath { get; protected set; } public string FileName { get; protected set; } public bool WriteDumpFile(MiniDumpType dmpType) { FileName = string.Format("{0}\\{1}_{2}.dmp", FilePath, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff"), Process.GetCurrentProcess().ProcessName); return WriteDump(FileName, dmpType); } }
|