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