Generating .NET crash dumps automatically

后端 未结 7 2232
清酒与你
清酒与你 2020-11-28 06:57

I know how to generate Crash Dump files with ADPlus or DebugDiag, but I\'m wondering if there is a way to do this on a customer\'s computer without installing these tools...

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 07:08

    I think if your app is hosed then you might as well take a shot at creating a mini dump file, what's the worst that's going to happen, your app will crash? It's doing that anyway, so you might as well try.
    The code in the MSDN forum mentioned by VoiDed seems pretty solid. I needed a VB.Net version so here's a VB version for anyone that might need it:

    Friend Class MiniDump
        'Code converted from C# code found here: http://social.msdn.microsoft.com/Forums/en-US/clr/thread/6c8d3529-a493-49b9-93d7-07a3a2d715dc
    
        Private Enum MINIDUMP_TYPE
            MiniDumpNormal = 0 
            MiniDumpWithDataSegs = 1
            MiniDumpWithFullMemory = 2
            MiniDumpWithHandleData = 4
            MiniDumpFilterMemory = 8
            MiniDumpScanMemory = 10
            MiniDumpWithUnloadedModules = 20
            MiniDumpWithIndirectlyReferencedMemory = 40
            MiniDumpFilterModulePaths = 80
            MiniDumpWithProcessThreadData = 100
            MiniDumpWithPrivateReadWriteMemory = 200
            MiniDumpWithoutOptionalData = 400
            MiniDumpWithFullMemoryInfo = 800
            MiniDumpWithThreadInfo = 1000
            MiniDumpWithCodeSegs = 2000
        End Enum
    
         _
        Private Shared Function MiniDumpWriteDump( _
             ByVal hProcess As IntPtr, _
             ByVal ProcessId As Int32, _
            ByVal hFile As IntPtr, _
             ByVal DumpType As MINIDUMP_TYPE, _
            ByVal ExceptionParam As IntPtr, _
             ByVal UserStreamParam As IntPtr, _
            ByVal CallackParam As IntPtr) As Boolean
        End Function
    
        Friend Shared Sub MiniDumpToFile(ByVal fileToDump As String)
            Dim fsToDump As IO.FileStream = Nothing
    
            If (IO.File.Exists(fileToDump)) Then
                fsToDump = IO.File.Open(fileToDump, IO.FileMode.Append)
            Else
                fsToDump = IO.File.Create(fileToDump)
            End If
    
            Dim thisProcess As Process = Process.GetCurrentProcess()
            MiniDumpWriteDump(thisProcess.Handle, _
                              thisProcess.Id, _
                              fsToDump.SafeFileHandle.DangerousGetHandle(), _
                              MINIDUMP_TYPE.MiniDumpNormal, _
                              IntPtr.Zero, _
                              IntPtr.Zero, _
                              IntPtr.Zero)
            fsToDump.Close()
        End Sub
    End Class
    

    Just make sure you solidly eception handle the calls to it and you should be relatively safe.

提交回复
热议问题