diff --git a/src/Device.Net/Windows/ApiService.cs b/src/Device.Net/Windows/ApiService.cs index a3ecc11a..4d2075ea 100644 --- a/src/Device.Net/Windows/ApiService.cs +++ b/src/Device.Net/Windows/ApiService.cs @@ -3,6 +3,7 @@ using Microsoft.Win32.SafeHandles; using System; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; #pragma warning disable CA1707 // Identifiers should not contain underscores #pragma warning disable CA1021 // Avoid out parameters @@ -14,17 +15,36 @@ namespace Device.Net.Windows internal class ApiService : IApiService { #region Fields -#if NETFRAMEWORK - private const uint FILE_FLAG_OVERLAPPED = 0; -#else private const uint FILE_FLAG_OVERLAPPED = 0x40000000; -#endif + + private readonly uint fileFlag; protected ILogger Logger { get; } #endregion #region Constructor - public ApiService(ILogger logger = null) => Logger = logger ?? NullLogger.Instance; + public ApiService(ILogger logger = null) + { + Logger = logger ?? NullLogger.Instance; + fileFlag = FILE_FLAG_OVERLAPPED; + var runtime = RuntimeInformation.FrameworkDescription; + if (runtime.StartsWith(".NET Framework", StringComparison.InvariantCultureIgnoreCase)) + { + fileFlag = 0; + } + else if (Regex.Match(runtime, @"^\.NET (\d+)") is { Success: true } m) + { + var majorVersion = m.Groups[1].Value; + if (majorVersion != "5") + { + var isSwitchConfigured = AppContext.TryGetSwitch("System.IO.UseNet5CompatFileStream", out var isSwitchEnabled); + if (!isSwitchConfigured || !isSwitchEnabled) + { + fileFlag = 0; + } + } + } + } #endregion #region Implementation @@ -44,7 +64,7 @@ internal class ApiService : IApiService private SafeFileHandle CreateConnection(string deviceId, FileAccessRights desiredAccess, uint shareMode, uint creationDisposition) { Logger.LogInformation("Calling {call} Area: {area} for DeviceId: {deviceId}. Desired Access: {desiredAccess}. Share mode: {shareMode}. Creation Disposition: {creationDisposition}", nameof(APICalls.CreateFile), nameof(ApiService), deviceId, desiredAccess, shareMode, creationDisposition); - return APICalls.CreateFile(deviceId, desiredAccess, shareMode, IntPtr.Zero, creationDisposition, FILE_FLAG_OVERLAPPED, IntPtr.Zero); + return APICalls.CreateFile(deviceId, desiredAccess, shareMode, IntPtr.Zero, creationDisposition, fileFlag, IntPtr.Zero); } #endregion diff --git a/src/Hid.Net/Windows/WindowsHidApiService.cs b/src/Hid.Net/Windows/WindowsHidApiService.cs index 97d454ee..6083322b 100644 --- a/src/Hid.Net/Windows/WindowsHidApiService.cs +++ b/src/Hid.Net/Windows/WindowsHidApiService.cs @@ -137,14 +137,9 @@ public Guid GetHidGuid() //TODO: These are not opening as async. If we do, we get an error. This is probably why cancellation tokens don't work. //https://github.com/MelbourneDeveloper/Device.Net/issues/188 -#if NETFRAMEWORK - private const bool _isAsync = false; -#else - private const bool _isAsync = true; -#endif - public Stream OpenRead(SafeFileHandle readSafeFileHandle, ushort readBufferSize) => new FileStream(readSafeFileHandle, FileAccess.Read, readBufferSize, _isAsync); - - public Stream OpenWrite(SafeFileHandle writeSafeFileHandle, ushort writeBufferSize) => new FileStream(writeSafeFileHandle, FileAccess.ReadWrite, writeBufferSize, _isAsync); + public Stream OpenRead(SafeFileHandle readSafeFileHandle, ushort readBufferSize) => new FileStream(readSafeFileHandle, FileAccess.Read, readBufferSize); + + public Stream OpenWrite(SafeFileHandle writeSafeFileHandle, ushort writeBufferSize) => new FileStream(writeSafeFileHandle, FileAccess.ReadWrite, writeBufferSize); #endregion #region Private Methods