-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynfload.h
More file actions
152 lines (133 loc) · 3.29 KB
/
Copy pathdynfload.h
File metadata and controls
152 lines (133 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#ifndef DYNFLOAD_H_GUARD
#define DYNFLOAD_H_GUARD
#include <string>
#if defined(_WIN32)
#include <windows.h>
#elif defined(__unix__)
#include <dlfcn.h>
#else
#define DYNFLOAD_UNSUPPORTED
#endif
#ifndef DYNFLOAD_UNSUPPORTED
namespace DynFLoad
{
class DynamicModule
{
public:
enum
{
LOCAL = 1,
LAZY = 2
};
class Exception: public std::exception
{
public:
Exception(){}
Exception(const char* str):descr(str)
{
#if defined(_WIN32)
LPSTR lpMsgBuf = NULL;
const DWORD res = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&lpMsgBuf),0,NULL );
if (res!=0)
{
descr.append(": ");
descr.append(lpMsgBuf);
::LocalFree(lpMsgBuf);
}
#else
const char* description = dlerror();
if (description)
{
descr.append(": ");
descr.append(description);
}
#endif
}
virtual ~Exception() throw(){}
virtual const char* what() const throw()
{
return descr.c_str();
}
private:
std::string descr;
};
DynamicModule(){}
DynamicModule(const char* path, int flags = 0)
{
load(path, flags);
}
void load(const char * path, int flags = 0)
{
#if defined(_WIN32)
library = ::LoadLibraryExA(path,NULL,flags&LAZY?DONT_RESOLVE_DLL_REFERENCES:0);
#else
library = dlopen(path,(flags&LOCAL==0?0:RTLD_GLOBAL)|(flags&LAZY==0?RTLD_NOW:RTLD_LAZY));
#endif
if (!library) throw Exception("Loading library error");
this->path = path;
}
void unload()
{
if (library)
{
#if defined(_WIN32)
bool err = ::FreeLibrary(library)==FALSE;
#else
bool err = dlclose(library)!=0;
#endif
if (err) throw Exception("Unloading library error");
library = 0;
}
}
bool isLoaded()
{
return library != 0;
}
template<class T>
class Caster
{
public:
Caster(void *ptr) { data.rawptr = ptr; }
T get() { return data.ptr; }
private:
union CastUnion
{
void* rawptr;
T ptr;
} data;
};
template<class T>
T getSymbol(const char* name)
{
if (!library) throw Exception("Library was not loaded");
#if defined(_WIN32)
void* ptr = reinterpret_cast<void*>(::GetProcAddress(library,name));
#else
void* ptr = dlsym(library,name);
#endif
if (!ptr) throw Exception("Loading symbol error");
return Caster<T>(ptr).get();
}
~DynamicModule()
{
try
{
unload();
}
catch(...) {}
}
private:
std::string path;
#if defined(_WIN32)
HMODULE library;
#else
void * library;
#endif
};
}
#endif // not supported
#endif //DYNFLOAD_H_GUARD