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