1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library loading classes
4 // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DYNLIB_H__
13 #define _WX_DYNLIB_H__
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 # pragma interface "dynlib.h"
21 #if wxUSE_DYNAMIC_LOADER
23 #include "wx/dynload.h" // Use the new (version of) wxDynamicLibrary instead
25 #elif wxUSE_DYNLIB_CLASS
27 #include "wx/string.h"
31 // this is normally done by configure, but I leave it here for now...
32 #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD))
33 # if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__)
35 # elif defined(__HPUX__)
36 # define HAVE_SHL_LOAD
37 # endif // Unix flavour
38 #endif // !Unix or already have some HAVE_xxx defined
40 // Note: WXPM/EMX has to be tested first, since we want to use
41 // native version, even if configure detected presence of DLOPEN.
42 #if defined(__WXPM__) || defined(__EMX__)
45 typedef HMODULE wxDllType
;
46 #elif defined(HAVE_DLOPEN)
48 typedef void *wxDllType
;
49 #elif defined(HAVE_SHL_LOAD)
51 typedef shl_t wxDllType
;
52 #elif defined(__WINDOWS__)
53 # include <windows.h> // needed to get HMODULE
54 typedef HMODULE wxDllType
;
55 #elif defined(__DARWIN__)
56 typedef void *wxDllType
;
57 #elif defined(__WXMAC__)
58 typedef void *wxDllType
;
60 # error "wxLibrary can't be compiled on this platform, sorry."
63 // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method
64 // should be called LoadLibrary, not LoadLibraryA or LoadLibraryW!
65 #if defined(__WIN32__) && defined(LoadLibrary)
66 # include "wx/msw/winundef.h"
69 // ----------------------------------------------------------------------------
70 // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
71 // ----------------------------------------------------------------------------
74 wxDllLoader is a class providing an interface similar to unix's dlopen().
75 It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
76 DLLs and the resolving of symbols in them. There are no instances of this
77 class, it simply serves as a namespace for its static member functions.
79 class WXDLLEXPORT wxDllLoader
83 This function loads the shared library libname into memory.
85 libname may be either the full path to the file or just the filename in
86 which case the library is searched for in all standard locations
87 (use GetDllExt() to construct the filename)
89 if success pointer is not NULL, it will be filled with TRUE if everything
90 went ok and FALSE otherwise
92 static wxDllType
LoadLibrary(const wxString
& libname
, bool *success
= 0);
95 This function unloads the shared library previously loaded with
98 static void UnloadLibrary(wxDllType dll
);
101 This function returns a valid handle for the main program
102 itself or NULL if back linking is not supported by the current platform
105 static wxDllType
GetProgramHandle();
108 This function resolves a symbol in a loaded DLL, such as a
109 variable or function name.
111 dllHandle Handle of the DLL, as returned by LoadDll().
112 name Name of the symbol.
114 Returns the pointer to the symbol or NULL on error.
116 static void *GetSymbol(wxDllType dllHandle
,
117 const wxString
&name
,
118 bool *success
= NULL
);
120 // return the standard DLL extension (with leading dot) for this platform
121 static const wxString
&GetDllExt() { return ms_dllext
; }
124 // forbid construction of objects
126 static const wxString ms_dllext
;
129 // ----------------------------------------------------------------------------
130 // wxDynamicLibrary - friendly interface to wxDllLoader
131 // ----------------------------------------------------------------------------
133 class WXDLLEXPORT wxDynamicLibrary
137 wxDynamicLibrary() { m_library
= 0; }
138 wxDynamicLibrary(const wxString
& name
) { Load(name
); }
140 // return TRUE if the library was loaded successfully
141 bool IsLoaded() const { return m_library
!= 0; }
142 operator bool() const { return IsLoaded(); }
144 // load the library with the given name (full or not), return TRUE on
146 bool Load(const wxString
& name
)
148 m_library
= wxDllLoader::LoadLibrary(name
);
153 // unload the library, also done automatically in dtor
157 wxDllLoader::UnloadLibrary(m_library
);
160 // load a symbol from the library, return NULL if an error occured or
161 // symbol wasn't found
162 void *GetSymbol(const wxString
& name
) const
164 wxCHECK_MSG( IsLoaded(), NULL
,
165 _T("can't load symbol from unloaded library") );
167 return wxDllLoader::GetSymbol(m_library
, name
);
170 // unload the library
172 // NB: dtor is not virtual, don't derive from this class
173 ~wxDynamicLibrary() { Unload(); }
176 // the handle to DLL or NULL
179 // no copy ctor/assignment operators (or we'd try to unload the library
181 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 class WXDLLEXPORT wxLibrary
: public wxObject
191 wxLibrary(wxDllType handle
);
192 virtual ~wxLibrary();
194 // Get a symbol from the dynamic library
195 void *GetSymbol(const wxString
& symbname
);
197 // Create the object whose classname is "name"
198 wxObject
*CreateObject(const wxString
& name
);
201 void PrepareClasses(wxClassInfo
*first
);
206 wxHashTable classTable
;
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 class WXDLLEXPORT wxLibraries
219 // caller is responsible for deleting the returned pointer if !NULL
220 wxLibrary
*LoadLibrary(const wxString
& basename
);
222 wxObject
*CreateObject(const wxString
& name
);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 extern WXDLLEXPORT_DATA(wxLibraries
) wxTheLibraries
;
234 // ----------------------------------------------------------------------------
235 // Interesting defines
236 // ----------------------------------------------------------------------------
238 #define WXDLL_ENTRY_FUNCTION() \
239 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
240 const wxClassInfo *wxGetClassFirst() { \
241 return wxClassInfo::GetFirst(); \
244 #endif // wxUSE_DYNLIB_CLASS
246 #endif // _WX_DYNLIB_H__