1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    The code included in this file is provided under the terms of the ISC license
11    http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12    To use, copy, modify, and/or distribute this software for any purpose with or
13    without fee is hereby granted provided that the above copyright notice and
14    this permission notice appear in all copies.
15 
16    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18    DISCLAIMED.
19 
20   ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
ScopedHString(String str)26 WinRTWrapper::ScopedHString::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 WinRTWrapper::ScopedHString::~ScopedHString()
35 {
36     if (WinRTWrapper::getInstance()->isInitialised() && hstr != nullptr)
37         WinRTWrapper::getInstance()->deleteHString (hstr);
38 }
39 
~WinRTWrapper()40 WinRTWrapper::~WinRTWrapper()
41 {
42     if (winRTHandle != nullptr)
43         ::FreeLibrary (winRTHandle);
44 
45     clearSingletonInstance();
46 }
47 
hStringToString(HSTRING hstr)48 String WinRTWrapper::hStringToString (HSTRING hstr)
49 {
50     if (isInitialised())
51         if (const wchar_t* str = getHStringRawBuffer (hstr, nullptr))
52             return String (str);
53 
54     return {};
55 }
56 
WinRTWrapper()57 WinRTWrapper::WinRTWrapper()
58 {
59     winRTHandle = ::LoadLibraryA ("api-ms-win-core-winrt-l1-1-0");
60 
61     if (winRTHandle == nullptr)
62         return;
63 
64     roInitialize           = (RoInitializeFuncPtr)              ::GetProcAddress (winRTHandle, "RoInitialize");
65     createHString          = (WindowsCreateStringFuncPtr)       ::GetProcAddress (winRTHandle, "WindowsCreateString");
66     deleteHString          = (WindowsDeleteStringFuncPtr)       ::GetProcAddress (winRTHandle, "WindowsDeleteString");
67     getHStringRawBuffer    = (WindowsGetStringRawBufferFuncPtr) ::GetProcAddress (winRTHandle, "WindowsGetStringRawBuffer");
68     roActivateInstance     = (RoActivateInstanceFuncPtr)        ::GetProcAddress (winRTHandle, "RoActivateInstance");
69     roGetActivationFactory = (RoGetActivationFactoryFuncPtr)    ::GetProcAddress (winRTHandle, "RoGetActivationFactory");
70 
71     if (roInitialize == nullptr || createHString == nullptr || deleteHString == nullptr
72         || getHStringRawBuffer == nullptr || roActivateInstance == nullptr || roGetActivationFactory == nullptr)
73         return;
74 
75     HRESULT status = roInitialize (1);
76     initialised = ! (status != S_OK && status != S_FALSE && status != 0x80010106L);
77 }
78 
79 JUCE_IMPLEMENT_SINGLETON (WinRTWrapper)
80 
81 }
82