WriteFile返回错误代码87

时间:2012-02-07 21:43:15

标签: c# usb hid

我正在编写一个正在写入HID设备的程序,并且在WriteFile函数上收到错误87,无效参数。我从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编程对我来说是新的。

1 个答案:

答案 0 :(得分:7)

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

删除FILE_FLAG_OVERLAPPED选项。