]>
Commit | Line | Data |
---|---|---|
7b0bfbb2 | 1 | ///////////////////////////////////////////////////////////////////////////// |
33a8563e VZ |
2 | // Name: wx/dynlib.h |
3 | // Purpose: Dynamic library loading classes | |
4 | // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik | |
7b0bfbb2 VZ |
5 | // Modified by: |
6 | // Created: 20/07/98 | |
7 | // RCS-ID: $Id$ | |
33a8563e | 8 | // Copyright: (c) 1998 Guilhem Lavaux |
65571936 | 9 | // Licence: wxWindows licence |
7b0bfbb2 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_DYNLIB_H__ |
13 | #define _WX_DYNLIB_H__ | |
7a4b9130 | 14 | |
2ecf902b | 15 | #include "wx/defs.h" |
8a0d4cf6 | 16 | |
1948bb32 | 17 | #if wxUSE_DYNLIB_CLASS |
8a0d4cf6 | 18 | |
ed58dbea | 19 | #include "wx/string.h" |
defbed48 | 20 | #include "wx/dynarray.h" |
7a4b9130 | 21 | |
67d0bba6 VZ |
22 | // note that we have our own dlerror() implementation under Darwin |
23 | #if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__) | |
da55d064 VZ |
24 | #define wxHAVE_DYNLIB_ERROR |
25 | #endif | |
26 | ||
b5dbe15d | 27 | class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator; |
defbed48 | 28 | |
1948bb32 VS |
29 | // ---------------------------------------------------------------------------- |
30 | // conditional compilation | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
55034339 | 33 | // Note: __OS2__/EMX has to be tested first, since we want to use |
4104ed92 | 34 | // native version, even if configure detected presence of DLOPEN. |
55034339 | 35 | #if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__) |
142ae7b3 | 36 | typedef WXHMODULE wxDllType; |
83135a2c DE |
37 | #elif defined(__DARWIN__) |
38 | // Don't include dlfcn.h on Darwin, we may be using our own replacements. | |
39 | typedef void *wxDllType; | |
1a787c5d | 40 | #elif defined(HAVE_DLOPEN) |
4104ed92 VZ |
41 | #include <dlfcn.h> |
42 | typedef void *wxDllType; | |
8a0d4cf6 | 43 | #elif defined(HAVE_SHL_LOAD) |
4104ed92 VZ |
44 | #include <dl.h> |
45 | typedef shl_t wxDllType; | |
7b0bfbb2 | 46 | #elif defined(__WXMAC__) |
609533d5 | 47 | #include <CodeFragments.h> |
4104ed92 | 48 | typedef CFragConnectionID wxDllType; |
7b0bfbb2 | 49 | #else |
4104ed92 | 50 | #error "Dynamic Loading classes can't be compiled on this platform, sorry." |
1948bb32 VS |
51 | #endif |
52 | ||
4104ed92 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // constants | |
55 | // ---------------------------------------------------------------------------- | |
1948bb32 VS |
56 | |
57 | enum wxDLFlags | |
58 | { | |
59 | wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use | |
e62e17d7 | 60 | // (only works on some Unix versions) |
1948bb32 | 61 | wxDL_NOW = 0x00000002, // resolve undefined symbols on load |
e62e17d7 | 62 | // (default, always the case under Win32) |
1948bb32 VS |
63 | wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently |
64 | // loaded libs. | |
e62e17d7 | 65 | wxDL_VERBATIM = 0x00000008, // attempt to load the supplied library |
1948bb32 VS |
66 | // name without appending the usual dll |
67 | // filename extension. | |
e2d4ce7d VZ |
68 | |
69 | // this flag is obsolete, don't use | |
1948bb32 | 70 | wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded |
e62e17d7 | 71 | // (only for wxPluginManager) |
1948bb32 | 72 | |
e2fc2bd5 VZ |
73 | wxDL_QUIET = 0x00000020, // don't log an error if failed to load |
74 | ||
5a9379d7 VZ |
75 | // this flag is dangerous, for internal use of wxMSW only, don't use at all |
76 | // and especially don't use directly, use wxLoadedDLL instead if you really | |
77 | // do need it | |
e2d4ce7d VZ |
78 | wxDL_GET_LOADED = 0x00000040, // Win32 only: return handle of already |
79 | // loaded DLL or NULL otherwise; Unload() | |
80 | // should not be called so don't forget to | |
81 | // Detach() if you use this function | |
82 | ||
e62e17d7 | 83 | wxDL_DEFAULT = wxDL_NOW // default flags correspond to Win32 |
1948bb32 VS |
84 | }; |
85 | ||
86 | enum wxDynamicLibraryCategory | |
87 | { | |
88 | wxDL_LIBRARY, // standard library | |
13e51f3a | 89 | wxDL_MODULE // loadable module/plugin |
1948bb32 VS |
90 | }; |
91 | ||
92 | enum wxPluginCategory | |
93 | { | |
94 | wxDL_PLUGIN_GUI, // plugin that uses GUI classes | |
13e51f3a | 95 | wxDL_PLUGIN_BASE // wxBase-only plugin |
1948bb32 VS |
96 | }; |
97 | ||
4104ed92 VZ |
98 | // ---------------------------------------------------------------------------- |
99 | // macros | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | // when loading a function from a DLL you always have to cast the returned | |
103 | // "void *" pointer to the correct type and, even more annoyingly, you have to | |
104 | // repeat this type twice if you want to declare and define a function pointer | |
105 | // all in one line | |
106 | // | |
107 | // this macro makes this slightly less painful by allowing you to specify the | |
108 | // type only once, as the first parameter, and creating a variable of this type | |
109 | // called "pfn<name>" initialized with the "name" from the "dynlib" | |
110 | #define wxDYNLIB_FUNCTION(type, name, dynlib) \ | |
9a83f860 | 111 | type pfn ## name = (type)(dynlib).GetSymbol(wxT(#name)) |
4104ed92 | 112 | |
420db5a5 | 113 | |
d0edb9da VZ |
114 | // a more convenient function replacing wxDYNLIB_FUNCTION above |
115 | // | |
116 | // it uses the convention that the type of the function is its name suffixed | |
117 | // with "_t" but it doesn't define a variable but just assigns the loaded value | |
118 | // to it and also allows to pass it the prefix to be used instead of hardcoding | |
119 | // "pfn" (the prefix can be "m_" or "gs_pfn" or whatever) | |
e2fc2bd5 VZ |
120 | // |
121 | // notice that this function doesn't generate error messages if the symbol | |
122 | // couldn't be loaded, the caller should generate the appropriate message | |
d0edb9da | 123 | #define wxDL_INIT_FUNC(pfx, name, dynlib) \ |
e2fc2bd5 | 124 | pfx ## name = (name ## _t)(dynlib).RawGetSymbol(#name) |
d0edb9da | 125 | |
d98a58c5 | 126 | #ifdef __WINDOWS__ |
d0edb9da VZ |
127 | |
128 | // same as wxDL_INIT_FUNC() but appends 'A' or 'W' to the function name, see | |
129 | // wxDynamicLibrary::GetSymbolAorW() | |
130 | #define wxDL_INIT_FUNC_AW(pfx, name, dynlib) \ | |
131 | pfx ## name = (name ## _t)(dynlib).GetSymbolAorW(#name) | |
132 | ||
d98a58c5 | 133 | #endif // __WINDOWS__ |
d0edb9da | 134 | |
ced3df77 VZ |
135 | // the following macros can be used to redirect a whole library to a class and |
136 | // check at run-time if the library is present and contains all required | |
137 | // methods | |
138 | // | |
139 | // notice that they are supposed to be used inside a class which has "m_ok" | |
140 | // member variable indicating if the library had been successfully loaded | |
141 | ||
142 | // helper macros constructing the name of the variable storing the function | |
143 | // pointer and the name of its type from the function name | |
144 | #define wxDL_METHOD_NAME(name) m_pfn ## name | |
145 | #define wxDL_METHOD_TYPE(name) name ## _t | |
146 | ||
147 | // parameters are: | |
148 | // - rettype: return type of the function, e.g. "int" | |
149 | // - name: name of the function, e.g. "foo" | |
150 | // - args: function signature in parentheses, e.g. "(int x, int y)" | |
151 | // - argnames: the names of the parameters in parentheses, e.g. "(x, y)" | |
152 | // - defret: the value to return if the library wasn't successfully loaded | |
153 | #define wxDL_METHOD_DEFINE( rettype, name, args, argnames, defret ) \ | |
154 | typedef rettype (* wxDL_METHOD_TYPE(name)) args ; \ | |
155 | wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \ | |
420db5a5 | 156 | rettype name args \ |
ced3df77 | 157 | { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; } |
420db5a5 | 158 | |
ced3df77 VZ |
159 | #define wxDL_VOIDMETHOD_DEFINE( name, args, argnames ) \ |
160 | typedef void (* wxDL_METHOD_TYPE(name)) args ; \ | |
161 | wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \ | |
420db5a5 | 162 | void name args \ |
ced3df77 VZ |
163 | { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; } |
164 | ||
165 | #define wxDL_METHOD_LOAD(lib, name) \ | |
166 | wxDL_METHOD_NAME(name) = \ | |
167 | (wxDL_METHOD_TYPE(name)) lib.GetSymbol(#name, &m_ok); \ | |
168 | if ( !m_ok ) return false | |
420db5a5 | 169 | |
defbed48 VZ |
170 | // ---------------------------------------------------------------------------- |
171 | // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
174 | class WXDLLIMPEXP_BASE wxDynamicLibraryDetails | |
175 | { | |
176 | public: | |
177 | // ctor, normally never used as these objects are only created by | |
178 | // wxDynamicLibrary::ListLoaded() | |
179 | wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; } | |
180 | ||
181 | // get the (base) name | |
182 | wxString GetName() const { return m_name; } | |
183 | ||
184 | // get the full path of this object | |
185 | wxString GetPath() const { return m_path; } | |
186 | ||
187 | // get the load address and the extent, return true if this information is | |
188 | // available | |
189 | bool GetAddress(void **addr, size_t *len) const | |
190 | { | |
191 | if ( !m_address ) | |
192 | return false; | |
193 | ||
194 | if ( addr ) | |
195 | *addr = m_address; | |
196 | if ( len ) | |
197 | *len = m_length; | |
198 | ||
199 | return true; | |
200 | } | |
201 | ||
202 | // return the version of the DLL (may be empty if no version info) | |
203 | wxString GetVersion() const | |
204 | { | |
205 | return m_version; | |
206 | } | |
207 | ||
208 | private: | |
209 | wxString m_name, | |
210 | m_path, | |
211 | m_version; | |
212 | ||
213 | void *m_address; | |
214 | size_t m_length; | |
215 | ||
216 | friend class wxDynamicLibraryDetailsCreator; | |
217 | }; | |
218 | ||
219 | WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails, | |
220 | wxDynamicLibraryDetailsArray, | |
221 | WXDLLIMPEXP_BASE); | |
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // wxDynamicLibrary: represents a handle to a DLL/shared object | |
225 | // ---------------------------------------------------------------------------- | |
1948bb32 VS |
226 | |
227 | class WXDLLIMPEXP_BASE wxDynamicLibrary | |
228 | { | |
229 | public: | |
4104ed92 VZ |
230 | // return a valid handle for the main program itself or NULL if back |
231 | // linking is not supported by the current platform (e.g. Win32) | |
1948bb32 VS |
232 | static wxDllType GetProgramHandle(); |
233 | ||
4104ed92 | 234 | // return the platform standard DLL extension (with leading dot) |
00711afd | 235 | static const wxString& GetDllExt() { return ms_dllext; } |
1948bb32 | 236 | |
4104ed92 | 237 | wxDynamicLibrary() : m_handle(0) { } |
1948bb32 VS |
238 | wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT) |
239 | : m_handle(0) | |
240 | { | |
241 | Load(libname, flags); | |
242 | } | |
1948bb32 | 243 | |
4104ed92 VZ |
244 | // NOTE: this class is (deliberately) not virtual, do not attempt |
245 | // to use it polymorphically. | |
246 | ~wxDynamicLibrary() { Unload(); } | |
1948bb32 | 247 | |
68379eaf | 248 | // return true if the library was loaded successfully |
1948bb32 VS |
249 | bool IsLoaded() const { return m_handle != 0; } |
250 | ||
4104ed92 | 251 | // load the library with the given name (full or not), return true if ok |
defbed48 | 252 | bool Load(const wxString& libname, int flags = wxDL_DEFAULT); |
1948bb32 | 253 | |
defbed48 VZ |
254 | // raw function for loading dynamic libs: always behaves as if |
255 | // wxDL_VERBATIM were specified and doesn't log error message if the | |
256 | // library couldn't be loaded but simply returns NULL | |
da55d064 VZ |
257 | static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT); |
258 | ||
4104ed92 VZ |
259 | // detach the library object from its handle, i.e. prevent the object from |
260 | // unloading the library in its dtor -- the caller is now responsible for | |
261 | // doing this | |
1948bb32 VS |
262 | wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; } |
263 | ||
169948a0 VZ |
264 | // unload the given library handle (presumably returned by Detach() before) |
265 | static void Unload(wxDllType handle); | |
266 | ||
4104ed92 | 267 | // unload the library, also done automatically in dtor |
169948a0 | 268 | void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } } |
1948bb32 | 269 | |
4104ed92 | 270 | // Return the raw handle from dlopen and friends. |
1948bb32 VS |
271 | wxDllType GetLibHandle() const { return m_handle; } |
272 | ||
a018a119 VZ |
273 | // check if the given symbol is present in the library, useful to verify if |
274 | // a loadable module is our plugin, for example, without provoking error | |
275 | // messages from GetSymbol() | |
276 | bool HasSymbol(const wxString& name) const | |
277 | { | |
278 | bool ok; | |
279 | DoGetSymbol(name, &ok); | |
280 | return ok; | |
281 | } | |
282 | ||
4104ed92 VZ |
283 | // resolve a symbol in a loaded DLL, such as a variable or function name. |
284 | // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to | |
285 | // export unmangled names) | |
286 | // | |
287 | // Since it is perfectly valid for the returned symbol to actually be NULL, | |
288 | // that is not always indication of an error. Pass and test the parameter | |
289 | // 'success' for a true indication of success or failure to load the | |
290 | // symbol. | |
291 | // | |
292 | // Returns a pointer to the symbol on success, or NULL if an error occurred | |
293 | // or the symbol wasn't found. | |
defbed48 | 294 | void *GetSymbol(const wxString& name, bool *success = NULL) const; |
1948bb32 | 295 | |
defbed48 VZ |
296 | // low-level version of GetSymbol() |
297 | static void *RawGetSymbol(wxDllType handle, const wxString& name); | |
298 | void *RawGetSymbol(const wxString& name) const | |
299 | { | |
fd6c9428 SN |
300 | #if defined (__WXPM__) || defined(__EMX__) |
301 | return GetSymbol(name); | |
302 | #else | |
defbed48 | 303 | return RawGetSymbol(m_handle, name); |
fd6c9428 | 304 | #endif |
defbed48 VZ |
305 | } |
306 | ||
d98a58c5 | 307 | #ifdef __WINDOWS__ |
93ed8ff7 VZ |
308 | // this function is useful for loading functions from the standard Windows |
309 | // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or | |
310 | // wide character build) suffix if they take string parameters | |
311 | static void *RawGetSymbolAorW(wxDllType handle, const wxString& name) | |
312 | { | |
313 | return RawGetSymbol | |
314 | ( | |
315 | handle, | |
55034339 | 316 | name + |
93ed8ff7 VZ |
317 | #if wxUSE_UNICODE |
318 | L'W' | |
319 | #else | |
320 | 'A' | |
321 | #endif | |
322 | ); | |
323 | } | |
324 | ||
325 | void *GetSymbolAorW(const wxString& name) const | |
326 | { | |
327 | return RawGetSymbolAorW(m_handle, name); | |
328 | } | |
d98a58c5 | 329 | #endif // __WINDOWS__ |
93ed8ff7 | 330 | |
defbed48 VZ |
331 | // return all modules/shared libraries in the address space of this process |
332 | // | |
3103e8a9 | 333 | // returns an empty array if not implemented or an error occurred |
defbed48 | 334 | static wxDynamicLibraryDetailsArray ListLoaded(); |
0c32066b | 335 | |
1948bb32 VS |
336 | // return platform-specific name of dynamic library with proper extension |
337 | // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux) | |
338 | static wxString CanonicalizeName(const wxString& name, | |
339 | wxDynamicLibraryCategory cat = wxDL_LIBRARY); | |
340 | ||
77ffb593 | 341 | // return name of wxWidgets plugin (adds compiler and version info |
1948bb32 | 342 | // to the filename): |
169948a0 VZ |
343 | static wxString |
344 | CanonicalizePluginName(const wxString& name, | |
345 | wxPluginCategory cat = wxDL_PLUGIN_GUI); | |
1948bb32 VS |
346 | |
347 | // return plugin directory on platforms where it makes sense and empty | |
348 | // string on others: | |
349 | static wxString GetPluginsDirectory(); | |
350 | ||
1948bb32 | 351 | |
d98a58c5 | 352 | #ifdef __WINDOWS__ |
c118a476 VZ |
353 | // return the handle (HMODULE/HINSTANCE) of the DLL with the given name |
354 | // and/or containing the specified address: for XP and later systems only | |
355 | // the address is used and the name is ignored but for the previous systems | |
356 | // only the name (which may be either a full path to the DLL or just its | |
357 | // base name, possibly even without extension) is used | |
358 | // | |
359 | // the returned handle reference count is not incremented so it doesn't | |
360 | // need to be freed using FreeLibrary() but it also means that it can | |
361 | // become invalid if the DLL is unloaded | |
142ae7b3 | 362 | static WXHMODULE MSWGetModuleHandle(const char *name, void *addr); |
d98a58c5 | 363 | #endif // __WINDOWS__ |
c118a476 | 364 | |
4104ed92 | 365 | protected: |
defbed48 | 366 | // common part of GetSymbol() and HasSymbol() |
a018a119 VZ |
367 | void *DoGetSymbol(const wxString& name, bool *success = 0) const; |
368 | ||
da55d064 VZ |
369 | #ifdef wxHAVE_DYNLIB_ERROR |
370 | // log the error after a dlxxx() function failure | |
371 | static void Error(); | |
372 | #endif // wxHAVE_DYNLIB_ERROR | |
373 | ||
a018a119 | 374 | |
4104ed92 | 375 | // platform specific shared lib suffix. |
00711afd | 376 | static const wxString ms_dllext; |
1948bb32 | 377 | |
4104ed92 | 378 | // the handle to DLL or NULL |
1948bb32 VS |
379 | wxDllType m_handle; |
380 | ||
4104ed92 VZ |
381 | // no copy ctor/assignment operators (or we'd try to unload the library |
382 | // twice) | |
c0c133e1 | 383 | wxDECLARE_NO_COPY_CLASS(wxDynamicLibrary); |
1948bb32 VS |
384 | }; |
385 | ||
d98a58c5 | 386 | #ifdef __WINDOWS__ |
5a9379d7 VZ |
387 | |
388 | // ---------------------------------------------------------------------------- | |
389 | // wxLoadedDLL is a MSW-only internal helper class allowing to dynamically bind | |
390 | // to a DLL already loaded into the project address space | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
393 | class wxLoadedDLL : public wxDynamicLibrary | |
394 | { | |
395 | public: | |
396 | wxLoadedDLL(const wxString& dllname) | |
397 | : wxDynamicLibrary(dllname, wxDL_GET_LOADED | wxDL_VERBATIM | wxDL_QUIET) | |
398 | { | |
399 | } | |
400 | ||
401 | ~wxLoadedDLL() | |
402 | { | |
403 | Detach(); | |
404 | } | |
405 | }; | |
406 | ||
d98a58c5 | 407 | #endif // __WINDOWS__ |
1948bb32 | 408 | |
7b0bfbb2 | 409 | // ---------------------------------------------------------------------------- |
7a4b9130 | 410 | // Interesting defines |
7b0bfbb2 | 411 | // ---------------------------------------------------------------------------- |
7a4b9130 | 412 | |
f4a8c29f | 413 | #define WXDLL_ENTRY_FUNCTION() \ |
ad8b3990 | 414 | extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \ |
1b733817 | 415 | const wxClassInfo *wxGetClassFirst() { \ |
856d2e52 | 416 | return wxClassInfo::GetFirst(); \ |
f4a8c29f | 417 | } |
7a4b9130 | 418 | |
8a0d4cf6 VZ |
419 | #endif // wxUSE_DYNLIB_CLASS |
420 | ||
7b0bfbb2 | 421 | #endif // _WX_DYNLIB_H__ |