]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/OS/DLLManager.cxx
Windows compilation 3/3
[cpPlugins.git] / lib / cpPlugins / OS / DLLManager.cxx
1 #include <cpPlugins/OS/DLLManager.h>
2
3 #ifdef cpPlugins_OS_Windows
4 #  include <Windows.h>
5 #else // cpPlugins_OS_Windows
6 #  include <dlfcn.h>
7 #endif // cpPlugins_OS_Windows
8
9 // -------------------------------------------------------------------------
10 void* cpPlugins::OS::DLLManager::
11 Load( const std::string& fname, std::string& error )
12 {
13   void* hnd = NULL;
14 #ifdef cpPlugins_OS_Windows
15   UINT old = ::SetErrorMode( SEM_FAILCRITICALERRORS );
16   ::SetErrorMode( old | SEM_FAILCRITICALERRORS );
17   hnd = ::LoadLibraryA( fname.c_str( ) );
18   ::SetErrorMode( old );
19   if( hnd == NULL )
20     error = "Could not load library.";
21 #else // cpPlugins_OS_Windows
22   hnd = dlopen( fname.c_str( ), RTLD_LAZY | RTLD_GLOBAL );
23   if( hnd == NULL )
24     error = dlerror( );
25   else
26     dlerror( );
27 #endif // cpPlugins_OS_Windows
28   return( hnd );
29 }
30
31 // -------------------------------------------------------------------------
32 void cpPlugins::OS::DLLManager::
33 UnLoad( void* hnd )
34 {
35 #ifdef cpPlugins_OS_Windows
36   ::FreeLibrary( ( HMODULE )hnd );
37 #else // cpPlugins_OS_Windows
38   dlclose( hnd );
39 #endif // cpPlugins_OS_Windows
40 }
41
42 // -------------------------------------------------------------------------
43 void* cpPlugins::OS::DLLManager::
44 GetFunctionHandle( void* hnd, const std::string& function )
45 {
46   void* f = NULL;
47   if( hnd != NULL )
48   {
49 #ifdef cpPlugins_OS_Windows
50     f = ::GetProcAddress( ( HMODULE )hnd, function.c_str( ) );
51 #else // cpPlugins_OS_Windows
52     f = dlsym( hnd, function.c_str( ) );
53 #endif // cpPlugins_OS_Windows
54   } // fi
55   return( f );
56 }
57
58 // eof - $RCSfile$