Esempio n. 1
0
//		/// <summary>Returns <see langword="false"/> if the user cancelled-out of the dialog. Returns <see langword="true"/> if the user completed the dialog. All other cases result in a thrown <see cref="Win32Exception"/> or <see cref="ExternalException"/> depending on the HRESULT returned from <see cref="IModalWindow.Show(IntPtr)"/>.</summary>
//		public static Boolean ShowDialogOrThrow<TModalWindow>( this IModalWindow dialog, IntPtr parentHWnd )
//			where TModalWindow : IModalWindow
//		{
//			HResult hresult = dialog.Show( parentHWnd );
//			return ShowDialogOrThrow( hresult );
//		}
//
//		// Curious - this gives me runtime errors when calling `Show` on `IModalWindow` directly.
//		public static Boolean ShowDialogOrThrow( this IModalWindow dialog, IntPtr parentHWnd )
//		{
//			HResult hresult = dialog.Show( parentHWnd );
//			return ShowDialogOrThrow( hresult );
//		}

        /// <summary>Returns <see langword="false"/> if the user cancelled-out of the dialog. Returns <see langword="true"/> if the user completed the dialog. All other cases result in a thrown <see cref="Win32Exception"/> or <see cref="ExternalException"/> depending on the HRESULT returned from <see cref="IModalWindow.Show(IntPtr)"/>.</summary>
        public static Boolean ValidateDialogShowHResult(this HResult dialogHResult)
        {
            if (dialogHResult.TryGetWin32ErrorCode(out Win32ErrorCodes win32Code))
            {
                if (win32Code == Win32ErrorCodes.Success)
                {
                    // OK.
                    return(true);
                }
                else if (win32Code == Win32ErrorCodes.ErrorCancelled)
                {
                    // Cancelled
                    return(false);
                }
                else
                {
                    // Other Win32 error:

                    String msg = String.Format(CultureInfo.CurrentCulture, "Unexpected Win32 error code 0x{0:X2} in HRESULT 0x{1:X4} returned from IModalWindow.Show(...).", (Int32)win32Code, (Int32)dialogHResult);
                    throw new Win32Exception(error: (Int32)win32Code, message: msg);
                }
            }
            else if (dialogHResult.IsValidHResult())
            {
                const UInt16 RPC_E_SERVERFAULT = 0x0105;

                if (dialogHResult.GetFacility() == HResultFacility.Rpc && dialogHResult.GetCode() == RPC_E_SERVERFAULT)
                {
                    // This error happens when calling `IModalWindow.Show` instead of using the `Show` method on a different interface, like `IFileOpenDialog.Show`.
                    String msg = String.Format(CultureInfo.CurrentCulture, "Unexpected RPC HRESULT: 0x{0:X4} (RPC Error {1:X2}) returned from IModalWindow.Show(...). This particular RPC error suggests the dialog was accessed via the wrong COM interface.", (Int32)dialogHResult, RPC_E_SERVERFAULT);
                    throw new ExternalException(msg, errorCode: (Int32)dialogHResult);
                }
                else
                {
                    // Fall-through to below:
                }
            }
            else
            {
                // Fall-through to below:
            }

            {
                // Other HRESULT (non-Win32 error):
                // https://stackoverflow.com/questions/11158379/how-can-i-throw-an-exception-with-a-certain-hresult

                String msg = String.Format(CultureInfo.CurrentCulture, "Unexpected HRESULT: 0x{0:X4} returned from IModalWindow.Show(...).", (Int32)dialogHResult);
                throw new ExternalException(msg, errorCode: (Int32)dialogHResult);
            }
        }