]>
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 |
371a5b4e | 9 | // Licence: wxWindows licence |
7b0bfbb2 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
34138703 JS |
12 | #ifndef _WX_DYNLIB_H__ |
13 | #define _WX_DYNLIB_H__ | |
7a4b9130 | 14 | |
af49c4b8 GD |
15 | #if defined(__GNUG__) && !defined(__APPLE__) |
16 | # pragma interface "dynlib.h" | |
7a4b9130 GL |
17 | #endif |
18 | ||
ed58dbea | 19 | #include "wx/setup.h" |
8a0d4cf6 | 20 | |
1948bb32 | 21 | #if wxUSE_DYNLIB_CLASS |
8a0d4cf6 | 22 | |
ed58dbea | 23 | #include "wx/string.h" |
7a4b9130 | 24 | |
1948bb32 | 25 | // FIXME: can this go in private.h or something too?? |
1a787c5d | 26 | #if defined(__WXPM__) || defined(__EMX__) |
1948bb32 VS |
27 | #define INCL_DOS |
28 | #include <os2.h> | |
29 | #endif | |
30 | ||
31 | #ifdef __WXMSW__ | |
32 | #include "wx/msw/private.h" | |
33 | #endif | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // conditional compilation | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | // Note: WXPM/EMX has to be tested first, since we want to use | |
40 | // native version, even if configure detected presence of DLOPEN. | |
41 | ||
42 | #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__) | |
43 | typedef HMODULE wxDllType; | |
1a787c5d | 44 | #elif defined(HAVE_DLOPEN) |
1948bb32 VS |
45 | #include <dlfcn.h> |
46 | typedef void *wxDllType; | |
8a0d4cf6 | 47 | #elif defined(HAVE_SHL_LOAD) |
1948bb32 VS |
48 | #include <dl.h> |
49 | typedef shl_t wxDllType; | |
f11bdd03 | 50 | #elif defined(__DARWIN__) |
1948bb32 | 51 | typedef void *wxDllType; |
7b0bfbb2 | 52 | #elif defined(__WXMAC__) |
1948bb32 | 53 | typedef CFragConnectionID wxDllType; |
7b0bfbb2 | 54 | #else |
1948bb32 VS |
55 | #error "Dynamic Loading classes can't be compiled on this platform, sorry." |
56 | #endif | |
57 | ||
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | // wxDynamicLibrary | |
61 | // --------------------------------------------------------------------------- | |
62 | ||
63 | //FIXME: This class isn't really common at all, it should be moved | |
64 | // into platform dependent files. | |
65 | ||
66 | // NOTE: this class is (deliberately) not virtual, do not attempt | |
67 | // to use it polymorphically. | |
68 | ||
69 | enum wxDLFlags | |
70 | { | |
71 | wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use | |
72 | wxDL_NOW = 0x00000002, // resolve undefined symbols on load | |
73 | wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently | |
74 | // loaded libs. | |
75 | wxDL_VERBATIM = 0x00000008, // Attempt to load the supplied library | |
76 | // name without appending the usual dll | |
77 | // filename extension. | |
78 | ||
79 | wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded | |
80 | ||
81 | // FIXME: why? (VZ) | |
82 | #ifdef __osf__ | |
83 | wxDL_DEFAULT = wxDL_LAZY | |
84 | #else | |
85 | wxDL_DEFAULT = wxDL_LAZY | wxDL_GLOBAL | |
86 | #endif | |
87 | }; | |
88 | ||
89 | enum wxDynamicLibraryCategory | |
90 | { | |
91 | wxDL_LIBRARY, // standard library | |
92 | wxDL_MODULE, // loadable module/plugin | |
93 | }; | |
94 | ||
95 | enum wxPluginCategory | |
96 | { | |
97 | wxDL_PLUGIN_GUI, // plugin that uses GUI classes | |
98 | wxDL_PLUGIN_BASE, // wxBase-only plugin | |
99 | }; | |
100 | ||
101 | ||
102 | class WXDLLIMPEXP_BASE wxDynamicLibrary | |
103 | { | |
104 | public: | |
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) | |
107 | ||
108 | static wxDllType GetProgramHandle(); | |
109 | ||
110 | // return the platform standard DLL extension (with leading dot) | |
111 | ||
112 | static const wxChar *GetDllExt() { return ms_dllext; } | |
113 | ||
114 | wxDynamicLibrary() : m_handle(0) {} | |
115 | wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT) | |
116 | : m_handle(0) | |
117 | { | |
118 | Load(libname, flags); | |
119 | } | |
120 | ~wxDynamicLibrary() { Unload(); } | |
121 | ||
122 | // return TRUE if the library was loaded successfully | |
123 | ||
124 | bool IsLoaded() const { return m_handle != 0; } | |
125 | ||
126 | // load the library with the given name | |
127 | // (full or not), return TRUE on success | |
128 | ||
129 | bool Load(wxString libname, int flags = wxDL_DEFAULT); | |
130 | ||
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; } | |
135 | ||
136 | // unload the library, also done automatically in dtor | |
137 | ||
138 | void Unload(); | |
139 | ||
140 | // Return the raw handle from dlopen and friends. | |
141 | ||
142 | wxDllType GetLibHandle() const { return m_handle; } | |
143 | ||
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) | |
147 | // | |
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 | |
151 | // load the symbol. | |
152 | // | |
153 | // Returns a pointer to the symbol on success, or NULL if an error | |
154 | // occurred or the symbol wasn't found. | |
7b0bfbb2 | 155 | |
1948bb32 VS |
156 | void *GetSymbol(const wxString& name, bool *success = 0) const; |
157 | ||
158 | #if WXWIN_COMPATIBILITY_2_2 | |
159 | operator bool() const { return IsLoaded(); } | |
7c0f3a1e | 160 | #endif |
0c32066b | 161 | |
1948bb32 VS |
162 | // return platform-specific name of dynamic library with proper extension |
163 | // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux) | |
164 | static wxString CanonicalizeName(const wxString& name, | |
165 | wxDynamicLibraryCategory cat = wxDL_LIBRARY); | |
166 | ||
167 | // return name of wxWindows plugin (adds compiler and version info | |
168 | // to the filename): | |
169 | static wxString CanonicalizePluginName(const wxString& name, | |
170 | wxPluginCategory cat); | |
171 | ||
172 | // return plugin directory on platforms where it makes sense and empty | |
173 | // string on others: | |
174 | static wxString GetPluginsDirectory(); | |
175 | ||
176 | protected: | |
177 | ||
178 | // Platform specific shared lib suffix. | |
179 | ||
180 | static const wxChar *ms_dllext; | |
181 | ||
182 | // the handle to DLL or NULL | |
183 | ||
184 | wxDllType m_handle; | |
185 | ||
186 | // no copy ctor/assignment operators | |
187 | // or we'd try to unload the library twice | |
188 | ||
189 | DECLARE_NO_COPY_CLASS(wxDynamicLibrary) | |
190 | }; | |
191 | ||
192 | ||
a585ca5c | 193 | // ---------------------------------------------------------------------------- |
33a8563e | 194 | // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code |
a585ca5c | 195 | // ---------------------------------------------------------------------------- |
3c1a88d8 | 196 | |
1948bb32 VS |
197 | #if WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER |
198 | ||
33a8563e VZ |
199 | /* |
200 | wxDllLoader is a class providing an interface similar to unix's dlopen(). | |
201 | It is used by wxDynamicLibrary wxLibrary and manages the actual loading of | |
202 | DLLs and the resolving of symbols in them. There are no instances of this | |
203 | class, it simply serves as a namespace for its static member functions. | |
a585ca5c | 204 | */ |
bddd7a8d | 205 | class WXDLLIMPEXP_BASE wxDllLoader |
a585ca5c | 206 | { |
3c1a88d8 | 207 | public: |
33a8563e VZ |
208 | /* |
209 | This function loads the shared library libname into memory. | |
210 | ||
211 | libname may be either the full path to the file or just the filename in | |
212 | which case the library is searched for in all standard locations | |
213 | (use GetDllExt() to construct the filename) | |
214 | ||
215 | if success pointer is not NULL, it will be filled with TRUE if everything | |
216 | went ok and FALSE otherwise | |
217 | */ | |
1948bb32 VS |
218 | static wxDllType LoadLibrary(const wxString& name, bool *success = NULL); |
219 | ||
33a8563e VZ |
220 | /* |
221 | This function unloads the shared library previously loaded with | |
222 | LoadLibrary | |
3c1a88d8 | 223 | */ |
3c1a88d8 | 224 | static void UnloadLibrary(wxDllType dll); |
33a8563e VZ |
225 | |
226 | /* | |
227 | This function returns a valid handle for the main program | |
228 | itself or NULL if back linking is not supported by the current platform | |
229 | (e.g. Win32). | |
230 | */ | |
1948bb32 | 231 | static wxDllType GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); } |
33a8563e VZ |
232 | |
233 | /* | |
234 | This function resolves a symbol in a loaded DLL, such as a | |
235 | variable or function name. | |
236 | ||
237 | dllHandle Handle of the DLL, as returned by LoadDll(). | |
238 | name Name of the symbol. | |
239 | ||
240 | Returns the pointer to the symbol or NULL on error. | |
3c1a88d8 | 241 | */ |
1948bb32 | 242 | static void *GetSymbol(wxDllType dllHandle, const wxString &name, bool *success = 0); |
3c1a88d8 | 243 | |
f6bcfd97 | 244 | // return the standard DLL extension (with leading dot) for this platform |
1948bb32 | 245 | static wxString GetDllExt() { return wxDynamicLibrary::GetDllExt(); } |
f6bcfd97 | 246 | |
3c1a88d8 | 247 | private: |
181b4068 | 248 | |
1948bb32 | 249 | wxDllLoader(); // forbid construction of objects |
33a8563e | 250 | }; |
181b4068 | 251 | |
1948bb32 | 252 | |
7b0bfbb2 | 253 | // ---------------------------------------------------------------------------- |
7a4b9130 | 254 | // wxLibrary |
7b0bfbb2 | 255 | // ---------------------------------------------------------------------------- |
7a4b9130 | 256 | |
1948bb32 VS |
257 | #include "wx/hash.h" |
258 | ||
bddd7a8d | 259 | class WXDLLIMPEXP_BASE wxLibrary : public wxObject |
7b0bfbb2 | 260 | { |
7b0bfbb2 | 261 | public: |
8a0d4cf6 | 262 | wxLibrary(wxDllType handle); |
33a8563e | 263 | virtual ~wxLibrary(); |
7a4b9130 | 264 | |
7b0bfbb2 VZ |
265 | // Get a symbol from the dynamic library |
266 | void *GetSymbol(const wxString& symbname); | |
7a4b9130 | 267 | |
7b0bfbb2 VZ |
268 | // Create the object whose classname is "name" |
269 | wxObject *CreateObject(const wxString& name); | |
7a4b9130 | 270 | |
7b0bfbb2 VZ |
271 | protected: |
272 | void PrepareClasses(wxClassInfo *first); | |
f4a8c29f | 273 | |
7b0bfbb2 | 274 | wxDllType m_handle; |
a585ca5c | 275 | |
33a8563e VZ |
276 | public: |
277 | wxHashTable classTable; | |
278 | }; | |
a585ca5c | 279 | |
7b0bfbb2 | 280 | // ---------------------------------------------------------------------------- |
7a4b9130 | 281 | // wxLibraries |
7b0bfbb2 VZ |
282 | // ---------------------------------------------------------------------------- |
283 | ||
bddd7a8d | 284 | class WXDLLIMPEXP_BASE wxLibraries |
7b0bfbb2 VZ |
285 | { |
286 | public: | |
287 | wxLibraries(); | |
288 | ~wxLibraries(); | |
7a4b9130 | 289 | |
7b0bfbb2 VZ |
290 | // caller is responsible for deleting the returned pointer if !NULL |
291 | wxLibrary *LoadLibrary(const wxString& basename); | |
7a4b9130 | 292 | |
7b0bfbb2 VZ |
293 | wxObject *CreateObject(const wxString& name); |
294 | ||
295 | protected: | |
296 | wxList m_loaded; | |
7a4b9130 GL |
297 | }; |
298 | ||
7b0bfbb2 | 299 | // ---------------------------------------------------------------------------- |
7a4b9130 | 300 | // Global variables |
7b0bfbb2 | 301 | // ---------------------------------------------------------------------------- |
7a4b9130 | 302 | |
bddd7a8d | 303 | extern WXDLLIMPEXP_DATA_BASE(wxLibraries) wxTheLibraries; |
7a4b9130 | 304 | |
1948bb32 VS |
305 | #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER |
306 | ||
7b0bfbb2 | 307 | // ---------------------------------------------------------------------------- |
7a4b9130 | 308 | // Interesting defines |
7b0bfbb2 | 309 | // ---------------------------------------------------------------------------- |
7a4b9130 | 310 | |
f4a8c29f | 311 | #define WXDLL_ENTRY_FUNCTION() \ |
ad8b3990 | 312 | extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \ |
1b733817 | 313 | const wxClassInfo *wxGetClassFirst() { \ |
856d2e52 | 314 | return wxClassInfo::GetFirst(); \ |
f4a8c29f | 315 | } |
7a4b9130 | 316 | |
8a0d4cf6 VZ |
317 | #endif // wxUSE_DYNLIB_CLASS |
318 | ||
7b0bfbb2 | 319 | #endif // _WX_DYNLIB_H__ |