-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetDefaultOutputDeviceID.cpp
More file actions
92 lines (75 loc) · 2.6 KB
/
GetDefaultOutputDeviceID.cpp
File metadata and controls
92 lines (75 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
#include "stdafx.h"
#include <cstdio>
#include <mmdeviceapi.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <iostream>
const CLSID CLSID_MMDeviceEnumerators = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerators = __uuidof(IMMDeviceEnumerator);
constexpr auto REFTIMES_PER_SEC = (10000000 * 25);
constexpr auto REFTIMES_PER_MILLISEC = 10000;
#define EXIT_ON_ERROR(hres) \
if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
static LPWSTR ListEndpoints();
static LPWSTR ListEndpoints()
{
wchar_t targetName[] = L"CABLE Output (VB-Audio Virtual Cable)";
LPWSTR targetID = NULL;
HRESULT hr = S_OK;
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pCollection = NULL;
IMMDevice* pEndpoint = NULL;
IPropertyStore* pProps = NULL;
LPWSTR pwszID = NULL;
hr = CoCreateInstance(CLSID_MMDeviceEnumerators, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerators, (void**)&pEnumerator);
EXIT_ON_ERROR(hr);
hr = pEnumerator->EnumAudioEndpoints(eCapture, 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");
}
// Each iteration prints the name of an endpoint device.
PROPVARIANT varName;
for (ULONG i = 0; i < count; i++)
{
// Get the pointer to endpoint number i.
hr = pCollection->Item(i, &pEndpoint);
EXIT_ON_ERROR(hr);
// Get the endpoint ID string.
hr = pEndpoint->GetId(&pwszID);
EXIT_ON_ERROR(hr);
hr = pEndpoint->OpenPropertyStore(
STGM_READ, &pProps);
EXIT_ON_ERROR(hr);
// Initialize the 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 the endpoint friendly name and endpoint ID.
//printf("Endpoint %d: \"%S\" (%S)\n", i, varName.pwszVal, pwszID);
if (wcscmp(varName.pwszVal, targetName) == 0) {
hr = pEndpoint->GetId(&targetID);
EXIT_ON_ERROR(hr);
}
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
}
Exit:
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
SAFE_RELEASE(pEnumerator);
SAFE_RELEASE(pCollection);
SAFE_RELEASE(pEndpoint);
SAFE_RELEASE(pProps);
return targetID;
}