]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynlib.h
Added GetTempDir
[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
defbed48
VZ
35class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator;
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
defbed48
VZ
108// ----------------------------------------------------------------------------
109// wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
110// ----------------------------------------------------------------------------
111
112class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
113{
114public:
115 // ctor, normally never used as these objects are only created by
116 // wxDynamicLibrary::ListLoaded()
117 wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; }
118
119 // get the (base) name
120 wxString GetName() const { return m_name; }
121
122 // get the full path of this object
123 wxString GetPath() const { return m_path; }
124
125 // get the load address and the extent, return true if this information is
126 // available
127 bool GetAddress(void **addr, size_t *len) const
128 {
129 if ( !m_address )
130 return false;
131
132 if ( addr )
133 *addr = m_address;
134 if ( len )
135 *len = m_length;
136
137 return true;
138 }
139
140 // return the version of the DLL (may be empty if no version info)
141 wxString GetVersion() const
142 {
143 return m_version;
144 }
145
146private:
147 wxString m_name,
148 m_path,
149 m_version;
150
151 void *m_address;
152 size_t m_length;
153
154 friend class wxDynamicLibraryDetailsCreator;
155};
156
157WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails,
158 wxDynamicLibraryDetailsArray,
159 WXDLLIMPEXP_BASE);
160
161// ----------------------------------------------------------------------------
162// wxDynamicLibrary: represents a handle to a DLL/shared object
163// ----------------------------------------------------------------------------
1948bb32
VS
164
165class WXDLLIMPEXP_BASE wxDynamicLibrary
166{
167public:
4104ed92
VZ
168 // return a valid handle for the main program itself or NULL if back
169 // linking is not supported by the current platform (e.g. Win32)
1948bb32
VS
170 static wxDllType GetProgramHandle();
171
4104ed92 172 // return the platform standard DLL extension (with leading dot)
1948bb32
VS
173 static const wxChar *GetDllExt() { return ms_dllext; }
174
4104ed92 175 wxDynamicLibrary() : m_handle(0) { }
1948bb32
VS
176 wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT)
177 : m_handle(0)
178 {
179 Load(libname, flags);
180 }
1948bb32 181
4104ed92
VZ
182 // NOTE: this class is (deliberately) not virtual, do not attempt
183 // to use it polymorphically.
184 ~wxDynamicLibrary() { Unload(); }
1948bb32 185
68379eaf 186 // return true if the library was loaded successfully
1948bb32
VS
187 bool IsLoaded() const { return m_handle != 0; }
188
4104ed92 189 // load the library with the given name (full or not), return true if ok
defbed48 190 bool Load(const wxString& libname, int flags = wxDL_DEFAULT);
1948bb32 191
defbed48
VZ
192 // raw function for loading dynamic libs: always behaves as if
193 // wxDL_VERBATIM were specified and doesn't log error message if the
194 // library couldn't be loaded but simply returns NULL
da55d064
VZ
195 static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT);
196
4104ed92
VZ
197 // detach the library object from its handle, i.e. prevent the object from
198 // unloading the library in its dtor -- the caller is now responsible for
199 // doing this
1948bb32
VS
200 wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; }
201
169948a0
VZ
202 // unload the given library handle (presumably returned by Detach() before)
203 static void Unload(wxDllType handle);
204
4104ed92 205 // unload the library, also done automatically in dtor
169948a0 206 void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } }
1948bb32 207
4104ed92 208 // Return the raw handle from dlopen and friends.
1948bb32
VS
209 wxDllType GetLibHandle() const { return m_handle; }
210
a018a119
VZ
211 // check if the given symbol is present in the library, useful to verify if
212 // a loadable module is our plugin, for example, without provoking error
213 // messages from GetSymbol()
214 bool HasSymbol(const wxString& name) const
215 {
216 bool ok;
217 DoGetSymbol(name, &ok);
218 return ok;
219 }
220
4104ed92
VZ
221 // resolve a symbol in a loaded DLL, such as a variable or function name.
222 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
223 // export unmangled names)
224 //
225 // Since it is perfectly valid for the returned symbol to actually be NULL,
226 // that is not always indication of an error. Pass and test the parameter
227 // 'success' for a true indication of success or failure to load the
228 // symbol.
229 //
230 // Returns a pointer to the symbol on success, or NULL if an error occurred
231 // or the symbol wasn't found.
defbed48 232 void *GetSymbol(const wxString& name, bool *success = NULL) const;
1948bb32 233
defbed48
VZ
234 // low-level version of GetSymbol()
235 static void *RawGetSymbol(wxDllType handle, const wxString& name);
236 void *RawGetSymbol(const wxString& name) const
237 {
fd6c9428
SN
238#if defined (__WXPM__) || defined(__EMX__)
239 return GetSymbol(name);
240#else
defbed48 241 return RawGetSymbol(m_handle, name);
fd6c9428 242#endif
defbed48
VZ
243 }
244
93ed8ff7
VZ
245#ifdef __WXMSW__
246 // this function is useful for loading functions from the standard Windows
247 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or
248 // wide character build) suffix if they take string parameters
249 static void *RawGetSymbolAorW(wxDllType handle, const wxString& name)
250 {
251 return RawGetSymbol
252 (
253 handle,
55034339 254 name +
93ed8ff7
VZ
255#if wxUSE_UNICODE
256 L'W'
257#else
258 'A'
259#endif
260 );
261 }
262
263 void *GetSymbolAorW(const wxString& name) const
264 {
265 return RawGetSymbolAorW(m_handle, name);
266 }
267#endif // __WXMSW__
268
defbed48
VZ
269 // return all modules/shared libraries in the address space of this process
270 //
3103e8a9 271 // returns an empty array if not implemented or an error occurred
defbed48 272 static wxDynamicLibraryDetailsArray ListLoaded();
0c32066b 273
1948bb32
VS
274 // return platform-specific name of dynamic library with proper extension
275 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
276 static wxString CanonicalizeName(const wxString& name,
277 wxDynamicLibraryCategory cat = wxDL_LIBRARY);
278
77ffb593 279 // return name of wxWidgets plugin (adds compiler and version info
1948bb32 280 // to the filename):
169948a0
VZ
281 static wxString
282 CanonicalizePluginName(const wxString& name,
283 wxPluginCategory cat = wxDL_PLUGIN_GUI);
1948bb32
VS
284
285 // return plugin directory on platforms where it makes sense and empty
286 // string on others:
287 static wxString GetPluginsDirectory();
288
1948bb32 289
4104ed92 290protected:
defbed48 291 // common part of GetSymbol() and HasSymbol()
a018a119
VZ
292 void *DoGetSymbol(const wxString& name, bool *success = 0) const;
293
da55d064
VZ
294#ifdef wxHAVE_DYNLIB_ERROR
295 // log the error after a dlxxx() function failure
296 static void Error();
297#endif // wxHAVE_DYNLIB_ERROR
298
a018a119 299
4104ed92 300 // platform specific shared lib suffix.
1948bb32
VS
301 static const wxChar *ms_dllext;
302
4104ed92 303 // the handle to DLL or NULL
1948bb32
VS
304 wxDllType m_handle;
305
4104ed92
VZ
306 // no copy ctor/assignment operators (or we'd try to unload the library
307 // twice)
1948bb32
VS
308 DECLARE_NO_COPY_CLASS(wxDynamicLibrary)
309};
310
311
7b0bfbb2 312// ----------------------------------------------------------------------------
7a4b9130 313// Interesting defines
7b0bfbb2 314// ----------------------------------------------------------------------------
7a4b9130 315
f4a8c29f 316#define WXDLL_ENTRY_FUNCTION() \
ad8b3990 317extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
1b733817 318const wxClassInfo *wxGetClassFirst() { \
856d2e52 319 return wxClassInfo::GetFirst(); \
f4a8c29f 320}
7a4b9130 321
8a0d4cf6
VZ
322#endif // wxUSE_DYNLIB_CLASS
323
7b0bfbb2 324#endif // _WX_DYNLIB_H__