Skip to content

Instantly share code, notes, and snippets.

@jedwardsol
Last active February 8, 2023 17:05
Show Gist options
  • Save jedwardsol/6eafba31d4c3aaa76ecaa35afdf847b6 to your computer and use it in GitHub Desktop.
Save jedwardsol/6eafba31d4c3aaa76ecaa35afdf847b6 to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <atlbase.h>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#define AUDCLNT_S_NO_SINGLE_PROCESS AUDCLNT_SUCCESS (0x00d)
#include "include/print.h"
#include "include/coInit.h"
int main()
try
{
CoInitialiser _;
CComPtr<IMMDeviceEnumerator> deviceEnumerator ;
auto hr=deviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator) );
throwIfFailedHr(hr, "deviceEnumerator.CoCreateInstance");
CComPtr<IMMDeviceCollection> deviceCollection;
hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &deviceCollection);
throwIfFailedHr(hr, "deviceEnumerator->EnumAudioEndpoints");
UINT numDevices{};
deviceCollection->GetCount(&numDevices);
print("There are {} devices\n",numDevices);
for(int device=0; device < numDevices; device++)
{
CComPtr<IMMDevice> audioDevice;
deviceCollection->Item(device,&audioDevice);
wchar_t *deviceId{};
audioDevice->GetId(&deviceId);
print(L" Device {} = {}\n", device,deviceId);
CoTaskMemFree(deviceId);
CComPtr<IAudioSessionManager2> sessionManager;
hr = audioDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&sessionManager));
throwIfFailedHr(hr, "device->Activate");
CComPtr<IAudioSessionEnumerator> sessionEnumerator;
sessionManager->GetSessionEnumerator(&sessionEnumerator);
int numSessions{};
sessionEnumerator->GetCount(&numSessions);
print(" There are {} sessions\n", numSessions);
for(int session=0;session<numSessions;session++)
{
CComPtr<IAudioSessionControl> sessionControl;
sessionEnumerator->GetSession(session,&sessionControl);
CComPtr<IAudioSessionControl2> sessionControl2;
sessionControl->QueryInterface<IAudioSessionControl2>(&sessionControl2);
DWORD pid{};
hr = sessionControl2->GetProcessId(&pid);
if(hr == AUDCLNT_E_DEVICE_INVALIDATED)
{
print(L" pid invalidated\n");
}
else if (hr == AUDCLNT_S_NO_SINGLE_PROCESS)
{
print(L" AUDCLNT_S_NO_SINGLE_PROCESS {}\n",pid );
}
else if(SUCCEEDED(hr))
{
print(L" pid {}\n", pid);
}
else
{
print(L" error {:x}\n", hr);
}
}
}
}
catch(std::exception const &e)
{
print("\n{}\n",e.what());
}
#include <Windows.h>
#include <winrt/base.h>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#define AUDCLNT_S_NO_SINGLE_PROCESS AUDCLNT_SUCCESS (0x00d)
#include "include/print.h"
#include "include/thrower.h"
int main()
try
{
winrt::init_apartment(winrt::apartment_type::single_threaded);
auto deviceEnumerator = winrt::create_instance<IMMDeviceEnumerator>(__uuidof(MMDeviceEnumerator));
winrt::com_ptr<IMMDeviceCollection> deviceCollection;
winrt::check_hresult(deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, deviceCollection.put()) );
// throwIfFailedHr(E_ACCESSDENIED,"E_ACCESSDENIED"); // better than winrt::check_hresult
UINT numDevices{};
deviceCollection->GetCount(&numDevices);
print("There are {} devices\n", numDevices);
for (int device = 0; device < numDevices; device++)
{
winrt::com_ptr<IMMDevice> audioDevice;
deviceCollection->Item(device, audioDevice.put());
wchar_t* deviceId{};
audioDevice->GetId(&deviceId);
print(L" Device {} = {}\n", device, deviceId);
CoTaskMemFree(deviceId);
winrt::com_ptr<IAudioSessionManager2> sessionManager;
winrt::check_hresult ( audioDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, nullptr, sessionManager.put_void() ));
winrt::com_ptr<IAudioSessionEnumerator> sessionEnumerator;
sessionManager->GetSessionEnumerator(sessionEnumerator.put());
int numSessions{};
sessionEnumerator->GetCount(&numSessions);
print(" There are {} sessions\n", numSessions);
for (int session = 0; session < numSessions; session++)
{
winrt::com_ptr<IAudioSessionControl> sessionControl;
sessionEnumerator->GetSession(session, sessionControl.put());
DWORD pid{};
auto hr = sessionControl.as<IAudioSessionControl2>()->GetProcessId(&pid);
if (hr == AUDCLNT_E_DEVICE_INVALIDATED)
{
print(L" pid invalidated\n");
}
else if (hr == AUDCLNT_S_NO_SINGLE_PROCESS)
{
print(L" AUDCLNT_S_NO_SINGLE_PROCESS {}\n", pid);
}
else if (SUCCEEDED(hr))
{
print(L" pid {}\n", pid);
}
else
{
print(L" error {:x}\n", hr);
}
}
}
}
catch (std::exception const &e)
{
print("\n{}\n", e.what());
}
catch (winrt::hresult_error const &e)
{
print(L"\n{}\n", e.message());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment