1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include "library_windows.h"
18
19 #include <windows.h>
20
21 #include <base/containers/string.h>
22 #include <core/log.h>
23 #include <core/namespace.h>
24
25 CORE_BEGIN_NAMESPACE()
26 using BASE_NS::string;
27 using BASE_NS::string_view;
28
LibraryWindows(const string_view filename)29 LibraryWindows::LibraryWindows(const string_view filename)
30 {
31 string tmp(filename);
32 // fix the slashes. (without the correct backslashes the LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR wont work)
33 for (auto& t : tmp) {
34 if (t == '/') {
35 t = '\\';
36 }
37 }
38 libraryHandle_ =
39 LoadLibraryEx(tmp.c_str(), NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
40 if (!libraryHandle_) {
41 DWORD errorCode = GetLastError();
42 CORE_LOG_E("Loading dynamic library '%s' failed: 0x%lx", tmp.c_str(), errorCode);
43 }
44 }
45
~LibraryWindows()46 LibraryWindows::~LibraryWindows()
47 {
48 if (libraryHandle_) {
49 FreeLibrary(libraryHandle_);
50 libraryHandle_ = nullptr;
51 }
52 }
53
GetPlugin() const54 IPlugin* LibraryWindows::GetPlugin() const
55 {
56 if (!libraryHandle_) {
57 return nullptr;
58 }
59
60 return reinterpret_cast<IPlugin*>(GetProcAddress(libraryHandle_, "gPluginData"));
61 }
62
Destroy()63 void LibraryWindows::Destroy()
64 {
65 delete this;
66 }
67
Load(const string_view filePath)68 ILibrary::Ptr ILibrary::Load(const string_view filePath)
69 {
70 return ILibrary::Ptr { new LibraryWindows(filePath) };
71 }
72
GetFileExtension()73 string_view ILibrary::GetFileExtension()
74 {
75 return ".dll";
76 }
77 CORE_END_NAMESPACE()
78
79