MyGUI  3.2.1
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 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
34  //APPLE SPECIFIC CODE HERE
35 #else
37 #endif
38 
39  return mInstance != 0;
40  }
41 
42 
44  {
45  // Log library unload
46  MYGUI_LOG(Info, "Unloading library " << mName);
47 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
48  //APPLE SPECIFIC CODE HERE
49 #else
51  {
52  MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
53  }
54 #endif
55  }
56 
57  void* DynLib::getSymbol( const std::string& strName ) const throw()
58  {
59 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
60  //APPLE SPECIFIC CODE HERE
61  return nullptr;
62 #else
63  return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName.c_str());
64 #endif
65  }
66 
67  std::string DynLib::dynlibError() const
68  {
69 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
70  LPVOID lpMsgBuf;
71  FormatMessage(
72  FORMAT_MESSAGE_ALLOCATE_BUFFER |
73  FORMAT_MESSAGE_FROM_SYSTEM |
74  FORMAT_MESSAGE_IGNORE_INSERTS,
75  NULL,
76  GetLastError(),
77  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
78  (LPTSTR) &lpMsgBuf,
79  0,
80  NULL);
81  std::string ret = (char*)lpMsgBuf;
82  // Free the buffer.
83  LocalFree( lpMsgBuf );
84  return ret;
85 #else
86  return "no unix error function defined yet";
87 #endif
88  }
89 
90  std::string DynLib::getName(void) const
91  {
92  return mName;
93  }
94 
95 } // namespace MyGUI
#define MYGUI_DYNLIB_UNLOAD(a)
Definition: MyGUI_DynLib.h:27
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)
std::string dynlibError() const
Gets the last loading error.
std::string getName(void) const
Get the name of the library.
void * getSymbol(const std::string &strName) const