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;
        }
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • I don't see anything wrong. Lots of stuff missing. – Hans Passant Oct 22 '12 at 17:24
  • What is missing? This same code works perfectly well from the context of an unhandled exception handler, however in another case fails on exception. What else can i add to my original question to make it clearer? – lysergic-acid Oct 22 '12 at 19:20
  • Well, the access violation exception itself seems to be missing in action for starters. Could you include a stack trace? – Maarten Bodewes Oct 25 '12 at 17:58

1 Answers1

0

Marshal.GetExceptionPointers() might return IntPtr.Zero (maybe when the OS sees the exception as handled). If this is the case you can try to put the call of 'MiniDumpWriteDump' somewhere else. I have had the same problem and solved it by putting 'MiniDumpWriteDump' into the event handler 'AppDomain.CurrentDomain.FirstChanceException'.

user1027167
  • 4,320
  • 6
  • 33
  • 40