Audio plugin host https://kx.studio/carla
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.

132 lines
4.6KB

  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. juce_DeclareSingleton (WinRTWrapper, true)
  23. class ScopedHString
  24. {
  25. public:
  26. ScopedHString (String str)
  27. {
  28. if (WinRTWrapper::getInstance()->isInitialised())
  29. WinRTWrapper::getInstance()->createHString (str.toWideCharPointer(),
  30. static_cast<uint32_t> (str.length()),
  31. &hstr);
  32. }
  33. ~ScopedHString()
  34. {
  35. if (WinRTWrapper::getInstance()->isInitialised() && hstr != nullptr)
  36. WinRTWrapper::getInstance()->deleteHString (hstr);
  37. }
  38. HSTRING get() const noexcept
  39. {
  40. return hstr;
  41. }
  42. private:
  43. HSTRING hstr = nullptr;
  44. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedHString)
  45. };
  46. ~WinRTWrapper()
  47. {
  48. if (winRTHandle != nullptr)
  49. ::FreeLibrary (winRTHandle);
  50. }
  51. String hStringToString (HSTRING hstr)
  52. {
  53. if (isInitialised())
  54. if (const wchar_t* str = getHStringRawBuffer (hstr, nullptr))
  55. return String (str);
  56. return {};
  57. }
  58. bool isInitialised() const noexcept
  59. {
  60. return initialised;
  61. }
  62. template <class ComClass>
  63. ComSmartPtr<ComClass> getWRLFactory (const wchar_t* runtimeClassID)
  64. {
  65. ComSmartPtr<ComClass> comPtr;
  66. if (isInitialised())
  67. {
  68. ScopedHString classID (runtimeClassID);
  69. if (classID.get() != nullptr)
  70. roGetActivationFactory (classID.get(), __uuidof (ComClass), (void**) comPtr.resetAndGetPointerAddress());
  71. }
  72. return comPtr;
  73. }
  74. private:
  75. WinRTWrapper()
  76. {
  77. winRTHandle = ::LoadLibraryA ("api-ms-win-core-winrt-l1-1-0");
  78. if (winRTHandle == nullptr)
  79. return;
  80. roInitialize = (RoInitializeFuncPtr) ::GetProcAddress (winRTHandle, "RoInitialize");
  81. createHString = (WindowsCreateStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsCreateString");
  82. deleteHString = (WindowsDeleteStringFuncPtr) ::GetProcAddress (winRTHandle, "WindowsDeleteString");
  83. getHStringRawBuffer = (WindowsGetStringRawBufferFuncPtr) ::GetProcAddress (winRTHandle, "WindowsGetStringRawBuffer");
  84. roGetActivationFactory = (RoGetActivationFactoryFuncPtr) ::GetProcAddress (winRTHandle, "RoGetActivationFactory");
  85. if (roInitialize == nullptr || createHString == nullptr || deleteHString == nullptr
  86. || getHStringRawBuffer == nullptr || roGetActivationFactory == nullptr)
  87. return;
  88. HRESULT status = roInitialize (1);
  89. initialised = ! (status != S_OK && status != S_FALSE && status != 0x80010106L);
  90. }
  91. HMODULE winRTHandle = nullptr;
  92. bool initialised = false;
  93. typedef HRESULT (WINAPI* RoInitializeFuncPtr) (int);
  94. typedef HRESULT (WINAPI* WindowsCreateStringFuncPtr) (LPCWSTR, UINT32, HSTRING*);
  95. typedef HRESULT (WINAPI* WindowsDeleteStringFuncPtr) (HSTRING);
  96. typedef PCWSTR (WINAPI* WindowsGetStringRawBufferFuncPtr) (HSTRING, UINT32*);
  97. typedef HRESULT (WINAPI* RoGetActivationFactoryFuncPtr) (HSTRING, REFIID, void**);
  98. RoInitializeFuncPtr roInitialize = nullptr;
  99. WindowsCreateStringFuncPtr createHString = nullptr;
  100. WindowsDeleteStringFuncPtr deleteHString = nullptr;
  101. WindowsGetStringRawBufferFuncPtr getHStringRawBuffer = nullptr;
  102. RoGetActivationFactoryFuncPtr roGetActivationFactory = nullptr;
  103. };
  104. } // namespace juce