1 ///////////////////////////////////////////////////////////////////////////// 2 // Name: wx/dynlib.h 3 // Purpose: Dynamic library loading classes 4 // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik 5 // Modified by: 6 // Created: 20/07/98 7 // RCS-ID: $Id: dynlib.h 61872 2009-09-09 22:37:05Z VZ $ 8 // Copyright: (c) 1998 Guilhem Lavaux 9 // Licence: wxWindows licence 10 ///////////////////////////////////////////////////////////////////////////// 11 12 #ifndef _WX_DYNLIB_H__ 13 #define _WX_DYNLIB_H__ 14 15 #include "wx/defs.h" 16 17 #if wxUSE_DYNLIB_CLASS 18 19 #include "wx/string.h" 20 #include "wx/dynarray.h" 21 22 #if defined(__OS2__) || defined(__EMX__) 23 #include "wx/os2/private.h" 24 #endif 25 26 #ifdef __WXMSW__ 27 #include "wx/msw/private.h" 28 #endif 29 30 // note that we have our own dlerror() implementation under Darwin 31 #if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__) 32 #define wxHAVE_DYNLIB_ERROR 33 #endif 34 35 class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator; 36 37 // ---------------------------------------------------------------------------- 38 // conditional compilation 39 // ---------------------------------------------------------------------------- 40 41 // Note: __OS2__/EMX has to be tested first, since we want to use 42 // native version, even if configure detected presence of DLOPEN. 43 #if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__) 44 typedef HMODULE wxDllType; 45 #elif defined(__DARWIN__) 46 // Don't include dlfcn.h on Darwin, we may be using our own replacements. 47 typedef void *wxDllType; 48 #elif defined(HAVE_DLOPEN) 49 #include <dlfcn.h> 50 typedef void *wxDllType; 51 #elif defined(HAVE_SHL_LOAD) 52 #include <dl.h> 53 typedef shl_t wxDllType; 54 #elif defined(__WXMAC__) 55 #include <CodeFragments.h> 56 typedef CFragConnectionID wxDllType; 57 #else 58 #error "Dynamic Loading classes can't be compiled on this platform, sorry." 59 #endif 60 61 // ---------------------------------------------------------------------------- 62 // constants 63 // ---------------------------------------------------------------------------- 64 65 enum wxDLFlags 66 { 67 wxDL_LAZY = 0x00000001, // resolve undefined symbols at first use 68 // (only works on some Unix versions) 69 wxDL_NOW = 0x00000002, // resolve undefined symbols on load 70 // (default, always the case under Win32) 71 wxDL_GLOBAL = 0x00000004, // export extern symbols to subsequently 72 // loaded libs. 73 wxDL_VERBATIM = 0x00000008, // attempt to load the supplied library 74 // name without appending the usual dll 75 // filename extension. 76 wxDL_NOSHARE = 0x00000010, // load new DLL, don't reuse already loaded 77 // (only for wxPluginManager) 78 79 wxDL_QUIET = 0x00000020, // don't log an error if failed to load 80 81 #if wxABI_VERSION >= 20810 82 // this flag is dangerous, for internal use of wxMSW only, don't use at all 83 // and especially don't use directly, use wxLoadedDLL instead if you really 84 // do need it 85 wxDL_GET_LOADED = 0x00000040, // Win32 only: return handle of already 86 // loaded DLL or NULL otherwise; Unload() 87 // should not be called so don't forget to 88 // Detach() if you use this function 89 #endif // wx 2.8.10+ 90 91 wxDL_DEFAULT = wxDL_NOW // default flags correspond to Win32 92 }; 93 94 enum wxDynamicLibraryCategory 95 { 96 wxDL_LIBRARY, // standard library 97 wxDL_MODULE // loadable module/plugin 98 }; 99 100 enum wxPluginCategory 101 { 102 wxDL_PLUGIN_GUI, // plugin that uses GUI classes 103 wxDL_PLUGIN_BASE // wxBase-only plugin 104 }; 105 106 // ---------------------------------------------------------------------------- 107 // macros 108 // ---------------------------------------------------------------------------- 109 110 // when loading a function from a DLL you always have to cast the returned 111 // "void *" pointer to the correct type and, even more annoyingly, you have to 112 // repeat this type twice if you want to declare and define a function pointer 113 // all in one line 114 // 115 // this macro makes this slightly less painful by allowing you to specify the 116 // type only once, as the first parameter, and creating a variable of this type 117 // called "pfn<name>" initialized with the "name" from the "dynlib" 118 #define wxDYNLIB_FUNCTION(type, name, dynlib) \ 119 type pfn ## name = (type)(dynlib).GetSymbol(wxT(#name)) 120 121 // ---------------------------------------------------------------------------- 122 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary 123 // ---------------------------------------------------------------------------- 124 125 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails 126 { 127 public: 128 // ctor, normally never used as these objects are only created by 129 // wxDynamicLibrary::ListLoaded() wxDynamicLibraryDetails()130 wxDynamicLibraryDetails() { m_address = NULL; m_length = 0; } 131 132 // get the (base) name GetName()133 wxString GetName() const { return m_name; } 134 135 // get the full path of this object GetPath()136 wxString GetPath() const { return m_path; } 137 138 // get the load address and the extent, return true if this information is 139 // available GetAddress(void ** addr,size_t * len)140 bool GetAddress(void **addr, size_t *len) const 141 { 142 if ( !m_address ) 143 return false; 144 145 if ( addr ) 146 *addr = m_address; 147 if ( len ) 148 *len = m_length; 149 150 return true; 151 } 152 153 // return the version of the DLL (may be empty if no version info) GetVersion()154 wxString GetVersion() const 155 { 156 return m_version; 157 } 158 159 private: 160 wxString m_name, 161 m_path, 162 m_version; 163 164 void *m_address; 165 size_t m_length; 166 167 friend class wxDynamicLibraryDetailsCreator; 168 }; 169 170 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails, 171 wxDynamicLibraryDetailsArray, 172 WXDLLIMPEXP_BASE); 173 174 // ---------------------------------------------------------------------------- 175 // wxDynamicLibrary: represents a handle to a DLL/shared object 176 // ---------------------------------------------------------------------------- 177 178 class WXDLLIMPEXP_BASE wxDynamicLibrary 179 { 180 public: 181 // return a valid handle for the main program itself or NULL if back 182 // linking is not supported by the current platform (e.g. Win32) 183 static wxDllType GetProgramHandle(); 184 185 // return the platform standard DLL extension (with leading dot) GetDllExt()186 static const wxChar *GetDllExt() { return ms_dllext; } 187 wxDynamicLibrary()188 wxDynamicLibrary() : m_handle(0) { } 189 wxDynamicLibrary(const wxString& libname, int flags = wxDL_DEFAULT) 190 : m_handle(0) 191 { 192 Load(libname, flags); 193 } 194 195 // NOTE: this class is (deliberately) not virtual, do not attempt 196 // to use it polymorphically. ~wxDynamicLibrary()197 ~wxDynamicLibrary() { Unload(); } 198 199 // return true if the library was loaded successfully IsLoaded()200 bool IsLoaded() const { return m_handle != 0; } 201 202 // load the library with the given name (full or not), return true if ok 203 bool Load(const wxString& libname, int flags = wxDL_DEFAULT); 204 205 // raw function for loading dynamic libs: always behaves as if 206 // wxDL_VERBATIM were specified and doesn't log error message if the 207 // library couldn't be loaded but simply returns NULL 208 static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT); 209 210 // detach the library object from its handle, i.e. prevent the object from 211 // unloading the library in its dtor -- the caller is now responsible for 212 // doing this Detach()213 wxDllType Detach() { wxDllType h = m_handle; m_handle = 0; return h; } 214 215 // unload the given library handle (presumably returned by Detach() before) 216 static void Unload(wxDllType handle); 217 218 // unload the library, also done automatically in dtor Unload()219 void Unload() { if ( IsLoaded() ) { Unload(m_handle); m_handle = 0; } } 220 221 // Return the raw handle from dlopen and friends. GetLibHandle()222 wxDllType GetLibHandle() const { return m_handle; } 223 224 // check if the given symbol is present in the library, useful to verify if 225 // a loadable module is our plugin, for example, without provoking error 226 // messages from GetSymbol() HasSymbol(const wxString & name)227 bool HasSymbol(const wxString& name) const 228 { 229 bool ok; 230 DoGetSymbol(name, &ok); 231 return ok; 232 } 233 234 // resolve a symbol in a loaded DLL, such as a variable or function name. 235 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to 236 // export unmangled names) 237 // 238 // Since it is perfectly valid for the returned symbol to actually be NULL, 239 // that is not always indication of an error. Pass and test the parameter 240 // 'success' for a true indication of success or failure to load the 241 // symbol. 242 // 243 // Returns a pointer to the symbol on success, or NULL if an error occurred 244 // or the symbol wasn't found. 245 void *GetSymbol(const wxString& name, bool *success = NULL) const; 246 247 // low-level version of GetSymbol() 248 static void *RawGetSymbol(wxDllType handle, const wxString& name); RawGetSymbol(const wxString & name)249 void *RawGetSymbol(const wxString& name) const 250 { 251 #if defined (__WXPM__) || defined(__EMX__) 252 return GetSymbol(name); 253 #else 254 return RawGetSymbol(m_handle, name); 255 #endif 256 } 257 258 #ifdef __WXMSW__ 259 // this function is useful for loading functions from the standard Windows 260 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or 261 // wide character build) suffix if they take string parameters RawGetSymbolAorW(wxDllType handle,const wxString & name)262 static void *RawGetSymbolAorW(wxDllType handle, const wxString& name) 263 { 264 return RawGetSymbol 265 ( 266 handle, 267 name + 268 #if wxUSE_UNICODE 269 L'W' 270 #else 271 'A' 272 #endif 273 ); 274 } 275 GetSymbolAorW(const wxString & name)276 void *GetSymbolAorW(const wxString& name) const 277 { 278 return RawGetSymbolAorW(m_handle, name); 279 } 280 #endif // __WXMSW__ 281 282 // return all modules/shared libraries in the address space of this process 283 // 284 // returns an empty array if not implemented or an error occurred 285 static wxDynamicLibraryDetailsArray ListLoaded(); 286 287 // return platform-specific name of dynamic library with proper extension 288 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux) 289 static wxString CanonicalizeName(const wxString& name, 290 wxDynamicLibraryCategory cat = wxDL_LIBRARY); 291 292 // return name of wxWidgets plugin (adds compiler and version info 293 // to the filename): 294 static wxString 295 CanonicalizePluginName(const wxString& name, 296 wxPluginCategory cat = wxDL_PLUGIN_GUI); 297 298 // return plugin directory on platforms where it makes sense and empty 299 // string on others: 300 static wxString GetPluginsDirectory(); 301 302 303 protected: 304 // common part of GetSymbol() and HasSymbol() 305 void *DoGetSymbol(const wxString& name, bool *success = 0) const; 306 307 #ifdef wxHAVE_DYNLIB_ERROR 308 // log the error after a dlxxx() function failure 309 static void Error(); 310 #endif // wxHAVE_DYNLIB_ERROR 311 312 313 // platform specific shared lib suffix. 314 static const wxChar *ms_dllext; 315 316 // the handle to DLL or NULL 317 wxDllType m_handle; 318 319 // no copy ctor/assignment operators (or we'd try to unload the library 320 // twice) 321 DECLARE_NO_COPY_CLASS(wxDynamicLibrary) 322 }; 323 324 #if defined(__WXMSW__) && wxABI_VERSION >= 20810 325 326 // ---------------------------------------------------------------------------- 327 // wxLoadedDLL is a MSW-only internal helper class allowing to dynamically bind 328 // to a DLL already loaded into the project address space 329 // ---------------------------------------------------------------------------- 330 331 class wxLoadedDLL : public wxDynamicLibrary 332 { 333 public: wxLoadedDLL(const wxString & dllname)334 wxLoadedDLL(const wxString& dllname) 335 : wxDynamicLibrary(dllname, wxDL_GET_LOADED | wxDL_VERBATIM | wxDL_QUIET) 336 { 337 } 338 ~wxLoadedDLL()339 ~wxLoadedDLL() 340 { 341 Detach(); 342 } 343 }; 344 345 #endif // __WXMSW__ 346 347 // ---------------------------------------------------------------------------- 348 // Interesting defines 349 // ---------------------------------------------------------------------------- 350 351 #define WXDLL_ENTRY_FUNCTION() \ 352 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \ 353 const wxClassInfo *wxGetClassFirst() { \ 354 return wxClassInfo::GetFirst(); \ 355 } 356 357 #endif // wxUSE_DYNLIB_CLASS 358 359 #endif // _WX_DYNLIB_H__ 360