Advertisement
Guest User

Untitled

a guest
May 25th, 2016
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include <endpointvolume.h>
  2. #include <mmdeviceapi.h>
  3. #include <Windows.h>
  4. #include <Windows.ApplicationModel.Core.h>
  5. #include <Windows.Media.Devices.h>
  6. #include <wrl.h>
  7.  
  8. #include <utility>
  9.  
  10. #pragma comment(lib, "Mmdevapi.lib")
  11.  
  12. #define Assert(x) do { if (!(x)) __debugbreak(); } while (false)
  13.  
  14. namespace WinRT
  15. {
  16.     using namespace ABI::Windows::ApplicationModel::Core;
  17.     using namespace ABI::Windows::UI::Core;
  18.     using namespace ABI::Windows::Media::Devices;
  19. }
  20.  
  21. namespace WRL
  22. {
  23.     using namespace Microsoft::WRL;
  24.     using namespace Microsoft::WRL::Wrappers;
  25. }
  26.  
  27. struct RoInitializer
  28. {
  29.     RoInitializer()
  30.     {
  31.         auto hr = RoInitialize(RO_INIT_MULTITHREADED);
  32.         Assert(SUCCEEDED(hr));
  33.     }
  34.  
  35.     ~RoInitializer()
  36.     {
  37.         RoUninitialize();
  38.     }
  39. };
  40.  
  41. struct ActivateAudioInterfaceCompletionHandler : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::ClassicCom>, IActivateAudioInterfaceCompletionHandler, WRL::FtmBase>
  42. {
  43.     ActivateAudioInterfaceCompletionHandler() :
  44.         m_CompletionEvent(CreateEventExW(nullptr, nullptr, 0, EVENT_ALL_ACCESS))
  45.     {
  46.         Assert(m_CompletionEvent != nullptr);
  47.     }
  48.  
  49.     virtual HRESULT STDMETHODCALLTYPE ActivateCompleted(IActivateAudioInterfaceAsyncOperation* operation) override
  50.     {
  51.         HRESULT operationHR;
  52.         auto hr = operation->GetActivateResult(&operationHR, reinterpret_cast<IUnknown**>(m_Result.ReleaseAndGetAddressOf()));
  53.         Assert(SUCCEEDED(hr));
  54.         Assert(SUCCEEDED(operationHR));
  55.  
  56.         auto setEventResult = SetEvent(m_CompletionEvent);
  57.         Assert(setEventResult != FALSE);
  58.  
  59.         return S_OK;
  60.     }
  61.  
  62.     inline WRL::ComPtr<IAudioEndpointVolume> WaitForCompletion()
  63.     {
  64.         auto waitResult = WaitForSingleObjectEx(m_CompletionEvent, INFINITE, FALSE);
  65.         Assert(waitResult == WAIT_OBJECT_0);
  66.  
  67.         return std::move(m_Result);
  68.     }
  69.  
  70. private:
  71.     HANDLE m_CompletionEvent;
  72.     WRL::ComPtr<IAudioEndpointVolume> m_Result;
  73. };
  74.  
  75. static inline void ChangeVolumeToLevel(float level)
  76. {
  77.     WRL::ComPtr<WinRT::IMediaDeviceStatics> mediaDeviceStatics;
  78.     auto hr = RoGetActivationFactory(WRL::HStringReference(L"Windows.Media.Devices.MediaDevice").Get(), __uuidof(WinRT::IMediaDeviceStatics), &mediaDeviceStatics);
  79.     Assert(SUCCEEDED(hr));
  80.  
  81.     WRL::HString defaultAudioRenderDevice;
  82.     hr = mediaDeviceStatics->GetDefaultAudioRenderId(WinRT::AudioDeviceRole_Default, defaultAudioRenderDevice.GetAddressOf());
  83.     Assert(SUCCEEDED(hr));
  84.  
  85.     UINT32 defaultAudioRenderDeviceLength;
  86.     auto defaultAudioRenderDeviceCStr = defaultAudioRenderDevice.GetRawBuffer(&defaultAudioRenderDeviceLength);
  87.  
  88.     WRL::ComPtr<IActivateAudioInterfaceAsyncOperation> activateOperation;
  89.     auto activateAudioInterfaceCompletionHandler = WRL::Make<ActivateAudioInterfaceCompletionHandler>();
  90.  
  91.     hr = ActivateAudioInterfaceAsync(defaultAudioRenderDeviceCStr, __uuidof(IAudioEndpointVolume), nullptr, activateAudioInterfaceCompletionHandler.Get(), &activateOperation);
  92.     Assert(SUCCEEDED(hr));
  93.  
  94.     auto audioEndpointVolume = activateAudioInterfaceCompletionHandler->WaitForCompletion();
  95.     Assert(audioEndpointVolume != nullptr);
  96.  
  97.     hr = audioEndpointVolume->SetMasterVolumeLevelScalar(level, nullptr);
  98.     Assert(SUCCEEDED(hr));
  99. }
  100.  
  101. struct FrameworkView : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::WinRtClassicComMix>, WinRT::IFrameworkView>
  102. {
  103.     virtual HRESULT STDMETHODCALLTYPE Initialize(WinRT::ICoreApplicationView* applicationView) override
  104.     {
  105.         return S_OK;
  106.     }
  107.  
  108.     virtual HRESULT STDMETHODCALLTYPE SetWindow(WinRT::ICoreWindow* window) override
  109.     {
  110.         Assert(window != nullptr);
  111.         m_CoreWindow = window;
  112.         return S_OK;
  113.     }
  114.  
  115.     virtual HRESULT STDMETHODCALLTYPE Load(HSTRING entryPoint) override
  116.     {
  117.         Assert(m_CoreWindow != nullptr);
  118.         return m_CoreWindow->Activate();
  119.     }
  120.  
  121.     virtual HRESULT STDMETHODCALLTYPE Run() override
  122.     {
  123.         ChangeVolumeToLevel(0.5f);
  124.         return S_OK;
  125.     }
  126.  
  127.     virtual HRESULT STDMETHODCALLTYPE Uninitialize() override
  128.     {
  129.         m_CoreWindow = nullptr;
  130.         return S_OK;
  131.     }
  132.  
  133. private:
  134.     WRL::ComPtr<WinRT::ICoreWindow> m_CoreWindow;
  135. };
  136.  
  137. struct FrameworkViewSource : WRL::RuntimeClass<WRL::RuntimeClassFlags<WRL::WinRtClassicComMix>, WinRT::IFrameworkViewSource>
  138. {
  139.     virtual HRESULT STDMETHODCALLTYPE CreateView(WinRT::IFrameworkView** view) override
  140.     {
  141.         *view = WRL::Make<FrameworkView>().Detach();
  142.         return S_OK;
  143.     }
  144. };
  145.  
  146. int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  147. {
  148.     RoInitializer roInit;
  149.  
  150.     WRL::ComPtr<WinRT::ICoreApplication> coreApplication;
  151.     auto hr = RoGetActivationFactory(WRL::HStringReference(L"Windows.ApplicationModel.Core.CoreApplication").Get(), __uuidof(WinRT::ICoreApplication), &coreApplication);
  152.     Assert(SUCCEEDED(hr));
  153.  
  154.     auto frameworkViewSource = WRL::Make<FrameworkViewSource>();
  155.     hr = coreApplication->Run(frameworkViewSource.Get());
  156.     Assert(SUCCEEDED(hr));
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement