Select Git revision
-
Uwe Wienkop authoredUwe Wienkop authored
Platform_Win32Console.cpp 6.38 KiB
// Win32 console Platform implementation.
#pragma comment(lib, "user32.lib")
// Since we need std::min() and std::max()...
#define NOMINMAX
#include <numeric>
#include <sstream>
#include <vector>
#include <windows.h>
#include "Platform_Win32Console.hpp"
void fatal(std::wstring_view message)
{
MessageBoxW(nullptr, message.data(), L"Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
void fatal_from_lasterror(
std::string_view file, unsigned int line, DWORD lasterror
)
{
auto fail = [lasterror]() {
std::wstring message = L"Error retrieving error string for error " +
std::to_wstring(lasterror) +
L"?!";
fatal(message);
};
LPWSTR formatted = nullptr;
DWORD flags = (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS
);
auto codepoints = FormatMessageW(
flags,
nullptr,
lasterror,
0,
reinterpret_cast<LPWSTR>(&formatted),
0,
nullptr
);
if(codepoints == 0) {
fail();
}
// std::wstring_convert is deprecated... So, MultiByteToWideChar it is.
int file_len = static_cast<int>(file.size());
int file_utf16_len = MultiByteToWideChar(
CP_UTF8, 0, file.data(), file_len, nullptr, 0
);
if(file_utf16_len == 0) {
fail();
}
// The terminating '\0' is already included here.
std::wstring file_utf16(file_utf16_len, '\0');
if(!MultiByteToWideChar(
CP_UTF8, 0, file.data(), file_len, file_utf16.data(), file_utf16_len
)) {
fail();
}
std::wstringstream message;
message << file_utf16 << ":" << std::to_wstring(line) << ": " << formatted;
fatal(message.str());
}