MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SoundSetGet_GetDevice

Function SoundSetGet_GetDevice

source/script2.cpp:7090–7160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7088
7089
7090HRESULT SoundSetGet_GetDevice(LPTSTR aDeviceString, IMMDevice *&aDevice)
7091{
7092 IMMDeviceEnumerator *deviceEnum;
7093 IMMDeviceCollection *devices;
7094 HRESULT hr;
7095
7096 aDevice = NULL;
7097
7098 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnum);
7099 if (SUCCEEDED(hr))
7100 {
7101 if (!*aDeviceString)
7102 {
7103 // Get default playback device.
7104 hr = deviceEnum->GetDefaultAudioEndpoint(eRender, eConsole, &aDevice);
7105 }
7106 else
7107 {
7108 // Parse Name:Index, Name or Index.
7109 int target_index = 0;
7110 LPWSTR target_name = L"";
7111 size_t target_name_length = 0;
7112 LPTSTR delim = _tcsrchr(aDeviceString, ':');
7113 if (delim)
7114 {
7115 target_index = ATOI(delim + 1) - 1;
7116 target_name = aDeviceString;
7117 target_name_length = delim - aDeviceString;
7118 }
7119 else if (IsNumeric(aDeviceString, FALSE, FALSE))
7120 {
7121 target_index = ATOI(aDeviceString) - 1;
7122 }
7123 else
7124 {
7125 target_name = aDeviceString;
7126 target_name_length = _tcslen(aDeviceString);
7127 }
7128
7129 // Enumerate devices; include unplugged devices so that indices don't change when a device is plugged in.
7130 hr = deviceEnum->EnumAudioEndpoints(eAll, DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED, &devices);
7131 if (SUCCEEDED(hr))
7132 {
7133 if (target_name_length)
7134 {
7135 IMMDevice *dev;
7136 for (UINT u = 0; SUCCEEDED(hr = devices->Item(u, &dev)); ++u)
7137 {
7138 if (LPWSTR dev_name = SoundDeviceGetName(dev))
7139 {
7140 if (!_wcsnicmp(dev_name, target_name, target_name_length)
7141 && target_index-- == 0)
7142 {
7143 CoTaskMemFree(dev_name);
7144 aDevice = dev;
7145 break;
7146 }
7147 CoTaskMemFree(dev_name);

Callers 1

BIF_DECLFunction · 0.85

Calls 4

ATOIFunction · 0.85
IsNumericFunction · 0.85
SoundDeviceGetNameFunction · 0.85
ReleaseMethod · 0.45

Tested by

no test coverage detected