LAMA
|
00001 00034 #ifndef LAMA_SHARED_LIB_HPP_ 00035 #define LAMA_SHARED_LIB_HPP_ 00036 00037 #ifdef WIN32 00038 #include <Windows.h> 00039 #define LAMA_LIB_HANDLE_TYPE HINSTANCE 00040 #ifdef max 00041 #undef max 00042 #endif 00043 #else 00044 #include <dlfcn.h> 00045 #define LAMA_LIB_HANDLE_TYPE void* 00046 #endif //WIN32 00047 00048 #include <string> 00049 #include <vector> 00050 #include <sstream> 00051 #include <iostream> 00052 00053 template<typename FunctionHandleType> 00054 int loadLibAndGetFunctionHandle( FunctionHandleType& functionHandle, LAMA_LIB_HANDLE_TYPE& handle, const char* const filename, const char* const functionName ) 00055 { 00056 #ifdef WIN32 00057 handle = LoadLibrary( filename ); 00058 if( !handle ) 00059 { 00060 std::stringstream message; 00061 message<<"Cannot load library: "<<filename<<", because LoadLibrary failed with: "<<GetLastError(); 00062 std::cout << message.str(); 00063 return 1; 00064 } 00065 00066 functionHandle = ( FunctionHandleType ) GetProcAddress ( handle , functionName ) ; 00067 00068 if( !functionHandle ) 00069 { 00070 std::stringstream message; 00071 message<<"Cannot load symbol '"<<functionName<<"' from '" 00072 <<filename<<"': "<<GetLastError(); 00073 std::cout << message.str(); 00074 FreeLibrary( handle ); 00075 return 1; 00076 } 00077 #else 00078 // load library 00079 handle = dlopen( filename,RTLD_LAZY|RTLD_GLOBAL ); 00080 if( !handle ) 00081 { 00082 std::stringstream message; 00083 message<<"Cannot load library: "<<dlerror( ); 00084 std::cout << message.str( ) ; 00085 return 1; 00086 } 00087 dlerror( ); 00088 00089 // getting function getInputSetRegistry( ) from loaded library. 00090 functionHandle = ( FunctionHandleType ) dlsym( handle,functionName ); 00091 00092 char* dlsym_error = NULL; 00093 if( ( dlsym_error=dlerror( ) )!=NULL ) 00094 { 00095 std::stringstream message; 00096 message<<"Cannot load symbol '"<<functionName<<"' from '" 00097 <<filename<<"': "<<dlsym_error; 00098 std::cout << message.str( ); 00099 dlclose( handle ); 00100 return 1; 00101 } 00102 #endif //WIN32 00103 return 0; 00104 } 00105 00106 template<typename FunctionHandleType> 00107 int getFunctionHandle( FunctionHandleType& functionHandle, LAMA_LIB_HANDLE_TYPE& handle, const char* const functionName ) 00108 { 00109 #ifdef WIN32 00110 functionHandle = ( FunctionHandleType ) GetProcAddress ( handle , functionName ) ; 00111 00112 if( !functionHandle ) 00113 { 00114 std::stringstream message; 00115 message<<"Cannot load symbol '"<<functionName<<"': "<<GetLastError(); 00116 std::cout << message.str( ); 00117 FreeLibrary( handle ); 00118 return 1; 00119 } 00120 #else 00121 // getting function getInputSetRegistry( ) from loaded library. 00122 functionHandle = ( FunctionHandleType ) dlsym( handle,functionName ); 00123 00124 char* dlsym_error = NULL; 00125 if( ( dlsym_error=dlerror( ) )!=NULL ) 00126 { 00127 std::stringstream message; 00128 message<<"Cannot load symbol '"<<functionName<<"': "<<dlsym_error; 00129 std::cout << message.str( ); 00130 dlclose( handle ); 00131 return 1; 00132 } 00133 #endif //WIN32 00134 return 0; 00135 00136 } 00137 00138 inline void freeLibHandle( LAMA_LIB_HANDLE_TYPE handle ) 00139 { 00140 #ifdef WIN32 00141 FreeLibrary( handle ); 00142 #else 00143 dlclose( handle ); 00144 #endif //WIN32 00145 } 00146 00147 #endif // LAMA_SHARED_LIB_HPP_