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
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
60 #endif
61
62#endif
63
64 return mInstance != nullptr;
65 }
66
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
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
#define MYGUI_EXCEPT(dest)
#define MYGUI_LOG(level, text)
#define MYGUI_DYNLIB_LOAD(a)
#define MYGUI_DYNLIB_UNLOAD(a)
#define MYGUI_DYNLIB_GETSYM(a, b)
#define MYGUI_DYNLIB_HANDLE
void * getSymbol(const char *strName) const noexcept
DynLib(std::string_view name)
const std::string & getName() const
Get the name of the library.
std::string mName
Name of library.
std::string dynlibError() const
Gets the last loading error.
void * mInstance
Handle to the loaded library.