HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
libloaderapi.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8#include "win32_error_intf.hpp"
9#include <expected>
10#include <string>
11#include <system_error>
12#include <filesystem>
13
14hi_export_module(hikogui.win32 : libloaderapi);
15
16hi_export namespace hi {
17inline namespace v1 {
18
19[[nodiscard]] inline std::expected<std::filesystem::path, win32_error> win32_GetModuleFileName(HMODULE module_handle = NULL) noexcept
20{
21 std::wstring module_path;
22 auto buffer_size = MAX_PATH; // initial default value = 256
23 static_assert(MAX_PATH != 0);
24
25 // iterative buffer resizing to max value of 32768 (256*2^7)
26 for (std::size_t i = 0; i < 7; ++i) {
27 module_path.resize(buffer_size);
28 auto num_chars = GetModuleFileNameW(module_handle, module_path.data(), buffer_size);
29 if (num_chars == 0) {
30 return std::unexpected{win32_GetLastError()};
31
32 } else if (num_chars < module_path.length()) {
33 module_path.resize(num_chars);
34 return std::filesystem::path{module_path};
35
36 } else {
37 buffer_size *= 2;
38 }
39 }
40 return std::unexpected{win32_error::insufficient_buffer};
41}
42
43} // namespace v1
44}
Rules for working with win32 headers.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T data(T... args)
T resize(T... args)
T length(T... args)
T unexpected(T... args)