From ba85aa32e551ea2817940ae1cd656841e71773c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Thu, 27 Jun 2024 18:36:28 +0200 Subject: [PATCH] Added ConvertNtStatusToWin32Error --- ...Utilities.DeviceManagement.sln.DotSettings | 1 + src/Drivers/DriverStore.cs | 14 +++++---- src/NativeMethods.txt | 2 ++ src/Util/NtStatusUtil.cs | 30 +++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/Util/NtStatusUtil.cs diff --git a/Nefarius.Utilities.DeviceManagement.sln.DotSettings b/Nefarius.Utilities.DeviceManagement.sln.DotSettings index d376678..d23ed84 100644 --- a/Nefarius.Utilities.DeviceManagement.sln.DotSettings +++ b/Nefarius.Utilities.DeviceManagement.sln.DotSettings @@ -28,6 +28,7 @@ True True True + True True True True diff --git a/src/Drivers/DriverStore.cs b/src/Drivers/DriverStore.cs index d6b4b14..6a5ef2a 100644 --- a/src/Drivers/DriverStore.cs +++ b/src/Drivers/DriverStore.cs @@ -29,8 +29,13 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; +using System.Threading; +using Windows.Win32; using Windows.Win32.Devices.Properties; +using Windows.Win32.Foundation; + +using Nefarius.Utilities.DeviceManagement.Util; namespace Nefarius.Utilities.DeviceManagement.Drivers; @@ -206,6 +211,7 @@ internal enum DriverStoreCopyFlag : uint /// /// Driver Store enumeration and manipulation utility. /// +[SuppressMessage("ReSharper", "UnusedMember.Global")] public static class DriverStore { private static readonly string WinDir = Environment.GetEnvironmentVariable("WINDIR"); @@ -239,7 +245,7 @@ public static IEnumerable ExistingDrivers if ((ntStatus & 0x80000000) != 0) { - throw new Win32Exception($"DriverStoreOfflineEnumDriverPackage: ntStatus=0x{ntStatus:x8}"); + throw new Win32Exception(NtStatusUtil.ConvertNtStatusToWin32Error(ntStatus)); } return existingDrivers; @@ -252,13 +258,11 @@ public static IEnumerable ExistingDrivers /// The absolute package path to remove. public static void RemoveDriver(string driverStoreFileName) { - uint ntStatus; - - ntStatus = DriverStoreNative.DriverStoreOfflineDeleteDriverPackage(driverStoreFileName, 0, IntPtr.Zero, + uint ntStatus = DriverStoreNative.DriverStoreOfflineDeleteDriverPackage(driverStoreFileName, 0, IntPtr.Zero, WinDir, Path.GetPathRoot(WinDir)); if ((ntStatus & 0x80000000) != 0) { - throw new Win32Exception(Marshal.GetLastWin32Error()); + throw new Win32Exception(NtStatusUtil.ConvertNtStatusToWin32Error(ntStatus)); } } } diff --git a/src/NativeMethods.txt b/src/NativeMethods.txt index 1aaf3bd..af704be 100644 --- a/src/NativeMethods.txt +++ b/src/NativeMethods.txt @@ -53,6 +53,7 @@ GUID_DEVINTERFACE_USB_HUB GetMessage GetModuleHandle GetProcAddress +GetOverlappedResult HDEVINFO HWND HidD_GetHidGuid @@ -78,6 +79,7 @@ SETUP_DI_BUILD_DRIVER_DRIVER_TYPE SPDRP_HARDWAREID SUOI_FORCEDELETE SetupDiOpenClassRegKey +SetLastError TranslateMessage UnregisterDeviceNotification UpdateDriverForPlugAndPlayDevices diff --git a/src/Util/NtStatusUtil.cs b/src/Util/NtStatusUtil.cs new file mode 100644 index 0000000..0de3a16 --- /dev/null +++ b/src/Util/NtStatusUtil.cs @@ -0,0 +1,30 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading; + +using Windows.Win32; +using Windows.Win32.Foundation; + +namespace Nefarius.Utilities.DeviceManagement.Util; + +/// +/// Utility methods for handling NTSTATUS values. +/// +public static class NtStatusUtil +{ + /// + /// Converts an NTSTATUS value to a . + /// + /// https://stackoverflow.com/a/32205631 + /// The NTSTATUS value to convert. + /// The converted Win32 error code. + public static int ConvertNtStatusToWin32Error(uint ntStatus) + { + NativeOverlapped ol = new() { InternalLow = (IntPtr)ntStatus }; + int oldError = Marshal.GetLastWin32Error(); + PInvoke.GetOverlappedResult(null, ol, out uint _, new BOOL(false)); + int result = Marshal.GetLastWin32Error(); + PInvoke.SetLastError((WIN32_ERROR)oldError); + return result; + } +} \ No newline at end of file