title: Getting Windows USB caption field from WMI, using C++
body: <p>I want to iterate through the Win32_USBControllerDevice outputting the USB device captions.  </p>
  
  <p>The examples I have found are in C#, Powershell or Python.</p>
  
  <p>Here is a fragment of my code:  </p>
  
  <pre><code>bool can_continue = true;
  HRESULT result;
  //...
          IEnumWbemClassObject* pEnumerator = NULL;
          if (can_continue)
          {
              result = pSvc-&gt;ExecQuery(L"WQL", 
                                       L"Select * From Win32_USBControllerDevice",
                                       WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
                                       NULL,
                                       &amp;pEnumerator);
              if (result != S_OK)
              {
                  status = TF_DEVICE_COM_USB_QUERY_FAILED;
                  error_text = "Query for Win32_USBControllerDevice failed";
                  can_continue = false;
              }
          }
          if (can_continue)
          {
              //  Iterate through the USB devices, looking for the LeCroy o'scope.
              IWbemClassObject *pclsObj = NULL;
              ULONG uReturn = 0;
  
              std::ostringstream name_stream;
              while (pEnumerator)
              {
                  result = pEnumerator-&gt;Next(WBEM_INFINITE, 1, 
                                             &amp;pclsObj, &amp;uReturn);
  
                  if(0 == uReturn)
                  {
                      break;
                  }
  
                  VARIANT vtProp;
                  result = pclsObj-&gt;Get(L"Dependent", 0, &amp;vtProp, 0, 0);
                  const _bstr_t device_name = vtProp.bstrVal;
                  OutputDebugStringA("USB Device found: ");
                  OutputDebugStringA(device_name);
                  OutputDebugStringA("\r\n");
                  VariantClear(&amp;vtProp);
  
                  pclsObj-&gt;Release();
              }
  
          }
  </code></pre>
  
  <p>The <a href="https://msdn.microsoft.com/en-us/library/aa394505(v=vs.85).aspx" rel="nofollow noreferrer">Win32_USBControllerDevice class</a> has the fields:</p>
  
  <pre><code>class Win32_USBControllerDevice : CIM_ControlledBy
  {
    uint32                NegotiatedDataWidth;
    uint64                NegotiatedSpeed;
    uint16                AccessState;
    uint32                NumberOfHardResets;
    uint32                NumberOfSoftResets;
    CIM_USBController REF Antecedent;
    CIM_LogicalDevice REF Dependent;
  };
  </code></pre>
  
  <p>The <a href="https://msdn.microsoft.com/en-us/library/aa387884(v=vs.85).aspx" rel="nofollow noreferrer"><code>CIM_LogicalDevice</code></a> has the fields:  </p>
  
  <pre><code>class CIM_LogicalDevice : CIM_LogicalElement
  {
    string   Caption;
    string   Description;
    datetime InstallDate;
    string   Name;
    string   Status;
    uint16   Availability;
    uint32   ConfigManagerErrorCode;
    boolean  ConfigManagerUserConfig;
    string   CreationClassName;
    string   DeviceID;
    uint16   PowerManagementCapabilities[];
    boolean  ErrorCleared;
    string   ErrorDescription;
    uint32   LastErrorCode;
    string   PNPDeviceID;
    boolean  PowerManagementSupported;
    uint16   StatusInfo;
    string   SystemCreationClassName;
    string   SystemName;
  };
  </code></pre>
  
  <p>The devices in the output of the above fragment (computer name removed):</p>
  
  <pre><code>USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;370B413&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB20\\4&amp;31D14255&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_058F&amp;PID_6364\\058F63646476"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_058F&amp;PID_6364&amp;MI_00\\6&amp;82F8931&amp;0&amp;0000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\\DISK&amp;VEN_GENERIC-&amp;PROD_SD/MMC&amp;REV_1.00\\7&amp;1C4905A4&amp;0&amp;058F63646476&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\\DISK&amp;VEN_GENERIC-&amp;PROD_COMPACT_FLASH&amp;REV_1.01\\7&amp;1C4905A4&amp;0&amp;058F63646476&amp;1"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\\DISK&amp;VEN_GENERIC-&amp;PROD_SM/XD-PICTURE&amp;REV_1.02\\7&amp;1C4905A4&amp;0&amp;058F63646476&amp;2"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\\DISK&amp;VEN_GENERIC-&amp;PROD_MS/MS-PRO&amp;REV_1.03\\7&amp;1C4905A4&amp;0&amp;058F63646476&amp;3"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_058F&amp;PID_6364&amp;MI_01\\6&amp;82F8931&amp;0&amp;0001"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="HID\\VID_058F&amp;PID_6364&amp;MI_01\\7&amp;918E966&amp;0&amp;0000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_272C&amp;PID_7D13\\5&amp;2080E735&amp;0&amp;6"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_04B4&amp;PID_00F1\\01.00.001__USB:2.0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB20\\4&amp;2AB741E&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;270802C8&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;10B856B0&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;7274EB2&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;276DDCD3&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_046D&amp;PID_C063\\5&amp;3A297DC0&amp;0&amp;1"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="HID\\VID_046D&amp;PID_C063\\6&amp;179AEF98&amp;0&amp;0000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_413C&amp;PID_2105\\5&amp;3A297DC0&amp;0&amp;2"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="HID\\VID_413C&amp;PID_2105\\6&amp;F6A3C73&amp;0&amp;0000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\ROOT_HUB\\4&amp;299C35A7&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="USB\\VID_0A5C&amp;PID_21E8\\00190E19540F"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTH\\MS_RFCOMM\\6&amp;36F407EC&amp;1&amp;0"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTH\\MS_BTHBRB\\6&amp;36F407EC&amp;1&amp;1"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001105-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001108-0000-1000-8000-00805F9B34FB}_LOCALMFG&amp;000F\\7&amp;1DE6EB2&amp;0&amp;D08A55436A2B_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000110A-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000110B-0000-1000-8000-00805F9B34FB}_LOCALMFG&amp;000F\\7&amp;1DE6EB2&amp;0&amp;D08A55436A2B_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000110C-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000110C-0000-1000-8000-00805F9B34FB}_LOCALMFG&amp;000F\\7&amp;1DE6EB2&amp;0&amp;D08A55436A2B_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000110E-0000-1000-8000-00805F9B34FB}_LOCALMFG&amp;000F\\7&amp;1DE6EB2&amp;0&amp;D08A55436A2B_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001112-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001115-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000111E-0000-1000-8000-00805F9B34FB}_LOCALMFG&amp;000F\\7&amp;1DE6EB2&amp;0&amp;D08A55436A2B_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{0000111F-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001132-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001800-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{00001801-0000-1000-8000-00805F9B34FB}_VID&amp;00010075_PID&amp;0100\\7&amp;1DE6EB2&amp;0&amp;9C2A83FAE93F_C00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{24DF01A9-3E4F-4C9F-9F66-5AA8AB14F8F4}_LOCALMFG&amp;0000\\7&amp;1DE6EB2&amp;0&amp;000000000000_00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{6E0C8F4C-D928-4852-B6B2-F0F0E0D126FA}_LOCALMFG&amp;0000\\7&amp;1DE6EB2&amp;0&amp;000000000000_00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTHENUM\\{84A1E9B8-12BA-4A9C-8AB0-A43784E0D149}_LOCALMFG&amp;0000\\7&amp;1DE6EB2&amp;0&amp;000000000000_00000000"
  USB Device found: \root\cimv2:Win32_PnPEntity.DeviceID="BTH\\MS_BTHPAN\\6&amp;36F407EC&amp;1&amp;2"
  </code></pre>
  
  <p>does not match my Python Script output:  </p>
  
  <pre><code>USB Root Hub
  USB Root Hub
  USB Composite Device
  USB Mass Storage Device
  Generic- SD/MMC USB Device
  Generic- Compact Flash USB Device
  Generic- SM/xD-Picture USB Device
  Generic- MS/MS-Pro USB Device
  USB Input Device
  HID-compliant device
  IAR Systems I-jet Probe
  Cypress USB StreamerExample
  USB Root Hub
  USB Root Hub
  USB Root Hub
  USB Root Hub
  USB Root Hub
  USB Input Device
  HID-compliant mouse
  USB Input Device
  HID Keyboard Device
  USB Root Hub
  Broadcom BCM20702 Bluetooth 4.0 USB Device
  Bluetooth Device (RFCOMM Protocol TDI) #3
  Microsoft Bluetooth Enumerator
  Bluetooth OPP
  Bluetooth Headset
  Bluetooth AV Source
  Bluetooth AV
  Bluetooth AV Remote Control Target
  Bluetooth AV Remote Control Target
  Bluetooth Remote Control
  Bluetooth Headset AG
  Bluetooth Personal Area Network User (PANU)
  Bluetooth Hands-free
  Bluetooth Hands-free AG
  Bluetooth Message Access
  GAP Protocol
  GATT Protocol
  Bluetooth Hands-free Audio
  Bluetooth L2CAP Interface
  Bluetooth Remote Control
  Bluetooth Device (Personal Area Network) #3
  </code></pre>
  
  <p><strong>Questions:</strong><br>
  1) How do I get "Caption" field, using C++?<br>
  <em>(The Python script is printing the "caption" field.)</em>    </p>
  
  <p>2) What MSDN references are used in determining how to access the "caption" field?  </p>
  
  <p>Environment:<br>
  Windows 7<br>
  Visual Studio 2010<br>
  C++ GUI project using MFC  </p>