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