shared_library_impl.hpp 6.3 KB

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