1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library loading classes
4 // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
7 // Copyright: (c) 1998 Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DYNLIB_H__
12 #define _WX_DYNLIB_H__
16 #if wxUSE_DYNLIB_CLASS
18 #include "wx/string.h"
19 #include "wx/dynarray.h"
21 // note that we have our own dlerror() implementation under Darwin
22 #if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__)
23 #define wxHAVE_DYNLIB_ERROR
26 class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator
;
28 // ----------------------------------------------------------------------------
29 // conditional compilation
30 // ----------------------------------------------------------------------------
32 // Note: __OS2__/EMX has to be tested first, since we want to use
33 // native version, even if configure detected presence of DLOPEN.
34 #if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__)
35 typedef WXHMODULE wxDllType
;
36 #elif defined(__DARWIN__)
37 // Don't include dlfcn.h on Darwin, we may be using our own replacements.
38 typedef void *wxDllType
;
39 #elif defined(HAVE_DLOPEN)
41 typedef void *wxDllType
;
42 #elif defined(HAVE_SHL_LOAD)
44 typedef shl_t wxDllType
;
45 #elif defined(__WXMAC__)
46 #include <CodeFragments.h>
47 typedef CFragConnectionID wxDllType
;
49 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
58 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
59 // (only works on some Unix versions)
60 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
61 // (default, always the case under Win32)
62 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
64 wxDL_VERBATIM
= 0x00000008, // attempt to load the supplied library
65 // name without appending the usual dll
66 // filename extension.
68 // this flag is obsolete, don't use
69 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
70 // (only for wxPluginManager)
72 wxDL_QUIET
= 0x00000020, // don't log an error if failed to load
74 // this flag is dangerous, for internal use of wxMSW only, don't use at all
75 // and especially don't use directly, use wxLoadedDLL instead if you really
77 wxDL_GET_LOADED
= 0x00000040, // Win32 only: return handle of already
78 // loaded DLL or NULL otherwise; Unload()
79 // should not be called so don't forget to
80 // Detach() if you use this function
82 wxDL_DEFAULT
= wxDL_NOW
// default flags correspond to Win32
85 enum wxDynamicLibraryCategory
87 wxDL_LIBRARY
, // standard library
88 wxDL_MODULE
// loadable module/plugin
93 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
94 wxDL_PLUGIN_BASE
// wxBase-only plugin
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // when loading a function from a DLL you always have to cast the returned
102 // "void *" pointer to the correct type and, even more annoyingly, you have to
103 // repeat this type twice if you want to declare and define a function pointer
106 // this macro makes this slightly less painful by allowing you to specify the
107 // type only once, as the first parameter, and creating a variable of this type
108 // called "pfn<name>" initialized with the "name" from the "dynlib"
109 #define wxDYNLIB_FUNCTION(type, name, dynlib) \
110 type pfn ## name = (type)(dynlib).GetSymbol(wxT(#name))
113 // a more convenient function replacing wxDYNLIB_FUNCTION above
115 // it uses the convention that the type of the function is its name suffixed
116 // with "_t" but it doesn't define a variable but just assigns the loaded value
117 // to it and also allows to pass it the prefix to be used instead of hardcoding
118 // "pfn" (the prefix can be "m_" or "gs_pfn" or whatever)
120 // notice that this function doesn't generate error messages if the symbol
121 // couldn't be loaded, the caller should generate the appropriate message
122 #define wxDL_INIT_FUNC(pfx, name, dynlib) \
123 pfx ## name = (name ## _t)(dynlib).RawGetSymbol(#name)
127 // same as wxDL_INIT_FUNC() but appends 'A' or 'W' to the function name, see
128 // wxDynamicLibrary::GetSymbolAorW()
129 #define wxDL_INIT_FUNC_AW(pfx, name, dynlib) \
130 pfx ## name = (name ## _t)(dynlib).GetSymbolAorW(#name)
132 #endif // __WINDOWS__
134 // the following macros can be used to redirect a whole library to a class and
135 // check at run-time if the library is present and contains all required
138 // notice that they are supposed to be used inside a class which has "m_ok"
139 // member variable indicating if the library had been successfully loaded
141 // helper macros constructing the name of the variable storing the function
142 // pointer and the name of its type from the function name
143 #define wxDL_METHOD_NAME(name) m_pfn ## name
144 #define wxDL_METHOD_TYPE(name) name ## _t
147 // - rettype: return type of the function, e.g. "int"
148 // - name: name of the function, e.g. "foo"
149 // - args: function signature in parentheses, e.g. "(int x, int y)"
150 // - argnames: the names of the parameters in parentheses, e.g. "(x, y)"
151 // - defret: the value to return if the library wasn't successfully loaded
152 #define wxDL_METHOD_DEFINE( rettype, name, args, argnames, defret ) \
153 typedef rettype (* wxDL_METHOD_TYPE(name)) args ; \
154 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
156 { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; }
158 #define wxDL_VOIDMETHOD_DEFINE( name, args, argnames ) \
159 typedef void (* wxDL_METHOD_TYPE(name)) args ; \
160 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
162 { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; }
164 #define wxDL_METHOD_LOAD(lib, name) \
165 wxDL_METHOD_NAME(name) = \
166 (wxDL_METHOD_TYPE(name)) lib.GetSymbol(#name, &m_ok); \
167 if ( !m_ok ) return false
169 // ----------------------------------------------------------------------------
170 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
171 // ----------------------------------------------------------------------------
173 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
176 // ctor, normally never used as these objects are only created by
177 // wxDynamicLibrary::ListLoaded()
178 wxDynamicLibraryDetails() { m_address
= NULL
; m_length
= 0; }
180 // get the (base) name
181 wxString
GetName() const { return m_name
; }
183 // get the full path of this object
184 wxString
GetPath() const { return m_path
; }
186 // get the load address and the extent, return true if this information is
188 bool GetAddress(void **addr
, size_t *len
) const
201 // return the version of the DLL (may be empty if no version info)
202 wxString
GetVersion() const
215 friend class wxDynamicLibraryDetailsCreator
;
218 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails
,
219 wxDynamicLibraryDetailsArray
,
222 // ----------------------------------------------------------------------------
223 // wxDynamicLibrary: represents a handle to a DLL/shared object
224 // ----------------------------------------------------------------------------
226 class WXDLLIMPEXP_BASE wxDynamicLibrary
229 // return a valid handle for the main program itself or NULL if back
230 // linking is not supported by the current platform (e.g. Win32)
231 static wxDllType
GetProgramHandle();
233 // return the platform standard DLL extension (with leading dot)
234 static wxString
GetDllExt(wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
236 wxDynamicLibrary() : m_handle(0) { }
237 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
240 Load(libname
, flags
);
243 // NOTE: this class is (deliberately) not virtual, do not attempt
244 // to use it polymorphically.
245 ~wxDynamicLibrary() { Unload(); }
247 // return true if the library was loaded successfully
248 bool IsLoaded() const { return m_handle
!= 0; }
250 // load the library with the given name (full or not), return true if ok
251 bool Load(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
253 // raw function for loading dynamic libs: always behaves as if
254 // wxDL_VERBATIM were specified and doesn't log error message if the
255 // library couldn't be loaded but simply returns NULL
256 static wxDllType
RawLoad(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
258 // detach the library object from its handle, i.e. prevent the object from
259 // unloading the library in its dtor -- the caller is now responsible for
261 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
263 // unload the given library handle (presumably returned by Detach() before)
264 static void Unload(wxDllType handle
);
266 // unload the library, also done automatically in dtor
267 void Unload() { if ( IsLoaded() ) { Unload(m_handle
); m_handle
= 0; } }
269 // Return the raw handle from dlopen and friends.
270 wxDllType
GetLibHandle() const { return m_handle
; }
272 // check if the given symbol is present in the library, useful to verify if
273 // a loadable module is our plugin, for example, without provoking error
274 // messages from GetSymbol()
275 bool HasSymbol(const wxString
& name
) const
278 DoGetSymbol(name
, &ok
);
282 // resolve a symbol in a loaded DLL, such as a variable or function name.
283 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
284 // export unmangled names)
286 // Since it is perfectly valid for the returned symbol to actually be NULL,
287 // that is not always indication of an error. Pass and test the parameter
288 // 'success' for a true indication of success or failure to load the
291 // Returns a pointer to the symbol on success, or NULL if an error occurred
292 // or the symbol wasn't found.
293 void *GetSymbol(const wxString
& name
, bool *success
= NULL
) const;
295 // low-level version of GetSymbol()
296 static void *RawGetSymbol(wxDllType handle
, const wxString
& name
);
297 void *RawGetSymbol(const wxString
& name
) const
299 #if defined (__WXPM__) || defined(__EMX__)
300 return GetSymbol(name
);
302 return RawGetSymbol(m_handle
, name
);
307 // this function is useful for loading functions from the standard Windows
308 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or
309 // wide character build) suffix if they take string parameters
310 static void *RawGetSymbolAorW(wxDllType handle
, const wxString
& name
)
324 void *GetSymbolAorW(const wxString
& name
) const
326 return RawGetSymbolAorW(m_handle
, name
);
328 #endif // __WINDOWS__
330 // return all modules/shared libraries in the address space of this process
332 // returns an empty array if not implemented or an error occurred
333 static wxDynamicLibraryDetailsArray
ListLoaded();
335 // return platform-specific name of dynamic library with proper extension
336 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
337 static wxString
CanonicalizeName(const wxString
& name
,
338 wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
340 // return name of wxWidgets plugin (adds compiler and version info
343 CanonicalizePluginName(const wxString
& name
,
344 wxPluginCategory cat
= wxDL_PLUGIN_GUI
);
346 // return plugin directory on platforms where it makes sense and empty
348 static wxString
GetPluginsDirectory();
352 // return the handle (HMODULE/HINSTANCE) of the DLL with the given name
353 // and/or containing the specified address: for XP and later systems only
354 // the address is used and the name is ignored but for the previous systems
355 // only the name (which may be either a full path to the DLL or just its
356 // base name, possibly even without extension) is used
358 // the returned handle reference count is not incremented so it doesn't
359 // need to be freed using FreeLibrary() but it also means that it can
360 // become invalid if the DLL is unloaded
361 static WXHMODULE
MSWGetModuleHandle(const wxString
& name
, void *addr
);
362 #endif // __WINDOWS__
365 // common part of GetSymbol() and HasSymbol()
366 void *DoGetSymbol(const wxString
& name
, bool *success
= 0) const;
368 #ifdef wxHAVE_DYNLIB_ERROR
369 // log the error after a dlxxx() function failure
371 #endif // wxHAVE_DYNLIB_ERROR
374 // the handle to DLL or NULL
377 // no copy ctor/assignment operators (or we'd try to unload the library
379 wxDECLARE_NO_COPY_CLASS(wxDynamicLibrary
);
384 // ----------------------------------------------------------------------------
385 // wxLoadedDLL is a MSW-only internal helper class allowing to dynamically bind
386 // to a DLL already loaded into the project address space
387 // ----------------------------------------------------------------------------
389 class wxLoadedDLL
: public wxDynamicLibrary
392 wxLoadedDLL(const wxString
& dllname
)
393 : wxDynamicLibrary(dllname
, wxDL_GET_LOADED
| wxDL_VERBATIM
| wxDL_QUIET
)
403 #endif // __WINDOWS__
405 // ----------------------------------------------------------------------------
406 // Interesting defines
407 // ----------------------------------------------------------------------------
409 #define WXDLL_ENTRY_FUNCTION() \
410 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
411 const wxClassInfo *wxGetClassFirst() { \
412 return wxClassInfo::GetFirst(); \
415 #endif // wxUSE_DYNLIB_CLASS
417 #endif // _WX_DYNLIB_H__