Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:25:43

0001 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
0002 // Copyright Antony Polukhin, 2015-2025.
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt
0006 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
0009 #define BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
0010 
0011 #include <boost/dll/config.hpp>
0012 #include <boost/dll/detail/system_error.hpp>
0013 #include <boost/winapi/dll.hpp>
0014 #include <boost/winapi/get_last_error.hpp>
0015 
0016 #ifdef BOOST_HAS_PRAGMA_ONCE
0017 # pragma once
0018 #endif
0019 
0020 namespace boost { namespace dll { namespace detail {
0021 
0022     inline std::error_code last_error_code() noexcept {
0023         boost::winapi::DWORD_ err = boost::winapi::GetLastError();
0024         return std::error_code(
0025             static_cast<int>(err),
0026             std::system_category()
0027         );
0028     }
0029 
0030     inline boost::dll::fs::path path_from_handle(boost::winapi::HMODULE_ handle, std::error_code &ec) {
0031         constexpr boost::winapi::DWORD_ ERROR_INSUFFICIENT_BUFFER_ = 0x7A;
0032         constexpr boost::winapi::DWORD_ DEFAULT_PATH_SIZE_ = 260;
0033 
0034         ec.clear();
0035 
0036         // If `handle` parameter is NULL, GetModuleFileName retrieves the path of the
0037         // executable file of the current process.
0038         boost::winapi::WCHAR_ path_hldr[DEFAULT_PATH_SIZE_];
0039         const boost::winapi::DWORD_ size = boost::winapi::GetModuleFileNameW(handle, path_hldr, DEFAULT_PATH_SIZE_);
0040 
0041         // If the function succeeds, the return value is the length of the string that is copied to the
0042         // buffer, in characters, not including the terminating null character.  If the buffer is too
0043         // small to hold the module name, the string is truncated to nSize characters including the
0044         // terminating null character, the function returns nSize, and the function sets the last
0045         // error to ERROR_INSUFFICIENT_BUFFER.
0046         if (size != 0 && size < DEFAULT_PATH_SIZE_) {
0047             // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
0048             return boost::dll::fs::path(path_hldr);
0049         }
0050 
0051         ec = boost::dll::detail::last_error_code();
0052         for (boost::winapi::DWORD_ new_size = 1024; new_size < 1024 * 1024 && static_cast<boost::winapi::DWORD_>(ec.value()) == ERROR_INSUFFICIENT_BUFFER_; new_size *= 2) {
0053             std::wstring p(new_size, L'\0');
0054             const std::size_t size = boost::winapi::GetModuleFileNameW(handle, &p[0], static_cast<boost::winapi::DWORD_>(p.size()));
0055             if (size != 0 && size < p.size()) {
0056                 // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
0057                 ec.clear();
0058                 p.resize(size);
0059                 return boost::dll::fs::path(std::move(p));
0060             }
0061 
0062             ec = boost::dll::detail::last_error_code();
0063         }
0064 
0065         // Error other than ERROR_INSUFFICIENT_BUFFER_ occurred or failed to allocate buffer big enough.
0066         return boost::dll::fs::path();
0067     }
0068 
0069 }}} // namespace boost::dll::detail
0070 
0071 #endif // BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
0072