1

I have the basic MiniDumpWriteDump method interop copied off the internet in my C# (3.5) project.

Up till now, i have used this code to register on the UnhandledException event, to take a crash dump before the process shuts down.

In a particular scenario i am facing now, i have set this function to be used in some other case, to take a diagnostic memory dump of the process.

Whenever this function gets called (not from the UnhandledException handler), it throws an AccessViolationException

Here's what the MiniDump code looks like (removed some redundant parts):

using (var fs = new System.IO.FileStream(fileName, 
                                         System.IO.FileMode.Create, 
                                         System.IO.FileAccess.Write, 
                                         System.IO.FileShare.None))
{
    MiniDumpExceptionInformation exp;
    exp.ThreadId = GetCurrentThreadId();
    exp.ClientPointers = false;
    exp.ExceptionPointers = Marshal.GetExceptionPointers();

    bool bRet = MiniDumpWriteDump(
                GetCurrentProcess(),
                GetCurrentProcessId(),
                fs.SafeFileHandle.DangerousGetHandle(),
                (uint)dumpType,
                ref exp,
                IntPtr.Zero,
                IntPtr.Zero);

    return bRet;
}

Native types are defined like so:

//typedef struct _MINIDUMP_EXCEPTION_INFORMATION {
        //    DWORD ThreadId;
        //    PEXCEPTION_POINTERS ExceptionPointers;
        //    BOOL ClientPointers;
        //} MINIDUMP_EXCEPTION_INFORMATION, *PMINIDUMP_EXCEPTION_INFORMATION;
        // Pack=4 is important! So it works also for x64!
        [StructLayout(LayoutKind.Sequential, Pack = 4)]  
        struct MiniDumpExceptionInformation
        {
            public uint ThreadId;
            public IntPtr ExceptionPointers;
            [MarshalAs(UnmanagedType.Bool)]
            public bool ClientPointers;
        }
4

1 回答 1

0

Marshal.GetExceptionPointers() 可能会返回 IntPtr.Zero (可能当操作系统看到异常已处理时)。如果是这种情况,您可以尝试将“MiniDumpWriteDump”的调用放在其他地方。我遇到了同样的问题并通过将“MiniDumpWriteDump”放入事件处理程序“AppDomain.CurrentDomain.FirstChanceException”来解决它。

于 2014-03-26T13:14:11.453 回答