C#FileStream.BeginRead抛出Win32错误1784 - 提供的用户缓冲区对请求的操作无效

时间:2018-03-11 20:53:30

标签: c# winapi bluetooth hid

上下文: 我正在尝试为C#创建一个Joy-Con库,其最终目标是让Unity社区可以访问原型Switch游戏。

详情: Joy-Cons使用HID接口。在C#中使用windows hid.dll绑定成功枚举HID设备后,我获得了一个有效的设备路径,然后尝试使FileStream对象与Joy-Con进行通信。其他项目使用的Joy-Con报告长度为49个字节。我成功创建了FileStream,但在调用stream.BeginRead();时出现错误“提供的用户缓冲区对请求的操作无效”。您应该知道的唯一其他上下文是HIDImports类提供WinHIDApi的接口。

问题代码

要点:https://gist.github.com/gjh33/3e05c2480a23755a61c67347ac72061b

    private void getFileStream()
    {
        SafeFileHandle deviceHandle = HIDImports.CreateFile(joycon.devicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero);

        // Get the attributes
        // See SimpleJoy search function for better explanations of this
        HIDImports.HIDD_ATTRIBUTES deviceAtts = new HIDImports.HIDD_ATTRIBUTES();
        deviceAtts.Size = Marshal.SizeOf(deviceAtts);

        if (HIDImports.HidD_GetAttributes(deviceHandle.DangerousGetHandle(), ref deviceAtts))
        {
            if (deviceAtts.VendorID == Joycon.VENDOR_ID && (deviceAtts.ProductID == Joycon.LEFT_PRODUCT_ID || deviceAtts.ProductID == Joycon.RIGHT_PRODUCT_ID))
            {
                stream = new FileStream(deviceHandle, FileAccess.ReadWrite, Joycon.REPORT_LENGTH, true);
            }
            else
            {
                throw new Exception("Attempted to communicate with a non joycon device");
            }
        }
        else
        {
            throw new Exception("Something went very wrong. When attemping to open filestream, HidD_GetAttributes failed");
        }
    }

    private void beginAsyncRead()
    {
        if (stream != null && stream.CanRead)
        {
            byte[] buff = new byte[Joycon.REPORT_LENGTH];
            Console.WriteLine("AttemptingRead");
            stream.BeginRead(buff, 0, Joycon.REPORT_LENGTH, new AsyncCallback(onRawReportReceived), buff);
        }
    }

0 个答案:

没有答案
相关问题