xref: /aosp_15_r20/external/webrtc/rtc_base/win/hstring.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
16 namespace {
17 
LoadComBaseFunction(const char * function_name)18 FARPROC 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 
GetWindowsCreateString()24 decltype(&::WindowsCreateString) GetWindowsCreateString() {
25   static decltype(&::WindowsCreateString) const function =
26       reinterpret_cast<decltype(&::WindowsCreateString)>(
27           LoadComBaseFunction("WindowsCreateString"));
28   return function;
29 }
30 
GetWindowsDeleteString()31 decltype(&::WindowsDeleteString) GetWindowsDeleteString() {
32   static decltype(&::WindowsDeleteString) const function =
33       reinterpret_cast<decltype(&::WindowsDeleteString)>(
34           LoadComBaseFunction("WindowsDeleteString"));
35   return function;
36 }
37 
38 }  // namespace
39 
40 namespace webrtc {
41 
ResolveCoreWinRTStringDelayload()42 bool ResolveCoreWinRTStringDelayload() {
43   return GetWindowsDeleteString() && GetWindowsCreateString();
44 }
45 
CreateHstring(const wchar_t * src,uint32_t len,HSTRING * out_hstr)46 HRESULT 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 
DeleteHstring(HSTRING hstr)54 HRESULT 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
63