1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic loading framework
4 // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
5 // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_DYNAMICLOADER_H__
14 #define _WX_DYNAMICLOADER_H__
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "dynload.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
26 #if wxUSE_DYNAMIC_LOADER
29 #include "wx/module.h"
31 // FIXME: can this go in private.h or something too??
32 #if defined(__WXPM__) || defined(__EMX__)
38 #include "wx/msw/private.h"
41 // Ugh, I'd much rather this was typesafe, but no time
42 // to rewrite wxHashTable right now..
44 typedef wxHashTable wxDLManifest
;
45 typedef wxHashTable wxDLImports
;
47 // ----------------------------------------------------------------------------
48 // conditional compilation
49 // ----------------------------------------------------------------------------
51 // Note: WXPM/EMX has to be tested first, since we want to use
52 // native version, even if configure detected presence of DLOPEN.
54 #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
55 typedef HMODULE wxDllType
;
56 #elif defined(HAVE_DLOPEN)
58 typedef void *wxDllType
;
59 #elif defined(HAVE_SHL_LOAD)
61 typedef shl_t wxDllType
;
62 #elif defined(__DARWIN__)
63 typedef void *wxDllType
;
64 #elif defined(__WXMAC__)
65 typedef CFragConnectionID wxDllType
;
67 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
71 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 //FIXME: This class isn't really common at all, it should be moved
76 // into platform dependent files.
78 // NOTE: this class is (deliberately) not virtual, do not attempt
79 // to use it polymorphically.
83 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
84 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
85 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
87 wxDL_VERBATIM
= 0x00000008, // Attempt to load the supplied library
88 // name without appending the usual dll
89 // filename extension.
91 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
95 wxDL_DEFAULT
= wxDL_LAZY
97 wxDL_DEFAULT
= wxDL_LAZY
| wxDL_GLOBAL
102 class WXDLLEXPORT wxDynamicLibrary
106 // return a valid handle for the main program itself or NULL if
107 // back linking is not supported by the current platform (e.g. Win32)
109 static wxDllType
GetProgramHandle();
111 // return the platform standard DLL extension (with leading dot)
113 static const wxChar
*GetDllExt() { return ms_dllext
; }
115 wxDynamicLibrary() : m_handle(0) {}
116 wxDynamicLibrary(wxString libname
, int flags
= wxDL_DEFAULT
)
119 Load(libname
, flags
);
121 ~wxDynamicLibrary() { Unload(); }
123 // return TRUE if the library was loaded successfully
125 bool IsLoaded() const { return m_handle
!= 0; }
127 // load the library with the given name
128 // (full or not), return TRUE on success
130 bool Load(wxString libname
, int flags
= wxDL_DEFAULT
);
132 // unload the library, also done automatically in dtor
136 // Return the raw handle from dlopen and friends.
138 wxDllType
GetLibHandle() const { return m_handle
; }
140 // resolve a symbol in a loaded DLL, such as a variable or function
141 // name. 'name' is the (possibly mangled) name of the symbol.
142 // (use extern "C" to export unmangled names)
144 // Since it is perfectly valid for the returned symbol to actually be
145 // NULL, that is not always indication of an error. Pass and test the
146 // parameter 'success' for a true indication of success or failure to
149 // Returns a pointer to the symbol on success, or NULL if an error
150 // occurred or the symbol wasn't found.
152 void *GetSymbol(const wxString
& name
, bool *success
= 0) const;
154 #if WXWIN_COMPATIBILITY_2_2
155 operator bool() const { return IsLoaded(); }
160 // Platform specific shared lib suffix.
162 static const wxChar
*ms_dllext
;
164 // the handle to DLL or NULL
168 // no copy ctor/assignment operators
169 // or we'd try to unload the library twice
171 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
175 // ---------------------------------------------------------------------------
177 // ---------------------------------------------------------------------------
179 // NOTE: Do not attempt to use a base class pointer to this class.
180 // wxDL is not virtual and we deliberately hide some of it's
183 // Unless you know exacty why you need to, you probably shouldn't
184 // instantiate this class directly anyway, use wxPluginManager
187 class WXDLLEXPORT wxPluginLibrary
: public wxDynamicLibrary
191 static wxDLImports
* ms_classes
; // Static hash of all imported classes.
193 wxPluginLibrary( const wxString
&libname
, int flags
= wxDL_DEFAULT
);
196 wxPluginLibrary
*RefLib();
199 // These two are called by the PluginSentinel on (PLUGGABLE) object
200 // creation/destruction. There is usually no reason for the user to
201 // call them directly. We have to separate this from the link count,
202 // since the two are not interchangeable.
204 // FIXME: for even better debugging PluginSentinel should register
205 // the name of the class created too, then we can state
206 // exactly which object was not destroyed which may be
207 // difficult to find otherwise. Also this code should
208 // probably only be active in DEBUG mode, but let's just
209 // get it right first.
211 void RefObj() { ++m_objcount
; }
214 wxASSERT_MSG( m_objcount
> 0, _T("Too many objects deleted??") );
218 // Override/hide some base class methods
220 bool IsLoaded() const { return m_linkcount
> 0; }
221 void Unload() { UnrefLib(); }
225 wxClassInfo
*m_before
; // sm_first before loading this lib
226 wxClassInfo
*m_after
; // ..and after.
228 size_t m_linkcount
; // Ref count of library link calls
229 size_t m_objcount
; // ..and (pluggable) object instantiations.
230 wxModuleList m_wxmodules
; // any wxModules that we initialised.
232 void UpdateClassInfo(); // Update the wxClassInfo table
233 void RestoreClassInfo(); // Restore the original wxClassInfo state.
234 void RegisterModules(); // Init any wxModules in the lib.
235 void UnregisterModules(); // Cleanup any wxModules we installed.
237 DECLARE_NO_COPY_CLASS(wxPluginLibrary
)
241 class WXDLLEXPORT wxPluginManager
247 static wxPluginLibrary
*LoadLibrary( const wxString
&libname
,
248 int flags
= wxDL_DEFAULT
);
249 static bool UnloadLibrary(const wxString
&libname
);
251 // This is used by wxDllLoader. It's wrapped in the compatibility
252 // macro because it's of arguable use outside of that.
254 #if WXWIN_COMPATIBILITY_2_2
255 static wxPluginLibrary
*GetObjectFromHandle(wxDllType handle
);
260 wxPluginManager() : m_entry(0) {};
261 wxPluginManager(const wxString
&libname
, int flags
= wxDL_DEFAULT
)
263 Load(libname
, flags
);
265 ~wxPluginManager() { Unload(); }
267 bool Load(const wxString
&libname
, int flags
= wxDL_DEFAULT
);
270 bool IsLoaded() const { return m_entry
&& m_entry
->IsLoaded(); }
271 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
273 return m_entry
->GetSymbol( symbol
, success
);
276 static void CreateManifest() { ms_manifest
= new wxDLManifest(wxKEY_STRING
); }
277 static void ClearManifest() { delete ms_manifest
; ms_manifest
= NULL
; }
281 static wxDLManifest
* ms_manifest
; // Static hash of loaded libs.
282 wxPluginLibrary
* m_entry
; // Cache our entry in the manifest.
284 // We could allow this class to be copied if we really
285 // wanted to, but not without modification.
287 DECLARE_NO_COPY_CLASS(wxPluginManager
)
291 // ---------------------------------------------------------------------------
293 // ---------------------------------------------------------------------------
295 // Cross platform wrapper for dlopen and friends.
296 // There are no instances of this class, it simply
297 // serves as a namespace for its static member functions.
299 #if WXWIN_COMPATIBILITY_2_2
300 class WXDLLEXPORT wxDllLoader
304 static wxDllType
LoadLibrary(const wxString
& name
, bool *success
= NULL
);
305 static void UnloadLibrary(wxDllType dll
);
307 static wxDllType
GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); }
309 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
311 static wxString
GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
315 wxDllLoader(); // forbid construction of objects
319 #endif // wxUSE_DYNAMIC_LOADER
320 #endif // _WX_DYNAMICLOADER_H__