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 licence
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
28 #include "wx/hashmap.h"
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 class WXDLLEXPORT wxPluginLibrary
;
43 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxPluginLibrary
*, wxDLManifest
);
44 typedef wxDLManifest wxDLImports
;
46 // ----------------------------------------------------------------------------
47 // conditional compilation
48 // ----------------------------------------------------------------------------
50 // Note: WXPM/EMX has to be tested first, since we want to use
51 // native version, even if configure detected presence of DLOPEN.
53 #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
54 typedef HMODULE wxDllType
;
55 #elif defined(HAVE_DLOPEN)
57 typedef void *wxDllType
;
58 #elif defined(HAVE_SHL_LOAD)
60 typedef shl_t wxDllType
;
61 #elif defined(__DARWIN__)
62 typedef void *wxDllType
;
63 #elif defined(__WXMAC__)
64 typedef CFragConnectionID wxDllType
;
66 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
70 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
74 //FIXME: This class isn't really common at all, it should be moved
75 // into platform dependent files.
77 // NOTE: this class is (deliberately) not virtual, do not attempt
78 // to use it polymorphically.
82 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
83 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
84 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
86 wxDL_VERBATIM
= 0x00000008, // Attempt to load the supplied library
87 // name without appending the usual dll
88 // filename extension.
90 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
94 wxDL_DEFAULT
= wxDL_LAZY
96 wxDL_DEFAULT
= wxDL_LAZY
| wxDL_GLOBAL
101 class WXDLLEXPORT wxDynamicLibrary
105 // return a valid handle for the main program itself or NULL if
106 // back linking is not supported by the current platform (e.g. Win32)
108 static wxDllType
GetProgramHandle();
110 // return the platform standard DLL extension (with leading dot)
112 static const wxChar
*GetDllExt() { return ms_dllext
; }
114 wxDynamicLibrary() : m_handle(0) {}
115 wxDynamicLibrary(wxString libname
, int flags
= wxDL_DEFAULT
)
118 Load(libname
, flags
);
120 ~wxDynamicLibrary() { Unload(); }
122 // return TRUE if the library was loaded successfully
124 bool IsLoaded() const { return m_handle
!= 0; }
126 // load the library with the given name
127 // (full or not), return TRUE on success
129 bool Load(wxString libname
, int flags
= wxDL_DEFAULT
);
131 // detach the library object from its handle, i.e. prevent the object
132 // from unloading the library in its dtor -- the caller is now
133 // responsible for doing this
134 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
136 // unload the library, also done automatically in dtor
140 // Return the raw handle from dlopen and friends.
142 wxDllType
GetLibHandle() const { return m_handle
; }
144 // resolve a symbol in a loaded DLL, such as a variable or function
145 // name. 'name' is the (possibly mangled) name of the symbol.
146 // (use extern "C" to export unmangled names)
148 // Since it is perfectly valid for the returned symbol to actually be
149 // NULL, that is not always indication of an error. Pass and test the
150 // parameter 'success' for a true indication of success or failure to
153 // Returns a pointer to the symbol on success, or NULL if an error
154 // occurred or the symbol wasn't found.
156 void *GetSymbol(const wxString
& name
, bool *success
= 0) const;
158 #if WXWIN_COMPATIBILITY_2_2
159 operator bool() const { return IsLoaded(); }
164 // Platform specific shared lib suffix.
166 static const wxChar
*ms_dllext
;
168 // the handle to DLL or NULL
172 // no copy ctor/assignment operators
173 // or we'd try to unload the library twice
175 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
179 // ---------------------------------------------------------------------------
181 // ---------------------------------------------------------------------------
183 // NOTE: Do not attempt to use a base class pointer to this class.
184 // wxDL is not virtual and we deliberately hide some of it's
187 // Unless you know exacty why you need to, you probably shouldn't
188 // instantiate this class directly anyway, use wxPluginManager
191 class WXDLLEXPORT wxPluginLibrary
: public wxDynamicLibrary
195 static wxDLImports
* ms_classes
; // Static hash of all imported classes.
197 wxPluginLibrary( const wxString
&libname
, int flags
= wxDL_DEFAULT
);
200 wxPluginLibrary
*RefLib();
203 // These two are called by the PluginSentinel on (PLUGGABLE) object
204 // creation/destruction. There is usually no reason for the user to
205 // call them directly. We have to separate this from the link count,
206 // since the two are not interchangeable.
208 // FIXME: for even better debugging PluginSentinel should register
209 // the name of the class created too, then we can state
210 // exactly which object was not destroyed which may be
211 // difficult to find otherwise. Also this code should
212 // probably only be active in DEBUG mode, but let's just
213 // get it right first.
215 void RefObj() { ++m_objcount
; }
218 wxASSERT_MSG( m_objcount
> 0, _T("Too many objects deleted??") );
222 // Override/hide some base class methods
224 bool IsLoaded() const { return m_linkcount
> 0; }
225 void Unload() { UnrefLib(); }
229 wxClassInfo
*m_before
; // sm_first before loading this lib
230 wxClassInfo
*m_after
; // ..and after.
232 size_t m_linkcount
; // Ref count of library link calls
233 size_t m_objcount
; // ..and (pluggable) object instantiations.
234 wxModuleList m_wxmodules
; // any wxModules that we initialised.
236 void UpdateClassInfo(); // Update the wxClassInfo table
237 void RestoreClassInfo(); // Restore the original wxClassInfo state.
238 void RegisterModules(); // Init any wxModules in the lib.
239 void UnregisterModules(); // Cleanup any wxModules we installed.
241 DECLARE_NO_COPY_CLASS(wxPluginLibrary
)
245 class WXDLLEXPORT wxPluginManager
251 static wxPluginLibrary
*LoadLibrary( const wxString
&libname
,
252 int flags
= wxDL_DEFAULT
);
253 static bool UnloadLibrary(const wxString
&libname
);
255 // This is used by wxDllLoader. It's wrapped in the compatibility
256 // macro because it's of arguable use outside of that.
258 #if WXWIN_COMPATIBILITY_2_2
259 static wxPluginLibrary
*GetObjectFromHandle(wxDllType handle
);
264 wxPluginManager() : m_entry(NULL
) {};
265 wxPluginManager(const wxString
&libname
, int flags
= wxDL_DEFAULT
)
267 Load(libname
, flags
);
269 ~wxPluginManager() { Unload(); }
271 bool Load(const wxString
&libname
, int flags
= wxDL_DEFAULT
);
274 bool IsLoaded() const { return m_entry
&& m_entry
->IsLoaded(); }
275 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
277 return m_entry
->GetSymbol( symbol
, success
);
280 static void CreateManifest() { ms_manifest
= new wxDLManifest(wxKEY_STRING
); }
281 static void ClearManifest() { delete ms_manifest
; ms_manifest
= NULL
; }
284 // return the pointer to the entry for the library with given name in
285 // ms_manifest or NULL if none
286 static wxPluginLibrary
*FindByName(const wxString
& name
)
288 const wxDLManifest::iterator i
= ms_manifest
->find(name
);
290 return i
== ms_manifest
->end() ? NULL
: i
->second
;
293 static wxDLManifest
* ms_manifest
; // Static hash of loaded libs.
294 wxPluginLibrary
* m_entry
; // Cache our entry in the manifest.
296 // We could allow this class to be copied if we really
297 // wanted to, but not without modification.
298 DECLARE_NO_COPY_CLASS(wxPluginManager
)
302 // ---------------------------------------------------------------------------
304 // ---------------------------------------------------------------------------
306 // Cross platform wrapper for dlopen and friends.
307 // There are no instances of this class, it simply
308 // serves as a namespace for its static member functions.
310 #if WXWIN_COMPATIBILITY_2_2
311 class WXDLLEXPORT wxDllLoader
315 static wxDllType
LoadLibrary(const wxString
& name
, bool *success
= NULL
);
316 static void UnloadLibrary(wxDllType dll
);
318 static wxDllType
GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); }
320 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
322 static wxString
GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
326 wxDllLoader(); // forbid construction of objects
330 #endif // wxUSE_DYNAMIC_LOADER
331 #endif // _WX_DYNAMICLOADER_H__