Sha256: 0d60afa6347bfb8269fd04ba49b27908b3158036fbe3f7ec25e6ab3c28d62bd8

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

#include <Gosu/Platform.hpp>
#if defined(GOSU_IS_WIN)

#include "WinUtility.hpp"
#include <Gosu/Utility.hpp>
#include <stdexcept>
#include <windows.h>

void Gosu::throw_last_winapi_error(const std::string& action)
{
    // Obtain error message from Windows.
    wchar_t* buffer;
                 
    if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(),
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) &buffer, 0, 0)
            || buffer == nullptr) {
        throw std::runtime_error("Unknown error");
    }

    // Safely move the message into a std::string.
    std::string message;
    try {
        message = wstring_to_utf8(buffer);
    }
    catch (...) {
        LocalFree(buffer);
        throw;
    }
    LocalFree(buffer);
    
    // Optionally prepend the action.
    if (!action.empty()) {
        message = "While " + action + ", the following error occured: " + message;
    }

    throw std::runtime_error(message);
}

#endif

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
gosu-0.12.1 src/WinUtility.cpp
gosu-0.12.0 src/WinUtility.cpp
gosu-0.11.4.pre3 src/WinUtility.cpp
gosu-0.11.4.pre2 src/WinUtility.cpp
gosu-0.11.4.pre1 src/WinUtility.cpp
gosu-0.11.3.1 src/WinUtility.cpp
gosu-0.11.3 src/WinUtility.cpp
gosu-0.11.3.pre1 src/WinUtility.cpp
gosu-0.11.2 src/WinUtility.cpp
gosu-0.11.1 src/WinUtility.cpp
gosu-0.11.1.pre1 src/WinUtility.cpp
gosu-0.11.0 src/WinUtility.cpp