Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <endpointvolume.h>
- #include <mmdeviceapi.h>
- #include <Windows.h>
- #include <Windows.ApplicationModel.Core.h>
- #include <Windows.Media.Devices.h>
- #include <wrl.h>
- #include <utility>
- #pragma comment(lib, "Mmdevapi.lib")
- #define Assert(x) do { if (!(x)) __debugbreak(); } while (false)
- namespace WinRT
- {
- using namespace ABI::Windows::ApplicationModel::Core;
- using namespace ABI::Windows::UI::Core;
- using namespace ABI::Windows::Media::Devices;
- }
- namespace WRL
- {
- using namespace Microsoft::WRL;
- using namespace Microsoft::WRL::Wrappers;
- }
- struct RoInitializer
- {
- RoInitializer()
- {
- auto hr = RoInitialize(RO_INIT_MULTITHREADED);
- Assert(SUCCEEDED(hr));
- }
- ~RoInitializer()
- {
- RoUninitialize();
- }
- };
- struct ActivateAudioInterfaceCompletionHandler : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::ClassicCom>, IActivateAudioInterfaceCompletionHandler, WRL::FtmBase>
- {
- ActivateAudioInterfaceCompletionHandler() :
- m_CompletionEvent(CreateEventExW(nullptr, nullptr, 0, EVENT_ALL_ACCESS))
- {
- Assert(m_CompletionEvent != nullptr);
- }
- virtual HRESULT STDMETHODCALLTYPE ActivateCompleted(IActivateAudioInterfaceAsyncOperation* operation) override
- {
- HRESULT operationHR;
- auto hr = operation->GetActivateResult(&operationHR, reinterpret_cast<IUnknown**>(m_Result.ReleaseAndGetAddressOf()));
- Assert(SUCCEEDED(hr));
- Assert(SUCCEEDED(operationHR));
- auto setEventResult = SetEvent(m_CompletionEvent);
- Assert(setEventResult != FALSE);
- return S_OK;
- }
- inline WRL::ComPtr<IAudioEndpointVolume> WaitForCompletion()
- {
- auto waitResult = WaitForSingleObjectEx(m_CompletionEvent, INFINITE, FALSE);
- Assert(waitResult == WAIT_OBJECT_0);
- return std::move(m_Result);
- }
- private:
- HANDLE m_CompletionEvent;
- WRL::ComPtr<IAudioEndpointVolume> m_Result;
- };
- static inline void ChangeVolumeToLevel(float level)
- {
- WRL::ComPtr<WinRT::IMediaDeviceStatics> mediaDeviceStatics;
- auto hr = RoGetActivationFactory(WRL::HStringReference(L"Windows.Media.Devices.MediaDevice").Get(), __uuidof(WinRT::IMediaDeviceStatics), &mediaDeviceStatics);
- Assert(SUCCEEDED(hr));
- WRL::HString defaultAudioRenderDevice;
- hr = mediaDeviceStatics->GetDefaultAudioRenderId(WinRT::AudioDeviceRole_Default, defaultAudioRenderDevice.GetAddressOf());
- Assert(SUCCEEDED(hr));
- UINT32 defaultAudioRenderDeviceLength;
- auto defaultAudioRenderDeviceCStr = defaultAudioRenderDevice.GetRawBuffer(&defaultAudioRenderDeviceLength);
- WRL::ComPtr<IActivateAudioInterfaceAsyncOperation> activateOperation;
- auto activateAudioInterfaceCompletionHandler = WRL::Make<ActivateAudioInterfaceCompletionHandler>();
- hr = ActivateAudioInterfaceAsync(defaultAudioRenderDeviceCStr, __uuidof(IAudioEndpointVolume), nullptr, activateAudioInterfaceCompletionHandler.Get(), &activateOperation);
- Assert(SUCCEEDED(hr));
- auto audioEndpointVolume = activateAudioInterfaceCompletionHandler->WaitForCompletion();
- Assert(audioEndpointVolume != nullptr);
- hr = audioEndpointVolume->SetMasterVolumeLevelScalar(level, nullptr);
- Assert(SUCCEEDED(hr));
- }
- struct FrameworkView : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::WinRtClassicComMix>, WinRT::IFrameworkView>
- {
- virtual HRESULT STDMETHODCALLTYPE Initialize(WinRT::ICoreApplicationView* applicationView) override
- {
- return S_OK;
- }
- virtual HRESULT STDMETHODCALLTYPE SetWindow(WinRT::ICoreWindow* window) override
- {
- Assert(window != nullptr);
- m_CoreWindow = window;
- return S_OK;
- }
- virtual HRESULT STDMETHODCALLTYPE Load(HSTRING entryPoint) override
- {
- Assert(m_CoreWindow != nullptr);
- return m_CoreWindow->Activate();
- }
- virtual HRESULT STDMETHODCALLTYPE Run() override
- {
- ChangeVolumeToLevel(0.5f);
- return S_OK;
- }
- virtual HRESULT STDMETHODCALLTYPE Uninitialize() override
- {
- m_CoreWindow = nullptr;
- return S_OK;
- }
- private:
- WRL::ComPtr<WinRT::ICoreWindow> m_CoreWindow;
- };
- struct FrameworkViewSource : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::WinRtClassicComMix>, WinRT::IFrameworkViewSource>
- {
- virtual HRESULT STDMETHODCALLTYPE CreateView(WinRT::IFrameworkView** view) override
- {
- *view = WRL::Make<FrameworkView>().Detach();
- return S_OK;
- }
- };
- int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- {
- RoInitializer roInit;
- WRL::ComPtr<WinRT::ICoreApplication> coreApplication;
- auto hr = RoGetActivationFactory(WRL::HStringReference(L"Windows.ApplicationModel.Core.CoreApplication").Get(), __uuidof(WinRT::ICoreApplication), &coreApplication);
- Assert(SUCCEEDED(hr));
- auto frameworkViewSource = WRL::Make<FrameworkViewSource>();
- hr = coreApplication->Run(frameworkViewSource.Get());
- Assert(SUCCEEDED(hr));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement