blob: 5a362a97c914efec054198f4685fdad2e40d1482 [file] [log] [blame]
Austin Orion0bb354c2020-10-29 11:30:10 -07001/*
2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_base/win/hstring.h"
12
13#include <libloaderapi.h>
14#include <winstring.h>
15
16namespace {
17
18FARPROC LoadComBaseFunction(const char* function_name) {
19 static HMODULE const handle =
20 ::LoadLibraryExW(L"combase.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
21 return handle ? ::GetProcAddress(handle, function_name) : nullptr;
22}
23
24decltype(&::WindowsCreateString) GetWindowsCreateString() {
25 static decltype(&::WindowsCreateString) const function =
26 reinterpret_cast<decltype(&::WindowsCreateString)>(
27 LoadComBaseFunction("WindowsCreateString"));
28 return function;
29}
30
31decltype(&::WindowsDeleteString) GetWindowsDeleteString() {
32 static decltype(&::WindowsDeleteString) const function =
33 reinterpret_cast<decltype(&::WindowsDeleteString)>(
34 LoadComBaseFunction("WindowsDeleteString"));
35 return function;
36}
37
38} // namespace
39
40namespace webrtc {
41
42bool ResolveCoreWinRTStringDelayload() {
43 return GetWindowsDeleteString() && GetWindowsCreateString();
44}
45
46HRESULT CreateHstring(const wchar_t* src, uint32_t len, HSTRING* out_hstr) {
47 decltype(&::WindowsCreateString) create_string_func =
48 GetWindowsCreateString();
49 if (!create_string_func)
50 return E_FAIL;
51 return create_string_func(src, len, out_hstr);
52}
53
54HRESULT DeleteHstring(HSTRING hstr) {
55 decltype(&::WindowsDeleteString) delete_string_func =
56 GetWindowsDeleteString();
57 if (!delete_string_func)
58 return E_FAIL;
59 return delete_string_func(hstr);
60}
61
62} // namespace webrtc