How to find audio devices that are connected

OPN_Analysis 41 Reputation points
2023-01-19T00:35:50.3533333+00:00

I'm trying to find the audio devices (both input and output) connected to my computer. I would like to switch my default audio out from headphones to speakers and back.

The docs feel cyclical, telling me I need to use IMMDeviceCollection to enumerate my devices, but to get the collection, I need to provide it to the IMMDevicenumerator, but it can't be empty otherwise it throws an error/warning.

The short, how do I list my audio devices? How can I switch what is considered my default audio endpoint?

TIA

void AudioControl::getDevices()
{
	IMMDeviceEnumerator* deviceEnumerator = NULL;
	IMMDeviceCollection* deviceCollection = NULL;

	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr))
	{
		std::cout << "Initialized" << std::endl;
		hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator);
		if (SUCCEEDED(hr))
		{
			std::cout << "Created instance" << std::endl;
			hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, (IMMDeviceCollection**)deviceCollection);
			if (SUCCEEDED(hr))
			{
				if (deviceCollection != NULL)
				{
					std::cout << "Enumerated devices" << std::endl;
					std::wcout << "Devices: " << deviceCollection << std::endl;
					std::wcout << "Type: " << typeid(deviceCollection).name() << std::endl;
				}
			}
			std::cout << "Done enumerating devices" << std::endl;
		}
	}
	deviceCollection->Release();
	deviceEnumerator->Release();
	CoUninitialize();

}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,897 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,631 questions
No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 5,851 Reputation points Microsoft Employee
    2023-01-19T02:32:43.09+00:00

    Hello,

    Welcome to Microsoft Q&A!

    IMMDeviceEnumerator::EnumAudioEndpoints method returns a IMMDeviceCollection interface which represents a collection of multimedia device resources. The code example prints the display names of all audio-rendering endpoint devices in the system. Also see the example.

    To set default audio endpoint, see the example(IPolicyConfig).

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 28,661 Reputation points
    2023-01-19T14:16:39.7766667+00:00

    The posted code contains an error --

    This line -

    hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, (IMMDeviceCollection**)deviceCollection);
    

    should be

    hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &deviceCollection);
    
    No comments