Advertisement
Combreal

ListAudioRenderingDevices.cpp

Mar 8th, 2023
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | Source Code | 0 0
  1. #include <windows.h>
  2. #include <MMDeviceAPI.h>
  3. #include <functiondiscoverykeys.h>
  4. #include <atlstr.h>
  5.  
  6. #define EXIT_ON_ERROR(hres) if (FAILED(hres)) { goto Exit; }
  7. #define SAFE_RELEASE(punk) if ((punk) != NULL) { (punk)->Release(); (punk) = NULL; }
  8.  
  9. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  10. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  11.  
  12. // This function enumerates all active (plugged in) audio rendering endpoint devices. It prints the friendly name and endpoint ID string of each endpoint device.
  13. //In short :
  14. //Create an IMMDeviceEnumerator
  15. //Call EnumAudioEndpoints specifying render to enumerate into an IMMDeviceCollection
  16. //Obtain individual IMMDevice instances from IMMDeviceCollection
  17. //Device name and description are queried from IMMDevice using OpenPropertyStore
  18.  
  19. void PrintEndpointNames()
  20. {
  21.     HRESULT hr = S_OK;
  22.     IMMDeviceEnumerator *pEnumerator = NULL;
  23.     IMMDeviceCollection *pCollection = NULL;
  24.     IMMDevice *pEndpoint = NULL;
  25.     IPropertyStore *pProps = NULL;
  26.     LPWSTR pwszID = NULL;
  27.  
  28.     hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
  29.     EXIT_ON_ERROR(hr)
  30.  
  31.     hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pCollection);
  32.     EXIT_ON_ERROR(hr)
  33.  
  34.     UINT  count;
  35.     hr = pCollection->GetCount(&count);
  36.     EXIT_ON_ERROR(hr)
  37.  
  38.     if (count == 0)
  39.     {
  40.         printf("No endpoints found.\n");
  41.     }
  42.  
  43.    
  44.     for (ULONG i = 0; i < count; i++) {
  45.         hr = pCollection->Item(i, &pEndpoint);
  46.         EXIT_ON_ERROR(hr)
  47.  
  48.         hr = pEndpoint->GetId(&pwszID);
  49.         EXIT_ON_ERROR(hr)
  50.  
  51.         hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
  52.         EXIT_ON_ERROR(hr)
  53.  
  54.         PROPVARIANT varName;
  55.         PropVariantInit(&varName);
  56.  
  57.         hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
  58.         EXIT_ON_ERROR(hr)
  59.  
  60.         printf("Endpoint %d: \"%S\" (%S)\n", i, varName.pwszVal, pwszID);
  61.  
  62.         CoTaskMemFree(pwszID);
  63.         pwszID = NULL;
  64.         PropVariantClear(&varName);
  65.         SAFE_RELEASE(pProps)
  66.         SAFE_RELEASE(pEndpoint)
  67.     }
  68.  
  69.     SAFE_RELEASE(pEnumerator)
  70.     SAFE_RELEASE(pCollection)
  71.     return;
  72.  
  73. Exit:
  74.     printf("Error!\n");
  75.     CoTaskMemFree(pwszID);
  76.     SAFE_RELEASE(pEnumerator)
  77.     SAFE_RELEASE(pCollection)
  78.     SAFE_RELEASE(pEndpoint)
  79.     SAFE_RELEASE(pProps)
  80. }
  81.  
  82. int main(void) {
  83.     HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); //Mandatory setup,  use COINIT_APARTMENTTHREADED for GUI app
  84.     PrintEndpointNames();
  85.     system("pause");
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement