Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <MMDeviceAPI.h>
- #include <functiondiscoverykeys.h>
- #include <atlstr.h>
- #define EXIT_ON_ERROR(hres) if (FAILED(hres)) { goto Exit; }
- #define SAFE_RELEASE(punk) if ((punk) != NULL) { (punk)->Release(); (punk) = NULL; }
- const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
- const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
- // This function enumerates all active (plugged in) audio rendering endpoint devices. It prints the friendly name and endpoint ID string of each endpoint device.
- //In short :
- //Create an IMMDeviceEnumerator
- //Call EnumAudioEndpoints specifying render to enumerate into an IMMDeviceCollection
- //Obtain individual IMMDevice instances from IMMDeviceCollection
- //Device name and description are queried from IMMDevice using OpenPropertyStore
- void PrintEndpointNames()
- {
- HRESULT hr = S_OK;
- IMMDeviceEnumerator *pEnumerator = NULL;
- IMMDeviceCollection *pCollection = NULL;
- IMMDevice *pEndpoint = NULL;
- IPropertyStore *pProps = NULL;
- LPWSTR pwszID = NULL;
- hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
- EXIT_ON_ERROR(hr)
- hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pCollection);
- EXIT_ON_ERROR(hr)
- UINT count;
- hr = pCollection->GetCount(&count);
- EXIT_ON_ERROR(hr)
- if (count == 0)
- {
- printf("No endpoints found.\n");
- }
- for (ULONG i = 0; i < count; i++) {
- hr = pCollection->Item(i, &pEndpoint);
- EXIT_ON_ERROR(hr)
- hr = pEndpoint->GetId(&pwszID);
- EXIT_ON_ERROR(hr)
- hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
- EXIT_ON_ERROR(hr)
- PROPVARIANT varName;
- PropVariantInit(&varName);
- hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
- EXIT_ON_ERROR(hr)
- printf("Endpoint %d: \"%S\" (%S)\n", i, varName.pwszVal, pwszID);
- CoTaskMemFree(pwszID);
- pwszID = NULL;
- PropVariantClear(&varName);
- SAFE_RELEASE(pProps)
- SAFE_RELEASE(pEndpoint)
- }
- SAFE_RELEASE(pEnumerator)
- SAFE_RELEASE(pCollection)
- return;
- Exit:
- printf("Error!\n");
- CoTaskMemFree(pwszID);
- SAFE_RELEASE(pEnumerator)
- SAFE_RELEASE(pCollection)
- SAFE_RELEASE(pEndpoint)
- SAFE_RELEASE(pProps)
- }
- int main(void) {
- HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); //Mandatory setup, use COINIT_APARTMENTTHREADED for GUI app
- PrintEndpointNames();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement