The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. class WinRTWrapper : public DeletedAtShutdown
  25. {
  26. public:
  27. juce_DeclareSingleton_SingleThreaded (WinRTWrapper, true)
  28. class ScopedHString
  29. {
  30. public:
  31. ScopedHString (String str)
  32. {
  33. if (WinRTWrapper::getInstance()->isInitialised())
  34. WinRTWrapper::getInstance()->createHString (str.toWideCharPointer(),
  35. static_cast<uint32_t> (str.length()),
  36. &hstr);
  37. }
  38. ~ScopedHString()
  39. {
  40. if (WinRTWrapper::getInstance()->isInitialised() && hstr != nullptr)
  41. WinRTWrapper::getInstance()->deleteHString (hstr);
  42. }
  43. HSTRING get() const noexcept
  44. {
  45. return hstr;
  46. }
  47. private:
  48. HSTRING hstr = nullptr;
  49. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
  50. };
  51. ~WinRTWrapper()
  52. {
  53. if (winRTHandle != nullptr)
  54. ::FreeLibrary (winRTHandle);
  55. }
  56. String hStringToString (HSTRING hstr)
  57. {
  58. const wchar_t* str = nullptr;
  59. if (isInitialised())
  60. {
  61. str = getHStringRawBuffer (hstr, nullptr);
  62. if (str != nullptr)
  63. return String (str);
  64. }
  65. return {};
  66. }
  67. bool isInitialised() const noexcept
  68. {
  69. return initialised;
  70. }
  71. template <class ComClass>
  72. ComSmartPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
  73. {
  74. ComSmartPtr<ComClass> comPtr;
  75. if (isInitialised())
  76. {
  77. ScopedHString classID (runtimeClassID);
  78. if (classID.get() != nullptr)
  79. roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
  80. }
  81. return comPtr;
  82. }
  83. private:
  84. WinRTWrapper()
  85. {
  86. winRTHandle = ::LoadLibraryA ("api-ms-win-core-winrt-l1-1-0");
  87. if (winRTHandle == nullptr)
  88. return;
  89. roInitialize = (RoInitializeFuncPtr) ::GetProcAddress (winRTHandle, "RoInitialize");
  90. createHString = (WindowsCreateStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsCreateString");
  91. deleteHString = (WindowsDeleteStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsDeleteString");
  92. getHStringRawBuffer = (WindowsGetStringRawBufferFuncPtr) ::GetProcAddress (winRTHandle, "WindowsGetStringRawBuffer");
  93. roGetActivationFactory = (RoGetActivationFactoryFuncPtr) ::GetProcAddress (winRTHandle, "RoGetActivationFactory");
  94. if (roInitialize == nullptr || createHString == nullptr || deleteHString == nullptr
  95. || getHStringRawBuffer == nullptr || roGetActivationFactory == nullptr)
  96. return;
  97. HRESULT status = roInitialize (1);
  98. initialised = ! (status != S_OK && status != S_FALSE && status != 0x80010106L);
  99. }
  100. HMODULE winRTHandle = nullptr;
  101. bool initialised = false;
  102. typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
  103. typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
  104. typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
  105. typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
  106. typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
  107. RoInitializeFuncPtr roInitialize = nullptr;
  108. WindowsCreateStringFuncPtr createHString = nullptr;
  109. WindowsDeleteStringFuncPtr deleteHString = nullptr;
  110. WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
  111. RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
  112. };