SynchronousOperation.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include "os/Win32/WindowsHeaders.h"
  3. #include <windows.foundation.collections.h>
  4. #include <wrl.h>
  5. namespace il2cpp
  6. {
  7. namespace winrt
  8. {
  9. template<typename T>
  10. class SynchronousOperation : Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>, ABI::Windows::Foundation::IAsyncOperationCompletedHandler<T> >
  11. {
  12. private:
  13. HANDLE m_Event;
  14. HRESULT m_HR;
  15. T m_Result;
  16. public:
  17. inline SynchronousOperation(ABI::Windows::Foundation::IAsyncOperation<T>* op)
  18. {
  19. m_Event = CreateEventExW(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
  20. Assert(m_Event);
  21. auto hr = op->put_Completed(this);
  22. Assert(SUCCEEDED(hr));
  23. }
  24. inline ~SynchronousOperation()
  25. {
  26. CloseHandle(m_Event);
  27. }
  28. HRESULT GetResults(T* result)
  29. {
  30. auto waitResult = WaitForSingleObjectEx(m_Event, INFINITE, FALSE);
  31. if (waitResult != WAIT_OBJECT_0)
  32. return E_FAIL;
  33. if (FAILED(m_HR))
  34. return m_HR;
  35. *result = m_Result;
  36. return S_OK;
  37. }
  38. private:
  39. virtual HRESULT STDMETHODCALLTYPE Invoke(ABI::Windows::Foundation::IAsyncOperation<T>* asyncInfo, ABI::Windows::Foundation::AsyncStatus status) override
  40. {
  41. m_HR = asyncInfo->GetResults(&m_Result);
  42. SetEvent(m_Event);
  43. return S_OK;
  44. }
  45. };
  46. }
  47. }