public static void CreateDump(string path, MiniDumpType miniDumpType) { var exceptionInfo = new MiniDumpExceptionInfo { ThreadId = GetCurrentThreadId(), ExceptionPointers = Marshal.GetExceptionPointers(), ClientPointers = false // false because own process }; Process process = Process.GetCurrentProcess(); using (var stream = new FileStream(path, FileMode.Create)) { Debug.Assert(stream.SafeFileHandle != null); // The problem Marshal.GetExceptionPointers can return null on x86 machines due to differences // in low-level exception handling. // Then passing a MiniDumpExceptionInfo structure with a NULL ExceptionPointers members causes an // access violation. So we only pass this structure if we got a valid ExceptionPointers member. // It will probably result that x86 machines will see the instruction pointer to the MiniDumpWriteDump // line and not the exception itself. IntPtr exceptionInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(exceptionInfo)); Marshal.StructureToPtr(exceptionInfo, exceptionInfoPtr, false); try { MiniDumpWriteDump( process.Handle, process.Id, stream.SafeFileHandle.DangerousGetHandle(), miniDumpType, exceptionInfo.ExceptionPointers == IntPtr.Zero ? IntPtr.Zero : exceptionInfoPtr, IntPtr.Zero, IntPtr.Zero); } catch (Exception /*exception*/) { } Marshal.FreeHGlobal(exceptionInfoPtr); } }
public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, SafeFileHandle hFile, MiniDumpType DumpType, ref MiniDumpExceptionInfo ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);
public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, SafeFileHandle hFile, MiniDumpType DumpType, ref MiniDumpExceptionInfo ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);