2 #ifndef comfrm_LibSupportH
3 #define comfrm_LibSupportH
67 #include "memorytools.h"
75 #define MAC_DLLEXPORT __declspec(dllexport)
76 #define MAC_DLLEXPORT_FROMEXE __declspec(dllimport)
78 #define MAC_DLLEXPORT __declspec(dllimport)
79 #define MAC_DLLEXPORT_FROMEXE __declspec(dllexport)
81 typedef HMODULE DllHandle;
82 #elif defined __linux__
85 #define MAC_DLLEXPORT_FROMEXE
86 typedef void* DllHandle;
109 template<
class TDllHandle>
115 o_dll = LoadLibrary(p_dllname.c_str());
116 #elif defined __linux__
119 std::string dllName(p_dllname);
124 int lastSlashPos = dllName.find_last_of(
'/');
125 if (lastSlashPos != std::string::npos)
127 path = dllName.substr(0, lastSlashPos);
128 libName = dllName.substr(lastSlashPos + 1);
138 if (libName.find_last_of(
'.') == std::string::npos)
141 if (libName.substr(0, 3) !=
"lib")
142 libName =
"lib" + libName;
144 std::string prettyLibName = path +
"/" + libName;
145 o_dll = dlopen(prettyLibName.c_str(),RTLD_LAZY);
149 std::cerr <<
"Не удалось загрузить DLL (-1):" << p_dllname << std::endl;
150 #if defined __linux__
151 std::cerr << dlerror() << std::endl;
166 template<
class FunctionType,
class TDllHandle>
167 FunctionType*
loadFunction(TDllHandle p_dll,
const std::string& p_regfunc)
169 FunctionType* reg = NULL;
171 reg = (FunctionType*) GetProcAddress(p_dll, p_regfunc.c_str());
172 #elif defined __linux__
173 reg = (FunctionType*) dlsym(p_dll, p_regfunc.c_str());
177 std::cerr <<
"Не удалось найти функцию:" << p_regfunc << std::endl;
178 #if defined __linux__
179 std::cerr << dlerror() << std::endl;
194 template<
class FunctionType,
class TDllHandle >
195 FunctionType*
loadFunction(
const std::string& p_dllname,
const std::string& p_regfunc,
198 int res = loadLibrary< TDllHandle >(p_dllname, o_dll);
201 return loadFunction< FunctionType >(o_dll, p_regfunc);
208 template<
class TDllHandle >
213 #elif defined __linux__
224 template<
class OutClass >
227 typedef OutClass* ftyp1(
const char*);
228 typedef OutClass* ftyp2();
231 if (p_input_file !=
"")
232 return ((ftyp1*) p_func)(p_input_file.c_str());
234 return ((ftyp2*) p_func)();
248 template<
class OutClass,
class TDllHandle >
249 OutClass*
registerObject(
const std::string& p_dllname,
const std::string& p_regfunc,
250 TDllHandle& o_dll,
const std::string& p_input_file =
"")
252 void* reg = loadFunction< void, TDllHandle >(p_dllname, p_regfunc, o_dll);
255 return registerObject< OutClass >(reg, p_input_file);
266 template<
class OutClass,
class InputParam,
class TDllHandle >
268 TDllHandle& o_dll,
const InputParam& p_input_data)
270 typedef OutClass* ftyp1(
const InputParam&);
271 ftyp1* reg = loadFunction< ftyp1, TDllHandle >(p_dllname, p_regfunc, o_dll);
273 return reg(p_input_data);
283 template<
class OutClass,
class TDllHandle >
291 #elif defined __linux__
301 template<
class TDllHandle >
318 int res = loadLibrary< TDllHandle >(p_dll_name, mDLL);
324 template<
typename OutObjectType >
325 int createObject(
const char* p_reg_func_name, OutObjectType*& o_obj,
const std::string& p_input_file =
"")
327 void* reg = loadFunction< void >(mDLL, p_reg_func_name);
328 o_obj = registerObject< OutObjectType >(reg, p_input_file);
OutClass * registerObject(void *p_func, const std::string &p_input_file="")
Definition: libtools.h:225
int loadLibrary(const std::string &p_dllname, TDllHandle &o_dll)
Definition: libtools.h:110
OutClass * registerQualifiedObject(const std::string &p_dllname, const std::string &p_regfunc, TDllHandle &o_dll, const InputParam &p_input_data)
Definition: libtools.h:267
void unregisterObject(OutClass *p_obj, TDllHandle p_dll)
Definition: libtools.h:284
FunctionType * loadFunction(TDllHandle p_dll, const std::string &p_regfunc)
Definition: libtools.h:167
#define MAC_MEMRELEASE(A)
Definition: memorytools.h:58
int createObject(const char *p_reg_func_name, OutObjectType *&o_obj, const std::string &p_input_file="")
Definition: libtools.h:325
void unloadLibrary(TDllHandle p_dll)
Definition: libtools.h:209
DllLoader(const std::string &p_dll_name)
Definition: libtools.h:315
Класс для загрузки библиотек и получения объектов из них
Definition: libtools.h:302