mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
win-utils.hpp
Go to the documentation of this file.
1#pragma once
2#include "include/win.hpp"
3#include "library-pch.hpp"
4
10namespace mal_toolkit
11{
18 inline std::string wstring_to_string(std::wstring_view const wstr)
19 {
20 if (wstr.empty())
21 return std::string();
22 int size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), NULL, 0, NULL, NULL);
23 std::string rv(size, 0);
24 WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), rv.data(), size, NULL, NULL);
25 return rv;
26 }
27
34 inline std::wstring string_to_wstring(std::string_view const str)
35 {
36 if (str.empty())
37 return std::wstring();
38 int size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
39 std::wstring rv(size, 0);
40 MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), rv.data(), size);
41 return rv;
42 }
43
53 inline std::string FormatErrorAsString(DWORD errorMessageID)
54 {
55 if (errorMessageID == 0)
56 {
57 return std::string(); // No error message has been recorded
58 }
59
60 LPSTR messageBuffer = nullptr;
61
62 // Ask Win32 to give us the string version of that message ID.
63 // The parameters we pass in, tell Win32 to create the buffer that holds the message for us
64 // (because we don't yet know how long the message string will be).
65 size_t size = FormatMessageA(
66 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
67 errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
68
69 // Copy the error message into a std::string.
70 std::string message(messageBuffer, size);
71
72 // Free the Win32's string's buffer.
73 LocalFree(messageBuffer);
74 return message.substr(0, message.find_last_of("\n", std::max(0ull, message.size() - 5)));
75 }
76} // namespace mal_toolkit
Precompiled header (PCH) file for common headers used across the library.
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition backoffs.hpp:7
std::wstring string_to_wstring(std::string_view const str)
Converts a UTF-8 string to a wide string (UTF16).
Definition win-utils.hpp:34
std::string wstring_to_string(std::wstring_view const wstr)
Converts a wide string (UTF16) to a UTF-8 string.
Definition win-utils.hpp:18
std::string FormatErrorAsString(DWORD errorMessageID)
Formats a Win32 error code as a string.
Definition win-utils.hpp:53
Provides general Windows-specific utilities and includes necessary headers.