OpenHome.Songcast.MainWindow.EventCurrentDomainUnhandledException C# (CSharp) Method

EventCurrentDomainUnhandledException() private method

private EventCurrentDomainUnhandledException ( object sender, UnhandledExceptionEventArgs e ) : void
sender object
e System.UnhandledExceptionEventArgs
return void
        void EventCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                throw (e.ExceptionObject as Exception);
            }
            catch (SEHException x)

            {
               // Marshal.GetExceptionCode() returns the exception code which was passed
                // by as the first parameter to RaiseException().
                int iExceptionCode = Marshal.GetExceptionCode();
                // Get a pointer to the unmanaged EXCEPTION_POINTERS structure.
                IntPtr pExceptionPointers = Marshal.GetExceptionPointers();

                // Convert the unmanaged EXCEPTION_POINTERS structure into its managed version
                // using Marshal.PtrToStructure().
                EXCEPTION_POINTERS exception_pointers
                  = (EXCEPTION_POINTERS)Marshal.PtrToStructure(pExceptionPointers, typeof(EXCEPTION_POINTERS));
                // Convert the unmanaged EXCEPTION_RECORD structure into its managed version
                // using Marshal.PtrToStructure().
                EXCEPTION_RECORD exception_record
                  = (EXCEPTION_RECORD)Marshal.PtrToStructure
                    (
                      exception_pointers.pExceptionRecord,
                      typeof(EXCEPTION_RECORD)
                    );

                // Check the exception code. If it is one we recognize, we proceed
                // to extract our customized exception information, e.i. a pointer
                // to an MyException structure.
                if (((UInt32)iExceptionCode == 100) && (exception_record.NumberParameters > 0))
                {
                    // The exception_record.ExceptionInformation[] array contains user-defined
                    // data. The first item is a pointer to the unmanaged MySEHExceptionStruct
                    // C++ structure.
                    // We must convert it into a managed version of the MySEHExceptionStruct
                    // structure.
                    MySEHExceptionStruct my_seh_exception_structure
                      = (MySEHExceptionStruct)Marshal.PtrToStructure
                        (
                          (IntPtr)(exception_record.ExceptionInformation[0]),
                          typeof(MySEHExceptionStruct)
                        );
                    // Display info on exception.
                    Console.WriteLine("Exception code : {0:D}.", iExceptionCode);
                    Console.WriteLine("Exception Message 1 : {0:S}", my_seh_exception_structure.m_strMessage1);
                    Console.WriteLine("Exception Message 2 : {0:S}", my_seh_exception_structure.m_strMessage2);
                    // It is the responsibility of the recipient of the exception to free
                    // the memory occupied by the unmanaged MySEHExceptionStruct as well
                    // as members of the unmanaged MySEHExceptionStruct which are references
                    // to other memory. We do this by calling Marshal.DestroyStructure().
                    //
                    // Marshal.DestroyStructure() requires adequate information for the fields
                    // of the target structure to destroy (in the form of MarshalAsAttributes).
                    Marshal.DestroyStructure((IntPtr)(exception_record.ExceptionInformation[0]), typeof(MySEHExceptionStruct));
                    // Finally, free the unmanaged MySEHExceptionStruct structure itself.
                    Marshal.FreeCoTaskMem((IntPtr)(exception_record.ExceptionInformation[0]));
                }
                MessageBox.Show(x.ToString());
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
            }
        }