開発環境 Microsoft Visual Studio Community 2017
実行環境 Microsoft Windows 10 Home (64bit)
プロジェクトの種類 Visual C++ / 空のプロジェクト
プロジェクト名 mmdev

  • 参考

mmdev.cpp
// マルチバイト文字セット
// https://msdn.microsoft.com/en-us/library/dd370812(v=vs.85).aspx
 
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>
#include <stdio.h>
#include <locale.h>
 
#define EXIT_ON_ERROR(hres) if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk) if ((punk) != NULL) { (punk)->Release(); (punk) = NULL; }
 
// 関数プロトタイプ宣言
void PrintEndpointNames();
void PrintEndpoint(IMMDevice *pEndpoint);
 
// グローバル変数
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
 
int main()
{
	HRESULT hr = CoInitialize(NULL);
	EXIT_ON_ERROR(hr);
 
	setlocale(LC_CTYPE, "");
 
	PrintEndpointNames();
 
Exit:
	CoUninitialize();
	return 0;
}
 
void PrintEndpointNames()
{
	IMMDeviceEnumerator *pEnumerator = NULL;
	IMMDeviceCollection *pCollection = NULL;
	IMMDevice *pEndpoint = NULL;
	HRESULT hr;
 
	hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&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 (UINT i = 0; i < count; i++)
	{
		// Get pointer to endpoint number i.
		hr = pCollection->Item(i, &pEndpoint);
		EXIT_ON_ERROR(hr);
 
		PrintEndpoint(pEndpoint);
 
		SAFE_RELEASE(pEndpoint);
	}
 
Exit:
	SAFE_RELEASE(pEnumerator);
	SAFE_RELEASE(pCollection);
	SAFE_RELEASE(pEndpoint);
}
 
void PrintEndpoint(IMMDevice *pEndpoint)
{
	LPWSTR pwszID = NULL;
	IPropertyStore *pProps = NULL;
	HRESULT hr;
 
	// Get the endpoint ID string.
	hr = pEndpoint->GetId(&pwszID);
	EXIT_ON_ERROR(hr);
 
	hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
	EXIT_ON_ERROR(hr);
 
	PROPVARIANT varName;
	// initialize container for property value.
	PropVariantInit(&varName);
 
	// Get the endpoint's friendly-name property.
	hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
	EXIT_ON_ERROR(hr);
 
	// Print endpoint friendly name and endpoint ID.
	printf("Endpoint : '%S' (%S)\n", varName.pwszVal, pwszID);
 
Exit:
	CoTaskMemFree(pwszID);
	//pwszID = NULL;
	PropVariantClear(&varName);
	SAFE_RELEASE(pProps);
}
 

出力
Endpoint : 'EX-LD2071T (Intel SST Audio Device (WDM))' ({0.0.0.00000000}.{0226a988-680d-43c9-b4e4-f71db4992a9c})
Endpoint : 'スピーカー (Intel SST Audio Device (WDM))' ({0.0.0.00000000}.{1dbfc4da-902e-4808-b91b-5381f6f64943})
 
最終更新:2018年05月26日 09:26