DCL 4.0
Loading...
Searching...
No Matches
Dll.cpp
Go to the documentation of this file.
1
2#include <dcl/Config.h>
3
4#if __DCL_WINDOWS
5 #include <windows.h>
6#else
7 #include <dcl/_dlfcn.h>
8#endif
9
10#include <dcl/Object.h>
11#include <dcl/Charset.h>
12
13#if __DCL_HAVE_ALLOC_DEBUG
14#undef __DCL_ALLOC_LEVEL
15#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
16#endif
17
18#include <dcl/Files.h>
19#include <dcl/Dll.h>
20
21#if __DCL_HAVE_THIS_FILE__
22#undef __THIS_FILE__
23static const char_t __THIS_FILE__[] = __T("dcl/Dll.cpp");
24#endif
25
26__DCL_BEGIN_NAMESPACE
27
29
30DllException::DllException(const String& _name, Exception* _cause)
31: SysError(_cause)
32{
33 __message = _name;
34}
35
36DllException::DllException(const String& _name, uint32_t _errorNo)
37 : SysError(_errorNo)
38{
39 __message = _name;
40}
41
42DllException::DllException(const String& _name, const String& _message)
43{
44 __message = _name + __T(" ") + _message;
45}
46
48{
49 StringBuilder r = __message;
50 if (SysError::errorNo() != 0)
51 r += __T(" ") + SysError::toString();
52
53 return r;
54}
55
57
58Dll::Dll()
59{
60 __handle = NULL;
61}
62
63Dll::~Dll()
64{
65 if (__handle) {
66#if __DCL_DEBUG
67 try {
68 close();
69 }
70 catch (DllException* e) {
71 __DCL_TRACE1(L"Warning Dll::close fail! %ls\n",
72 e->toString().data());
73 e->destroy();
74 }
75#else
76 close();
77#endif
78 }
79}
80
81void Dll::open(
82 const String& _filename, int _flags /* = DEFAULT */
84{
85 __DCL_ASSERT(__handle == NULL);
86 __DCL_ASSERT(!_filename.isEmpty());
87
88#if __DCL_WINDOWS
89 HINSTANCE handle = LoadLibraryExW(
90 _filename,
91 NULL,
92 LOAD_WITH_ALTERED_SEARCH_PATH
93 );
94
95 if (handle == NULL)
96 throw new DllException(_filename, WINAPI_ERROR(GetLastError()));
97
98#else
99 if (_filename[(size_t) 0] == L'/') {
100 if(!Files::exists(_filename))
101 throw new DllException(_filename, ENOENT);
102 }
103
104 if ((_flags & RTLD_BINDING_MASK) == 0)
105 _flags |= RTLD_NOW;
106
107 void* handle = __dlopen(_filename, _flags);
108 if (handle == NULL) {
109 String msg;
110 __DCL_VERIFY(__dlerror(msg) == 0);
111 throw new DllException(_filename, msg);
112 }
113
114#endif
115 __filename = _filename;
116 __handle = (void*)handle;
117}
118
119void Dll::close()
121{
122 if (__handle) {
123 void* handle = __handle;
124 __handle = NULL;
125
126#if __DCL_WINDOWS
127 if (::FreeLibrary((HINSTANCE) handle) == FALSE)
128 throw new DllException(__filename, WINAPI_ERROR(GetLastError()));
129
130#else
131 if (dlclose(handle) != 0) {
132 String msg;
133 __DCL_VERIFY(__dlerror(msg) == 0);
134 throw new DllException(__filename, msg);
135 }
136
137#endif
138 }
139}
140
141void* Dll::getAddress(const char* _symbol)
143{
144 __DCL_ASSERT(__handle != NULL);
145 __DCL_ASSERT(_symbol != NULL && *_symbol != '\0');
146
147 void* r = NULL;
148
149#if __DCL_WINDOWS
150 r = (void*) GetProcAddress((HINSTANCE)__handle, _symbol);
151 if (r == NULL) {
152 String symbol = AsciiDecoder::decode(_symbol);
153 throw new DllException(
154 __filename + L"::" + symbol,
155 WINAPI_ERROR(GetLastError()));
156 }
157
158#else
159 r = dlsym(__handle, _symbol);
160 if (r == NULL) {
161 String symbol = AsciiDecoder::decode(_symbol);
162 String msg;
163 __DCL_VERIFY(__dlerror(msg) == 0);
164
165 throw new DllException(
166 __filename + L"::" + symbol,
167 msg
168 );
169 }
170
171#endif
172 return r;
173}
174
175__DCL_END_NAMESPACE
DCLCAPI int __dlerror(String &r)
Definition _dlfcn.cpp:32
DCLCAPI void * __dlopen(const String &_filename, int _flag)
Definition _dlfcn.cpp:20
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_THROWS1(e)
Definition Config.h:167
#define WINAPI_ERROR(n)
Definition Exception.h:72
#define FALSE
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:376
#define __DCL_VERIFY(expr)
Definition Object.h:373
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
ByteString r
static String decode(const char *_mbs, size_t _mbslen=(size_t) -1)
DllException(const String &_name, Exception *_cause)
virtual String toString() const
Definition Dll.cpp:47
Definition Dll.h:29
void * __handle
Definition Dll.h:57
String __filename
Definition Dll.h:58
virtual void destroy()
Definition Exception.cpp:74
static bool exists(const String &_path)
Definition Files.cpp:109
uint32_t errorNo() const
Definition Exception.h:78
virtual String toString() const
SysError(Exception *_cause=NULL)