1 #include <cpPlugins/OS/DLLManager.h>
2 #include <cpPlugins/Utility.h>
4 #ifdef cpPlugins_OS_Windows
6 #else // cpPlugins_OS_Windows
8 #endif // cpPlugins_OS_Windows
10 // -------------------------------------------------------------------------
11 void cpPlugins::OS::DLLManager::
12 TeaseLoadedLibraries( )
14 void* hnd = cpPlugins::OS::DLLManager::_Load( "", 0 );
16 throw std::runtime_error( "Something really nasty just happened." );
19 // -------------------------------------------------------------------------
20 void cpPlugins::OS::DLLManager::
21 GetPluginsLibraryContents(
22 std::map< std::string, std::set< std::string > >& filters,
23 const std::string& libname
26 typedef const char* ( *_TFunction )( );
29 void* hnd = cpPlugins::OS::DLLManager::_Load( libname, RTLD_LAZY );
31 throw std::runtime_error( "Something nasty just happened." );
33 // Get library contents
35 _TFunction descriptors_func = NULL;
39 reinterpret_cast< _TFunction >(
40 cpPlugins::OS::DLLManager::_Sym( hnd, "cpPlugins_LoadedFilters" )
44 if( descriptors_func != NULL )
46 std::string descriptors = descriptors_func( );
47 std::vector< std::string > tokens;
48 cpPlugins::Tokenize( tokens, descriptors, ";" );
49 for( auto t : tokens )
51 std::size_t pos = t.find( ":" );
52 filters[ t.substr( 0, pos ) ].insert( t.substr( pos + 1 ) );
55 cpPlugins::OS::DLLManager::_UnLoad( hnd );
59 cpPlugins::OS::DLLManager::_UnLoad( hnd );
60 throw std::runtime_error(
61 std::string( "Library \"" ) +
63 std::string( "\" not recognized as a cpPlugins library" )
69 // -------------------------------------------------------------------------
70 void* cpPlugins::OS::DLLManager::
71 LoadPlugins( const std::string& lname )
73 return( cpPlugins::OS::DLLManager::_Load( lname, RTLD_NOW | RTLD_GLOBAL ) );
76 // -------------------------------------------------------------------------
77 void* cpPlugins::OS::DLLManager::
79 void* lib_hnd, const std::string& category, const std::string& name
83 cpPlugins::OS::DLLManager::_Sym(
84 lib_hnd, ( category + "_" + name ).c_str( )
89 // -------------------------------------------------------------------------
90 #ifdef cpPlugins_OS_Windows
91 #else // cpPlugins_OS_Windows
101 struct link_struct* ptr;
111 struct link_map* next;
112 struct link_map* prev;
118 #endif // cpPlugins_OS_Windows
120 // -------------------------------------------------------------------------
121 void* cpPlugins::OS::DLLManager::
122 _Load( const std::string& libname, int flags )
124 void* return_handle = NULL;
126 #ifdef cpPlugins_OS_Windows
127 #else // cpPlugins_OS_Windows
128 std::queue< std::string > q;
129 std::set< std::string > m;
134 std::string lname = q.front( );
136 if( m.find( lname ) != m.end( ) )
139 // Get library handle (main program vs. linked libraries)
143 if( lname == libname )
145 h = dlopen( lname.c_str( ), flags );
149 h = dlopen( lname.c_str( ), RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE );
153 h = dlopen( NULL, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE );
157 char* err = dlerror( );
159 throw std::runtime_error( err );
164 link_struct* p = reinterpret_cast< link_struct* >( h )->ptr;
168 link_map* m = reinterpret_cast< link_map* >( p->ptr );
181 #endif // cpPlugins_OS_Windows
182 return( return_handle );
185 // -------------------------------------------------------------------------
186 void* cpPlugins::OS::DLLManager::
187 _Sym( void* hnd, const std::string& symname )
190 #ifdef cpPlugins_OS_Windows
191 #else // cpPlugins_OS_Windows
192 s = dlsym( hnd, symname.c_str( ) );
193 char* err = dlerror( );
195 throw std::runtime_error( err );
196 #endif // cpPlugins_OS_Windows
200 // -------------------------------------------------------------------------
201 void cpPlugins::OS::DLLManager::
204 #ifdef cpPlugins_OS_Windows
205 #else // cpPlugins_OS_Windows
209 char* err = dlerror( );
211 throw std::runtime_error( err );
214 #endif // cpPlugins_OS_Windows
218 void* cpPlugins::OS::DLLManager::
219 Load( const std::string& fname, std::string& error )
222 #ifdef cpPlugins_OS_Windows
223 UINT old = ::SetErrorMode( SEM_FAILCRITICALERRORS );
224 ::SetErrorMode( old | SEM_FAILCRITICALERRORS );
225 hnd = ::LoadLibraryA( fname.c_str( ) );
226 ::SetErrorMode( old );
228 error = "Could not load library.";
229 #else // cpPlugins_OS_Windows
230 hnd = dlopen( fname.c_str( ), RTLD_LAZY | RTLD_GLOBAL );
235 #endif // cpPlugins_OS_Windows
239 // -------------------------------------------------------------------------
240 void cpPlugins::OS::DLLManager::
243 #ifdef cpPlugins_OS_Windows
244 ::FreeLibrary( ( HMODULE )hnd );
245 #else // cpPlugins_OS_Windows
247 #endif // cpPlugins_OS_Windows
250 // -------------------------------------------------------------------------
251 void* cpPlugins::OS::DLLManager::
252 GetFunctionHandle( void* hnd, const std::string& function )
257 #ifdef cpPlugins_OS_Windows
258 f = ::GetProcAddress( ( HMODULE )hnd, function.c_str( ) );
259 #else // cpPlugins_OS_Windows
260 f = dlsym( hnd, function.c_str( ) );
261 #endif // cpPlugins_OS_Windows