From 7eee997084080180721bf0f0c5e9ce5779732f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Sat, 13 Jul 2024 15:28:19 +0200 Subject: [PATCH] Migrated all legacy PInvoke --- .../NativeMethods.txt | 1 - Nefarius.Peripherals.SerialPort/QueueStatus.cs | 2 +- .../SerialPort.Properties.cs | 2 -- Nefarius.Peripherals.SerialPort/SerialPort.cs | 14 ++++++-------- .../Win32PInvoke/Win32Com.cs | 17 ----------------- 5 files changed, 7 insertions(+), 29 deletions(-) delete mode 100644 Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs diff --git a/Nefarius.Peripherals.SerialPort/NativeMethods.txt b/Nefarius.Peripherals.SerialPort/NativeMethods.txt index 88e99b5..48e53bd 100644 --- a/Nefarius.Peripherals.SerialPort/NativeMethods.txt +++ b/Nefarius.Peripherals.SerialPort/NativeMethods.txt @@ -5,7 +5,6 @@ FILE_ACCESS_RIGHTS GetCommModemStatus GetCommProperties GetHandleInformation -GetHandleInformation GetOverlappedResult MODEM_STATUS_FLAGS ReadFile diff --git a/Nefarius.Peripherals.SerialPort/QueueStatus.cs b/Nefarius.Peripherals.SerialPort/QueueStatus.cs index 63469fd..661b5dc 100644 --- a/Nefarius.Peripherals.SerialPort/QueueStatus.cs +++ b/Nefarius.Peripherals.SerialPort/QueueStatus.cs @@ -1,5 +1,5 @@ using System.Text; -using Nefarius.Peripherals.SerialPort.Win32PInvoke; + namespace Nefarius.Peripherals.SerialPort { diff --git a/Nefarius.Peripherals.SerialPort/SerialPort.Properties.cs b/Nefarius.Peripherals.SerialPort/SerialPort.Properties.cs index d44f695..55214cf 100644 --- a/Nefarius.Peripherals.SerialPort/SerialPort.Properties.cs +++ b/Nefarius.Peripherals.SerialPort/SerialPort.Properties.cs @@ -1,8 +1,6 @@ using Windows.Win32; using Windows.Win32.Devices.Communication; -using Nefarius.Peripherals.SerialPort.Win32PInvoke; - namespace Nefarius.Peripherals.SerialPort; public partial class SerialPort diff --git a/Nefarius.Peripherals.SerialPort/SerialPort.cs b/Nefarius.Peripherals.SerialPort/SerialPort.cs index a98a6da..75114e8 100644 --- a/Nefarius.Peripherals.SerialPort/SerialPort.cs +++ b/Nefarius.Peripherals.SerialPort/SerialPort.cs @@ -11,8 +11,6 @@ using Windows.Win32.Storage.FileSystem; using Microsoft.Win32.SafeHandles; -using Nefarius.Peripherals.SerialPort.Win32PInvoke; - namespace Nefarius.Peripherals.SerialPort; /// @@ -344,7 +342,7 @@ public partial class SerialPort : IDisposable public void SendImmediate(byte tosend) { CheckOnline(); - if (!Win32Com.TransmitCommChar(_hPort.DangerousGetHandle(), tosend)) + if (!PInvoke.TransmitCommChar(_hPort, new CHAR((sbyte)tosend))) { ThrowException("Transmission failure"); } @@ -357,12 +355,12 @@ public partial class SerialPort : IDisposable protected ModemStatus GetModemStatus() { CheckOnline(); - if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out uint f)) + if (!PInvoke.GetCommModemStatus(_hPort, out MODEM_STATUS_FLAGS f)) { ThrowException("Unexpected failure"); } - return new ModemStatus((MODEM_STATUS_FLAGS)f); + return new ModemStatus(f); } /// @@ -591,12 +589,12 @@ public partial class SerialPort : IDisposable if (i != 0) { - if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out uint f)) + if (!PInvoke.GetCommModemStatus(_hPort, out MODEM_STATUS_FLAGS f)) { throw new CommPortException("IO Error [005]"); } - OnStatusChange(new ModemStatus(i), new ModemStatus((MODEM_STATUS_FLAGS)f)); + OnStatusChange(new ModemStatus(i), new ModemStatus(f)); } } } @@ -620,7 +618,7 @@ public partial class SerialPort : IDisposable if (_online) { - if (Win32Com.GetHandleInformation(_hPort.DangerousGetHandle(), out uint _)) + if (PInvoke.GetHandleInformation(_hPort, out uint _)) { return true; } diff --git a/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs b/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs deleted file mode 100644 index 5603c89..0000000 --- a/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace Nefarius.Peripherals.SerialPort.Win32PInvoke; - -[Obsolete("use CsWin32 instead.")] -internal class Win32Com -{ - [DllImport("kernel32.dll")] - internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags); - - [DllImport("kernel32.dll")] - internal static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar); - - [DllImport("kernel32.dll")] - internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat); -} \ No newline at end of file