Migrated all legacy PInvoke

This commit is contained in:
Benjamin Höglinger-Stelzer 2024-07-13 15:28:19 +02:00
parent 086633b8a2
commit 7eee997084
5 changed files with 7 additions and 29 deletions

View File

@ -5,7 +5,6 @@ FILE_ACCESS_RIGHTS
GetCommModemStatus GetCommModemStatus
GetCommProperties GetCommProperties
GetHandleInformation GetHandleInformation
GetHandleInformation
GetOverlappedResult GetOverlappedResult
MODEM_STATUS_FLAGS MODEM_STATUS_FLAGS
ReadFile ReadFile

View File

@ -1,5 +1,5 @@
using System.Text; using System.Text;
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
namespace Nefarius.Peripherals.SerialPort namespace Nefarius.Peripherals.SerialPort
{ {

View File

@ -1,8 +1,6 @@
using Windows.Win32; using Windows.Win32;
using Windows.Win32.Devices.Communication; using Windows.Win32.Devices.Communication;
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
namespace Nefarius.Peripherals.SerialPort; namespace Nefarius.Peripherals.SerialPort;
public partial class SerialPort public partial class SerialPort

View File

@ -11,8 +11,6 @@ using Windows.Win32.Storage.FileSystem;
using Microsoft.Win32.SafeHandles; using Microsoft.Win32.SafeHandles;
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
namespace Nefarius.Peripherals.SerialPort; namespace Nefarius.Peripherals.SerialPort;
/// <summary> /// <summary>
@ -344,7 +342,7 @@ public partial class SerialPort : IDisposable
public void SendImmediate(byte tosend) public void SendImmediate(byte tosend)
{ {
CheckOnline(); CheckOnline();
if (!Win32Com.TransmitCommChar(_hPort.DangerousGetHandle(), tosend)) if (!PInvoke.TransmitCommChar(_hPort, new CHAR((sbyte)tosend)))
{ {
ThrowException("Transmission failure"); ThrowException("Transmission failure");
} }
@ -357,12 +355,12 @@ public partial class SerialPort : IDisposable
protected ModemStatus GetModemStatus() protected ModemStatus GetModemStatus()
{ {
CheckOnline(); CheckOnline();
if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out uint f)) if (!PInvoke.GetCommModemStatus(_hPort, out MODEM_STATUS_FLAGS f))
{ {
ThrowException("Unexpected failure"); ThrowException("Unexpected failure");
} }
return new ModemStatus((MODEM_STATUS_FLAGS)f); return new ModemStatus(f);
} }
/// <summary> /// <summary>
@ -591,12 +589,12 @@ public partial class SerialPort : IDisposable
if (i != 0) 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]"); 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 (_online)
{ {
if (Win32Com.GetHandleInformation(_hPort.DangerousGetHandle(), out uint _)) if (PInvoke.GetHandleInformation(_hPort, out uint _))
{ {
return true; return true;
} }

View File

@ -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);
}