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 wxDLManifestEntry
135 static wxDLImports ms_classes
; // Static hash of all imported classes.
137 wxDLManifestEntry( const wxString
&libname
);
138 ~wxDLManifestEntry();
140 wxDLManifestEntry
*Ref() { ++m_count
; return this; }
141 bool Unref() { return (m_count
-- < 2) ? (delete this, TRUE
) : FALSE
; }
143 bool IsLoaded() const { return m_count
> 0; }
145 wxDllType
GetLinkHandle() const { return m_handle
; }
146 wxDllType
GetProgramHandle() const { return wxDllLoader::GetProgramHandle(); }
147 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
149 return wxDllLoader::GetSymbol( m_handle
, symbol
, success
);
154 // Order of these three *is* important, do not change it
156 wxClassInfo
*m_before
; // sm_first before loading this lib
157 wxDllType m_handle
; // Handle from dlopen.
158 wxClassInfo
*m_after
; // ..and after.
160 size_t m_count
; // Ref count of Link and Create calls.
161 wxModuleList m_wxmodules
; // any wxModules that we initialised.
163 void UpdateClassInfo(); // Update the wxClassInfo table
164 void RestoreClassInfo(); // Restore the original wxClassInfo state.
165 void RegisterModules(); // Init any wxModules in the lib.
166 void UnregisterModules(); // Cleanup any wxModules we installed.
168 DECLARE_NO_COPY_CLASS(wxDLManifestEntry
)
172 class WXDLLEXPORT wxDynamicLibrary
178 static wxDLManifestEntry
*Link(const wxString
&libname
);
179 static bool Unlink(const wxString
&libname
);
183 wxDynamicLibrary(const wxString
&libname
);
186 bool IsLoaded() const { return m_entry
&& m_entry
->IsLoaded(); }
187 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
189 return m_entry
->GetSymbol( symbol
, success
);
194 static wxDLManifest ms_manifest
; // Static hash of loaded libs.
195 wxDLManifestEntry
*m_entry
; // Cache our entry in the manifest.
197 // We could allow this class to be copied if we really
198 // wanted to, but not without modification.
200 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
204 #endif // wxUSE_DYNAMIC_LOADER
205 #endif // _WX_DYNAMICLOADER_H__