Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:43

0001 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
0002 // Copyright Antony Polukhin, 2015-2023.
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 boost::dll::fs::error_code last_error_code() BOOST_NOEXCEPT {
0023         boost::winapi::DWORD_ err = boost::winapi::GetLastError();
0024         return boost::dll::fs::error_code(
0025             static_cast<int>(err),
0026             boost::dll::fs::system_category()
0027         );
0028     }
0029 
0030     inline boost::dll::fs::path path_from_handle(boost::winapi::HMODULE_ handle, boost::dll::fs::error_code &ec) {
0031         BOOST_STATIC_CONSTANT(boost::winapi::DWORD_, ERROR_INSUFFICIENT_BUFFER_ = 0x7A);
0032         BOOST_STATIC_CONSTANT(boost::winapi::DWORD_, DEFAULT_PATH_SIZE_ = 260);
0033 
0034         // If `handle` parameter is NULL, GetModuleFileName retrieves the path of the
0035         // executable file of the current process.
0036         boost::winapi::WCHAR_ path_hldr[DEFAULT_PATH_SIZE_];
0037         const boost::winapi::DWORD_ ret = boost::winapi::GetModuleFileNameW(handle, path_hldr, DEFAULT_PATH_SIZE_);
0038         if (ret) {
0039             // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
0040             ec.clear();
0041             return boost::dll::fs::path(path_hldr);
0042         }
0043 
0044         ec = boost::dll::detail::last_error_code();
0045         for (unsigned i = 2; i < 1025 && static_cast<boost::winapi::DWORD_>(ec.value()) == ERROR_INSUFFICIENT_BUFFER_; i *= 2) {
0046             std::wstring p(DEFAULT_PATH_SIZE_ * i, L'\0');
0047             const std::size_t size = boost::winapi::GetModuleFileNameW(handle, &p[0], DEFAULT_PATH_SIZE_ * i);
0048             if (size) {
0049                 // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
0050                 ec.clear();
0051                 p.resize(size);
0052                 return boost::dll::fs::path(p);
0053             }
0054 
0055             ec = boost::dll::detail::last_error_code();
0056         }
0057 
0058         // Error other than ERROR_INSUFFICIENT_BUFFER_ occurred or failed to allocate buffer big enough.
0059         return boost::dll::fs::path();
0060     }
0061 
0062 }}} // namespace boost::dll::detail
0063 
0064 #endif // BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
0065