dynlib.h 15 KB

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