Last active
December 28, 2021 01:25
-
-
Save huihut/68a10bf0b4809fbc916b71bf7bbeeec9 to your computer and use it in GitHub Desktop.
WPF Create Dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* [MiniDumpWriteDump function](https://docs.microsoft.com/zh-cn/windows/desktop/api/minidumpapiset/nf-minidumpapiset-minidumpwritedump) | |
* [_MINIDUMP_TYPE Enumeration](https://docs.microsoft.com/zh-cn/windows/desktop/api/minidumpapiset/ne-minidumpapiset-_minidump_type) | |
* [Programmatically Generating a Dump File](http://blogs.microsoft.co.il/sasha/2008/05/28/programmatically-generating-a-dump-file/) | |
* [Create your own crash dumps](https://blogs.msdn.microsoft.com/calvin_hsia/2015/08/31/create-your-own-crash-dumps/) | |
*/ | |
using Microsoft.Win32.SafeHandles; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
using System.Windows; | |
namespace TestWPFDump | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
Application.Current.DispatcherUnhandledException += OnUIException; | |
AppDomain.CurrentDomain.UnhandledException += OnLogicalException; | |
DoSomething(); | |
} | |
// 做某事引发异常 | |
private void DoSomething() | |
{ | |
List<int> list = null; | |
list[1] = 2; | |
} | |
// UI thread exception | |
private void OnUIException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) | |
{ | |
DumpHelper.WriteTinyDumpForThisProcess(@"D:\code\VS\TestWPFDump\Dump\MiniDump_UIException.dmp"); | |
} | |
// Logical thread exception | |
private void OnLogicalException(object sender, UnhandledExceptionEventArgs e) | |
{ | |
DumpHelper.WriteTinyDumpForThisProcess(@"D:\code\VS\TestWPFDump\Dump\MiniDump_LogicalException.dmp"); | |
} | |
} | |
// Dump | |
public static class DumpHelper | |
{ | |
public enum DumpType | |
{ | |
MiniDumpNormal = 0, | |
MiniDumpWithDataSegs = 1, | |
MiniDumpWithFullMemory = 2, | |
MiniDumpWithHandleData = 4, | |
MiniDumpFilterMemory = 8, | |
MiniDumpScanMemory = 16, | |
MiniDumpWithUnloadedModules = 32, | |
MiniDumpWithIndirectlyReferencedMemory = 64, | |
MiniDumpFilterModulePaths = 128, | |
MiniDumpWithProcessThreadData = 256, | |
MiniDumpWithPrivateReadWriteMemory = 512, | |
MiniDumpWithoutOptionalData = 1024, | |
MiniDumpWithFullMemoryInfo = 2048, | |
MiniDumpWithThreadInfo = 4096, | |
MiniDumpWithCodeSegs = 8192, | |
} | |
[DllImportAttribute("dbghelp.dll")] | |
[return: MarshalAsAttribute(UnmanagedType.Bool)] | |
private static extern bool MiniDumpWriteDump( | |
[In] IntPtr hProcess, | |
uint ProcessId, | |
SafeFileHandle hFile, | |
DumpType DumpType, | |
[In] IntPtr ExceptionParam, | |
[In] IntPtr UserStreamParam, | |
[In] IntPtr CallbackParam); | |
public static void WriteTinyDumpForThisProcess(string fileName) | |
{ | |
WriteDumpForThisProcess(fileName, DumpType.MiniDumpNormal); | |
} | |
public static void WriteFullDumpForThisProcess(string fileName) | |
{ | |
WriteDumpForThisProcess(fileName, DumpType.MiniDumpWithFullMemory); | |
} | |
public static void WriteDumpForThisProcess(string fileName, DumpType dumpType) | |
{ | |
WriteDumpForProcess(Process.GetCurrentProcess(), fileName, dumpType); | |
} | |
public static void WriteTinyDumpForProcess(Process process, string fileName) | |
{ | |
WriteDumpForProcess(process, fileName, DumpType.MiniDumpNormal); | |
} | |
public static void WriteFullDumpForProcess(Process process, string fileName) | |
{ | |
WriteDumpForProcess(process, fileName, DumpType.MiniDumpWithFullMemory); | |
} | |
public static void WriteDumpForProcess(Process process, string fileName, DumpType dumpType) | |
{ | |
using (FileStream fs = File.Create(fileName)) | |
{ | |
if (!MiniDumpWriteDump(Process.GetCurrentProcess().Handle, | |
(uint)process.Id, fs.SafeFileHandle, dumpType, | |
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)) | |
{ | |
//throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling MiniDumpWriteDump."); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment