File indexing completed on 2025-09-17 08:25:43
0001
0002
0003
0004
0005
0006
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
0037
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
0042
0043
0044
0045
0046 if (size != 0 && size < DEFAULT_PATH_SIZE_) {
0047
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
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
0066 return boost::dll::fs::path();
0067 }
0068
0069 }}}
0070
0071 #endif
0072