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         char buffer[MAX_PATH];
39         int ret = snprintf(buffer, MAX_PATH, "%s.%s", libraryName, GetSharedLibraryExtension());
40 
41         std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
42         std::wstring wideBuffer = converter.from_bytes(buffer);
43 
44         if (ret > 0 && ret < MAX_PATH)
45         {
46             switch (searchType)
47             {
48                 case SearchType::ApplicationDir:
49                     mModule = LoadPackagedLibrary(wideBuffer.c_str(), 0);
50                     break;
51                 case SearchType::SystemDir:
52                     // Not supported in UWP
53                     break;
54             }
55         }
56     }
57 
~UwpLibrary()58     ~UwpLibrary() override
59     {
60         if (mModule)
61         {
62             FreeLibrary(mModule);
63         }
64     }
65 
getSymbol(const char * symbolName)66     void *getSymbol(const char *symbolName) override
67     {
68         if (!mModule)
69         {
70             return nullptr;
71         }
72 
73         return reinterpret_cast<void *>(GetProcAddress(mModule, symbolName));
74     }
75 
getNative() const76     void *getNative() const override { return reinterpret_cast<void *>(mModule); }
77 
78   private:
79     HMODULE mModule = nullptr;
80 };
81 
OpenSharedLibrary(const char * libraryName,SearchType searchType)82 Library *OpenSharedLibrary(const char *libraryName, SearchType searchType)
83 {
84     return new UwpLibrary(libraryName, searchType);
85 }
86 }  // namespace angle
87