]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynlib.h | |
e54c96f1 | 3 | // Purpose: interface of wxDynamicLibraryDetails |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxDynamicLibraryDetails | |
11 | @wxheader{dynlib.h} | |
7c913512 FM |
12 | |
13 | This class is used for the objects returned by | |
23324ae1 FM |
14 | wxDynamicLibrary::ListLoaded method and |
15 | contains the information about a single module loaded into the address space of | |
16 | the current process. A module in this context may be either a dynamic library | |
17 | or the main program itself. | |
7c913512 | 18 | |
23324ae1 FM |
19 | @library{wxbase} |
20 | @category{FIXME} | |
21 | */ | |
7c913512 | 22 | class wxDynamicLibraryDetails |
23324ae1 FM |
23 | { |
24 | public: | |
25 | /** | |
26 | Retrieves the load address and the size of this module. | |
3c4f71cc | 27 | |
7c913512 | 28 | @param addr |
4cc4bfaf FM |
29 | the pointer to the location to return load address in, may be |
30 | @NULL | |
7c913512 | 31 | @param len |
4cc4bfaf FM |
32 | pointer to the location to return the size of this module in |
33 | memory in, may be @NULL | |
3c4f71cc | 34 | |
23324ae1 | 35 | @returns @true if the load address and module size were retrieved, @false |
4cc4bfaf | 36 | if this information is not available. |
23324ae1 | 37 | */ |
328f5751 | 38 | bool GetAddress(void** addr, size_t len) const; |
23324ae1 FM |
39 | |
40 | /** | |
7c913512 | 41 | Returns the base name of this module, e.g. @c kernel32.dll or |
23324ae1 FM |
42 | @c libc-2.3.2.so. |
43 | */ | |
328f5751 | 44 | wxString GetName() const; |
23324ae1 FM |
45 | |
46 | /** | |
7c913512 FM |
47 | Returns the full path of this module if available, e.g. |
48 | @c c:\windows\system32\kernel32.dll or | |
23324ae1 FM |
49 | @c /lib/libc-2.3.2.so. |
50 | */ | |
328f5751 | 51 | wxString GetPath() const; |
23324ae1 FM |
52 | |
53 | /** | |
7c913512 | 54 | Returns the version of this module, e.g. @c 5.2.3790.0 or |
23324ae1 FM |
55 | @c 2.3.2. The returned string is empty if the version information is not |
56 | available. | |
57 | */ | |
328f5751 | 58 | wxString GetVersion() const; |
23324ae1 FM |
59 | }; |
60 | ||
61 | ||
e54c96f1 | 62 | |
23324ae1 FM |
63 | /** |
64 | @class wxDllLoader | |
65 | @wxheader{dynlib.h} | |
7c913512 | 66 | |
23324ae1 FM |
67 | @b Deprecation note: This class is deprecated since version 2.4 and is |
68 | not compiled in by default in version 2.6 and will be removed in 2.8. Please | |
69 | use wxDynamicLibrary instead. | |
7c913512 | 70 | |
23324ae1 FM |
71 | wxDllLoader is a class providing an interface similar to Unix's @c dlopen(). It |
72 | is used by the wxLibrary framework and manages the actual | |
73 | loading of shared libraries and the resolving of symbols in them. There are no | |
74 | instances of this class, it simply serves as a namespace for its static member | |
75 | functions. | |
7c913512 FM |
76 | |
77 | Please note that class wxDynamicLibrary provides | |
23324ae1 | 78 | alternative, friendlier interface to wxDllLoader. |
7c913512 | 79 | |
23324ae1 | 80 | The terms @e DLL and @e shared library/object will both be used in the |
7c913512 | 81 | documentation to refer to the same thing: a @c .dll file under Windows or |
23324ae1 | 82 | @c .so or @c .sl one under Unix. |
7c913512 | 83 | |
23324ae1 | 84 | Example of using this class to dynamically load the @c strlen() function: |
7c913512 | 85 | |
23324ae1 FM |
86 | @code |
87 | #if defined(__WXMSW__) | |
88 | static const wxChar *LIB_NAME = _T("kernel32"); | |
89 | static const wxChar *FUNC_NAME = _T("lstrlenA"); | |
90 | #elif defined(__UNIX__) | |
91 | static const wxChar *LIB_NAME = _T("/lib/libc-2.0.7.so"); | |
92 | static const wxChar *FUNC_NAME = _T("strlen"); | |
93 | #endif | |
7c913512 | 94 | |
23324ae1 FM |
95 | wxDllType dllHandle = wxDllLoader::LoadLibrary(LIB_NAME); |
96 | if ( !dllHandle ) | |
97 | { | |
98 | ... error ... | |
99 | } | |
100 | else | |
101 | { | |
102 | typedef int (*strlenType)(char *); | |
103 | strlenType pfnStrlen = (strlenType)wxDllLoader::GetSymbol(dllHandle, | |
104 | FUNC_NAME); | |
105 | if ( !pfnStrlen ) | |
106 | { | |
107 | ... error ... | |
108 | } | |
109 | else | |
110 | { | |
111 | if ( pfnStrlen("foo") != 3 ) | |
112 | { | |
113 | ... error ... | |
114 | } | |
115 | else | |
116 | { | |
117 | ... ok! ... | |
118 | } | |
119 | } | |
7c913512 | 120 | |
23324ae1 FM |
121 | wxDllLoader::UnloadLibrary(dllHandle); |
122 | } | |
123 | @endcode | |
7c913512 | 124 | |
23324ae1 FM |
125 | @library{wxbase} |
126 | @category{appmanagement} | |
127 | */ | |
7c913512 | 128 | class wxDllLoader |
23324ae1 FM |
129 | { |
130 | public: | |
131 | /** | |
132 | Returns the string containing the usual extension for shared libraries for the | |
133 | given systems (including the leading dot if not empty). | |
7c913512 | 134 | For example, this function will return @c ".dll" under Windows or (usually) |
23324ae1 FM |
135 | @c ".so" under Unix. |
136 | */ | |
137 | static wxString GetDllExt(); | |
138 | ||
139 | /** | |
140 | This function returns a valid handle for the main program itself. Notice that | |
141 | the @NULL return value is valid for some systems (i.e. doesn't mean that | |
142 | the function failed). | |
1f1d2182 | 143 | @note This function is Unix specific. It will always fail under Windows |
23324ae1 FM |
144 | or OS/2. |
145 | */ | |
146 | wxDllType GetProgramHandle(); | |
147 | ||
148 | /** | |
149 | This function resolves a symbol in a loaded DLL, such as a variable or | |
150 | function name. | |
23324ae1 FM |
151 | Returned value will be @NULL if the symbol was not found in the DLL or if |
152 | an error occurred. | |
3c4f71cc | 153 | |
7c913512 | 154 | @param dllHandle |
4cc4bfaf FM |
155 | Valid handle previously returned by |
156 | LoadLibrary | |
7c913512 | 157 | @param name |
4cc4bfaf | 158 | Name of the symbol. |
23324ae1 | 159 | */ |
4cc4bfaf | 160 | void* GetSymbol(wxDllType dllHandle, const wxString& name); |
23324ae1 FM |
161 | |
162 | /** | |
4cc4bfaf | 163 | This function loads a shared library into memory, with @a libname being the |
23324ae1 FM |
164 | name of the library: it may be either the full name including path and |
165 | (platform-dependent) extension, just the basename (no path and no extension) | |
166 | or a basename with extension. In the last two cases, the library will be | |
167 | searched in all standard locations. | |
4cc4bfaf | 168 | Returns a handle to the loaded DLL. Use @a success parameter to test if it |
7c913512 | 169 | is valid. If the handle is valid, the library must be unloaded later with |
23324ae1 | 170 | UnloadLibrary(). |
3c4f71cc | 171 | |
7c913512 | 172 | @param libname |
4cc4bfaf | 173 | Name of the shared object to load. |
7c913512 | 174 | @param success |
4cc4bfaf FM |
175 | May point to a bool variable which will be set to @true or |
176 | @false; may also be @NULL. | |
23324ae1 | 177 | */ |
4cc4bfaf FM |
178 | wxDllType LoadLibrary(const wxString& libname, |
179 | bool* success = NULL); | |
23324ae1 FM |
180 | |
181 | /** | |
4cc4bfaf | 182 | This function unloads the shared library. The handle @a dllhandle must have |
23324ae1 FM |
183 | been returned by LoadLibrary() previously. |
184 | */ | |
185 | void UnloadLibrary(wxDllType dllhandle); | |
186 | }; | |
187 | ||
188 | ||
e54c96f1 | 189 | |
23324ae1 FM |
190 | /** |
191 | @class wxDynamicLibrary | |
192 | @wxheader{dynlib.h} | |
7c913512 | 193 | |
23324ae1 FM |
194 | wxDynamicLibrary is a class representing dynamically loadable library |
195 | (Windows DLL, shared library under Unix etc.). Just create an object of | |
196 | this class to load a library and don't worry about unloading it -- it will be | |
197 | done in the objects destructor automatically. | |
7c913512 | 198 | |
23324ae1 FM |
199 | @library{wxbase} |
200 | @category{FIXME} | |
7c913512 | 201 | |
e54c96f1 | 202 | @see wxDynamicLibrary::CanonicalizePluginName |
23324ae1 | 203 | */ |
7c913512 | 204 | class wxDynamicLibrary |
23324ae1 FM |
205 | { |
206 | public: | |
207 | //@{ | |
208 | /** | |
209 | Constructor. Second form calls Load(). | |
210 | */ | |
211 | wxDynamicLibrary(); | |
7c913512 FM |
212 | wxDynamicLibrary(const wxString& name, |
213 | int flags = wxDL_DEFAULT); | |
23324ae1 FM |
214 | //@} |
215 | ||
216 | /** | |
217 | Returns the platform-specific full name for the library called @e name. E.g. | |
7c913512 | 218 | it adds a @c ".dll" extension under Windows and @c "lib" prefix and |
23324ae1 | 219 | @c ".so", @c ".sl" or maybe @c ".dylib" extension under Unix. |
4cc4bfaf | 220 | The possible values for @a cat are: |
3c4f71cc VS |
221 | |
222 | ||
23324ae1 | 223 | wxDL_LIBRARY |
3c4f71cc | 224 | |
23324ae1 | 225 | normal library |
3c4f71cc | 226 | |
23324ae1 | 227 | wxDL_MODULE |
3c4f71cc | 228 | |
23324ae1 | 229 | a loadable module or plugin |
3c4f71cc | 230 | |
4cc4bfaf | 231 | @see CanonicalizePluginName() |
23324ae1 FM |
232 | */ |
233 | static wxString CanonicalizeName(const wxString& name, | |
234 | wxDynamicLibraryCategory cat = wxDL_LIBRARY); | |
235 | ||
236 | /** | |
7c913512 | 237 | This function does the same thing as |
23324ae1 FM |
238 | CanonicalizeName() but for wxWidgets |
239 | plugins. The only difference is that compiler and version information are added | |
240 | to the name to ensure that the plugin which is going to be loaded will be | |
241 | compatible with the main program. | |
4cc4bfaf | 242 | The possible values for @a cat are: |
3c4f71cc VS |
243 | |
244 | ||
23324ae1 | 245 | wxDL_PLUGIN_GUI |
3c4f71cc | 246 | |
23324ae1 | 247 | plugin which uses GUI classes (default) |
3c4f71cc | 248 | |
23324ae1 | 249 | wxDL_PLUGIN_BASE |
3c4f71cc | 250 | |
23324ae1 FM |
251 | plugin which only uses wxBase |
252 | */ | |
253 | static wxString CanonicalizePluginName(const wxString& name, | |
254 | wxPluginCategory cat = wxDL_PLUGIN_GUI); | |
255 | ||
256 | /** | |
257 | Detaches this object from its library handle, i.e. the object will not unload | |
258 | the library any longer in its destructor but it is now the callers | |
259 | responsibility to do this using Unload(). | |
260 | */ | |
261 | wxDllType Detach(); | |
262 | ||
263 | /** | |
264 | Return a valid handle for the main program itself or @NULL if symbols | |
265 | from the main program can't be loaded on this platform. | |
266 | */ | |
267 | static wxDllType GetProgramHandle(); | |
268 | ||
269 | /** | |
4cc4bfaf | 270 | Returns pointer to symbol @a name in the library or @NULL if the library |
23324ae1 | 271 | contains no such symbol. |
3c4f71cc | 272 | |
e54c96f1 | 273 | @see wxDYNLIB_FUNCTION() |
23324ae1 | 274 | */ |
328f5751 | 275 | void* GetSymbol(const wxString& name) const; |
23324ae1 FM |
276 | |
277 | /** | |
278 | This function is available only under Windows as it is only useful when | |
279 | dynamically loading symbols from standard Windows DLLs. Such functions have | |
280 | either @c 'A' (in ANSI build) or @c 'W' (in Unicode, or wide | |
281 | character build) suffix if they take string parameters. Using this function you | |
282 | can use just the base name of the function and the correct suffix is appende | |
283 | automatically depending on the current build. Otherwise, this method is | |
284 | identical to GetSymbol(). | |
285 | */ | |
328f5751 | 286 | void* GetSymbolAorW(const wxString& name) const; |
23324ae1 FM |
287 | |
288 | /** | |
4cc4bfaf | 289 | Returns @true if the symbol with the given @a name is present in the dynamic |
23324ae1 FM |
290 | library, @false otherwise. Unlike GetSymbol(), |
291 | this function doesn't log an error message if the symbol is not found. | |
3c4f71cc | 292 | |
e54c96f1 | 293 | @wxsince{2.5.4} |
23324ae1 | 294 | */ |
328f5751 | 295 | bool HasSymbol(const wxString& name) const; |
23324ae1 FM |
296 | |
297 | /** | |
298 | Returns @true if the library was successfully loaded, @false otherwise. | |
299 | */ | |
328f5751 | 300 | bool IsLoaded() const; |
23324ae1 FM |
301 | |
302 | /** | |
e54c96f1 | 303 | This static method returns an array() containing the details |
23324ae1 FM |
304 | of all modules loaded into the address space of the current project, the array |
305 | elements are object of @c wxDynamicLibraryDetails class. The array will | |
306 | be empty if an error occurred. | |
23324ae1 FM |
307 | This method is currently implemented only under Win32 and Linux and is useful |
308 | mostly for diagnostics purposes. | |
309 | */ | |
310 | static wxDynamicLibraryDetailsArray ListLoaded(); | |
311 | ||
312 | /** | |
4cc4bfaf | 313 | Loads DLL with the given @a name into memory. The @a flags argument can |
23324ae1 | 314 | be a combination of the following bits: |
3c4f71cc | 315 | |
23324ae1 | 316 | wxDL_LAZY |
3c4f71cc | 317 | |
23324ae1 | 318 | equivalent of RTLD_LAZY under Unix, ignored elsewhere |
3c4f71cc | 319 | |
23324ae1 | 320 | wxDL_NOW |
3c4f71cc | 321 | |
23324ae1 | 322 | equivalent of RTLD_NOW under Unix, ignored elsewhere |
3c4f71cc | 323 | |
23324ae1 | 324 | wxDL_GLOBAL |
3c4f71cc | 325 | |
23324ae1 | 326 | equivalent of RTLD_GLOBAL under Unix, ignored elsewhere |
3c4f71cc | 327 | |
23324ae1 | 328 | wxDL_VERBATIM |
3c4f71cc | 329 | |
23324ae1 FM |
330 | don't try to append the appropriate extension to |
331 | the library name (this is done by default). | |
3c4f71cc | 332 | |
23324ae1 | 333 | wxDL_DEFAULT |
3c4f71cc | 334 | |
23324ae1 | 335 | default flags, same as wxDL_NOW currently |
3c4f71cc | 336 | |
23324ae1 | 337 | wxDL_QUIET |
3c4f71cc | 338 | |
23324ae1 FM |
339 | don't log an error message if the library couldn't be |
340 | loaded. | |
3c4f71cc | 341 | |
23324ae1 FM |
342 | Returns @true if the library was successfully loaded, @false otherwise. |
343 | */ | |
344 | bool Load(const wxString& name, int flags = wxDL_DEFAULT); | |
345 | ||
346 | //@{ | |
347 | /** | |
348 | Unloads the library from memory. wxDynamicLibrary object automatically calls | |
349 | this method from its destructor if it had been successfully loaded. | |
23324ae1 FM |
350 | The second version is only used if you need to keep the library in memory |
351 | during a longer period of time than the scope of the wxDynamicLibrary object. | |
352 | In this case you may call Detach() and store | |
353 | the handle somewhere and call this static method later to unload it. | |
354 | */ | |
355 | void Unload(); | |
7c913512 | 356 | static void Unload(wxDllType handle); |
23324ae1 FM |
357 | //@} |
358 | }; | |
359 | ||
360 | ||
e54c96f1 | 361 | |
23324ae1 FM |
362 | // ============================================================================ |
363 | // Global functions/macros | |
364 | // ============================================================================ | |
365 | ||
7fa7088e BP |
366 | /** @ingroup group_funcmacro_misc */ |
367 | //@{ | |
368 | ||
23324ae1 FM |
369 | /** |
370 | When loading a function from a DLL you always have to cast the returned | |
7fa7088e BP |
371 | <tt>void *</tt> pointer to the correct type and, even more annoyingly, you |
372 | have to repeat this type twice if you want to declare and define a function | |
373 | pointer all in one line. | |
374 | ||
23324ae1 | 375 | This macro makes this slightly less painful by allowing you to specify the |
7fa7088e BP |
376 | type only once, as the first parameter, and creating a variable of this |
377 | type named after the function but with @c pfn prefix and initialized with | |
378 | the function @a name from the wxDynamicLibrary @a dynlib. | |
7c913512 FM |
379 | |
380 | @param type | |
7fa7088e | 381 | The type of the function. |
7c913512 | 382 | @param name |
7fa7088e BP |
383 | The name of the function to load, not a string (without quotes, it is |
384 | quoted automatically by the macro). | |
7c913512 | 385 | @param dynlib |
7fa7088e BP |
386 | The library to load the function from. |
387 | ||
388 | @header{wx/dynlib.h} | |
23324ae1 | 389 | */ |
7fa7088e BP |
390 | #define wxDYNLIB_FUNCTION(type, name, dynlib) |
391 | ||
392 | //@} | |
23324ae1 | 393 |