Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/Device.Net/Windows/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cupsos I can see what this is about. I see the documentation from Microsoft.

Unfortunately, this is the wrong place to put this code. This service must remain dumb with no logic. We will need to pass the fileFlag in as part of the CreateConnection() method.

{
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
Expand All @@ -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

Expand Down
11 changes: 3 additions & 8 deletions src/Hid.Net/Windows/WindowsHidApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down