1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright Antony Polukhin, 2015-2022.
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)
8 #ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
9 #define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
11 #include <boost/dll/config.hpp>
12 #include <boost/dll/shared_library_load_mode.hpp>
13 #include <boost/dll/detail/aggressive_ptr_cast.hpp>
14 #include <boost/dll/detail/system_error.hpp>
15 #include <boost/dll/detail/windows/path_from_handle.hpp>
17 #include <boost/move/utility.hpp>
18 #include <boost/swap.hpp>
20 #include <boost/winapi/dll.hpp>
22 #ifdef BOOST_HAS_PRAGMA_ONCE
26 namespace boost { namespace dll { namespace detail {
28 class shared_library_impl {
29 BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
32 typedef boost::winapi::HMODULE_ native_handle_t;
34 shared_library_impl() BOOST_NOEXCEPT
38 ~shared_library_impl() BOOST_NOEXCEPT {
42 shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
48 shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
53 static boost::dll::fs::path decorate(const boost::dll::fs::path& sl) {
54 boost::dll::fs::path actual_path = sl;
55 actual_path += suffix();
59 void load(boost::dll::fs::path sl, load_mode::type portable_mode, boost::dll::fs::error_code &ec) {
60 typedef boost::winapi::DWORD_ native_mode_t;
61 native_mode_t native_mode = static_cast<native_mode_t>(portable_mode);
64 if (!sl.is_absolute() && !(native_mode & load_mode::search_system_folders)) {
65 boost::dll::fs::error_code current_path_ec;
66 boost::dll::fs::path prog_loc = boost::dll::fs::current_path(current_path_ec);
68 if (!current_path_ec) {
73 native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::search_system_folders);
75 // Trying to open with appended decorations
76 if (!!(native_mode & load_mode::append_decorations)) {
77 native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::append_decorations);
79 if (load_impl(decorate(sl), native_mode, ec)) {
83 // MinGW loves 'lib' prefix and puts it even on Windows platform.
84 const boost::dll::fs::path mingw_load_path = (
86 ? sl.parent_path() / L"lib"
88 ).native() + sl.filename().native() + suffix().native();
89 if (load_impl(mingw_load_path, native_mode, ec)) {
94 // From MSDN: If the string specifies a module name without a path and the
95 // file name extension is omitted, the function appends the default library
96 // extension .dll to the module name.
98 // From experiments: Default library extension appended to the module name even if
99 // we have some path. So we do not check for path, only for extension. We can not be sure that
100 // such behavior remain across all platforms, so we add L"." by hand.
101 if (sl.has_extension()) {
102 handle_ = boost::winapi::LoadLibraryExW(sl.c_str(), 0, native_mode);
104 handle_ = boost::winapi::LoadLibraryExW((sl.native() + L".").c_str(), 0, native_mode);
107 // LoadLibraryExW method is capable of self loading from program_location() path. No special actions
108 // must be taken to allow self loading.
110 ec = boost::dll::detail::last_error_code();
114 bool is_loaded() const BOOST_NOEXCEPT {
115 return (handle_ != 0);
118 void unload() BOOST_NOEXCEPT {
120 boost::winapi::FreeLibrary(handle_);
125 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
126 boost::swap(handle_, rhs.handle_);
129 boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
130 return boost::dll::detail::path_from_handle(handle_, ec);
133 static boost::dll::fs::path suffix() {
137 void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT {
139 // `GetProcAddress` could not be called for libraries loaded with
140 // `LOAD_LIBRARY_AS_DATAFILE`, `LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE`
141 // or `LOAD_LIBRARY_AS_IMAGE_RESOURCE`.
142 ec = boost::dll::fs::make_error_code(
143 boost::dll::fs::errc::operation_not_supported
149 // Judging by the documentation of GetProcAddress
150 // there is no version for UNICODE on desktop/server Windows, because
151 // names of functions are stored in narrow characters.
152 void* const symbol = boost::dll::detail::aggressive_ptr_cast<void*>(
153 boost::winapi::get_proc_address(handle_, sb)
155 if (symbol == NULL) {
156 ec = boost::dll::detail::last_error_code();
162 native_handle_t native() const BOOST_NOEXCEPT {
167 // Returns true if this load attempt should be the last one.
168 bool load_impl(const boost::dll::fs::path &load_path, boost::winapi::DWORD_ mode, boost::dll::fs::error_code &ec) {
169 handle_ = boost::winapi::LoadLibraryExW(load_path.c_str(), 0, mode);
174 ec = boost::dll::detail::last_error_code();
175 if (boost::dll::fs::exists(load_path)) {
176 // decorated path exists : current error is not a bad file descriptor
184 bool is_resource() const BOOST_NOEXCEPT {
186 reinterpret_cast<boost::winapi::ULONG_PTR_>(handle_) & static_cast<boost::winapi::ULONG_PTR_>(3)
190 native_handle_t handle_;
193 }}} // boost::dll::detail
195 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP