path_from_handle.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright Antony Polukhin, 2015-2025.
  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. #ifndef BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
  8. #define BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP
  9. #include <boost/dll/config.hpp>
  10. #include <boost/dll/detail/system_error.hpp>
  11. #include <boost/winapi/dll.hpp>
  12. #include <boost/winapi/get_last_error.hpp>
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. # pragma once
  15. #endif
  16. namespace boost { namespace dll { namespace detail {
  17. inline std::error_code last_error_code() noexcept {
  18. boost::winapi::DWORD_ err = boost::winapi::GetLastError();
  19. return std::error_code(
  20. static_cast<int>(err),
  21. std::system_category()
  22. );
  23. }
  24. inline boost::dll::fs::path path_from_handle(boost::winapi::HMODULE_ handle, std::error_code &ec) {
  25. constexpr boost::winapi::DWORD_ ERROR_INSUFFICIENT_BUFFER_ = 0x7A;
  26. constexpr boost::winapi::DWORD_ DEFAULT_PATH_SIZE_ = 260;
  27. ec.clear();
  28. {
  29. // If `handle` parameter is NULL, GetModuleFileName retrieves the path of the
  30. // executable file of the current process.
  31. boost::winapi::WCHAR_ path_hldr[DEFAULT_PATH_SIZE_];
  32. const boost::winapi::DWORD_ size = boost::winapi::GetModuleFileNameW(handle, path_hldr, DEFAULT_PATH_SIZE_);
  33. // If the function succeeds, the return value is the length of the string that is copied to the
  34. // buffer, in characters, not including the terminating null character. If the buffer is too
  35. // small to hold the module name, the string is truncated to nSize characters including the
  36. // terminating null character, the function returns nSize, and the function sets the last
  37. // error to ERROR_INSUFFICIENT_BUFFER.
  38. if (size != 0 && size < DEFAULT_PATH_SIZE_) {
  39. // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
  40. return boost::dll::fs::path(path_hldr);
  41. }
  42. ec = boost::dll::detail::last_error_code();
  43. }
  44. for (boost::winapi::DWORD_ new_size = 1024; new_size < 1024 * 1024 && static_cast<boost::winapi::DWORD_>(ec.value()) == ERROR_INSUFFICIENT_BUFFER_; new_size *= 2) {
  45. std::wstring p(new_size, L'\0');
  46. const std::size_t size = boost::winapi::GetModuleFileNameW(handle, &p[0], static_cast<boost::winapi::DWORD_>(p.size()));
  47. if (size != 0 && size < p.size()) {
  48. // On success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS. Resetting it manually.
  49. ec.clear();
  50. p.resize(size);
  51. return boost::dll::fs::path(std::move(p));
  52. }
  53. ec = boost::dll::detail::last_error_code();
  54. }
  55. // Error other than ERROR_INSUFFICIENT_BUFFER_ occurred or failed to allocate buffer big enough.
  56. return boost::dll::fs::path();
  57. }
  58. }}} // namespace boost::dll::detail
  59. #endif // BOOST_DLL_DETAIL_WINDOWS_PATH_FROM_HANDLE_HPP