MyGUI  3.2.2
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 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
11 # include <windows.h>
12 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
13 # include <dlfcn.h>
14 #endif
15 
16 namespace MyGUI
17 {
18  DynLib::DynLib(const std::string& name) :
19  mName(name),
20  mInstance(nullptr)
21  {
22  }
23 
25  {
26  }
27 
28  bool DynLib::load()
29  {
30  // Log library load
31  MYGUI_LOG(Info, "Loading library " << mName);
32 
33  std::string name = mName;
34 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
35  const std::string extension = ".dll";
36 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
37  const std::string extension = ".so";
38 #else
39  const std::string extension = "";
40 #endif
41 
42  if (!extension.empty() && name.find(extension) == std::string::npos)
43  name += extension;
44 
45 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
46  //APPLE SPECIFIC CODE HERE
47 #else
49 #endif
50 
51  return mInstance != 0;
52  }
53 
54 
56  {
57  // Log library unload
58  MYGUI_LOG(Info, "Unloading library " << mName);
59 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
60  //APPLE SPECIFIC CODE HERE
61 #else
63  {
64  MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
65  }
66 #endif
67  }
68 
69  void* DynLib::getSymbol( const std::string& strName ) const throw()
70  {
71 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
72  //APPLE SPECIFIC CODE HERE
73  return nullptr;
74 #else
75  return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName.c_str());
76 #endif
77  }
78 
79  std::string DynLib::dynlibError() const
80  {
81 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
82  LPVOID lpMsgBuf;
83  FormatMessage(
84  FORMAT_MESSAGE_ALLOCATE_BUFFER |
85  FORMAT_MESSAGE_FROM_SYSTEM |
86  FORMAT_MESSAGE_IGNORE_INSERTS,
87  NULL,
88  GetLastError(),
89  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
90  (LPTSTR) &lpMsgBuf,
91  0,
92  NULL);
93  std::string ret = (char*)lpMsgBuf;
94  // Free the buffer.
95  LocalFree( lpMsgBuf );
96  return ret;
97 #else
98  return "no unix error function defined yet";
99 #endif
100  }
101 
102  std::string DynLib::getName(void) const
103  {
104  return mName;
105  }
106 
107 } // namespace MyGUI
#define MYGUI_DYNLIB_UNLOAD(a)
Definition: MyGUI_DynLib.h:27
std::string getName(void) const
Get the name of the library.
void * mInstance
Handle to the loaded library.
Definition: MyGUI_DynLib.h:87
DynLib(const std::string &name)
#define MYGUI_DYNLIB_LOAD(a)
Definition: MyGUI_DynLib.h:25
#define MYGUI_DYNLIB_GETSYM(a, b)
Definition: MyGUI_DynLib.h:26
#define nullptr
std::string mName
Name of library.
Definition: MyGUI_DynLib.h:84
#define MYGUI_LOG(level, text)
#define MYGUI_DYNLIB_HANDLE
Definition: MyGUI_DynLib.h:24
#define MYGUI_EXCEPT(dest)
void * getSymbol(const std::string &strName) const
std::string dynlibError() const
Gets the last loading error.