]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynlib.h
correct wxIntPtr definition
[wxWidgets.git] / include / wx / dynlib.h
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 wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded
77 // (only for wxPluginManager)
78
79 wxDL_QUIET = 0x00000020, // don't log an error if failed to load
80
81 wxDL_DEFAULT = wxDL_NOW // default flags correspond to Win32
82 };
83
84 enum wxDynamicLibraryCategory
85 {
86 wxDL_LIBRARY, // standard library
87 wxDL_MODULE // loadable module/plugin
88 };
89
90 enum wxPluginCategory
91 {
92 wxDL_PLUGIN_GUI, // plugin that uses GUI classes
93 wxDL_PLUGIN_BASE // wxBase-only plugin
94 };
95
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
111
112 // a more convenient function replacing wxDYNLIB_FUNCTION above
113 //
114 // it uses the convention that the type of the function is its name suffixed
115 // with "_t" but it doesn't define a variable but just assigns the loaded value
116 // to it and also allows to pass it the prefix to be used instead of hardcoding
117 // "pfn" (the prefix can be "m_" or "gs_pfn" or whatever)
118 //
119 // notice that this function doesn't generate error messages if the symbol
120 // couldn't be loaded, the caller should generate the appropriate message
121 #define wxDL_INIT_FUNC(pfx, name, dynlib) \
122 pfx ## name = (name ## _t)(dynlib).RawGetSymbol(#name)
123
124 #ifdef __WXMSW__
125
126 // same as wxDL_INIT_FUNC() but appends 'A' or 'W' to the function name, see
127 // wxDynamicLibrary::GetSymbolAorW()
128 #define wxDL_INIT_FUNC_AW(pfx, name, dynlib) \
129 pfx ## name = (name ## _t)(dynlib).GetSymbolAorW(#name)
130
131 #endif // __WXMSW__
132
133 // the following macros can be used to redirect a whole library to a class and
134 // check at run-time if the library is present and contains all required
135 // methods
136 //
137 // notice that they are supposed to be used inside a class which has "m_ok"
138 // member variable indicating if the library had been successfully loaded
139
140 // helper macros constructing the name of the variable storing the function
141 // pointer and the name of its type from the function name
142 #define wxDL_METHOD_NAME(name) m_pfn ## name
143 #define wxDL_METHOD_TYPE(name) name ## _t
144
145 // parameters are:
146 // - rettype: return type of the function, e.g. "int"
147 // - name: name of the function, e.g. "foo"
148 // - args: function signature in parentheses, e.g. "(int x, int y)"
149 // - argnames: the names of the parameters in parentheses, e.g. "(x, y)"
150 // - defret: the value to return if the library wasn't successfully loaded
151 #define wxDL_METHOD_DEFINE( rettype, name, args, argnames, defret ) \
152 typedef rettype (* wxDL_METHOD_TYPE(name)) args ; \
153 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
154 rettype name args \
155 { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; }
156
157 #define wxDL_VOIDMETHOD_DEFINE( name, args, argnames ) \
158 typedef void (* wxDL_METHOD_TYPE(name)) args ; \
159 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
160 void name args \
161 { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; }
162
163 #define wxDL_METHOD_LOAD(lib, name) \
164 wxDL_METHOD_NAME(name) = \
165 (wxDL_METHOD_TYPE(name)) lib.GetSymbol(#name, &m_ok); \
166 if ( !m_ok ) return false
167
168 // ----------------------------------------------------------------------------
169 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
170 // ----------------------------------------------------------------------------
171
172 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
173 {
174 public:
175 // ctor, normally never used as these objects are only created by
176 // wxDynamicLibrary::ListLoaded()
177 wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; }
178
179 // get the (base) name
180 wxString GetName() const { return m_name; }
181
182 // get the full path of this object
183 wxString GetPath() const { return m_path; }
184
185 // get the load address and the extent, return true if this information is
186 // available
187 bool GetAddress(void **addr, size_t *len) const
188 {
189 if ( !m_address )
190 return false;
191
192 if ( addr )
193 *addr = m_address;
194 if ( len )
195 *len = m_length;
196
197 return true;
198 }
199
200 // return the version of the DLL (may be empty if no version info)
201 wxString GetVersion() const
202 {
203 return m_version;
204 }
205
206 private:
207 wxString m_name,
208 m_path,
209 m_version;
210
211 void *m_address;
212 size_t m_length;
213
214 friend class wxDynamicLibraryDetailsCreator;
215 };
216
217 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails,
218 wxDynamicLibraryDetailsArray,
219 WXDLLIMPEXP_BASE);
220
221 // ----------------------------------------------------------------------------
222 // wxDynamicLibrary: represents a handle to a DLL/shared object
223 // ----------------------------------------------------------------------------
224
225 class WXDLLIMPEXP_BASE wxDynamicLibrary
226 {
227 public:
228 // return a valid handle for the main program itself or NULL if back
229 // linking is not supported by the current platform (e.g. Win32)
230 static wxDllType GetProgramHandle();
231
232 // return the platform standard DLL extension (with leading dot)
233 static const wxString& GetDllExt() { return ms_dllext; }
234
235 wxDynamicLibrary() : m_handle(0) { }
236 wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT)
237 : m_handle(0)
238 {
239 Load(libname, flags);
240 }
241
242 // NOTE: this class is (deliberately) not virtual, do not attempt
243 // to use it polymorphically.
244 ~wxDynamicLibrary() { Unload(); }
245
246 // return true if the library was loaded successfully
247 bool IsLoaded() const { return m_handle != 0; }
248
249 // load the library with the given name (full or not), return true if ok
250 bool Load(const wxString& libname, int flags = wxDL_DEFAULT);
251
252 // raw function for loading dynamic libs: always behaves as if
253 // wxDL_VERBATIM were specified and doesn't log error message if the
254 // library couldn't be loaded but simply returns NULL
255 static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT);
256
257 // detach the library object from its handle, i.e. prevent the object from
258 // unloading the library in its dtor -- the caller is now responsible for
259 // doing this
260 wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; }
261
262 // unload the given library handle (presumably returned by Detach() before)
263 static void Unload(wxDllType handle);
264
265 // unload the library, also done automatically in dtor
266 void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } }
267
268 // Return the raw handle from dlopen and friends.
269 wxDllType GetLibHandle() const { return m_handle; }
270
271 // check if the given symbol is present in the library, useful to verify if
272 // a loadable module is our plugin, for example, without provoking error
273 // messages from GetSymbol()
274 bool HasSymbol(const wxString& name) const
275 {
276 bool ok;
277 DoGetSymbol(name, &ok);
278 return ok;
279 }
280
281 // resolve a symbol in a loaded DLL, such as a variable or function name.
282 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
283 // export unmangled names)
284 //
285 // Since it is perfectly valid for the returned symbol to actually be NULL,
286 // that is not always indication of an error. Pass and test the parameter
287 // 'success' for a true indication of success or failure to load the
288 // symbol.
289 //
290 // Returns a pointer to the symbol on success, or NULL if an error occurred
291 // or the symbol wasn't found.
292 void *GetSymbol(const wxString& name, bool *success = NULL) const;
293
294 // low-level version of GetSymbol()
295 static void *RawGetSymbol(wxDllType handle, const wxString& name);
296 void *RawGetSymbol(const wxString& name) const
297 {
298 #if defined (__WXPM__) || defined(__EMX__)
299 return GetSymbol(name);
300 #else
301 return RawGetSymbol(m_handle, name);
302 #endif
303 }
304
305 #ifdef __WXMSW__
306 // this function is useful for loading functions from the standard Windows
307 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or
308 // wide character build) suffix if they take string parameters
309 static void *RawGetSymbolAorW(wxDllType handle, const wxString& name)
310 {
311 return RawGetSymbol
312 (
313 handle,
314 name +
315 #if wxUSE_UNICODE
316 L'W'
317 #else
318 'A'
319 #endif
320 );
321 }
322
323 void *GetSymbolAorW(const wxString& name) const
324 {
325 return RawGetSymbolAorW(m_handle, name);
326 }
327 #endif // __WXMSW__
328
329 // return all modules/shared libraries in the address space of this process
330 //
331 // returns an empty array if not implemented or an error occurred
332 static wxDynamicLibraryDetailsArray ListLoaded();
333
334 // return platform-specific name of dynamic library with proper extension
335 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
336 static wxString CanonicalizeName(const wxString& name,
337 wxDynamicLibraryCategory cat = wxDL_LIBRARY);
338
339 // return name of wxWidgets plugin (adds compiler and version info
340 // to the filename):
341 static wxString
342 CanonicalizePluginName(const wxString& name,
343 wxPluginCategory cat = wxDL_PLUGIN_GUI);
344
345 // return plugin directory on platforms where it makes sense and empty
346 // string on others:
347 static wxString GetPluginsDirectory();
348
349
350 protected:
351 // common part of GetSymbol() and HasSymbol()
352 void *DoGetSymbol(const wxString& name, bool *success = 0) const;
353
354 #ifdef wxHAVE_DYNLIB_ERROR
355 // log the error after a dlxxx() function failure
356 static void Error();
357 #endif // wxHAVE_DYNLIB_ERROR
358
359
360 // platform specific shared lib suffix.
361 static const wxString ms_dllext;
362
363 // the handle to DLL or NULL
364 wxDllType m_handle;
365
366 // no copy ctor/assignment operators (or we'd try to unload the library
367 // twice)
368 DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
369 };
370
371
372 // ----------------------------------------------------------------------------
373 // Interesting defines
374 // ----------------------------------------------------------------------------
375
376 #define WXDLL_ENTRY_FUNCTION() \
377 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
378 const wxClassInfo *wxGetClassFirst() { \
379 return wxClassInfo::GetFirst(); \
380 }
381
382 #endif // wxUSE_DYNLIB_CLASS
383
384 #endif // _WX_DYNLIB_H__