MyGUI: MyGUI_DynLib.cpp Source File

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
24 DynLib::~DynLib()
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
48 mInstance = (MYGUI_DYNLIB_HANDLE)MYGUI_DYNLIB_LOAD( name.c_str() );
49 #endif
50
51 return mInstance != 0;
52 }
53
54
55 void DynLib::unload()
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
62 if (MYGUI_DYNLIB_UNLOAD(mInstance))
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
MyGUI::DynLib::mName
std::string mName
Name of library.
Definition: MyGUI_DynLib.h:84
MyGUI::DynLib::unload
void unload()
Definition: MyGUI_DynLib.cpp:55
MYGUI_DYNLIB_HANDLE
#define MYGUI_DYNLIB_HANDLE
Definition: MyGUI_DynLib.h:24
MyGUI::DynLib::getName
std::string getName(void) const
Get the name of the library.
Definition: MyGUI_DynLib.cpp:102
MyGUI::DynLib::load
bool load()
Definition: MyGUI_DynLib.cpp:28
MyGUI_Precompiled.h
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:33
MYGUI_DYNLIB_LOAD
#define MYGUI_DYNLIB_LOAD(a)
Definition: MyGUI_DynLib.h:25
MyGUI::DynLib::dynlibError
std::string dynlibError() const
Gets the last loading error.
Definition: MyGUI_DynLib.cpp:79
MYGUI_DYNLIB_UNLOAD
#define MYGUI_DYNLIB_UNLOAD(a)
Definition: MyGUI_DynLib.h:27
MYGUI_LOG
#define MYGUI_LOG(level, text)
Definition: MyGUI_Diagnostic.h:22
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MYGUI_DYNLIB_GETSYM
#define MYGUI_DYNLIB_GETSYM(a, b)
Definition: MyGUI_DynLib.h:26
MyGUI::DynLib::getSymbol
void * getSymbol(const std::string &strName) const
Definition: MyGUI_DynLib.cpp:69
MyGUI::DynLib::~DynLib
~DynLib()
Definition: MyGUI_DynLib.cpp:24
MyGUI::DynLib::mInstance
void * mInstance
Handle to the loaded library.
Definition: MyGUI_DynLib.h:87
MyGUI::DynLib::DynLib
DynLib(const std::string &name)
Definition: MyGUI_DynLib.cpp:18
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI_DynLib.h