Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ConvertNtStatusToWin32Error #58

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// See https://aka.ms/new-console-template for more information

using Nefarius.Utilities.DeviceManagement.Drivers;
using Nefarius.Utilities.DeviceManagement.PnP;

DriverStore.RemoveDriver(@"C:\temp\nonexistent");

const string instanceId = @"USB\VID_054C&PID_0CE6&MI_03\9&DC32669&3&0003";

PnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);
Expand Down
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 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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
1 change: 1 addition & 0 deletions src/Generator/DeviceManagementPropertiesGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
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
42 changes: 42 additions & 0 deletions src/Util/NtStatusUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 unsafe int ConvertNtStatusToWin32Error(uint ntStatus)
{
NativeOverlapped ol = new() { InternalLow = (IntPtr)ntStatus };
int oldError = Marshal.GetLastWin32Error();
// tricking GetOverlappedResult to do the conversion for us :)
GetOverlappedResult(IntPtr.Zero, &ol, out int _, new BOOL(false));
int result = Marshal.GetLastWin32Error();
PInvoke.SetLastError((WIN32_ERROR)oldError);
return result;
}

/// <summary>
/// The CsWin32-generated variant doesn't allow for an empty file handle so we gotta cheat a bit :)
/// </summary>
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern unsafe bool GetOverlappedResult(
IntPtr hFile,
NativeOverlapped* lpOverlapped,
out int lpNumberOfBytesTransferred,
bool bWait
);
}
Loading