]>
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 | |
12028905 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
af49c4b8 | 16 | # pragma interface "dynlib.h" |
7a4b9130 GL |
17 | #endif |
18 | ||
2ecf902b | 19 | #include "wx/defs.h" |
8a0d4cf6 | 20 | |
1948bb32 | 21 | #if wxUSE_DYNLIB_CLASS |
8a0d4cf6 | 22 | |
ed58dbea | 23 | #include "wx/string.h" |
defbed48 | 24 | #include "wx/dynarray.h" |
7a4b9130 | 25 | |
1a787c5d | 26 | #if defined(__WXPM__) || defined(__EMX__) |
0872eaf9 | 27 | #include "wx/os2/private.h" |
1948bb32 VS |
28 | #endif |
29 | ||
30 | #ifdef __WXMSW__ | |
31 | #include "wx/msw/private.h" | |
32 | #endif | |
33 | ||
da55d064 VZ |
34 | #if defined(HAVE_DLERROR) && !defined(__EMX__) |
35 | #define wxHAVE_DYNLIB_ERROR | |
36 | #endif | |
37 | ||
defbed48 VZ |
38 | class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator; |
39 | ||
1948bb32 VS |
40 | // ---------------------------------------------------------------------------- |
41 | // conditional compilation | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
4104ed92 VZ |
44 | // Note: WXPM/EMX has to be tested first, since we want to use |
45 | // native version, even if configure detected presence of DLOPEN. | |
1948bb32 | 46 | #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__) |
4104ed92 | 47 | typedef HMODULE wxDllType; |
1a787c5d | 48 | #elif defined(HAVE_DLOPEN) |
4104ed92 VZ |
49 | #include <dlfcn.h> |
50 | typedef void *wxDllType; | |
8a0d4cf6 | 51 | #elif defined(HAVE_SHL_LOAD) |
4104ed92 VZ |
52 | #include <dl.h> |
53 | typedef shl_t wxDllType; | |
f11bdd03 | 54 | #elif defined(__DARWIN__) |
4104ed92 | 55 | typedef void *wxDllType; |
7b0bfbb2 | 56 | #elif defined(__WXMAC__) |
609533d5 | 57 | #include <CodeFragments.h> |
4104ed92 | 58 | typedef CFragConnectionID wxDllType; |
7b0bfbb2 | 59 | #else |
4104ed92 | 60 | #error "Dynamic Loading classes can't be compiled on this platform, sorry." |
1948bb32 VS |
61 | #endif |
62 | ||
4104ed92 VZ |
63 | // ---------------------------------------------------------------------------- |
64 | // constants | |
65 | // ---------------------------------------------------------------------------- | |
1948bb32 VS |
66 | |
67 | enum wxDLFlags | |
68 | { | |
69 | wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use | |
e62e17d7 | 70 | // (only works on some Unix versions) |
1948bb32 | 71 | wxDL_NOW = 0x00000002, // resolve undefined symbols on load |
e62e17d7 | 72 | // (default, always the case under Win32) |
1948bb32 VS |
73 | wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently |
74 | // loaded libs. | |
e62e17d7 | 75 | wxDL_VERBATIM = 0x00000008, // attempt to load the supplied library |
1948bb32 VS |
76 | // name without appending the usual dll |
77 | // filename extension. | |
1948bb32 | 78 | wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded |
e62e17d7 | 79 | // (only for wxPluginManager) |
1948bb32 | 80 | |
e62e17d7 | 81 | wxDL_DEFAULT = wxDL_NOW // default flags correspond to Win32 |
1948bb32 VS |
82 | }; |
83 | ||
84 | enum wxDynamicLibraryCategory | |
85 | { | |
86 | wxDL_LIBRARY, // standard library | |
13e51f3a | 87 | wxDL_MODULE // loadable module/plugin |
1948bb32 VS |
88 | }; |
89 | ||
90 | enum wxPluginCategory | |
91 | { | |
92 | wxDL_PLUGIN_GUI, // plugin that uses GUI classes | |
13e51f3a | 93 | wxDL_PLUGIN_BASE // wxBase-only plugin |
1948bb32 VS |
94 | }; |
95 | ||
4104ed92 VZ |
96 | // ---------------------------------------------------------------------------- |
97 | // macros | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | // when loading a function from a DLL you always have to cast the returned | |
101 | // "void *" pointer to the correct type and, even more annoyingly, you have to | |
102 | // repeat this type twice if you want to declare and define a function pointer | |
103 | // all in one line | |
104 | // | |
105 | // this macro makes this slightly less painful by allowing you to specify the | |
106 | // type only once, as the first parameter, and creating a variable of this type | |
107 | // called "pfn<name>" initialized with the "name" from the "dynlib" | |
108 | #define wxDYNLIB_FUNCTION(type, name, dynlib) \ | |
109 | type pfn ## name = (type)(dynlib).GetSymbol(_T(#name)) | |
110 | ||
defbed48 VZ |
111 | // ---------------------------------------------------------------------------- |
112 | // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | class WXDLLIMPEXP_BASE wxDynamicLibraryDetails | |
116 | { | |
117 | public: | |
118 | // ctor, normally never used as these objects are only created by | |
119 | // wxDynamicLibrary::ListLoaded() | |
120 | wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; } | |
121 | ||
122 | // get the (base) name | |
123 | wxString GetName() const { return m_name; } | |
124 | ||
125 | // get the full path of this object | |
126 | wxString GetPath() const { return m_path; } | |
127 | ||
128 | // get the load address and the extent, return true if this information is | |
129 | // available | |
130 | bool GetAddress(void **addr, size_t *len) const | |
131 | { | |
132 | if ( !m_address ) | |
133 | return false; | |
134 | ||
135 | if ( addr ) | |
136 | *addr = m_address; | |
137 | if ( len ) | |
138 | *len = m_length; | |
139 | ||
140 | return true; | |
141 | } | |
142 | ||
143 | // return the version of the DLL (may be empty if no version info) | |
144 | wxString GetVersion() const | |
145 | { | |
146 | return m_version; | |
147 | } | |
148 | ||
149 | private: | |
150 | wxString m_name, | |
151 | m_path, | |
152 | m_version; | |
153 | ||
154 | void *m_address; | |
155 | size_t m_length; | |
156 | ||
157 | friend class wxDynamicLibraryDetailsCreator; | |
158 | }; | |
159 | ||
160 | WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails, | |
161 | wxDynamicLibraryDetailsArray, | |
162 | WXDLLIMPEXP_BASE); | |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // wxDynamicLibrary: represents a handle to a DLL/shared object | |
166 | // ---------------------------------------------------------------------------- | |
1948bb32 VS |
167 | |
168 | class WXDLLIMPEXP_BASE wxDynamicLibrary | |
169 | { | |
170 | public: | |
4104ed92 VZ |
171 | // return a valid handle for the main program itself or NULL if back |
172 | // linking is not supported by the current platform (e.g. Win32) | |
1948bb32 VS |
173 | static wxDllType GetProgramHandle(); |
174 | ||
4104ed92 | 175 | // return the platform standard DLL extension (with leading dot) |
1948bb32 VS |
176 | static const wxChar *GetDllExt() { return ms_dllext; } |
177 | ||
4104ed92 | 178 | wxDynamicLibrary() : m_handle(0) { } |
1948bb32 VS |
179 | wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT) |
180 | : m_handle(0) | |
181 | { | |
182 | Load(libname, flags); | |
183 | } | |
1948bb32 | 184 | |
4104ed92 VZ |
185 | // NOTE: this class is (deliberately) not virtual, do not attempt |
186 | // to use it polymorphically. | |
187 | ~wxDynamicLibrary() { Unload(); } | |
1948bb32 | 188 | |
68379eaf | 189 | // return true if the library was loaded successfully |
1948bb32 VS |
190 | bool IsLoaded() const { return m_handle != 0; } |
191 | ||
4104ed92 | 192 | // load the library with the given name (full or not), return true if ok |
defbed48 | 193 | bool Load(const wxString& libname, int flags = wxDL_DEFAULT); |
1948bb32 | 194 | |
defbed48 VZ |
195 | // raw function for loading dynamic libs: always behaves as if |
196 | // wxDL_VERBATIM were specified and doesn't log error message if the | |
197 | // library couldn't be loaded but simply returns NULL | |
da55d064 VZ |
198 | static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT); |
199 | ||
4104ed92 VZ |
200 | // detach the library object from its handle, i.e. prevent the object from |
201 | // unloading the library in its dtor -- the caller is now responsible for | |
202 | // doing this | |
1948bb32 VS |
203 | wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; } |
204 | ||
169948a0 VZ |
205 | // unload the given library handle (presumably returned by Detach() before) |
206 | static void Unload(wxDllType handle); | |
207 | ||
4104ed92 | 208 | // unload the library, also done automatically in dtor |
169948a0 | 209 | void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } } |
1948bb32 | 210 | |
4104ed92 | 211 | // Return the raw handle from dlopen and friends. |
1948bb32 VS |
212 | wxDllType GetLibHandle() const { return m_handle; } |
213 | ||
a018a119 VZ |
214 | // check if the given symbol is present in the library, useful to verify if |
215 | // a loadable module is our plugin, for example, without provoking error | |
216 | // messages from GetSymbol() | |
217 | bool HasSymbol(const wxString& name) const | |
218 | { | |
219 | bool ok; | |
220 | DoGetSymbol(name, &ok); | |
221 | return ok; | |
222 | } | |
223 | ||
4104ed92 VZ |
224 | // resolve a symbol in a loaded DLL, such as a variable or function name. |
225 | // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to | |
226 | // export unmangled names) | |
227 | // | |
228 | // Since it is perfectly valid for the returned symbol to actually be NULL, | |
229 | // that is not always indication of an error. Pass and test the parameter | |
230 | // 'success' for a true indication of success or failure to load the | |
231 | // symbol. | |
232 | // | |
233 | // Returns a pointer to the symbol on success, or NULL if an error occurred | |
234 | // or the symbol wasn't found. | |
defbed48 | 235 | void *GetSymbol(const wxString& name, bool *success = NULL) const; |
1948bb32 | 236 | |
defbed48 VZ |
237 | // low-level version of GetSymbol() |
238 | static void *RawGetSymbol(wxDllType handle, const wxString& name); | |
239 | void *RawGetSymbol(const wxString& name) const | |
240 | { | |
fd6c9428 SN |
241 | #if defined (__WXPM__) || defined(__EMX__) |
242 | return GetSymbol(name); | |
243 | #else | |
defbed48 | 244 | return RawGetSymbol(m_handle, name); |
fd6c9428 | 245 | #endif |
defbed48 VZ |
246 | } |
247 | ||
93ed8ff7 VZ |
248 | #ifdef __WXMSW__ |
249 | // this function is useful for loading functions from the standard Windows | |
250 | // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or | |
251 | // wide character build) suffix if they take string parameters | |
252 | static void *RawGetSymbolAorW(wxDllType handle, const wxString& name) | |
253 | { | |
254 | return RawGetSymbol | |
255 | ( | |
256 | handle, | |
257 | name + | |
258 | #if wxUSE_UNICODE | |
259 | L'W' | |
260 | #else | |
261 | 'A' | |
262 | #endif | |
263 | ); | |
264 | } | |
265 | ||
266 | void *GetSymbolAorW(const wxString& name) const | |
267 | { | |
268 | return RawGetSymbolAorW(m_handle, name); | |
269 | } | |
270 | #endif // __WXMSW__ | |
271 | ||
defbed48 VZ |
272 | // return all modules/shared libraries in the address space of this process |
273 | // | |
3103e8a9 | 274 | // returns an empty array if not implemented or an error occurred |
defbed48 | 275 | static wxDynamicLibraryDetailsArray ListLoaded(); |
0c32066b | 276 | |
1948bb32 VS |
277 | // return platform-specific name of dynamic library with proper extension |
278 | // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux) | |
279 | static wxString CanonicalizeName(const wxString& name, | |
280 | wxDynamicLibraryCategory cat = wxDL_LIBRARY); | |
281 | ||
77ffb593 | 282 | // return name of wxWidgets plugin (adds compiler and version info |
1948bb32 | 283 | // to the filename): |
169948a0 VZ |
284 | static wxString |
285 | CanonicalizePluginName(const wxString& name, | |
286 | wxPluginCategory cat = wxDL_PLUGIN_GUI); | |
1948bb32 VS |
287 | |
288 | // return plugin directory on platforms where it makes sense and empty | |
289 | // string on others: | |
290 | static wxString GetPluginsDirectory(); | |
291 | ||
1948bb32 | 292 | |
4104ed92 VZ |
293 | #if WXWIN_COMPATIBILITY_2_2 |
294 | operator bool() const { return IsLoaded(); } | |
295 | #endif | |
1948bb32 | 296 | |
4104ed92 | 297 | protected: |
defbed48 | 298 | // common part of GetSymbol() and HasSymbol() |
a018a119 VZ |
299 | void *DoGetSymbol(const wxString& name, bool *success = 0) const; |
300 | ||
da55d064 VZ |
301 | #ifdef wxHAVE_DYNLIB_ERROR |
302 | // log the error after a dlxxx() function failure | |
303 | static void Error(); | |
304 | #endif // wxHAVE_DYNLIB_ERROR | |
305 | ||
a018a119 | 306 | |
4104ed92 | 307 | // platform specific shared lib suffix. |
1948bb32 VS |
308 | static const wxChar *ms_dllext; |
309 | ||
4104ed92 | 310 | // the handle to DLL or NULL |
1948bb32 VS |
311 | wxDllType m_handle; |
312 | ||
4104ed92 VZ |
313 | // no copy ctor/assignment operators (or we'd try to unload the library |
314 | // twice) | |
1948bb32 VS |
315 | DECLARE_NO_COPY_CLASS(wxDynamicLibrary) |
316 | }; | |
317 | ||
318 | ||
a585ca5c | 319 | // ---------------------------------------------------------------------------- |
33a8563e | 320 | // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code |
a585ca5c | 321 | // ---------------------------------------------------------------------------- |
3c1a88d8 | 322 | |
1948bb32 VS |
323 | #if WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER |
324 | ||
f3cb0d85 VZ |
325 | #include "wx/object.h" |
326 | ||
33a8563e VZ |
327 | /* |
328 | wxDllLoader is a class providing an interface similar to unix's dlopen(). | |
329 | It is used by wxDynamicLibrary wxLibrary and manages the actual loading of | |
330 | DLLs and the resolving of symbols in them. There are no instances of this | |
331 | class, it simply serves as a namespace for its static member functions. | |
a585ca5c | 332 | */ |
bddd7a8d | 333 | class WXDLLIMPEXP_BASE wxDllLoader |
a585ca5c | 334 | { |
3c1a88d8 | 335 | public: |
33a8563e VZ |
336 | /* |
337 | This function loads the shared library libname into memory. | |
338 | ||
339 | libname may be either the full path to the file or just the filename in | |
340 | which case the library is searched for in all standard locations | |
341 | (use GetDllExt() to construct the filename) | |
342 | ||
68379eaf WS |
343 | if success pointer is not NULL, it will be filled with true if everything |
344 | went ok and false otherwise | |
33a8563e | 345 | */ |
1948bb32 | 346 | static wxDllType LoadLibrary(const wxString& name, bool *success = NULL); |
68379eaf | 347 | |
33a8563e VZ |
348 | /* |
349 | This function unloads the shared library previously loaded with | |
350 | LoadLibrary | |
3c1a88d8 | 351 | */ |
3c1a88d8 | 352 | static void UnloadLibrary(wxDllType dll); |
33a8563e VZ |
353 | |
354 | /* | |
355 | This function returns a valid handle for the main program | |
356 | itself or NULL if back linking is not supported by the current platform | |
357 | (e.g. Win32). | |
358 | */ | |
1948bb32 | 359 | static wxDllType GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); } |
33a8563e VZ |
360 | |
361 | /* | |
362 | This function resolves a symbol in a loaded DLL, such as a | |
363 | variable or function name. | |
364 | ||
365 | dllHandle Handle of the DLL, as returned by LoadDll(). | |
366 | name Name of the symbol. | |
367 | ||
368 | Returns the pointer to the symbol or NULL on error. | |
3c1a88d8 | 369 | */ |
1948bb32 | 370 | static void *GetSymbol(wxDllType dllHandle, const wxString &name, bool *success = 0); |
3c1a88d8 | 371 | |
f6bcfd97 | 372 | // return the standard DLL extension (with leading dot) for this platform |
1948bb32 | 373 | static wxString GetDllExt() { return wxDynamicLibrary::GetDllExt(); } |
f6bcfd97 | 374 | |
3c1a88d8 | 375 | private: |
181b4068 | 376 | |
1948bb32 | 377 | wxDllLoader(); // forbid construction of objects |
33a8563e | 378 | }; |
181b4068 | 379 | |
1948bb32 | 380 | |
7b0bfbb2 | 381 | // ---------------------------------------------------------------------------- |
7a4b9130 | 382 | // wxLibrary |
7b0bfbb2 | 383 | // ---------------------------------------------------------------------------- |
7a4b9130 | 384 | |
1948bb32 VS |
385 | #include "wx/hash.h" |
386 | ||
bddd7a8d | 387 | class WXDLLIMPEXP_BASE wxLibrary : public wxObject |
7b0bfbb2 | 388 | { |
7b0bfbb2 | 389 | public: |
8a0d4cf6 | 390 | wxLibrary(wxDllType handle); |
33a8563e | 391 | virtual ~wxLibrary(); |
7a4b9130 | 392 | |
7b0bfbb2 VZ |
393 | // Get a symbol from the dynamic library |
394 | void *GetSymbol(const wxString& symbname); | |
7a4b9130 | 395 | |
7b0bfbb2 VZ |
396 | // Create the object whose classname is "name" |
397 | wxObject *CreateObject(const wxString& name); | |
7a4b9130 | 398 | |
7b0bfbb2 VZ |
399 | protected: |
400 | void PrepareClasses(wxClassInfo *first); | |
f4a8c29f | 401 | |
7b0bfbb2 | 402 | wxDllType m_handle; |
a585ca5c | 403 | |
33a8563e VZ |
404 | public: |
405 | wxHashTable classTable; | |
406 | }; | |
a585ca5c | 407 | |
7b0bfbb2 | 408 | // ---------------------------------------------------------------------------- |
7a4b9130 | 409 | // wxLibraries |
7b0bfbb2 VZ |
410 | // ---------------------------------------------------------------------------- |
411 | ||
bddd7a8d | 412 | class WXDLLIMPEXP_BASE wxLibraries |
7b0bfbb2 VZ |
413 | { |
414 | public: | |
415 | wxLibraries(); | |
416 | ~wxLibraries(); | |
7a4b9130 | 417 | |
7b0bfbb2 VZ |
418 | // caller is responsible for deleting the returned pointer if !NULL |
419 | wxLibrary *LoadLibrary(const wxString& basename); | |
7a4b9130 | 420 | |
7b0bfbb2 VZ |
421 | wxObject *CreateObject(const wxString& name); |
422 | ||
423 | protected: | |
424 | wxList m_loaded; | |
7a4b9130 GL |
425 | }; |
426 | ||
7b0bfbb2 | 427 | // ---------------------------------------------------------------------------- |
7a4b9130 | 428 | // Global variables |
7b0bfbb2 | 429 | // ---------------------------------------------------------------------------- |
7a4b9130 | 430 | |
bddd7a8d | 431 | extern WXDLLIMPEXP_DATA_BASE(wxLibraries) wxTheLibraries; |
7a4b9130 | 432 | |
1948bb32 VS |
433 | #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER |
434 | ||
7b0bfbb2 | 435 | // ---------------------------------------------------------------------------- |
7a4b9130 | 436 | // Interesting defines |
7b0bfbb2 | 437 | // ---------------------------------------------------------------------------- |
7a4b9130 | 438 | |
f4a8c29f | 439 | #define WXDLL_ENTRY_FUNCTION() \ |
ad8b3990 | 440 | extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \ |
1b733817 | 441 | const wxClassInfo *wxGetClassFirst() { \ |
856d2e52 | 442 | return wxClassInfo::GetFirst(); \ |
f4a8c29f | 443 | } |
7a4b9130 | 444 | |
8a0d4cf6 VZ |
445 | #endif // wxUSE_DYNLIB_CLASS |
446 | ||
7b0bfbb2 | 447 | #endif // _WX_DYNLIB_H__ |