]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynlib.h
rename wxFileList to wxFileListCtrl
[wxWidgets.git] / include / wx / dynlib.h
CommitLineData
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
65571936 9// Licence: wxWindows licence
7b0bfbb2
VZ
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_DYNLIB_H__
13#define _WX_DYNLIB_H__
7a4b9130 14
2ecf902b 15#include "wx/defs.h"
8a0d4cf6 16
1948bb32 17#if wxUSE_DYNLIB_CLASS
8a0d4cf6 18
ed58dbea 19#include "wx/string.h"
defbed48 20#include "wx/dynarray.h"
7a4b9130 21
55034339 22#if defined(__OS2__) || defined(__EMX__)
0872eaf9 23#include "wx/os2/private.h"
1948bb32
VS
24#endif
25
26#ifdef __WXMSW__
27#include "wx/msw/private.h"
28#endif
29
67d0bba6
VZ
30// note that we have our own dlerror() implementation under Darwin
31#if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__)
da55d064
VZ
32 #define wxHAVE_DYNLIB_ERROR
33#endif
34
b5dbe15d 35class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator;
defbed48 36
1948bb32
VS
37// ----------------------------------------------------------------------------
38// conditional compilation
39// ----------------------------------------------------------------------------
40
55034339 41// Note: __OS2__/EMX has to be tested first, since we want to use
4104ed92 42// native version, even if configure detected presence of DLOPEN.
55034339 43#if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__)
4104ed92 44 typedef HMODULE wxDllType;
1a787c5d 45#elif defined(HAVE_DLOPEN)
4104ed92
VZ
46 #include <dlfcn.h>
47 typedef void *wxDllType;
8a0d4cf6 48#elif defined(HAVE_SHL_LOAD)
4104ed92
VZ
49 #include <dl.h>
50 typedef shl_t wxDllType;
f11bdd03 51#elif defined(__DARWIN__)
4104ed92 52 typedef void *wxDllType;
7b0bfbb2 53#elif defined(__WXMAC__)
609533d5 54 #include <CodeFragments.h>
4104ed92 55 typedef CFragConnectionID wxDllType;
7b0bfbb2 56#else
4104ed92 57 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
1948bb32
VS
58#endif
59
4104ed92
VZ
60// ----------------------------------------------------------------------------
61// constants
62// ----------------------------------------------------------------------------
1948bb32
VS
63
64enum wxDLFlags
65{
66 wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use
e62e17d7 67 // (only works on some Unix versions)
1948bb32 68 wxDL_NOW = 0x00000002, // resolve undefined symbols on load
e62e17d7 69 // (default, always the case under Win32)
1948bb32
VS
70 wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently
71 // loaded libs.
e62e17d7 72 wxDL_VERBATIM = 0x00000008, // attempt to load the supplied library
1948bb32
VS
73 // name without appending the usual dll
74 // filename extension.
1948bb32 75 wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded
e62e17d7 76 // (only for wxPluginManager)
1948bb32 77
e62e17d7 78 wxDL_DEFAULT = wxDL_NOW // default flags correspond to Win32
1948bb32
VS
79};
80
81enum wxDynamicLibraryCategory
82{
83 wxDL_LIBRARY, // standard library
13e51f3a 84 wxDL_MODULE // loadable module/plugin
1948bb32
VS
85};
86
87enum 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
420db5a5 108
ced3df77
VZ
109// the following macros can be used to redirect a whole library to a class and
110// check at run-time if the library is present and contains all required
111// methods
112//
113// notice that they are supposed to be used inside a class which has "m_ok"
114// member variable indicating if the library had been successfully loaded
115
116// helper macros constructing the name of the variable storing the function
117// pointer and the name of its type from the function name
118#define wxDL_METHOD_NAME(name) m_pfn ## name
119#define wxDL_METHOD_TYPE(name) name ## _t
120
121// parameters are:
122// - rettype: return type of the function, e.g. "int"
123// - name: name of the function, e.g. "foo"
124// - args: function signature in parentheses, e.g. "(int x, int y)"
125// - argnames: the names of the parameters in parentheses, e.g. "(x, y)"
126// - defret: the value to return if the library wasn't successfully loaded
127#define wxDL_METHOD_DEFINE( rettype, name, args, argnames, defret ) \
128 typedef rettype (* wxDL_METHOD_TYPE(name)) args ; \
129 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
420db5a5 130 rettype name args \
ced3df77 131 { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; }
420db5a5 132
ced3df77
VZ
133#define wxDL_VOIDMETHOD_DEFINE( name, args, argnames ) \
134 typedef void (* wxDL_METHOD_TYPE(name)) args ; \
135 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
420db5a5 136 void name args \
ced3df77
VZ
137 { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; }
138
139#define wxDL_METHOD_LOAD(lib, name) \
140 wxDL_METHOD_NAME(name) = \
141 (wxDL_METHOD_TYPE(name)) lib.GetSymbol(#name, &m_ok); \
142 if ( !m_ok ) return false
420db5a5 143
defbed48
VZ
144// ----------------------------------------------------------------------------
145// wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
146// ----------------------------------------------------------------------------
147
148class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
149{
150public:
151 // ctor, normally never used as these objects are only created by
152 // wxDynamicLibrary::ListLoaded()
153 wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; }
154
155 // get the (base) name
156 wxString GetName() const { return m_name; }
157
158 // get the full path of this object
159 wxString GetPath() const { return m_path; }
160
161 // get the load address and the extent, return true if this information is
162 // available
163 bool GetAddress(void **addr, size_t *len) const
164 {
165 if ( !m_address )
166 return false;
167
168 if ( addr )
169 *addr = m_address;
170 if ( len )
171 *len = m_length;
172
173 return true;
174 }
175
176 // return the version of the DLL (may be empty if no version info)
177 wxString GetVersion() const
178 {
179 return m_version;
180 }
181
182private:
183 wxString m_name,
184 m_path,
185 m_version;
186
187 void *m_address;
188 size_t m_length;
189
190 friend class wxDynamicLibraryDetailsCreator;
191};
192
193WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails,
194 wxDynamicLibraryDetailsArray,
195 WXDLLIMPEXP_BASE);
196
197// ----------------------------------------------------------------------------
198// wxDynamicLibrary: represents a handle to a DLL/shared object
199// ----------------------------------------------------------------------------
1948bb32
VS
200
201class WXDLLIMPEXP_BASE wxDynamicLibrary
202{
203public:
4104ed92
VZ
204 // return a valid handle for the main program itself or NULL if back
205 // linking is not supported by the current platform (e.g. Win32)
1948bb32
VS
206 static wxDllType GetProgramHandle();
207
4104ed92 208 // return the platform standard DLL extension (with leading dot)
00711afd 209 static const wxString& GetDllExt() { return ms_dllext; }
1948bb32 210
4104ed92 211 wxDynamicLibrary() : m_handle(0) { }
1948bb32
VS
212 wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT)
213 : m_handle(0)
214 {
215 Load(libname, flags);
216 }
1948bb32 217
4104ed92
VZ
218 // NOTE: this class is (deliberately) not virtual, do not attempt
219 // to use it polymorphically.
220 ~wxDynamicLibrary() { Unload(); }
1948bb32 221
68379eaf 222 // return true if the library was loaded successfully
1948bb32
VS
223 bool IsLoaded() const { return m_handle != 0; }
224
4104ed92 225 // load the library with the given name (full or not), return true if ok
defbed48 226 bool Load(const wxString& libname, int flags = wxDL_DEFAULT);
1948bb32 227
defbed48
VZ
228 // raw function for loading dynamic libs: always behaves as if
229 // wxDL_VERBATIM were specified and doesn't log error message if the
230 // library couldn't be loaded but simply returns NULL
da55d064
VZ
231 static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT);
232
4104ed92
VZ
233 // detach the library object from its handle, i.e. prevent the object from
234 // unloading the library in its dtor -- the caller is now responsible for
235 // doing this
1948bb32
VS
236 wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; }
237
169948a0
VZ
238 // unload the given library handle (presumably returned by Detach() before)
239 static void Unload(wxDllType handle);
240
4104ed92 241 // unload the library, also done automatically in dtor
169948a0 242 void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } }
1948bb32 243
4104ed92 244 // Return the raw handle from dlopen and friends.
1948bb32
VS
245 wxDllType GetLibHandle() const { return m_handle; }
246
a018a119
VZ
247 // check if the given symbol is present in the library, useful to verify if
248 // a loadable module is our plugin, for example, without provoking error
249 // messages from GetSymbol()
250 bool HasSymbol(const wxString& name) const
251 {
252 bool ok;
253 DoGetSymbol(name, &ok);
254 return ok;
255 }
256
4104ed92
VZ
257 // resolve a symbol in a loaded DLL, such as a variable or function name.
258 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
259 // export unmangled names)
260 //
261 // Since it is perfectly valid for the returned symbol to actually be NULL,
262 // that is not always indication of an error. Pass and test the parameter
263 // 'success' for a true indication of success or failure to load the
264 // symbol.
265 //
266 // Returns a pointer to the symbol on success, or NULL if an error occurred
267 // or the symbol wasn't found.
defbed48 268 void *GetSymbol(const wxString& name, bool *success = NULL) const;
1948bb32 269
defbed48
VZ
270 // low-level version of GetSymbol()
271 static void *RawGetSymbol(wxDllType handle, const wxString& name);
272 void *RawGetSymbol(const wxString& name) const
273 {
fd6c9428
SN
274#if defined (__WXPM__) || defined(__EMX__)
275 return GetSymbol(name);
276#else
defbed48 277 return RawGetSymbol(m_handle, name);
fd6c9428 278#endif
defbed48
VZ
279 }
280
93ed8ff7
VZ
281#ifdef __WXMSW__
282 // this function is useful for loading functions from the standard Windows
283 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or
284 // wide character build) suffix if they take string parameters
285 static void *RawGetSymbolAorW(wxDllType handle, const wxString& name)
286 {
287 return RawGetSymbol
288 (
289 handle,
55034339 290 name +
93ed8ff7
VZ
291#if wxUSE_UNICODE
292 L'W'
293#else
294 'A'
295#endif
296 );
297 }
298
299 void *GetSymbolAorW(const wxString& name) const
300 {
301 return RawGetSymbolAorW(m_handle, name);
302 }
303#endif // __WXMSW__
304
defbed48
VZ
305 // return all modules/shared libraries in the address space of this process
306 //
3103e8a9 307 // returns an empty array if not implemented or an error occurred
defbed48 308 static wxDynamicLibraryDetailsArray ListLoaded();
0c32066b 309
1948bb32
VS
310 // return platform-specific name of dynamic library with proper extension
311 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
312 static wxString CanonicalizeName(const wxString& name,
313 wxDynamicLibraryCategory cat = wxDL_LIBRARY);
314
77ffb593 315 // return name of wxWidgets plugin (adds compiler and version info
1948bb32 316 // to the filename):
169948a0
VZ
317 static wxString
318 CanonicalizePluginName(const wxString& name,
319 wxPluginCategory cat = wxDL_PLUGIN_GUI);
1948bb32
VS
320
321 // return plugin directory on platforms where it makes sense and empty
322 // string on others:
323 static wxString GetPluginsDirectory();
324
1948bb32 325
4104ed92 326protected:
defbed48 327 // common part of GetSymbol() and HasSymbol()
a018a119
VZ
328 void *DoGetSymbol(const wxString& name, bool *success = 0) const;
329
da55d064
VZ
330#ifdef wxHAVE_DYNLIB_ERROR
331 // log the error after a dlxxx() function failure
332 static void Error();
333#endif // wxHAVE_DYNLIB_ERROR
334
a018a119 335
4104ed92 336 // platform specific shared lib suffix.
00711afd 337 static const wxString ms_dllext;
1948bb32 338
4104ed92 339 // the handle to DLL or NULL
1948bb32
VS
340 wxDllType m_handle;
341
4104ed92
VZ
342 // no copy ctor/assignment operators (or we'd try to unload the library
343 // twice)
1948bb32
VS
344 DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
345};
346
347
7b0bfbb2 348// ----------------------------------------------------------------------------
7a4b9130 349// Interesting defines
7b0bfbb2 350// ----------------------------------------------------------------------------
7a4b9130 351
f4a8c29f 352#define WXDLL_ENTRY_FUNCTION() \
ad8b3990 353extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
1b733817 354const wxClassInfo *wxGetClassFirst() { \
856d2e52 355 return wxClassInfo::GetFirst(); \
f4a8c29f 356}
7a4b9130 357
8a0d4cf6
VZ
358#endif // wxUSE_DYNLIB_CLASS
359
7b0bfbb2 360#endif // _WX_DYNLIB_H__