Skip to content

Commit

Permalink
Added ConvertNtStatusToWin32Error
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Jun 27, 2024
1 parent 9d9187d commit ba85aa3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions Nefarius.Utilities.DeviceManagement.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=newdev/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=NONINFRINGEMENT/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nonpresent/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=NTSTATUS/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=NVIDIA/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pcap/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=REMOVEDEVICE/@EntryIndexedValue">True</s:Boolean>
Expand Down
14 changes: 9 additions & 5 deletions src/Drivers/DriverStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -206,6 +211,7 @@ internal enum DriverStoreCopyFlag : uint
/// <summary>
/// Driver Store enumeration and manipulation utility.
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public static class DriverStore
{
private static readonly string WinDir = Environment.GetEnvironmentVariable("WINDIR");
Expand Down Expand Up @@ -239,7 +245,7 @@ public static IEnumerable<string> ExistingDrivers

if ((ntStatus & 0x80000000) != 0)
{
throw new Win32Exception($"DriverStoreOfflineEnumDriverPackage: ntStatus=0x{ntStatus:x8}");
throw new Win32Exception(NtStatusUtil.ConvertNtStatusToWin32Error(ntStatus));
}

return existingDrivers;
Expand All @@ -252,13 +258,11 @@ public static IEnumerable<string> ExistingDrivers
/// <param name="driverStoreFileName">The absolute package path to remove.</param>
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));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ GUID_DEVINTERFACE_USB_HUB
GetMessage
GetModuleHandle
GetProcAddress
GetOverlappedResult
HDEVINFO
HWND
HidD_GetHidGuid
Expand All @@ -78,6 +79,7 @@ SETUP_DI_BUILD_DRIVER_DRIVER_TYPE
SPDRP_HARDWAREID
SUOI_FORCEDELETE
SetupDiOpenClassRegKey
SetLastError
TranslateMessage
UnregisterDeviceNotification
UpdateDriverForPlugAndPlayDevices
Expand Down
30 changes: 30 additions & 0 deletions src/Util/NtStatusUtil.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Utility methods for handling NTSTATUS values.
/// </summary>
public static class NtStatusUtil
{
/// <summary>
/// Converts an NTSTATUS value to a <see cref="WIN32_ERROR" />.
/// </summary>
/// <remarks>https://stackoverflow.com/a/32205631</remarks>
/// <param name="ntStatus">The NTSTATUS value to convert.</param>
/// <returns>The converted Win32 error code.</returns>
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;
}
}

0 comments on commit ba85aa3

Please sign in to comment.