1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 |
|
---|
27 | #include "OgreDynLib.h"
|
---|
28 |
|
---|
29 | #include "OgreException.h"
|
---|
30 | #include "OgreLogManager.h"
|
---|
31 |
|
---|
32 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
33 | # define WIN32_LEAN_AND_MEAN
|
---|
34 | # include <windows.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
---|
38 | # include "macPlugins.h"
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | namespace Ogre {
|
---|
43 |
|
---|
44 | //-----------------------------------------------------------------------
|
---|
45 | DynLib::DynLib( const String& name )
|
---|
46 | {
|
---|
47 | OgreGuard("DynLib::DynLib");
|
---|
48 |
|
---|
49 | mName = name;
|
---|
50 | m_hInst = NULL;
|
---|
51 |
|
---|
52 | OgreUnguard();
|
---|
53 | }
|
---|
54 |
|
---|
55 | //-----------------------------------------------------------------------
|
---|
56 | DynLib::~DynLib()
|
---|
57 | {
|
---|
58 | }
|
---|
59 |
|
---|
60 | //-----------------------------------------------------------------------
|
---|
61 | void DynLib::load()
|
---|
62 | {
|
---|
63 | OgreGuard("DynLib::load");
|
---|
64 |
|
---|
65 | // Log library load
|
---|
66 | LogManager::getSingleton().logMessage("Loading library " + mName);
|
---|
67 |
|
---|
68 | std::string name = mName;
|
---|
69 | #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
|
---|
70 | // dlopen() does not add .so to the filename, like windows does for .dll
|
---|
71 | if (name.substr(name.length() - 3, 3) != ".so")
|
---|
72 | name += ".so";
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() );
|
---|
76 |
|
---|
77 | if( !m_hInst )
|
---|
78 | OGRE_EXCEPT(
|
---|
79 | Exception::ERR_INTERNAL_ERROR,
|
---|
80 | "Could not load dynamic library " + mName +
|
---|
81 | ". System Error: " + dynlibError(),
|
---|
82 | "DynLib::load" );
|
---|
83 |
|
---|
84 | OgreUnguard();
|
---|
85 | }
|
---|
86 |
|
---|
87 | //-----------------------------------------------------------------------
|
---|
88 | void DynLib::unload()
|
---|
89 | {
|
---|
90 | OgreGuard("DynLib::unload");
|
---|
91 |
|
---|
92 | // Log library unload
|
---|
93 | LogManager::getSingleton().logMessage("Unloading library " + mName);
|
---|
94 |
|
---|
95 | if( DYNLIB_UNLOAD( m_hInst ) )
|
---|
96 | {
|
---|
97 | OGRE_EXCEPT(
|
---|
98 | Exception::ERR_INTERNAL_ERROR,
|
---|
99 | "Could not unload dynamic library " + mName +
|
---|
100 | ". System Error: " + dynlibError(),
|
---|
101 | "DynLib::unload");
|
---|
102 | }
|
---|
103 |
|
---|
104 | OgreUnguard();
|
---|
105 | }
|
---|
106 |
|
---|
107 | //-----------------------------------------------------------------------
|
---|
108 | void* DynLib::getSymbol( const String& strName ) const throw()
|
---|
109 | {
|
---|
110 | return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
|
---|
111 | }
|
---|
112 | //-----------------------------------------------------------------------
|
---|
113 | String DynLib::dynlibError( void )
|
---|
114 | {
|
---|
115 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
116 | LPVOID lpMsgBuf;
|
---|
117 | FormatMessage(
|
---|
118 | FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
119 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
120 | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
121 | NULL,
|
---|
122 | GetLastError(),
|
---|
123 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
124 | (LPTSTR) &lpMsgBuf,
|
---|
125 | 0,
|
---|
126 | NULL
|
---|
127 | );
|
---|
128 | String ret = (char*)lpMsgBuf;
|
---|
129 | // Free the buffer.
|
---|
130 | LocalFree( lpMsgBuf );
|
---|
131 | return ret;
|
---|
132 | #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
|
---|
133 | return String(dlerror());
|
---|
134 | #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
---|
135 | return String(mac_errorBundle());
|
---|
136 | #else
|
---|
137 | return String("");
|
---|
138 | #endif
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|