title: retrieving the serial number of a USB keyboard under Windows
body: <p>Many USB devices contain a unique serial number (which is actually a Unicode string) which the host can use in conjunction with the 16-bit vendor and product ID numbers to uniquely identify the device.</p>
  
  <p>I'm trying to figure out how to write a Windows application that would be able to display a list of all USB human interface devices attached to the system.  The list would have one row for each HID, including system keyboards.  There would be columns in the list for the vendor ID, product ID, and serial number.</p>
  
  <p>I can get a list of USB HIDs by calling <code>SetupDiGetClassDevs</code> with the GUID returned by <code>HidD_GetHidGuid</code> and looping through the result by repeatedly calling <code>SetupDiEnumDeviceInterfaces</code>.  I can then call <code>SetupDiGetDeviceInterfaceDetail</code> to get the path to each device, which I can open with <code>CreateFile</code>, so long as I am careful to request neither read nor write permission, which would be denied for a system keyboard.  From there I can get the vendor and product ID numbers by invoking <code>HidD_GetAttributes</code>.</p>
  
  <p>What I'm having trouble figuring out is how to retrieve the serial number string.  When I search for solutions to this problem, I find a lot of information about how to get serial numbers for USB mass storage devices, but nothing that looks like it might apply to any other type of USB device.  I would be happy to discover either a generic method or a HID-specific method of retrieving the serial number string.</p>
  
  <p>I have a feeling that the Win32 port of <code>libusb</code> could manage this without too much trouble, but unfortunately I need a solution that depends only on libraries that come with Windows, such as the <code>setupapi</code> and <code>hid</code> DLLs that contain the functions mentioned above.</p>
  
  <p>Any suggestions would be very much appreciated!</p>