1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // system_utils_winuwp.cpp: Implementation of OS-specific functions for Windows UWP
8
9 #include "system_utils.h"
10
11 #include <stdarg.h>
12 #include <windows.h>
13 #include <array>
14 #include <codecvt>
15 #include <locale>
16 #include <string>
17
18 namespace angle
19 {
20
SetEnvironmentVar(const char * variableName,const char * value)21 bool SetEnvironmentVar(const char *variableName, const char *value)
22 {
23 // Not supported for UWP
24 return false;
25 }
26
GetEnvironmentVar(const char * variableName)27 std::string GetEnvironmentVar(const char *variableName)
28 {
29 // Not supported for UWP
30 return "";
31 }
32
33 class UwpLibrary : public Library
34 {
35 public:
UwpLibrary(const char * libraryName,SearchType searchType)36 UwpLibrary(const char *libraryName, SearchType searchType)
37 {
38 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
39 std::wstring wideBuffer = converter.from_bytes(libraryName);
40
41 switch (searchType)
42 {
43 case SearchType::ModuleDir:
44 mModule = LoadPackagedLibrary(wideBuffer.c_str(), 0);
45 break;
46 case SearchType::SystemDir:
47 case SearchType::AlreadyLoaded:
48 // Not supported in UWP
49 break;
50 }
51 }
52
~UwpLibrary()53 ~UwpLibrary() override
54 {
55 if (mModule)
56 {
57 FreeLibrary(mModule);
58 }
59 }
60
getSymbol(const char * symbolName)61 void *getSymbol(const char *symbolName) override
62 {
63 if (!mModule)
64 {
65 return nullptr;
66 }
67
68 return reinterpret_cast<void *>(GetProcAddress(mModule, symbolName));
69 }
70
getNative() const71 void *getNative() const override { return reinterpret_cast<void *>(mModule); }
72
getPath() const73 std::string getPath() const override
74 {
75 if (!mModule)
76 {
77 return "";
78 }
79
80 std::array<char, MAX_PATH> buffer;
81 if (GetModuleFileNameA(mModule, buffer.data(), buffer.size()) == 0)
82 {
83 return "";
84 }
85
86 return std::string(buffer.data());
87 }
88
89 private:
90 HMODULE mModule = nullptr;
91 };
92
OpenSharedLibrary(const char * libraryName,SearchType searchType)93 Library *OpenSharedLibrary(const char *libraryName, SearchType searchType)
94 {
95 char buffer[MAX_PATH];
96 int ret = snprintf(buffer, MAX_PATH, "%s.%s", libraryName, GetSharedLibraryExtension());
97
98 if (ret > 0 && ret < MAX_PATH)
99 {
100 return OpenSharedLibraryWithExtension(buffer, searchType);
101 }
102 else
103 {
104 fprintf(stderr, "Error loading shared library: 0x%x", ret);
105 return nullptr;
106 }
107 }
108
OpenSharedLibraryWithExtension(const char * libraryName,SearchType searchType)109 Library *OpenSharedLibraryWithExtension(const char *libraryName, SearchType searchType)
110 {
111 return new UwpLibrary(libraryName, searchType);
112 }
113 } // namespace angle
114