shared_library_impl.hpp 6.4 KB

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