]>
Commit | Line | Data |
---|---|---|
7b0bfbb2 | 1 | ///////////////////////////////////////////////////////////////////////////// |
33a8563e VZ |
2 | // Name: wx/dynlib.h |
3 | // Purpose: Dynamic library loading classes | |
4 | // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik | |
7b0bfbb2 VZ |
5 | // Modified by: |
6 | // Created: 20/07/98 | |
7 | // RCS-ID: $Id$ | |
33a8563e | 8 | // Copyright: (c) 1998 Guilhem Lavaux |
7b0bfbb2 VZ |
9 | // Licence: wxWindows license |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
34138703 JS |
12 | #ifndef _WX_DYNLIB_H__ |
13 | #define _WX_DYNLIB_H__ | |
7a4b9130 GL |
14 | |
15 | #ifdef __GNUG__ | |
a585ca5c | 16 | # pragma interface |
7a4b9130 GL |
17 | #endif |
18 | ||
ed58dbea | 19 | #include "wx/setup.h" |
8a0d4cf6 VZ |
20 | |
21 | #if wxUSE_DYNLIB_CLASS | |
22 | ||
ed58dbea RR |
23 | #include "wx/string.h" |
24 | #include "wx/list.h" | |
25 | #include "wx/hash.h" | |
7a4b9130 | 26 | |
8a0d4cf6 VZ |
27 | // this is normally done by configure, but I leave it here for now... |
28 | #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)) | |
a585ca5c KB |
29 | # if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__) |
30 | # define HAVE_DLOPEN | |
31 | # elif defined(__HPUX__) | |
32 | # define HAVE_SHL_LOAD | |
33 | # endif // Unix flavour | |
3e4efd7c VZ |
34 | #endif // !Unix or already have some HAVE_xxx defined |
35 | ||
1a787c5d SN |
36 | // Note: WXPM/EMX has to be tested first, since we want to use |
37 | // native version, even if configure detected presence of DLOPEN. | |
38 | #if defined(__WXPM__) || defined(__EMX__) | |
39 | # define INCL_DOS | |
40 | # include <os2.h> | |
41 | typedef HMODULE wxDllType; | |
42 | #elif defined(HAVE_DLOPEN) | |
a585ca5c | 43 | # include <dlfcn.h> |
7b0bfbb2 | 44 | typedef void *wxDllType; |
8a0d4cf6 | 45 | #elif defined(HAVE_SHL_LOAD) |
a585ca5c | 46 | # include <dl.h> |
8a0d4cf6 | 47 | typedef shl_t wxDllType; |
7b0bfbb2 | 48 | #elif defined(__WINDOWS__) |
3c1a88d8 | 49 | # include <windows.h> // needed to get HMODULE |
7b0bfbb2 | 50 | typedef HMODULE wxDllType; |
f11bdd03 | 51 | #elif defined(__DARWIN__) |
03e11df5 | 52 | typedef void *wxDllType; |
7b0bfbb2 VZ |
53 | #elif defined(__WXMAC__) |
54 | typedef CFragConnectionID wxDllType; | |
55 | #else | |
a585ca5c | 56 | # error "wxLibrary can't be compiled on this platform, sorry." |
7b0bfbb2 VZ |
57 | #endif // OS |
58 | ||
7cc98b3e VZ |
59 | // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method |
60 | // should be called LoadLibrary, not LoadLibraryA or LoadLibraryW! | |
61 | #if defined(__WIN32__) && defined(LoadLibrary) | |
62 | # include "wx/msw/winundef.h" | |
7c0f3a1e | 63 | #endif |
0c32066b | 64 | |
a585ca5c | 65 | // ---------------------------------------------------------------------------- |
33a8563e | 66 | // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code |
a585ca5c | 67 | // ---------------------------------------------------------------------------- |
3c1a88d8 | 68 | |
33a8563e VZ |
69 | /* |
70 | wxDllLoader is a class providing an interface similar to unix's dlopen(). | |
71 | It is used by wxDynamicLibrary wxLibrary and manages the actual loading of | |
72 | DLLs and the resolving of symbols in them. There are no instances of this | |
73 | class, it simply serves as a namespace for its static member functions. | |
a585ca5c | 74 | */ |
b97a2c53 | 75 | class WXDLLEXPORT wxDllLoader |
a585ca5c | 76 | { |
3c1a88d8 | 77 | public: |
33a8563e VZ |
78 | /* |
79 | This function loads the shared library libname into memory. | |
80 | ||
81 | libname may be either the full path to the file or just the filename in | |
82 | which case the library is searched for in all standard locations | |
83 | (use GetDllExt() to construct the filename) | |
84 | ||
85 | if success pointer is not NULL, it will be filled with TRUE if everything | |
86 | went ok and FALSE otherwise | |
87 | */ | |
88 | static wxDllType LoadLibrary(const wxString& libname, bool *success = NULL); | |
89 | ||
90 | /* | |
91 | This function unloads the shared library previously loaded with | |
92 | LoadLibrary | |
3c1a88d8 | 93 | */ |
3c1a88d8 | 94 | static void UnloadLibrary(wxDllType dll); |
33a8563e VZ |
95 | |
96 | /* | |
97 | This function returns a valid handle for the main program | |
98 | itself or NULL if back linking is not supported by the current platform | |
99 | (e.g. Win32). | |
100 | */ | |
101 | static wxDllType GetProgramHandle(); | |
102 | ||
103 | /* | |
104 | This function resolves a symbol in a loaded DLL, such as a | |
105 | variable or function name. | |
106 | ||
107 | dllHandle Handle of the DLL, as returned by LoadDll(). | |
108 | name Name of the symbol. | |
109 | ||
110 | Returns the pointer to the symbol or NULL on error. | |
3c1a88d8 VZ |
111 | */ |
112 | static void * GetSymbol(wxDllType dllHandle, const wxString &name); | |
113 | ||
f6bcfd97 BP |
114 | // return the standard DLL extension (with leading dot) for this platform |
115 | static wxString GetDllExt(); | |
116 | ||
3c1a88d8 | 117 | private: |
33a8563e | 118 | // forbid construction of objects |
3c1a88d8 | 119 | wxDllLoader(); |
a585ca5c KB |
120 | }; |
121 | ||
181b4068 VS |
122 | // ---------------------------------------------------------------------------- |
123 | // wxDynamicLibrary - friendly interface to wxDllLoader | |
124 | // ---------------------------------------------------------------------------- | |
125 | ||
33a8563e | 126 | class WXDLLEXPORT wxDynamicLibrary |
181b4068 VS |
127 | { |
128 | public: | |
33a8563e | 129 | // ctors |
181b4068 VS |
130 | wxDynamicLibrary() { m_library = 0; } |
131 | wxDynamicLibrary(const wxString& name) { Load(name); } | |
132 | ||
33a8563e | 133 | // return TRUE if the library was loaded successfully |
181b4068 | 134 | bool IsLoaded() const { return m_library != 0; } |
33a8563e | 135 | operator bool() const { return IsLoaded(); } |
181b4068 | 136 | |
33a8563e VZ |
137 | // load the library with the given name (full or not), return TRUE on |
138 | // success | |
181b4068 VS |
139 | bool Load(const wxString& name) |
140 | { | |
141 | m_library = wxDllLoader::LoadLibrary(name); | |
142 | ||
143 | return IsLoaded(); | |
144 | } | |
145 | ||
33a8563e | 146 | // unload the library, also done automatically in dtor |
181b4068 VS |
147 | void Unload() |
148 | { | |
149 | if ( IsLoaded() ) | |
33a8563e | 150 | wxDllLoader::UnloadLibrary(m_library); |
181b4068 VS |
151 | } |
152 | ||
33a8563e VZ |
153 | // load a symbol from the library, return NULL if an error occured or |
154 | // symbol wasn't found | |
181b4068 VS |
155 | void *GetSymbol(const wxString& name) const |
156 | { | |
157 | wxCHECK_MSG( IsLoaded(), NULL, | |
158 | _T("can't load symbol from unloaded library") ); | |
159 | ||
160 | return wxDllLoader::GetSymbol(m_library, name); | |
161 | } | |
162 | ||
33a8563e VZ |
163 | // unload the library |
164 | // | |
165 | // NB: dtor is not virtual, don't derive from this class | |
181b4068 VS |
166 | ~wxDynamicLibrary() { Unload(); } |
167 | ||
168 | private: | |
33a8563e | 169 | // the handle to DLL or NULL |
181b4068 | 170 | wxDllType m_library; |
181b4068 | 171 | |
33a8563e VZ |
172 | // no copy ctor/assignment operators (or we'd try to unload the library |
173 | // twice) | |
1a0d517e | 174 | DECLARE_NO_COPY_CLASS(wxDynamicLibrary) |
33a8563e | 175 | }; |
181b4068 | 176 | |
7b0bfbb2 | 177 | // ---------------------------------------------------------------------------- |
7a4b9130 | 178 | // wxLibrary |
7b0bfbb2 | 179 | // ---------------------------------------------------------------------------- |
7a4b9130 | 180 | |
b97a2c53 | 181 | class WXDLLEXPORT wxLibrary : public wxObject |
7b0bfbb2 | 182 | { |
7b0bfbb2 | 183 | public: |
8a0d4cf6 | 184 | wxLibrary(wxDllType handle); |
33a8563e | 185 | virtual ~wxLibrary(); |
7a4b9130 | 186 | |
7b0bfbb2 VZ |
187 | // Get a symbol from the dynamic library |
188 | void *GetSymbol(const wxString& symbname); | |
7a4b9130 | 189 | |
7b0bfbb2 VZ |
190 | // Create the object whose classname is "name" |
191 | wxObject *CreateObject(const wxString& name); | |
7a4b9130 | 192 | |
7b0bfbb2 VZ |
193 | protected: |
194 | void PrepareClasses(wxClassInfo *first); | |
f4a8c29f | 195 | |
7b0bfbb2 | 196 | wxDllType m_handle; |
a585ca5c | 197 | |
33a8563e VZ |
198 | public: |
199 | wxHashTable classTable; | |
200 | }; | |
a585ca5c | 201 | |
7b0bfbb2 | 202 | // ---------------------------------------------------------------------------- |
7a4b9130 | 203 | // wxLibraries |
7b0bfbb2 VZ |
204 | // ---------------------------------------------------------------------------- |
205 | ||
b97a2c53 | 206 | class WXDLLEXPORT wxLibraries |
7b0bfbb2 VZ |
207 | { |
208 | public: | |
209 | wxLibraries(); | |
210 | ~wxLibraries(); | |
7a4b9130 | 211 | |
7b0bfbb2 VZ |
212 | // caller is responsible for deleting the returned pointer if !NULL |
213 | wxLibrary *LoadLibrary(const wxString& basename); | |
7a4b9130 | 214 | |
7b0bfbb2 VZ |
215 | wxObject *CreateObject(const wxString& name); |
216 | ||
217 | protected: | |
218 | wxList m_loaded; | |
7a4b9130 GL |
219 | }; |
220 | ||
7b0bfbb2 | 221 | // ---------------------------------------------------------------------------- |
7a4b9130 | 222 | // Global variables |
7b0bfbb2 | 223 | // ---------------------------------------------------------------------------- |
7a4b9130 GL |
224 | |
225 | extern wxLibraries wxTheLibraries; | |
226 | ||
7b0bfbb2 | 227 | // ---------------------------------------------------------------------------- |
7a4b9130 | 228 | // Interesting defines |
7b0bfbb2 | 229 | // ---------------------------------------------------------------------------- |
7a4b9130 | 230 | |
f4a8c29f | 231 | #define WXDLL_ENTRY_FUNCTION() \ |
856d2e52 GL |
232 | extern "C" wxClassInfo *wxGetClassFirst(); \ |
233 | wxClassInfo *wxGetClassFirst() { \ | |
234 | return wxClassInfo::GetFirst(); \ | |
f4a8c29f | 235 | } |
7a4b9130 | 236 | |
8a0d4cf6 VZ |
237 | #endif // wxUSE_DYNLIB_CLASS |
238 | ||
7b0bfbb2 | 239 | #endif // _WX_DYNLIB_H__ |