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__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 # pragma interface "dynlib.h"
21 #if wxUSE_DYNLIB_CLASS
23 #include "wx/string.h"
24 #include "wx/dynarray.h"
26 // FIXME: can this go in private.h or something too??
27 #if defined(__WXPM__) || defined(__EMX__)
33 #include "wx/msw/private.h"
36 #if defined(HAVE_DLERROR) && !defined(__EMX__)
37 #define wxHAVE_DYNLIB_ERROR
40 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
;
42 // ----------------------------------------------------------------------------
43 // conditional compilation
44 // ----------------------------------------------------------------------------
46 // Note: WXPM/EMX has to be tested first, since we want to use
47 // native version, even if configure detected presence of DLOPEN.
48 #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
49 typedef HMODULE wxDllType
;
50 #elif defined(HAVE_DLOPEN)
52 typedef void *wxDllType
;
53 #elif defined(HAVE_SHL_LOAD)
55 typedef shl_t wxDllType
;
56 #elif defined(__DARWIN__)
57 typedef void *wxDllType
;
58 #elif defined(__WXMAC__)
59 #include <CodeFragments.h>
60 typedef CFragConnectionID wxDllType
;
62 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
71 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
72 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
73 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
75 wxDL_VERBATIM
= 0x00000008, // Attempt to load the supplied library
76 // name without appending the usual dll
77 // filename extension.
79 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
83 wxDL_DEFAULT
= wxDL_LAZY
85 wxDL_DEFAULT
= wxDL_LAZY
| wxDL_GLOBAL
89 enum wxDynamicLibraryCategory
91 wxDL_LIBRARY
, // standard library
92 wxDL_MODULE
// loadable module/plugin
97 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
98 wxDL_PLUGIN_BASE
// wxBase-only plugin
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // when loading a function from a DLL you always have to cast the returned
106 // "void *" pointer to the correct type and, even more annoyingly, you have to
107 // repeat this type twice if you want to declare and define a function pointer
110 // this macro makes this slightly less painful by allowing you to specify the
111 // type only once, as the first parameter, and creating a variable of this type
112 // called "pfn<name>" initialized with the "name" from the "dynlib"
113 #define wxDYNLIB_FUNCTION(type, name, dynlib) \
114 type pfn ## name = (type)(dynlib).GetSymbol(_T(#name))
116 // ----------------------------------------------------------------------------
117 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
118 // ----------------------------------------------------------------------------
120 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
123 // ctor, normally never used as these objects are only created by
124 // wxDynamicLibrary::ListLoaded()
125 wxDynamicLibraryDetails() { m_address
= NULL
; m_length
= 0; }
127 // get the (base) name
128 wxString
GetName() const { return m_name
; }
130 // get the full path of this object
131 wxString
GetPath() const { return m_path
; }
133 // get the load address and the extent, return true if this information is
135 bool GetAddress(void **addr
, size_t *len
) const
148 // return the version of the DLL (may be empty if no version info)
149 wxString
GetVersion() const
162 friend class wxDynamicLibraryDetailsCreator
;
165 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails
,
166 wxDynamicLibraryDetailsArray
,
169 // ----------------------------------------------------------------------------
170 // wxDynamicLibrary: represents a handle to a DLL/shared object
171 // ----------------------------------------------------------------------------
173 class WXDLLIMPEXP_BASE wxDynamicLibrary
176 // return a valid handle for the main program itself or NULL if back
177 // linking is not supported by the current platform (e.g. Win32)
178 static wxDllType
GetProgramHandle();
180 // return the platform standard DLL extension (with leading dot)
181 static const wxChar
*GetDllExt() { return ms_dllext
; }
183 wxDynamicLibrary() : m_handle(0) { }
184 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
187 Load(libname
, flags
);
190 // NOTE: this class is (deliberately) not virtual, do not attempt
191 // to use it polymorphically.
192 ~wxDynamicLibrary() { Unload(); }
194 // return true if the library was loaded successfully
195 bool IsLoaded() const { return m_handle
!= 0; }
197 // load the library with the given name (full or not), return true if ok
198 bool Load(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
200 // raw function for loading dynamic libs: always behaves as if
201 // wxDL_VERBATIM were specified and doesn't log error message if the
202 // library couldn't be loaded but simply returns NULL
203 static wxDllType
RawLoad(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
205 // detach the library object from its handle, i.e. prevent the object from
206 // unloading the library in its dtor -- the caller is now responsible for
208 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
210 // unload the given library handle (presumably returned by Detach() before)
211 static void Unload(wxDllType handle
);
213 // unload the library, also done automatically in dtor
214 void Unload() { if ( IsLoaded() ) { Unload(m_handle
); m_handle
= 0; } }
216 // Return the raw handle from dlopen and friends.
217 wxDllType
GetLibHandle() const { return m_handle
; }
219 // check if the given symbol is present in the library, useful to verify if
220 // a loadable module is our plugin, for example, without provoking error
221 // messages from GetSymbol()
222 bool HasSymbol(const wxString
& name
) const
225 DoGetSymbol(name
, &ok
);
229 // resolve a symbol in a loaded DLL, such as a variable or function name.
230 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
231 // export unmangled names)
233 // Since it is perfectly valid for the returned symbol to actually be NULL,
234 // that is not always indication of an error. Pass and test the parameter
235 // 'success' for a true indication of success or failure to load the
238 // Returns a pointer to the symbol on success, or NULL if an error occurred
239 // or the symbol wasn't found.
240 void *GetSymbol(const wxString
& name
, bool *success
= NULL
) const;
242 // low-level version of GetSymbol()
243 static void *RawGetSymbol(wxDllType handle
, const wxString
& name
);
244 void *RawGetSymbol(const wxString
& name
) const
246 return RawGetSymbol(m_handle
, name
);
249 // return all modules/shared libraries in the address space of this process
251 // returns an empty array if not implemented or an error occured
252 static wxDynamicLibraryDetailsArray
ListLoaded();
254 // return platform-specific name of dynamic library with proper extension
255 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
256 static wxString
CanonicalizeName(const wxString
& name
,
257 wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
259 // return name of wxWidgets plugin (adds compiler and version info
262 CanonicalizePluginName(const wxString
& name
,
263 wxPluginCategory cat
= wxDL_PLUGIN_GUI
);
265 // return plugin directory on platforms where it makes sense and empty
267 static wxString
GetPluginsDirectory();
270 #if WXWIN_COMPATIBILITY_2_2
271 operator bool() const { return IsLoaded(); }
275 // common part of GetSymbol() and HasSymbol()
276 void *DoGetSymbol(const wxString
& name
, bool *success
= 0) const;
278 #ifdef wxHAVE_DYNLIB_ERROR
279 // log the error after a dlxxx() function failure
281 #endif // wxHAVE_DYNLIB_ERROR
284 // platform specific shared lib suffix.
285 static const wxChar
*ms_dllext
;
287 // the handle to DLL or NULL
290 // no copy ctor/assignment operators (or we'd try to unload the library
292 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
296 // ----------------------------------------------------------------------------
297 // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
298 // ----------------------------------------------------------------------------
300 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
303 wxDllLoader is a class providing an interface similar to unix's dlopen().
304 It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
305 DLLs and the resolving of symbols in them. There are no instances of this
306 class, it simply serves as a namespace for its static member functions.
308 class WXDLLIMPEXP_BASE wxDllLoader
312 This function loads the shared library libname into memory.
314 libname may be either the full path to the file or just the filename in
315 which case the library is searched for in all standard locations
316 (use GetDllExt() to construct the filename)
318 if success pointer is not NULL, it will be filled with true if everything
319 went ok and false otherwise
321 static wxDllType
LoadLibrary(const wxString
& name
, bool *success
= NULL
);
324 This function unloads the shared library previously loaded with
327 static void UnloadLibrary(wxDllType dll
);
330 This function returns a valid handle for the main program
331 itself or NULL if back linking is not supported by the current platform
334 static wxDllType
GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); }
337 This function resolves a symbol in a loaded DLL, such as a
338 variable or function name.
340 dllHandle Handle of the DLL, as returned by LoadDll().
341 name Name of the symbol.
343 Returns the pointer to the symbol or NULL on error.
345 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
347 // return the standard DLL extension (with leading dot) for this platform
348 static wxString
GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
352 wxDllLoader(); // forbid construction of objects
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
362 class WXDLLIMPEXP_BASE wxLibrary
: public wxObject
365 wxLibrary(wxDllType handle
);
366 virtual ~wxLibrary();
368 // Get a symbol from the dynamic library
369 void *GetSymbol(const wxString
& symbname
);
371 // Create the object whose classname is "name"
372 wxObject
*CreateObject(const wxString
& name
);
375 void PrepareClasses(wxClassInfo
*first
);
380 wxHashTable classTable
;
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 class WXDLLIMPEXP_BASE wxLibraries
393 // caller is responsible for deleting the returned pointer if !NULL
394 wxLibrary
*LoadLibrary(const wxString
& basename
);
396 wxObject
*CreateObject(const wxString
& name
);
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 extern WXDLLIMPEXP_DATA_BASE(wxLibraries
) wxTheLibraries
;
408 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
410 // ----------------------------------------------------------------------------
411 // Interesting defines
412 // ----------------------------------------------------------------------------
414 #define WXDLL_ENTRY_FUNCTION() \
415 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
416 const wxClassInfo *wxGetClassFirst() { \
417 return wxClassInfo::GetFirst(); \
420 #endif // wxUSE_DYNLIB_CLASS
422 #endif // _WX_DYNLIB_H__