1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library loading classes
4 // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DYNLIB_H__
13 #define _WX_DYNLIB_H__
17 #if wxUSE_DYNLIB_CLASS
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
22 // note that we have our own dlerror() implementation under Darwin
23 #if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__)
24 #define wxHAVE_DYNLIB_ERROR
27 class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator
;
29 // ----------------------------------------------------------------------------
30 // conditional compilation
31 // ----------------------------------------------------------------------------
33 // Note: __OS2__/EMX has to be tested first, since we want to use
34 // native version, even if configure detected presence of DLOPEN.
35 #if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__)
36 typedef WXHMODULE wxDllType
;
37 #elif defined(__DARWIN__)
38 // Don't include dlfcn.h on Darwin, we may be using our own replacements.
39 typedef void *wxDllType
;
40 #elif defined(HAVE_DLOPEN)
42 typedef void *wxDllType
;
43 #elif defined(HAVE_SHL_LOAD)
45 typedef shl_t wxDllType
;
46 #elif defined(__WXMAC__)
47 #include <CodeFragments.h>
48 typedef CFragConnectionID wxDllType
;
50 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
59 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
60 // (only works on some Unix versions)
61 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
62 // (default, always the case under Win32)
63 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
65 wxDL_VERBATIM
= 0x00000008, // attempt to load the supplied library
66 // name without appending the usual dll
67 // filename extension.
69 // this flag is obsolete, don't use
70 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
71 // (only for wxPluginManager)
73 wxDL_QUIET
= 0x00000020, // don't log an error if failed to load
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
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
83 wxDL_DEFAULT
= wxDL_NOW
// default flags correspond to Win32
86 enum wxDynamicLibraryCategory
88 wxDL_LIBRARY
, // standard library
89 wxDL_MODULE
// loadable module/plugin
94 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
95 wxDL_PLUGIN_BASE
// wxBase-only plugin
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
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
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) \
111 type pfn ## name = (type)(dynlib).GetSymbol(wxT(#name))
114 // a more convenient function replacing wxDYNLIB_FUNCTION above
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)
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
123 #define wxDL_INIT_FUNC(pfx, name, dynlib) \
124 pfx ## name = (name ## _t)(dynlib).RawGetSymbol(#name)
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)
133 #endif // __WINDOWS__
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
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
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
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); \
157 { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; }
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); \
163 { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; }
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
170 // ----------------------------------------------------------------------------
171 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
172 // ----------------------------------------------------------------------------
174 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
177 // ctor, normally never used as these objects are only created by
178 // wxDynamicLibrary::ListLoaded()
179 wxDynamicLibraryDetails() { m_address
= NULL
; m_length
= 0; }
181 // get the (base) name
182 wxString
GetName() const { return m_name
; }
184 // get the full path of this object
185 wxString
GetPath() const { return m_path
; }
187 // get the load address and the extent, return true if this information is
189 bool GetAddress(void **addr
, size_t *len
) const
202 // return the version of the DLL (may be empty if no version info)
203 wxString
GetVersion() const
216 friend class wxDynamicLibraryDetailsCreator
;
219 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails
,
220 wxDynamicLibraryDetailsArray
,
223 // ----------------------------------------------------------------------------
224 // wxDynamicLibrary: represents a handle to a DLL/shared object
225 // ----------------------------------------------------------------------------
227 class WXDLLIMPEXP_BASE wxDynamicLibrary
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)
232 static wxDllType
GetProgramHandle();
234 // return the platform standard DLL extension (with leading dot)
235 static const wxString
& GetDllExt() { return ms_dllext
; }
237 wxDynamicLibrary() : m_handle(0) { }
238 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
241 Load(libname
, flags
);
244 // NOTE: this class is (deliberately) not virtual, do not attempt
245 // to use it polymorphically.
246 ~wxDynamicLibrary() { Unload(); }
248 // return true if the library was loaded successfully
249 bool IsLoaded() const { return m_handle
!= 0; }
251 // load the library with the given name (full or not), return true if ok
252 bool Load(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
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
257 static wxDllType
RawLoad(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
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
262 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
264 // unload the given library handle (presumably returned by Detach() before)
265 static void Unload(wxDllType handle
);
267 // unload the library, also done automatically in dtor
268 void Unload() { if ( IsLoaded() ) { Unload(m_handle
); m_handle
= 0; } }
270 // Return the raw handle from dlopen and friends.
271 wxDllType
GetLibHandle() const { return m_handle
; }
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
279 DoGetSymbol(name
, &ok
);
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)
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
292 // Returns a pointer to the symbol on success, or NULL if an error occurred
293 // or the symbol wasn't found.
294 void *GetSymbol(const wxString
& name
, bool *success
= NULL
) const;
296 // low-level version of GetSymbol()
297 static void *RawGetSymbol(wxDllType handle
, const wxString
& name
);
298 void *RawGetSymbol(const wxString
& name
) const
300 #if defined (__WXPM__) || defined(__EMX__)
301 return GetSymbol(name
);
303 return RawGetSymbol(m_handle
, name
);
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
)
325 void *GetSymbolAorW(const wxString
& name
) const
327 return RawGetSymbolAorW(m_handle
, name
);
329 #endif // __WINDOWS__
331 // return all modules/shared libraries in the address space of this process
333 // returns an empty array if not implemented or an error occurred
334 static wxDynamicLibraryDetailsArray
ListLoaded();
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
);
341 // return name of wxWidgets plugin (adds compiler and version info
344 CanonicalizePluginName(const wxString
& name
,
345 wxPluginCategory cat
= wxDL_PLUGIN_GUI
);
347 // return plugin directory on platforms where it makes sense and empty
349 static wxString
GetPluginsDirectory();
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
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
362 static WXHMODULE
MSWGetModuleHandle(const char *name
, void *addr
);
363 #endif // __WINDOWS__
366 // common part of GetSymbol() and HasSymbol()
367 void *DoGetSymbol(const wxString
& name
, bool *success
= 0) const;
369 #ifdef wxHAVE_DYNLIB_ERROR
370 // log the error after a dlxxx() function failure
372 #endif // wxHAVE_DYNLIB_ERROR
375 // platform specific shared lib suffix.
376 static const wxString ms_dllext
;
378 // the handle to DLL or NULL
381 // no copy ctor/assignment operators (or we'd try to unload the library
383 wxDECLARE_NO_COPY_CLASS(wxDynamicLibrary
);
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 // ----------------------------------------------------------------------------
393 class wxLoadedDLL
: public wxDynamicLibrary
396 wxLoadedDLL(const wxString
& dllname
)
397 : wxDynamicLibrary(dllname
, wxDL_GET_LOADED
| wxDL_VERBATIM
| wxDL_QUIET
)
407 #endif // __WINDOWS__
409 // ----------------------------------------------------------------------------
410 // Interesting defines
411 // ----------------------------------------------------------------------------
413 #define WXDLL_ENTRY_FUNCTION() \
414 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
415 const wxClassInfo *wxGetClassFirst() { \
416 return wxClassInfo::GetFirst(); \
419 #endif // wxUSE_DYNLIB_CLASS
421 #endif // _WX_DYNLIB_H__