Files
UpdateDisplay/Exception.h
2025-12-13 18:19:55 +01:00

66 lines
1.9 KiB
C++

#pragma once
#include <windows.h>
#include <exception>
#include <string>
// For windows system errors
class SystemException : public std::exception
{
public:
SystemException(DWORD error) : _error(error)
{
char buffer[2048];
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error,
LANG_USER_DEFAULT,
buffer,
sizeof(buffer),
nullptr
);
_message = std::string(buffer);
}
virtual const char* what() const { return _message.c_str(); }
virtual DWORD error() const { return _error; }
private:
DWORD _error;
std::string _message;
};
// For display errors (ChangeDisplaySettingsExA)
class DisplayException : public std::exception
{
public:
DisplayException(DWORD error) : _error(error), _message(_num2str(error))
{
}
virtual const char* what() const { return _message; }
virtual DWORD error() const { return _error; }
private:
static const char* _num2str(DWORD error)
{
switch (error)
{
case DISP_CHANGE_SUCCESSFUL: return "The settings change was successful.";
case DISP_CHANGE_BADDUALVIEW: return "The settings change was unsuccessful because the system is DualView capable.";
case DISP_CHANGE_BADFLAGS: return "An invalid set of flags was passed in.";
case DISP_CHANGE_BADMODE: return "The graphics mode is not supported.";
case DISP_CHANGE_BADPARAM: return "An invalid parameter was passed in. This can include an invalid flag or combination of flags.";
case DISP_CHANGE_FAILED: return "The display driver failed the specified graphics mode.";
case DISP_CHANGE_NOTUPDATED: return "Unable to write settings to the registry.";
case DISP_CHANGE_RESTART: return "The computer must be restarted for the graphics mode to work.";
default: return "Unknown error";
}
}
DWORD _error;
const char* _message;
};