Search code examples
windowswinapidevicemsdn

How to get Device Instance path from Bus Relations in Win32 API


Is there a good way to get the Device Instance path(e.g: USB\VID_021D&PID_0C51&MI_00\6&192CE49&4&0000) from Bus Relations (e.g:{0.0.1.00000000}.{4234a4c6-3535-49d6-971c-76ce1f22521e}) ?

I realize some terms in Windows have aliases, by "Device Instance path" and "Bus Relations", I mean the two properties found in the device manager: enter image description here

I have tried to get the Device Instance path from the PropertyStore

int getProperty(std::string& sampleID)
{
   HRESULT hr = S_OK;
   IMMDeviceEnumerator* pEnumerator = NULL;


   //CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

   hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
       NULL,
       CLSCTX_INPROC_SERVER,
       __uuidof(IMMDeviceEnumerator),
       (void**)&pEnumerator);

   if (hr != S_OK)
   {
       // error cleanup
       return hr;
   }

   IMMDevice* pEndpoint = NULL;
   std::wstring wSampleID(sampleID.begin(), sampleID.end());
   hr = pEnumerator->GetDevice(wSampleID.c_str(), &pEndpoint);
   if (hr != S_OK || pEndpoint == NULL)
   {
       // error cleanup
       return hr;
   }

   if (!pEndpoint)
   {
       // error cleanup
       return hr;
   }

   IPropertyStore* pProps = NULL;
   hr = pEndpoint->OpenPropertyStore(
       STGM_READ, &pProps);

   PROPVARIANT varName;
   // Initialize container for property value.
   PropVariantInit(&varName);

   hr = pProps->GetValue(
       PKEY_AudioEndpoint_GUID, &varName);
   if (hr != S_OK)
   {
       //error cleanup
       return hr;
   }
   std::cout << varName.pwszVal << std::endl;

   PropVariantClear(&varName);
   SAFE_RELEASE(pProps)
   SAFE_RELEASE(pEndpoint)
   SAFE_RELEASE(pEnumerator);
   return hr;
}

but there does not seem to be a property key to retrieve this piece of information for Audio Endpoint IMMDevice. Any suggestion is appreciated.


Solution

  • An example would be a headphone + microphone device which yields two Bus Relation strings, one for headphone and the other for microphone. My goal is to group the Bus Relations belonging to the same physical device.

    Bus Relations belonging to the same physical device have already been grouped and you can get it through "Bus relations" property of the same physical audio device (parent). You can find that via Device Manager (View -> Devices by connection) like this:

    enter image description here

    Is there a good way to get the Device Instance path(e.g: USB\VID_021D&PID_0C51&MI_00\6&192CE49&4&0000) from Bus Relations (e.g:{0.0.1.00000000}.{4234a4c6-3535-49d6-971c-76ce1f22521e}) ?

    Although it is not clear that what grouped Bus relations will you use for. But you will find that not all (USB) devices have Device Instance path in this format: USB\VID_xxxx&PID_xxxx*. Take Microphone showed in above snapshot as an example, its Device Instance path like below, same with one of bus relations of its parent.

    enter image description here