-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharedObject.cc
101 lines (80 loc) · 1.55 KB
/
SharedObject.cc
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
#include "SharedObject.h"
#ifndef _WIN32
#include <dlfcn.h>
#include <stdlib.h>
#include <pthread.h>
#endif
SharedObject::SharedObject(const char* fname) : _so_handle(0)
{ }
bool SharedObject::open(const char *fname)
{
#ifdef _WIN32
_so_handle = LoadLibrary(fname);
if (_so_handle == NULL)
{
_error = "SharedObject::open() LoadLibrary failed";
return false;
}
#else
if (fname != 0)
{
_so_handle = dlopen(fname, RTLD_NOW | RTLD_GLOBAL);
setupError();
return _error.empty();
}
else
{
_so_handle = 0;
_error.clear();
}
#endif
return true;
}
void SharedObject::close()
{
#ifdef _WIN32
if (_so_handle)
{
FreeLibrary(_so_handle);
_so_handle = 0;
}
#else
if (_so_handle)
{
dlclose(_so_handle);
_so_handle = 0;
}
#endif
}
bool SharedObject::getSymbol(void** value, const char* keyname)
{
// Get key-symbol/value pair from library
if (value == 0) return false;
#ifdef _WIN32
*value = (void *)GetProcAddress(_so_handle, keyname);
if (*value != 0) return true;
#else
if (_so_handle != 0)
{
*value = dlsym(_so_handle, keyname);
setupError();
if (_error.empty())
{
return true;
}
}
#endif
return false;
}
void SharedObject::setupError()
{
char *error = dlerror();
if (error == 0)
_error.clear();
else
_error = error;
}
const std::string& SharedObject::lastError()
{
return _error;
}