title: Getting Vendor ID and Product ID from Winusb
body: <p>I am trying to get the Vendor and Product IDs from a Winusb device and am getting accessviolationexception when trying to use the winusb_GetDesicriptor() and cannot figure out why I am getting that exception.</p>
  
  <p>Here are my declarations:</p>
  
  <pre><code>    internal devInfo myDevInfo = new devInfo();
      USB_INTERFACE_DESCRIPTOR ifaceDescriptor;
      USB_DEVICE_DESCRIPTOR deviceDescriptor;
  
  
      internal struct USB_INTERFACE_DESCRIPTOR
      {
          internal Byte bLength;
          internal Byte bDescriptorType;
          internal Byte bInterfaceNumber;
          internal Byte bAlternateSetting;
          internal Byte bNumEndpoints;
          internal Byte bInterfaceClass;
          internal Byte bInterfaceSubClass;
          internal Byte bInterfaceProtocol;
          internal Byte iInterface;
      }
  
  
      [StructLayout(LayoutKind.Explicit, Size = 18, CharSet = CharSet.Auto)]
      internal struct USB_DEVICE_DESCRIPTOR
      {
          [FieldOffset(0)]internal byte bLength;
          [FieldOffset(1)]internal byte bDescriptorType;
          [FieldOffset(2)]internal ushort bcdUSB;
          [FieldOffset(4)]internal byte bDeviceClass;
          [FieldOffset(5)]internal byte bDeviceSubClass;
          [FieldOffset(6)]internal byte bDeviceProtocol;
          [FieldOffset(7)]internal byte bMaxPacketSize0;
          [FieldOffset(8)]internal ushort idVendor;
          [FieldOffset(10)]internal ushort idProduct;
          [FieldOffset(12)]internal ushort bcdDevice;
          [FieldOffset(14)]internal byte iManufacturer;
          [FieldOffset(15)]internal byte iProduct;
          [FieldOffset(16)]internal byte iSerialNumber;
          [FieldOffset(17)]internal byte bNumConfigurations;
      }
  
      [DllImport("winusb.dll", SetLastError = true)]
      internal static extern Boolean WinUsb_Initialize(SafeFileHandle DeviceHandle, ref IntPtr InterfaceHandle);
  
      [DllImport("winusb.dll", SetLastError = true)]
      internal static extern Boolean WinUsb_QueryInterfaceSettings(IntPtr InterfaceHandle, Byte AlternateInterfaceNumber, ref USB_INTERFACE_DESCRIPTOR UsbAltInterfaceDescriptor);
  
      [DllImport("winusb.dll", SetLastError = true)]
      internal static extern Boolean WinUsb_GetDescriptor(IntPtr InterfaceHandle, byte DescriptorType, byte Index, ushort LanguageID, ref USB_DEVICE_DESCRIPTOR UsbAltDeviceDescriptor, ulong BufferLength, ref long LengthTransferred);
  </code></pre>
  
  <p>And then here is the code I am trying to run:</p>
  
  <pre><code>deviceHandle = CreateFile
                     (devicePathName,
                     (GENERIC_WRITE | GENERIC_READ),
                     FILE_SHARE_READ | FILE_SHARE_WRITE,
                     IntPtr.Zero,
                     OPEN_EXISTING,
                     FILE_FLAG_OVERLAPPED,
                     0);
  
              success = WinUsb_Initialize(deviceHandle, ref myDevInfo.winUsbHandle);
              success = WinUsb_QueryInterfaceSettings(myDevInfo.winUsbHandle, 0, ref ifaceDescriptor);
              success = WinUsb_GetDescriptor(myDevInfo.winUsbHandle,
                             0x01,
                             (byte)deviceIndex,
                             0,
                             ref deviceDescriptor,
                             18,
                             ref lengthTransferred);
  </code></pre>
  
  <p>I am getting through the initialize and getting the values back from queryinterfacesettings, but getting the accessviolation on the GetDescriptor call</p>