juce_win32_WinRTWrapper.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. class WinRTWrapper : public DeletedAtShutdown
  20. {
  21. public:
  22. class ScopedHString
  23. {
  24. public:
  25. ScopedHString (String);
  26. ~ScopedHString();
  27. HSTRING get() const noexcept { return hstr; }
  28. private:
  29. HSTRING hstr = nullptr;
  30. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
  31. };
  32. template <class ComClass>
  33. class ComPtr
  34. {
  35. public:
  36. ComPtr() noexcept {}
  37. ComPtr (ComClass* obj) : p (obj) { if (p) p->AddRef(); }
  38. ComPtr (const ComPtr& other) : p (other.p) { if (p) p->AddRef(); }
  39. ~ComPtr() { release(); }
  40. operator ComClass*() const noexcept { return p; }
  41. ComClass& operator*() const noexcept { return *p; }
  42. ComClass* operator->() const noexcept { return p; }
  43. ComPtr& operator= (ComClass* const newP)
  44. {
  45. if (newP != nullptr)
  46. newP->AddRef();
  47. release();
  48. p = newP;
  49. return *this;
  50. }
  51. ComPtr& operator= (const ComPtr& newP) { return operator= (newP.p); }
  52. ComClass** resetAndGetPointerAddress()
  53. {
  54. release();
  55. p = nullptr;
  56. return &p;
  57. }
  58. private:
  59. ComClass* p = nullptr;
  60. void release() { if (p != nullptr) p->Release(); }
  61. ComClass** operator&() noexcept; // private to avoid it being used accidentally
  62. };
  63. JUCE_DECLARE_SINGLETON (WinRTWrapper, true)
  64. ~WinRTWrapper();
  65. String hStringToString (HSTRING);
  66. bool isInitialised() const noexcept { return initialised; }
  67. template <class ComClass>
  68. ComPtr<ComClass> activateInstance (const wchar_t* runtimeClassID, REFCLSID classUUID)
  69. {
  70. ComPtr<ComClass> result;
  71. if (isInitialised())
  72. {
  73. ComPtr<IInspectable> inspectable;
  74. ScopedHString runtimeClass (runtimeClassID);
  75. auto hr = roActivateInstance (runtimeClass.get(), inspectable.resetAndGetPointerAddress());
  76. if (SUCCEEDED (hr))
  77. inspectable->QueryInterface (classUUID, (void**) result.resetAndGetPointerAddress());
  78. }
  79. return result;
  80. }
  81. template <class ComClass>
  82. ComPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
  83. {
  84. ComPtr<ComClass> comPtr;
  85. if (isInitialised())
  86. {
  87. ScopedHString classID (runtimeClassID);
  88. if (classID.get() != nullptr)
  89. roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
  90. }
  91. return comPtr;
  92. }
  93. private:
  94. WinRTWrapper();
  95. HMODULE winRTHandle = nullptr;
  96. bool initialised = false;
  97. typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
  98. typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
  99. typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
  100. typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
  101. typedef HRESULT (WINAPI* RoActivateInstanceFuncPtr) (HSTRING, IInspectable**);
  102. typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
  103. RoInitializeFuncPtr roInitialize = nullptr;
  104. WindowsCreateStringFuncPtr createHString = nullptr;
  105. WindowsDeleteStringFuncPtr deleteHString = nullptr;
  106. WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
  107. RoActivateInstanceFuncPtr roActivateInstance = nullptr;
  108. RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
  109. };
  110. } // namespace juce