From be728e1b291fa88418e8dc4ea7141b216cc7cfb6 Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 2 Sep 2019 17:09:01 -0600 Subject: [PATCH] Added extension methods that use FormatMessage to an errors descriptive text. --- PInvoke/Kernel32/ErrHandlingApi.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/PInvoke/Kernel32/ErrHandlingApi.cs b/PInvoke/Kernel32/ErrHandlingApi.cs index b9200284..830cee92 100644 --- a/PInvoke/Kernel32/ErrHandlingApi.cs +++ b/PInvoke/Kernel32/ErrHandlingApi.cs @@ -1308,4 +1308,28 @@ namespace Vanara.PInvoke public SafeExceptionHandlerHandle(IntPtr handle) : base(handle, RemoveVectoredExceptionHandler) { } } } + + public static class WinErrExtensions + { + /// Gets the system message for the provided system error with optional parameters. + /// The error. + /// The arguments. + /// The formatted system message. + public static string FormatMessage(this IErrorProvider err, params object[] args) + { + try { return Kernel32.FormatMessage((uint)err.ToHRESULT(), args); } + catch { return string.Empty; } + } + + /// Gets the system message for the provided system error with optional parameters. + /// The error. + /// A handle to the module that contains the message table to search. + /// The arguments. + /// The formatted system message. + public static string FormatMessage(this IErrorProvider err, HINSTANCE hLib, params object[] args) + { + try { return Kernel32.FormatMessage((uint)err.ToHRESULT(), args, hLib); } + catch { return string.Empty; } + } + } } \ No newline at end of file