From 6f9ec4d2a4051c556287c7769775c6d39c216001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Mon, 3 Oct 2022 19:20:01 +0200 Subject: [PATCH] Migrated more types --- Nefarius.Peripherals.SerialPort/SerialPort.cs | 13 +++++++------ .../Win32PInvoke/Win32Com.cs | 3 --- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Nefarius.Peripherals.SerialPort/SerialPort.cs b/Nefarius.Peripherals.SerialPort/SerialPort.cs index 36f23d3..cfddb9a 100644 --- a/Nefarius.Peripherals.SerialPort/SerialPort.cs +++ b/Nefarius.Peripherals.SerialPort/SerialPort.cs @@ -43,7 +43,7 @@ public class SerialPort : IDisposable if (_hPort.IsInvalid) { - if (Marshal.GetLastWin32Error() == Win32Com.ERROR_ACCESS_DENIED) return false; + if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_ACCESS_DENIED) return false; throw new CommPortException("Port Open Failure"); } @@ -210,7 +210,8 @@ public class SerialPort : IDisposable } else { - if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure"); + if (Marshal.GetLastWin32Error() != (int)WIN32_ERROR.ERROR_IO_PENDING) + ThrowException("Unexpected failure"); } } } @@ -263,7 +264,7 @@ public class SerialPort : IDisposable } else { - if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure"); + if (Marshal.GetLastWin32Error() != (int)WIN32_ERROR.ERROR_IO_PENDING) ThrowException("Unexpected failure"); } } @@ -406,7 +407,7 @@ public class SerialPort : IDisposable Marshal.WriteInt32(uMask, 0); if (!Win32Com.WaitCommEvent(_hPort.DangerousGetHandle(), uMask, unmanagedOv)) { - if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) + if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING) sg.WaitOne(); else throw new CommPortException("IO Error [002]"); @@ -425,7 +426,7 @@ public class SerialPort : IDisposable if (((uint)errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Overflow,"); if (((uint)errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,"); if (((uint)errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,"); - s.Length = s.Length - 1; + s.Length -= 1; throw new CommPortException(s.ToString()); } @@ -439,7 +440,7 @@ public class SerialPort : IDisposable { if (!Win32Com.ReadFile(_hPort.DangerousGetHandle(), buf, 1, out gotbytes, unmanagedOv)) { - if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) + if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING) { Win32Com.CancelIo(_hPort.DangerousGetHandle()); gotbytes = 0; diff --git a/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs b/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs index 4c5f52f..a8ee677 100644 --- a/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs +++ b/Nefarius.Peripherals.SerialPort/Win32PInvoke/Win32Com.cs @@ -5,9 +5,6 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke { internal class Win32Com { - //Constants for errors: - internal const UInt32 ERROR_ACCESS_DENIED = 5; - internal const UInt32 ERROR_IO_PENDING = 997; [DllImport("kernel32.dll")] internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags);