dynlib.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dynlib.h
  3. // Purpose: interface of wxDynamicLibrary and wxDynamicLibraryDetails
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxDynamicLibraryDetails
  9. This class is used for the objects returned by the
  10. wxDynamicLibrary::ListLoaded() method and contains the information about a
  11. single module loaded into the address space of the current process. A
  12. module in this context may be either a dynamic library or the main program
  13. itself.
  14. @library{wxbase}
  15. @category{appmanagement}
  16. */
  17. class wxDynamicLibraryDetails
  18. {
  19. public:
  20. /**
  21. Retrieves the load address and the size of this module.
  22. @param addr
  23. The pointer to the location to return load address in, may be
  24. @NULL.
  25. @param len
  26. Pointer to the location to return the size of this module in
  27. memory in, may be @NULL.
  28. @return @true if the load address and module size were retrieved,
  29. @false if this information is not available.
  30. */
  31. bool GetAddress(void* addr, size_t* len) const;
  32. /**
  33. Returns the base name of this module, e.g.\ @c "kernel32.dll" or
  34. @c "libc-2.3.2.so".
  35. */
  36. wxString GetName() const;
  37. /**
  38. Returns the full path of this module if available, e.g.\ @c "c:\windows\system32\kernel32.dll"
  39. or @c "/lib/libc-2.3.2.so".
  40. */
  41. wxString GetPath() const;
  42. /**
  43. Returns the version of this module, e.g.\ @c "5.2.3790.0" or @c "2.3.2".
  44. The returned string is empty if the version information is not
  45. available.
  46. */
  47. wxString GetVersion() const;
  48. };
  49. /**
  50. Dynamic library category used with wxDynamicLibrary::CanonicalizeName().
  51. */
  52. enum wxDynamicLibraryCategory
  53. {
  54. wxDL_LIBRARY, ///< Standard library.
  55. wxDL_MODULE ///< Loadable module/plugin.
  56. };
  57. /**
  58. Dynamic library plugin category used with
  59. wxDynamicLibrary::CanonicalizePluginName().
  60. */
  61. enum wxPluginCategory
  62. {
  63. wxDL_PLUGIN_GUI, ///< Plugin that uses GUI classes.
  64. wxDL_PLUGIN_BASE ///< wxBase-only plugin.
  65. };
  66. /**
  67. @class wxDynamicLibrary
  68. wxDynamicLibrary is a class representing dynamically loadable library
  69. (Windows DLL, shared library under Unix etc). Just create an object of
  70. this class to load a library and don't worry about unloading it -- it will
  71. be done in the objects destructor automatically.
  72. The following flags can be used with wxDynamicLibrary() or Load():
  73. @beginStyleTable
  74. @style{wxDL_LAZY}
  75. Equivalent of RTLD_LAZY under Unix, ignored elsewhere.
  76. @style{wxDL_NOW}
  77. Equivalent of RTLD_NOW under Unix, ignored elsewhere.
  78. @style{wxDL_GLOBAL}
  79. Equivalent of RTLD_GLOBAL under Unix, ignored elsewhere.
  80. @style{wxDL_VERBATIM}
  81. Don't try to append the appropriate extension to the library name
  82. (this is done by default).
  83. @style{wxDL_DEFAULT}
  84. Default flags, same as wxDL_NOW currently.
  85. @style{wxDL_QUIET}
  86. Don't log an error message if the library couldn't be loaded.
  87. @endStyleTable
  88. @library{wxbase}
  89. @category{appmanagement}
  90. */
  91. class wxDynamicLibrary
  92. {
  93. public:
  94. /**
  95. Default constructor.
  96. */
  97. wxDynamicLibrary();
  98. /**
  99. Constructor. Calls Load() with the given @a name.
  100. */
  101. wxDynamicLibrary(const wxString& name, int flags = wxDL_DEFAULT);
  102. /**
  103. Returns the platform-specific full name for the library called @a name.
  104. E.g. it adds a @c ".dll" extension under Windows and @c "lib" prefix
  105. and @c ".so", @c ".sl" or @c ".dylib" extension under Unix.
  106. @see CanonicalizePluginName()
  107. */
  108. static wxString CanonicalizeName(const wxString& name,
  109. wxDynamicLibraryCategory cat = wxDL_LIBRARY);
  110. /**
  111. This function does the same thing as CanonicalizeName() but for
  112. wxWidgets plugins. The only difference is that compiler and version
  113. information are added to the name to ensure that the plugin which is
  114. going to be loaded will be compatible with the main program.
  115. */
  116. static wxString CanonicalizePluginName(const wxString& name,
  117. wxPluginCategory cat = wxDL_PLUGIN_GUI);
  118. /**
  119. Detaches this object from its library handle, i.e.\ the object will not
  120. unload the library any longer in its destructor but it is now the
  121. callers responsibility to do this using Unload().
  122. */
  123. wxDllType Detach();
  124. /**
  125. Return a valid handle for the main program itself or @NULL if symbols
  126. from the main program can't be loaded on this platform.
  127. */
  128. static wxDllType GetProgramHandle();
  129. /**
  130. Returns pointer to symbol @a name in the library or @NULL if the
  131. library contains no such symbol.
  132. @see wxDYNLIB_FUNCTION()
  133. */
  134. void* GetSymbol(const wxString& name, bool* success = 0) const;
  135. /**
  136. This function is available only under Windows as it is only useful when
  137. dynamically loading symbols from standard Windows DLLs. Such functions
  138. have either @c 'A' (in ANSI build) or @c 'W' (in Unicode, or wide
  139. character build) suffix if they take string parameters. Using this
  140. function, you can use just the base name of the function and the
  141. correct suffix is appended automatically depending on the current
  142. build. Otherwise, this method is identical to GetSymbol().
  143. @onlyfor{wxmsw}
  144. */
  145. void* GetSymbolAorW(const wxString& name) const;
  146. /**
  147. Returns @true if the symbol with the given @a name is present in the
  148. dynamic library, @false otherwise. Unlike GetSymbol(), this function
  149. doesn't log an error message if the symbol is not found.
  150. @since 2.5.4
  151. */
  152. bool HasSymbol(const wxString& name) const;
  153. /**
  154. Returns @true if the library was successfully loaded, @false otherwise.
  155. */
  156. bool IsLoaded() const;
  157. /**
  158. This static method returns a wxArray containing the details of all
  159. modules loaded into the address space of the current project. The array
  160. elements are objects of the type: wxDynamicLibraryDetails. The array
  161. will be empty if an error occurred.
  162. This method is currently implemented only under Win32 and Linux and is
  163. useful mostly for diagnostics purposes.
  164. */
  165. static wxDynamicLibraryDetailsArray ListLoaded();
  166. /**
  167. Loads DLL with the given @a name into memory. The @a flags argument can
  168. be a combination of the styles outlined in the class description.
  169. Returns @true if the library was successfully loaded, @false otherwise.
  170. */
  171. bool Load(const wxString& name, int flags = wxDL_DEFAULT);
  172. /**
  173. Unloads the library from memory. wxDynamicLibrary object automatically
  174. calls this method from its destructor if it had been successfully
  175. loaded.
  176. */
  177. void Unload();
  178. /**
  179. Unloads the library from memory. wxDynamicLibrary object automatically
  180. calls this method from its destructor if it had been successfully
  181. loaded.
  182. This version of Unload() is only used if you need to keep the library
  183. in memory during a longer period of time than the scope of the
  184. wxDynamicLibrary object. In this case you may call Detach() and store
  185. the handle somewhere and call this static method later to unload it.
  186. */
  187. static void Unload(wxDllType handle);
  188. };
  189. // ============================================================================
  190. // Global functions/macros
  191. // ============================================================================
  192. /** @addtogroup group_funcmacro_misc */
  193. //@{
  194. /**
  195. When loading a function from a DLL you always have to cast the returned
  196. <tt>void *</tt> pointer to the correct type and, even more annoyingly, you
  197. have to repeat this type twice if you want to declare and define a function
  198. pointer all in one line.
  199. This macro makes this slightly less painful by allowing you to specify the
  200. type only once, as the first parameter, and creating a variable of this
  201. type named after the function but with @c pfn prefix and initialized with
  202. the function @a name from the wxDynamicLibrary @a dynlib.
  203. @param type
  204. The type of the function.
  205. @param name
  206. The name of the function to load, not a string (without quotes, it is
  207. quoted automatically by the macro).
  208. @param dynlib
  209. The library to load the function from.
  210. @header{wx/dynlib.h}
  211. */
  212. #define wxDYNLIB_FUNCTION(type, name, dynlib)
  213. //@}