none
after I use CLRHost to launch a C# App, the API SetUnhandledExceptionFilter cannot work RRS feed

  • Question

  • I had make a c++ launcher to host clr because of the problem of "CoInitializeSecurity", I use ICLRRuntimeHost.ExecuteInDefaultAppDomain to start a managed method which calls App.Main to run the C# app.
    But there rise a new problem, before I use ICLRRuntimeHost to host C# app, SetUnhandledExceptionFilter will hook SEH exceptions, in which I would like to do some thing like writing a MiniDump, now the callback set by SetUnhandledExceptionFilter can't be call any more, instead c# will catch System.Runtime.InteropServices.SEHException by set Application.Current.DispatcherUnhandledException(WPF).

    The app interoperates to a native dll via p/invoke, so it's the best if can it record a MiniDump when crash happened in native dll. Can anyone help me? Thanks. 

    Thursday, September 5, 2013 9:19 AM

Answers

  • Hi Oldareyou,

    I have some code about create dump file when we encounter exception in project for you to try:

    public static class MiniDump

        {

            private static int mDumpError;

            private static MakeDumpArgs mArgs;

            private static MinidumpExceptionInfo mMei;

            public static bool TryDump(String dmpPath, MiniDumpType dmpType)

            {

                mArgs.path = dmpPath;

                mArgs.type = dmpType;

                mMei.ThreadId = GetCurrentThreadId();

                mMei.ExceptionPointers = Marshal.GetExceptionPointers();

                mMei.ClientPointers = false;

                Thread t = new Thread(new ThreadStart(MakeDump));

                t.Start();

                t.Join();

                return mDumpError == 0;

            }

            private static void MakeDump()

            {

                using (FileStream stream = new FileStream(mArgs.path, FileMode.Create))

                {

                    Process process = Process.GetCurrentProcess();

                    IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(mMei));

                    Marshal.StructureToPtr(mMei, mem, false);

                    Boolean res = MiniDumpWriteDump(

                                        process.Handle,

                                        process.Id,

                                        stream.SafeFileHandle.DangerousGetHandle(),

                                        mArgs.type,

                                        mMei.ClientPointers ? mem : IntPtr.Zero,

                                        IntPtr.Zero,

                                        IntPtr.Zero);

                    mDumpError = res ? 0 : Marshal.GetLastWin32Error();

                    Marshal.FreeHGlobal(mem);

                }

            }

            private struct MakeDumpArgs

            {

                public string path;

                public MiniDumpType type;

            }

            [DllImport("DbgHelp.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]

            private static extern Boolean MiniDumpWriteDump(

                                        IntPtr hProcess,

                                        Int32 processId,

                                        IntPtr fileHandle,

                                        MiniDumpType dumpType,

                                        IntPtr excepInfo,

                                        IntPtr userInfo,

                                        IntPtr extInfo);

            [DllImport("kernel32.dll")]

            private static extern int GetCurrentThreadId();

            [StructLayout(LayoutKind.Sequential)]

            struct MinidumpExceptionInfo

            {

                public Int32 ThreadId;

                public IntPtr ExceptionPointers;

                public bool ClientPointers;

            }

        }

        public enum MiniDumpType

        {

            Normal = 0x00000000,

            WithDataSegs = 0x00000001,

            WithFullMemory = 0x00000002,

            WithHandleData = 0x00000004,

            FilterMemory = 0x00000008,

            ScanMemory = 0x00000010,

            WithUnloadedModules = 0x00000020,

            WithIndirectlyReferencedMemory = 0x00000040,

            FilterModulePaths = 0x00000080,

            WithProcessThreadData = 0x00000100,

            WithPrivateReadWriteMemory = 0x00000200,

            WithoutOptionalData = 0x00000400,

            WithFullMemoryInfo = 0x00000800,

            WithThreadInfo = 0x00001000,

            WithCodeSegs = 0x00002000,

            WithoutAuxiliaryState = 0x00004000,

            WithFullAuxiliaryState = 0x00008000

    }

    try

                {

                    int i = 1;

                    int j = 0;

                    int k = i / j;

                }

                catch

                {

                    Utility.MiniDump.TryDump("your dump file path", Utility.MiniDumpType.Normal);

                }

                finally {

                    Console.WriteLine("complete");

                }

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by oldareyou Thursday, September 12, 2013 4:05 AM
    Friday, September 6, 2013 9:19 AM
    Moderator

All replies

  • Hi Oldareyou,

    I have some code about create dump file when we encounter exception in project for you to try:

    public static class MiniDump

        {

            private static int mDumpError;

            private static MakeDumpArgs mArgs;

            private static MinidumpExceptionInfo mMei;

            public static bool TryDump(String dmpPath, MiniDumpType dmpType)

            {

                mArgs.path = dmpPath;

                mArgs.type = dmpType;

                mMei.ThreadId = GetCurrentThreadId();

                mMei.ExceptionPointers = Marshal.GetExceptionPointers();

                mMei.ClientPointers = false;

                Thread t = new Thread(new ThreadStart(MakeDump));

                t.Start();

                t.Join();

                return mDumpError == 0;

            }

            private static void MakeDump()

            {

                using (FileStream stream = new FileStream(mArgs.path, FileMode.Create))

                {

                    Process process = Process.GetCurrentProcess();

                    IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(mMei));

                    Marshal.StructureToPtr(mMei, mem, false);

                    Boolean res = MiniDumpWriteDump(

                                        process.Handle,

                                        process.Id,

                                        stream.SafeFileHandle.DangerousGetHandle(),

                                        mArgs.type,

                                        mMei.ClientPointers ? mem : IntPtr.Zero,

                                        IntPtr.Zero,

                                        IntPtr.Zero);

                    mDumpError = res ? 0 : Marshal.GetLastWin32Error();

                    Marshal.FreeHGlobal(mem);

                }

            }

            private struct MakeDumpArgs

            {

                public string path;

                public MiniDumpType type;

            }

            [DllImport("DbgHelp.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]

            private static extern Boolean MiniDumpWriteDump(

                                        IntPtr hProcess,

                                        Int32 processId,

                                        IntPtr fileHandle,

                                        MiniDumpType dumpType,

                                        IntPtr excepInfo,

                                        IntPtr userInfo,

                                        IntPtr extInfo);

            [DllImport("kernel32.dll")]

            private static extern int GetCurrentThreadId();

            [StructLayout(LayoutKind.Sequential)]

            struct MinidumpExceptionInfo

            {

                public Int32 ThreadId;

                public IntPtr ExceptionPointers;

                public bool ClientPointers;

            }

        }

        public enum MiniDumpType

        {

            Normal = 0x00000000,

            WithDataSegs = 0x00000001,

            WithFullMemory = 0x00000002,

            WithHandleData = 0x00000004,

            FilterMemory = 0x00000008,

            ScanMemory = 0x00000010,

            WithUnloadedModules = 0x00000020,

            WithIndirectlyReferencedMemory = 0x00000040,

            FilterModulePaths = 0x00000080,

            WithProcessThreadData = 0x00000100,

            WithPrivateReadWriteMemory = 0x00000200,

            WithoutOptionalData = 0x00000400,

            WithFullMemoryInfo = 0x00000800,

            WithThreadInfo = 0x00001000,

            WithCodeSegs = 0x00002000,

            WithoutAuxiliaryState = 0x00004000,

            WithFullAuxiliaryState = 0x00008000

    }

    try

                {

                    int i = 1;

                    int j = 0;

                    int k = i / j;

                }

                catch

                {

                    Utility.MiniDump.TryDump("your dump file path", Utility.MiniDumpType.Normal);

                }

                finally {

                    Console.WriteLine("complete");

                }

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by oldareyou Thursday, September 12, 2013 4:05 AM
    Friday, September 6, 2013 9:19 AM
    Moderator
  • YES IT IS!!!!!!!THANKS SO MUCH!!!!!!!!

    Thursday, September 12, 2013 4:05 AM