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