MyGUI: MyGUI_DynLib.cpp Source File

MyGUI 3.4.3

MyGUI_DynLib.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_DynLib.h"
9
10#ifndef MYGUI_DISABLE_PLUGINS
11 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
12 #include <windows.h>
13 #define MYGUI_DYNLIB_LOAD(a) LoadLibrary(a)
14 #define MYGUI_DYNLIB_GETSYM(a, b) GetProcAddress(a, b)
15 #define MYGUI_DYNLIB_UNLOAD(a) !FreeLibrary(a)
16 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
17 #include <dlfcn.h>
18 #define MYGUI_DYNLIB_LOAD(a) dlopen(a, RTLD_LAZY | RTLD_GLOBAL)
19 #define MYGUI_DYNLIB_GETSYM(a, b) dlsym(a, b)
20 #define MYGUI_DYNLIB_UNLOAD(a) dlclose(a)
21 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
22 //# define MYGUI_DYNLIB_LOAD( a ) mac_loadExeBundle( a )
23 //# define MYGUI_DYNLIB_GETSYM( a, b ) mac_getBundleSym( a, b )
24 //# define MYGUI_DYNLIB_UNLOAD( a ) mac_unloadExeBundle( a )
25 #endif
26#endif
27
28namespace MyGUI
29{
30 DynLib::DynLib(std::string_view name) :
31 mName(name),
32 mInstance(nullptr)
33 {
34 }
35
36 bool DynLib::load()
37 {
38#ifdef MYGUI_DISABLE_PLUGINS
39 MYGUI_EXCEPT("Plugins support disabled, rebuild MyGUI without MYGUI_DISABLE_PLUGINS");
40#else
41 // Log library load
42 MYGUI_LOG(Info, "Loading library " << mName);
43
44 std::string name = mName;
45 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
46 const std::string_view extension = ".dll";
47 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
48 const std::string_view extension = ".so";
49 #else
50 const std::string_view extension;
51 #endif
52
53 if (name.find(extension) == std::string::npos)
54 name += extension;
55
56 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
57 //APPLE SPECIFIC CODE HERE
58 #else
59 mInstance = (MYGUI_DYNLIB_HANDLE)MYGUI_DYNLIB_LOAD(name.c_str());
60 #endif
61
62#endif
63
64 return mInstance != nullptr;
65 }
66
67 void DynLib::unload()
68 {
69#ifdef MYGUI_DISABLE_PLUGINS
70 MYGUI_EXCEPT("Plugins support disabled, rebuild MyGUI without MYGUI_DISABLE_PLUGINS");
71#else
72 // Log library unload
73 MYGUI_LOG(Info, "Unloading library " << mName);
74 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
75 //APPLE SPECIFIC CODE HERE
76 #else
77 if (MYGUI_DYNLIB_UNLOAD(mInstance))
78 {
79 MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
80 }
81 #endif
82#endif
83 }
84
85 void* DynLib::getSymbol(const char* strName) const noexcept
86 {
87#ifdef MYGUI_DISABLE_PLUGINS
88 MYGUI_EXCEPT("Plugins support disabled, rebuild MyGUI without MYGUI_DISABLE_PLUGINS");
89#else
90 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
91 //APPLE SPECIFIC CODE HERE
92 return nullptr;
93 #else
94 return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName);
95 #endif
96#endif
97 }
98
99 std::string DynLib::dynlibError() const
100 {
101#ifdef MYGUI_DISABLE_PLUGINS
102 MYGUI_EXCEPT("Plugins support disabled, rebuild MyGUI without MYGUI_DISABLE_PLUGINS");
103#else
104 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
105 LPVOID lpMsgBuf;
106 FormatMessage(
107 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
108 nullptr,
109 GetLastError(),
110 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
111 (LPTSTR)&lpMsgBuf,
112 0,
113 nullptr);
114 std::string ret = (char*)lpMsgBuf;
115 // Free the buffer.
116 LocalFree(lpMsgBuf);
117 return ret;
118 #else
119 return "no unix error function defined yet";
120 #endif
121#endif
122 }
123
124 const std::string& DynLib::getName() const
125 {
126 return mName;
127 }
128
129} // namespace MyGUI
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition MyGUI_Diagnostic.h:21
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition MyGUI_Diagnostic.h:17
MYGUI_DYNLIB_LOAD
#define MYGUI_DYNLIB_LOAD(a)
Definition MyGUI_DynLib.cpp:13
MYGUI_DYNLIB_UNLOAD
#define MYGUI_DYNLIB_UNLOAD(a)
Definition MyGUI_DynLib.cpp:15
MYGUI_DYNLIB_GETSYM
#define MYGUI_DYNLIB_GETSYM(a, b)
Definition MyGUI_DynLib.cpp:14
MyGUI_DynLib.h
MYGUI_DYNLIB_HANDLE
#define MYGUI_DYNLIB_HANDLE
Definition MyGUI_DynLib.h:19
MyGUI_Precompiled.h
MyGUI::DynLib::getSymbol
void * getSymbol(const char *strName) const noexcept
Definition MyGUI_DynLib.cpp:85
MyGUI::DynLib::DynLib
DynLib(std::string_view name)
Definition MyGUI_DynLib.cpp:30
MyGUI::DynLib::getName
const std::string & getName() const
Get the name of the library.
Definition MyGUI_DynLib.cpp:124
MyGUI::DynLib::mName
std::string mName
Name of library.
Definition MyGUI_DynLib.h:69
MyGUI::DynLib::dynlibError
std::string dynlibError() const
Gets the last loading error.
Definition MyGUI_DynLib.cpp:99
MyGUI::DynLib::unload
void unload()
Definition MyGUI_DynLib.cpp:67
MyGUI::DynLib::mInstance
void * mInstance
Handle to the loaded library.
Definition MyGUI_DynLib.h:72
MyGUI::DynLib::load
bool load()
Definition MyGUI_DynLib.cpp:36
MyGUI
Definition MyGUI_ActionController.h:15