MyGUI  3.4.0
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 
28 namespace MyGUI
29 {
30  DynLib::DynLib(const std::string& 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 extension = ".dll";
47 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
48  const std::string extension = ".so";
49 #else
50  const std::string extension = "";
51 #endif
52 
53  if (!extension.empty() && 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 std::string& 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.c_str());
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 |
108  FORMAT_MESSAGE_FROM_SYSTEM |
109  FORMAT_MESSAGE_IGNORE_INSERTS,
110  nullptr,
111  GetLastError(),
112  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
113  (LPTSTR) &lpMsgBuf,
114  0,
115  nullptr);
116  std::string ret = (char*)lpMsgBuf;
117  // Free the buffer.
118  LocalFree( lpMsgBuf );
119  return ret;
120 #else
121  return "no unix error function defined yet";
122 #endif
123 #endif
124  }
125 
126  std::string DynLib::getName(void) const
127  {
128  return mName;
129  }
130 
131 } // namespace MyGUI
MyGUI::DynLib::mName
std::string mName
Name of library.
Definition: MyGUI_DynLib.h:70
MyGUI::DynLib::unload
void unload()
Definition: MyGUI_DynLib.cpp:67
MYGUI_DYNLIB_HANDLE
#define MYGUI_DYNLIB_HANDLE
Definition: MyGUI_DynLib.h:19
MyGUI::DynLib::getName
std::string getName(void) const
Get the name of the library.
Definition: MyGUI_DynLib.cpp:126
MyGUI::DynLib::load
bool load()
Definition: MyGUI_DynLib.cpp:36
MyGUI_Precompiled.h
MYGUI_DYNLIB_LOAD
#define MYGUI_DYNLIB_LOAD(a)
Definition: MyGUI_DynLib.cpp:13
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:26
MyGUI::DynLib::dynlibError
std::string dynlibError() const
Gets the last loading error.
Definition: MyGUI_DynLib.cpp:99
MyGUI::DynLib::getSymbol
void * getSymbol(const std::string &strName) const noexcept
Definition: MyGUI_DynLib.cpp:85
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
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::mInstance
void * mInstance
Handle to the loaded library.
Definition: MyGUI_DynLib.h:73
MyGUI::DynLib::DynLib
DynLib(const std::string &name)
Definition: MyGUI_DynLib.cpp:30
MyGUI
Definition: MyGUI_ActionController.h:15
MyGUI_DynLib.h