调用“ WinUsb_WritePipe”方法时出现问题。
首先,我首先调用为我提供句柄的“ CreateFile”方法。 然后,我调用“ WinUsb_Initialize”方法来获取WinUsbHandle。
然后,我通过调用“ WinUsb_QueryDeviceInformation”,“ WinUsb_QueryInterfaceSettings”和“ WinUsb_QueryPipe”检索有关USB设备的一些信息。
当我必须调用“ WinUsb_WritePipe”方法时,我的问题就出现了。 在我用C语言制作的第一个库中,它可以正常工作。 但是对于新库,我必须从C切换到C#,并且该方法返回False,而我得到的最后一个错误是INVALID_PARAMETER。
似乎来自重叠的参数。我无法按照规范中的指示将NULL传递给此参数。
我看不到我在做错什么。
我已经尝试过:
我已经尝试将Overlapped参数的类型从IntPtr更改为Int,以将0用作C语言调用。
我试图通过NativeOverlapped结构更改IntPtr。第一次使用NULL结构,但我遇到了同样的问题。
如果我给出了一个初始化的结构,我会尝试调用GetOverlappedResult方法,但它只返回INVALID_PARAMETER,因此问题始终存在。
我尝试使用不安全的方法以C调用方式管理该方法,但问题仍然存在。
您可以在下面找到我的代码:
[DllImport("winusb.dll", SetLastError = true)]
internal static extern Boolean WinUsb_WritePipe(IntPtr InterfaceHandle, Byte PipeId, Byte[] Buffer, UInt32 BufferLength, ref UInt32 LengthTransferred, IntPtr Overlapped);
Byte[] SendBuffer = new Byte[3];
SendBuffer[0] = 0x01;
SendBuffer[1] = 0x0D;
SendBuffer[2] = 0x00;
UInt32 BytesToWrite = Convert.ToUInt32(SendBuffer.Length);
UInt32 BytesWritten=0;
IntPtr Handle = DeviceUSB.GetHandle(); //corresponding to the WinUsbHandle
Byte Pipe = DeviceUSB.GetPipe(DeviceUSB.GetSelectedMode()).PipeOutId; // 0x04
Success = WinUsbApiCalls.WinUsb_WritePipe(Handle, Pipe, SendBuffer, SizeToSend, ref SizeSent, IntPtr.Zero);
我希望将返回值设置为true并将SizeSent设置为3。但是实际返回false,而lastError设置为INVALID_PARAMETER且SizeSent为0。
答案 0 :(得分:0)
我再次尝试用UInt32而不是UInt64声明DllImport。堆栈不平衡也不例外,只有最后一个错误INVALID_PARAMETER会导致错误。我认为参数重叠而不是大小参数类型存在问题。似乎writepipe方法不支持将IntPtr.Zero重叠放置到此参数中。
答案 1 :(得分:-1)
如果您正在使用此API https://docs.microsoft.com/en-us/windows/desktop/api/winusb/nf-winusb-winusb_writepipe,则需要将BufferLength和LengthTransferred参数从 UInt32 更改为 UInt64
os.listdir()