Beispiel #1
0
 public int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length)
 {
     using (SafeFileHandle handle = Parent.OpenHandle()) {
         int szRequest = Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST));
         USB_DESCRIPTOR_REQUEST request = new USB_DESCRIPTOR_REQUEST();
         request.ConnectionIndex    = AdapterNumber;
         request.SetupPacket.Value  = (short)((descriptorType << 8) + index);
         request.SetupPacket.Index  = (short)langId;
         request.SetupPacket.Length = (short)length;
         int    nBytes    = length + szRequest;
         Byte[] bigbuffer = new Byte[nBytes];
         if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref request, Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)), bigbuffer, nBytes, out nBytes, IntPtr.Zero))
         {
             if (descriptorType == (Byte)UsbDescriptorType.Device && index == 0 && langId == 0)
             {
                 Byte[] descbytes = DeviceDescriptor.GetBytes();
                 length = Math.Min(descbytes.Length, length);
                 Array.Copy(descbytes, 0, buffer, 0, length);
                 return(length);
             }
             int err = Marshal.GetLastWin32Error();
             if (err != 31)
             {
                 throw new Win32Exception(err);
             }
             return(0);
         }
         nBytes -= szRequest;
         if (nBytes > length)
         {
             nBytes = length;
         }
         if (nBytes < 0)
         {
             return(0);
         }
         if (nBytes > 0)
         {
             Buffer.BlockCopy(bigbuffer, szRequest, buffer, offset, nBytes);
         }
         return(nBytes);
     }
 }