MyGUI  3.2.0
MyGUI_DynLib.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_DynLib.h"
25 
26 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
27 # include <Windows.h>
28 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
29 # include <dlfcn.h>
30 #endif
31 
32 namespace MyGUI
33 {
34  DynLib::DynLib( const std::string& name )
35  {
36  mName = name;
37  mInstance = nullptr;
38  }
39 
40 
42  {
43  }
44 
45 
46  bool DynLib::load()
47  {
48  // Log library load
49  MYGUI_LOG(Info, "Loading library " << mName);
50 
51 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
52  //APPLE SPECIFIC CODE HERE
53 #else
55 #endif
56 
57  return mInstance != 0;
58  }
59 
60 
62  {
63  // Log library unload
64  MYGUI_LOG(Info, "Unloading library " << mName);
65 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
66  //APPLE SPECIFIC CODE HERE
67 #else
69  {
70  MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
71  }
72 #endif
73  }
74 
75  void* DynLib::getSymbol( const std::string& strName ) const throw()
76  {
77 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
78  //APPLE SPECIFIC CODE HERE
79  return nullptr;
80 #else
81  return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName.c_str());
82 #endif
83  }
84 
85  std::string DynLib::dynlibError() const
86  {
87 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
88  LPVOID lpMsgBuf;
89  FormatMessage(
90  FORMAT_MESSAGE_ALLOCATE_BUFFER |
91  FORMAT_MESSAGE_FROM_SYSTEM |
92  FORMAT_MESSAGE_IGNORE_INSERTS,
93  NULL,
94  GetLastError(),
95  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
96  (LPTSTR) &lpMsgBuf,
97  0,
98  NULL
99  );
100  std::string ret = (char*)lpMsgBuf;
101  // Free the buffer.
102  LocalFree( lpMsgBuf );
103  return ret;
104 #else
105  return "no unix error function defined yet";
106 #endif
107  }
108 
109  std::string DynLib::getName(void) const
110  {
111  return mName;
112  }
113 
114 } // namespace MyGUI
#define MYGUI_DYNLIB_UNLOAD(a)
Definition: MyGUI_DynLib.h:44
void * mInstance
Handle to the loaded library.
Definition: MyGUI_DynLib.h:104
DynLib(const std::string &name)
#define MYGUI_DYNLIB_LOAD(a)
Definition: MyGUI_DynLib.h:42
#define MYGUI_DYNLIB_GETSYM(a, b)
Definition: MyGUI_DynLib.h:43
std::string mName
Name of library.
Definition: MyGUI_DynLib.h:101
#define MYGUI_LOG(level, text)
#define MYGUI_DYNLIB_HANDLE
Definition: MyGUI_DynLib.h:41
#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