diff --git a/App/Program.cs b/App/Program.cs index 0790b53..bce8c2c 100644 --- a/App/Program.cs +++ b/App/Program.cs @@ -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); diff --git a/src/Util/NtStatusUtil.cs b/src/Util/NtStatusUtil.cs index 0de3a16..c5dc44e 100644 --- a/src/Util/NtStatusUtil.cs +++ b/src/Util/NtStatusUtil.cs @@ -18,13 +18,25 @@ public static class NtStatusUtil /// https://stackoverflow.com/a/32205631 /// The NTSTATUS value to convert. /// The converted Win32 error code. - public static int ConvertNtStatusToWin32Error(uint ntStatus) + public static unsafe int ConvertNtStatusToWin32Error(uint ntStatus) { NativeOverlapped ol = new() { InternalLow = (IntPtr)ntStatus }; int oldError = Marshal.GetLastWin32Error(); - PInvoke.GetOverlappedResult(null, ol, out uint _, new BOOL(false)); + // 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; } + + /// + /// The CsWin32-generated variant doesn't allow for an empty file handle so we gotta cheat a bit :) + /// + [DllImport("Kernel32.dll", SetLastError = true)] + private static extern unsafe bool GetOverlappedResult( + IntPtr hFile, + NativeOverlapped* lpOverlapped, + out int lpNumberOfBytesTransferred, + bool bWait + ); } \ No newline at end of file