HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
winbase.hpp
1// Copyright Take Vos 2024.
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
7#include "../macros.hpp"
8#include "win32_error_intf.hpp"
9#include "stringapiset.hpp"
10#include <string>
11#include <expected>
12#include <Windows.h>
13#include <WinBase.h>
14
15hi_export_module(hikogui.win32 : winbase);
16
17hi_export namespace hi { inline namespace v1 {
18
19[[nodiscard]] inline std::expected<std::string, win32_error> win32_FormatMessage(win32_error error_code) noexcept
20{
21 auto const error_code_ = static_cast<DWORD>(std::to_underlying(error_code));
22
23 // FormatMessageW() is unable to tell what the buffer size should be.
24 // But 64Kbyte is the largest buffer that one should pass.
25 LPWSTR buffer = nullptr;
26 auto const result = ::FormatMessageW(
27 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
28 NULL, // source
29 error_code_,
30 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
31 reinterpret_cast<LPWSTR>(&buffer),
32 0,
33 NULL);
34
35 if (result == 0) {
36 return std::unexpected(win32_GetLastError());
37 }
38
39 auto r = win32_WideCharToMultiByte(std::wstring_view{buffer, result});
40 LocalFree(buffer);
41 return r;
42}
43
44}}
The HikoGUI namespace.
Definition array_generic.hpp:20
std::expected< std::string, win32_error > win32_WideCharToMultiByte(std::wstring_view s, unsigned int code_page=CP_UTF8, uint32_t flags=0) noexcept
Convert a win32-API compatible std::wstring to a multi-byte std::string.
Definition stringapiset.hpp:26
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T unexpected(T... args)