shared_library_impl.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
  8. #define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/dll/shared_library_load_mode.hpp>
  11. #include <boost/dll/detail/aggressive_ptr_cast.hpp>
  12. #include <boost/dll/detail/system_error.hpp>
  13. #include <boost/dll/detail/windows/path_from_handle.hpp>
  14. #include <boost/move/utility.hpp>
  15. #include <boost/swap.hpp>
  16. #include <boost/winapi/dll.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. # pragma once
  19. #endif
  20. namespace boost { namespace dll { namespace detail {
  21. class shared_library_impl {
  22. BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
  23. public:
  24. typedef boost::winapi::HMODULE_ native_handle_t;
  25. shared_library_impl() BOOST_NOEXCEPT
  26. : handle_(NULL)
  27. {}
  28. ~shared_library_impl() BOOST_NOEXCEPT {
  29. unload();
  30. }
  31. shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
  32. : handle_(sl.handle_)
  33. {
  34. sl.handle_ = NULL;
  35. }
  36. shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
  37. swap(sl);
  38. return *this;
  39. }
  40. static boost::dll::fs::path decorate(const boost::dll::fs::path& sl) {
  41. boost::dll::fs::path actual_path = sl;
  42. actual_path += suffix();
  43. return actual_path;
  44. }
  45. void load(boost::dll::fs::path sl, load_mode::type mode, boost::dll::fs::error_code &ec) {
  46. typedef boost::winapi::DWORD_ native_mode_t;
  47. unload();
  48. if (!sl.is_absolute() && !(mode & load_mode::search_system_folders)) {
  49. boost::dll::fs::error_code current_path_ec;
  50. boost::dll::fs::path prog_loc = boost::dll::fs::current_path(current_path_ec);
  51. if (!current_path_ec) {
  52. prog_loc /= sl;
  53. sl.swap(prog_loc);
  54. }
  55. }
  56. mode &= ~load_mode::search_system_folders;
  57. // Trying to open with appended decorations
  58. if (!!(mode & load_mode::append_decorations)) {
  59. mode &= ~load_mode::append_decorations;
  60. if (load_impl(decorate(sl), static_cast<native_mode_t>(mode), ec)) {
  61. return;
  62. }
  63. // MinGW loves 'lib' prefix and puts it even on Windows platform.
  64. const boost::dll::fs::path mingw_load_path = (
  65. sl.has_parent_path()
  66. ? sl.parent_path() / L"lib"
  67. : L"lib"
  68. ).native() + sl.filename().native() + suffix().native();
  69. if (load_impl(mingw_load_path, static_cast<native_mode_t>(mode), ec)) {
  70. return;
  71. }
  72. }
  73. // From MSDN: If the string specifies a module name without a path and the
  74. // file name extension is omitted, the function appends the default library
  75. // extension .dll to the module name.
  76. //
  77. // From experiments: Default library extension appended to the module name even if
  78. // we have some path. So we do not check for path, only for extension. We can not be sure that
  79. // such behavior remain across all platforms, so we add L"." by hand.
  80. if (sl.has_extension()) {
  81. handle_ = boost::winapi::LoadLibraryExW(sl.c_str(), 0, static_cast<native_mode_t>(mode));
  82. } else {
  83. handle_ = boost::winapi::LoadLibraryExW((sl.native() + L".").c_str(), 0, static_cast<native_mode_t>(mode));
  84. }
  85. // LoadLibraryExW method is capable of self loading from program_location() path. No special actions
  86. // must be taken to allow self loading.
  87. if (!handle_) {
  88. ec = boost::dll::detail::last_error_code();
  89. }
  90. }
  91. bool is_loaded() const BOOST_NOEXCEPT {
  92. return (handle_ != 0);
  93. }
  94. void unload() BOOST_NOEXCEPT {
  95. if (handle_) {
  96. boost::winapi::FreeLibrary(handle_);
  97. handle_ = 0;
  98. }
  99. }
  100. void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
  101. boost::swap(handle_, rhs.handle_);
  102. }
  103. boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
  104. return boost::dll::detail::path_from_handle(handle_, ec);
  105. }
  106. static boost::dll::fs::path suffix() {
  107. return L".dll";
  108. }
  109. void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT {
  110. if (is_resource()) {
  111. // `GetProcAddress` could not be called for libraries loaded with
  112. // `LOAD_LIBRARY_AS_DATAFILE`, `LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE`
  113. // or `LOAD_LIBRARY_AS_IMAGE_RESOURCE`.
  114. ec = boost::dll::fs::make_error_code(
  115. boost::dll::fs::errc::operation_not_supported
  116. );
  117. return NULL;
  118. }
  119. // Judging by the documentation of GetProcAddress
  120. // there is no version for UNICODE on desktop/server Windows, because
  121. // names of functions are stored in narrow characters.
  122. void* const symbol = boost::dll::detail::aggressive_ptr_cast<void*>(
  123. boost::winapi::get_proc_address(handle_, sb)
  124. );
  125. if (symbol == NULL) {
  126. ec = boost::dll::detail::last_error_code();
  127. }
  128. return symbol;
  129. }
  130. native_handle_t native() const BOOST_NOEXCEPT {
  131. return handle_;
  132. }
  133. private:
  134. // Returns true if this load attempt should be the last one.
  135. bool load_impl(const boost::dll::fs::path &load_path, boost::winapi::DWORD_ mode, boost::dll::fs::error_code &ec) {
  136. handle_ = boost::winapi::LoadLibraryExW(load_path.c_str(), 0, mode);
  137. if (handle_) {
  138. return true;
  139. }
  140. ec = boost::dll::detail::last_error_code();
  141. if (boost::dll::fs::exists(load_path)) {
  142. // decorated path exists : current error is not a bad file descriptor
  143. return true;
  144. }
  145. ec.clear();
  146. return false;
  147. }
  148. bool is_resource() const BOOST_NOEXCEPT {
  149. return false; /*!!(
  150. reinterpret_cast<boost::winapi::ULONG_PTR_>(handle_) & static_cast<boost::winapi::ULONG_PTR_>(3)
  151. );*/
  152. }
  153. native_handle_t handle_;
  154. };
  155. }}} // boost::dll::detail
  156. #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP