Beispiel #1
0
        /// <summary>
        /// Try to get the error message for GetLastError result
        /// </summary>
        public static string LastErrorToString(WindowsError error)
        {
            string message = ErrorMethods.FormatMessage(
                messageId: (uint)error,
                source: IntPtr.Zero,
                flags: FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM);


            return(Enum.IsDefined(typeof(WindowsError), error)
                ? $"{error} ({(uint)error}): {message}"
                : $"Error {error}: {message}");
        }
Beispiel #2
0
        /// <summary>
        /// Try to get the string for an HRESULT
        /// </summary>
        public static string HResultToString(HRESULT hr)
        {
            string message;

            if (ErrorMacros.HRESULT_FACILITY(hr) == Facility.WIN32)
            {
                // Win32 Error, extract the code
                message = ErrorMethods.FormatMessage(
                    messageId: (uint)ErrorMacros.HRESULT_CODE(hr),
                    source: IntPtr.Zero,
                    flags: FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM);
            }
            else
            {
                // Hope that we get a rational IErrorInfo
                Exception exception = Marshal.GetExceptionForHR((int)hr);
                message = exception.Message;
            }

            return($"HRESULT {(int)hr:D} [0x{(int)hr:X}]: {message}");
        }