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 portable_mode,boost::dll::fs::error_code & ec)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); 62 unload(); 63 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); 67 68 if (!current_path_ec) { 69 prog_loc /= sl; 70 sl.swap(prog_loc); 71 } 72 } 73 native_mode = static_cast<unsigned>(native_mode) & ~static_cast<unsigned>(load_mode::search_system_folders); 74 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); 78 79 if (load_impl(decorate(sl), native_mode, ec)) { 80 return; 81 } 82 83 // MinGW loves 'lib' prefix and puts it even on Windows platform. 84 const boost::dll::fs::path mingw_load_path = ( 85 sl.has_parent_path() 86 ? sl.parent_path() / L"lib" 87 : L"lib" 88 ).native() + sl.filename().native() + suffix().native(); 89 if (load_impl(mingw_load_path, native_mode, ec)) { 90 return; 91 } 92 } 93 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. 97 // 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); 103 } else { 104 handle_ = boost::winapi::LoadLibraryExW((sl.native() + L".").c_str(), 0, native_mode); 105 } 106 107 // LoadLibraryExW method is capable of self loading from program_location() path. No special actions 108 // must be taken to allow self loading. 109 if (!handle_) { 110 ec = boost::dll::detail::last_error_code(); 111 } 112 } 113 is_loaded() const114 bool is_loaded() const BOOST_NOEXCEPT { 115 return (handle_ != 0); 116 } 117 unload()118 void unload() BOOST_NOEXCEPT { 119 if (handle_) { 120 boost::winapi::FreeLibrary(handle_); 121 handle_ = 0; 122 } 123 } 124 swap(shared_library_impl & rhs)125 void swap(shared_library_impl& rhs) BOOST_NOEXCEPT { 126 boost::swap(handle_, rhs.handle_); 127 } 128 full_module_path(boost::dll::fs::error_code & ec) const129 boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const { 130 return boost::dll::detail::path_from_handle(handle_, ec); 131 } 132 suffix()133 static boost::dll::fs::path suffix() { 134 return L".dll"; 135 } 136 symbol_addr(const char * sb,boost::dll::fs::error_code & ec) const137 void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT { 138 if (is_resource()) { 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 144 ); 145 146 return NULL; 147 } 148 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) 154 ); 155 if (symbol == NULL) { 156 ec = boost::dll::detail::last_error_code(); 157 } 158 159 return symbol; 160 } 161 native() const162 native_handle_t native() const BOOST_NOEXCEPT { 163 return handle_; 164 } 165 166 private: 167 // 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)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); 170 if (handle_) { 171 return true; 172 } 173 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 177 return true; 178 } 179 180 ec.clear(); 181 return false; 182 } 183 is_resource() const184 bool is_resource() const BOOST_NOEXCEPT { 185 return false; /*!!( 186 reinterpret_cast<boost::winapi::ULONG_PTR_>(handle_) & static_cast<boost::winapi::ULONG_PTR_>(3) 187 );*/ 188 } 189 190 native_handle_t handle_; 191 }; 192 193 }}} // boost::dll::detail 194 195 #endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP 196