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__
17 #pragma interface "dynload.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
26 #if wxUSE_DYNAMIC_LOADER
29 #include "wx/module.h"
32 // Ugh, I'd much rather this was typesafe, but no time
33 // to rewrite wxHashTable right now..
35 typedef wxHashTable wxDLManifest
;
36 typedef wxHashTable wxDLImports
;
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
42 // Note: WXPM/EMX has to be tested first, since we want to use
43 // native version, even if configure detected presence of DLOPEN.
45 #if defined(__WXPM__) || defined(__EMX__)
48 typedef HMODULE wxDllType
;
49 #elif defined(HAVE_DLOPEN)
51 typedef void *wxDllType
;
52 #elif defined(HAVE_SHL_LOAD)
54 typedef shl_t wxDllType
;
55 #elif defined(__WINDOWS__)
56 #include <windows.h> // needed to get HMODULE
57 typedef HMODULE wxDllType
;
58 #elif defined(__DARWIN__)
59 typedef void *wxDllType
;
60 #elif defined(__WXMAC__)
61 typedef CFragConnectionID wxDllType
;
63 #error "wxLibrary can't be compiled on this platform, sorry."
66 // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader
67 // method should be called LoadLibrary, not LoadLibraryA or LoadLibraryW!
69 #if defined(__WIN32__) && defined(LoadLibrary)
70 # include "wx/msw/winundef.h"
74 // ---------------------------------------------------------------------------
76 // ---------------------------------------------------------------------------
78 // Cross platform wrapper for dlopen and friends.
79 // There are no instances of this class, it simply
80 // serves as a namespace for its static member functions.
82 class WXDLLEXPORT wxDllLoader
86 // libname may be either the full path to the file or just the filename
87 // in which case the library is searched for in all standard locations.
88 // The platform specific library extension is automatically appended.
90 static wxDllType
Load(const wxString
& name
);
92 // The same as Load, except 'name' is searched for without modification.
94 static wxDllType
LoadLibrary(const wxString
& name
);
95 static void UnloadLibrary(wxDllType dll
);
97 // return a valid handle for the main program itself or NULL if
98 // back linking is not supported by the current platform (e.g. Win32)
100 static wxDllType
GetProgramHandle();
102 // resolve a symbol in a loaded DLL, such as a variable or function
103 // name. dllHandle is a handle previously returned by LoadLibrary(),
104 // symbol is the (possibly mangled) name of the symbol.
105 // (use extern "C" to export unmangled names)
107 // Since it is perfectly valid for the returned symbol to actually be
108 // NULL, that is not always indication of an error. Pass and test the
109 // parameter 'success' for a true indication of success or failure to
112 // Returns a pointer to the symbol on success.
114 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
116 // return the platform standard DLL extension (with leading dot)
118 static const wxString
&GetDllExt() { return ms_dllext
; }
122 wxDllLoader(); // forbid construction of objects
123 static const wxString ms_dllext
; // Platform specific shared lib suffix.
127 // ---------------------------------------------------------------------------
129 // ---------------------------------------------------------------------------
131 class WXDLLEXPORT wxDLManifestEntry
135 static wxDLImports ms_classes
; // Static hash of all imported classes.
137 wxDLManifestEntry( const wxString
&libname
);
138 ~wxDLManifestEntry();
140 wxDLManifestEntry
*RefLib() { ++m_linkcount
; return this; }
143 // These two are called by the PluginSentinel on (PLUGGABLE) object
144 // creation/destruction. There is usually no reason for the user to
145 // call them directly. We have to separate this from the link count,
146 // since the two are not interchangeable.
148 // FIXME: for even better debugging PluginSentinel should register
149 // the name of the class created too, then we can state
150 // exactly which object was not destroyed which may be
151 // difficult to find otherwise. Also this code should
152 // probably only be active in DEBUG mode, but let's just
153 // get it right first.
155 void RefObj() { ++m_objcount
; }
158 wxASSERT_MSG( m_objcount
> 0, _T("Too many objects deleted??") );
162 bool IsLoaded() const { return m_linkcount
> 0; }
164 wxDllType
GetLinkHandle() const { return m_handle
; }
165 wxDllType
GetProgramHandle() const { return wxDllLoader::GetProgramHandle(); }
166 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
168 return wxDllLoader::GetSymbol( m_handle
, symbol
, success
);
173 // Order of these three *is* important, do not change it
175 wxClassInfo
*m_before
; // sm_first before loading this lib
176 wxDllType m_handle
; // Handle from dlopen.
177 wxClassInfo
*m_after
; // ..and after.
179 size_t m_linkcount
; // Ref count of library link calls
180 size_t m_objcount
; // ..and (pluggable) object instantiations.
181 wxModuleList m_wxmodules
; // any wxModules that we initialised.
183 void UpdateClassInfo(); // Update the wxClassInfo table
184 void RestoreClassInfo(); // Restore the original wxClassInfo state.
185 void RegisterModules(); // Init any wxModules in the lib.
186 void UnregisterModules(); // Cleanup any wxModules we installed.
188 DECLARE_NO_COPY_CLASS(wxDLManifestEntry
)
192 class WXDLLEXPORT wxDynamicLibrary
198 static wxDLManifestEntry
*Link(const wxString
&libname
);
199 static bool Unlink(const wxString
&libname
);
203 wxDynamicLibrary(const wxString
&libname
);
206 bool IsLoaded() const { return m_entry
&& m_entry
->IsLoaded(); }
207 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
209 return m_entry
->GetSymbol( symbol
, success
);
214 static wxDLManifest ms_manifest
; // Static hash of loaded libs.
215 wxDLManifestEntry
*m_entry
; // Cache our entry in the manifest.
217 // We could allow this class to be copied if we really
218 // wanted to, but not without modification.
220 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
224 #endif // wxUSE_DYNAMIC_LOADER
225 #endif // _WX_DYNAMICLOADER_H__