Problem: The structure UsbSetupPacket only fully works on C#
The following code works as expected in C#:
int iTransferred = 0;
short lowaddr = (short) (0xffff & addr);
short highaddr = (short) (addr >> 0x10);
UsbSetupPacket setupPacket = new UsbSetupPacket(0x40, request, lowaddr, highaddr, (short) count);
usbdev.ControlTransfer(ref setupPacket, dataout, count, out iTransferred);
However, VB.net (VS2005) does not handle Uint16 to Int16 excactly as its C# version:
Dim iTransferred As Integer = 0
Dim lowaddr As Short = CShort((&HFFFF And addr))
Dim highaddr As Short = CShort((addr >> &H10))
Dim setupPacket As New UsbSetupPacket(&H40, request, lowaddr, highaddr, CShort(count))
usbdev.ControlTransfer((setupPacket), dataout, count, iTransferred)
The result on the VB.net side is a overflow crash when the upper or lower portion of the address is 16 bits wide. When it is, C# converts the 16th bit into the sign bit, but vb.net will crash. One possible workaround is to convert the first 15 bits of the value into a signed short integer and then add the sign if the 16th bit was high. However this then leaves the problem of converting the value 0x8000 (which converts to -0 and thus 0).
Solution: the UsbSetupPacket should have a overloaded version that allows you to create one with using unsigned 16 bit integers, or have a version specifically for vb.net.