WriteFile返回错误代码87

本文关键字:错误代码 返回 WriteFile | 更新日期: 2023-09-27 18:23:53

我正在编写一个程序,该程序正在写入HID设备,并收到错误87,WriteFile函数上的无效参数。我从Jan Axelson的USB Complete中获得了这些功能,所以我不确定为什么会出现错误。我正在使用这个来查找我的设备:

private void USBInit()
    {
        IntPtr deviceInfoSet;
        Int32 memberIndex = 0;
        SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
        Int32 bufferSize = 0;
        IntPtr detailDataBuffer;
        Boolean success = false;
        deviceFound = false;
        HidD_GetHidGuid(ref hidGuid);           // Get the GUID
        deviceInfoSet = SetupDiGetClassDevs     // Get pointer to a device info set
            (ref hidGuid,
            IntPtr.Zero,
            IntPtr.Zero,
            DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
        do
        {
            MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData);   // Identify Device Interface
            success = SetupDiEnumDeviceInterfaces
                (deviceInfoSet,
                IntPtr.Zero,
                ref hidGuid,
                memberIndex,
                ref MyDeviceInterfaceData);
            success = SetupDiGetDeviceInterfaceDetail           // Request Structure with Device Path Name
                (deviceInfoSet,
                ref MyDeviceInterfaceData,
                IntPtr.Zero,
                0,
                ref bufferSize,
                IntPtr.Zero);
            detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
            Marshal.WriteInt32(detailDataBuffer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);
            success = SetupDiGetDeviceInterfaceDetail
                (deviceInfoSet,
                ref MyDeviceInterfaceData,
                detailDataBuffer,
                bufferSize,
                ref bufferSize,
                IntPtr.Zero);
            IntPtr pDevicePathName = new IntPtr(detailDataBuffer.ToInt32() + 4);
            devicePathName = Marshal.PtrToStringAuto(pDevicePathName);
            Marshal.FreeHGlobal(detailDataBuffer);
            /* Request Communications Handle */
            deviceHandle = CreateFile
                (devicePathName,
                (GENERIC_WRITE | GENERIC_READ),
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                IntPtr.Zero,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                0);
            /* Get Vendor ID and Product ID */
            DeviceAttributes.Size = Marshal.SizeOf(DeviceAttributes);
            success = HidD_GetAttributes(deviceHandle, ref DeviceAttributes);
            // Compare Vendor ID and Product ID.
            if ((DeviceAttributes.VendorID == myVendorID) && (DeviceAttributes.ProductID == myProductID))
            {
                MessageBoxResult res = System.Windows.MessageBox.Show("Device Found", "K-IX", MessageBoxButton.OK);
                deviceFound = true;
                /* Get pointer to capabilities */
                success = HidD_GetPreparsedData(deviceHandle, ref preparsedData);
                /* Get Device Capabilities */
                Int32 result = 0;
                result = HidP_GetCaps(preparsedData, ref Capabilities);
                return;
            }
            else
            {
                // Not my device
                memberIndex++;
                deviceHandle.Close();
                if (memberIndex == 128)
                { break; }
            }
        } while (!deviceFound);
    }

这是我用来尝试发送到设备的代码:

private void SendUSB()
    {
        Int32 numberOfBytesWritten = 0;
        Byte[] outputReportBuffer = null;
        Boolean success;
        Boolean success2;
        // Set size of the Output report buffer.
        Array.Resize(ref outputReportBuffer, Capabilities.InputReportByteLength);
        // Store Report ID in first byte of header.
        outputReportBuffer[0] = 0;
        // Store report data following the Report ID.
        outputReportBuffer[1] = 0x01;
        //outputReportBuffer[2] = 0x02;
       // outputReportBuffer[3] = 0x03;
        // Send Report
        success = WriteFile
            (deviceHandle,
            outputReportBuffer,
            outputReportBuffer.Length,
            ref numberOfBytesWritten,
            IntPtr.Zero);
        if (!success)
        {
            Int32 lastError = Marshal.GetLastWin32Error();
        }
        success2 = HidD_FreePreparsedData(preparsedData);
    }

我已经验证了我的设备报告长度为2,但不确定从这里开始该怎么办,因为USB和HID编程对我来说是新的。

WriteFile返回错误代码87

您在CreateFile()调用中指定了FILE_FLAG_OVERLAPPED,但在WriteFile()函数中为lpOverlapped参数传递了null。这是不合法的。

删除FILE_FLAG_OVERLAPPED选项。