From 70f42c6ca65e224e44b24f12965f5c7294c7d44e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Krupi=C5=84ski?= Date: Tue, 25 Jul 2023 22:57:01 +0200 Subject: [PATCH] Add WindowsMessageBox class --- Source/Helpers/PatternNotFoundHandler.cpp | 6 ++++- Source/Interfaces/InterfaceFinderWithLog.h | 5 ++-- Source/Osiris.vcxproj | 1 + Source/Osiris.vcxproj.filters | 3 +++ Source/Platform/Windows/WindowsMessageBox.h | 27 +++++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 Source/Platform/Windows/WindowsMessageBox.h diff --git a/Source/Helpers/PatternNotFoundHandler.cpp b/Source/Helpers/PatternNotFoundHandler.cpp index ec5b412644a..b428da6bda3 100644 --- a/Source/Helpers/PatternNotFoundHandler.cpp +++ b/Source/Helpers/PatternNotFoundHandler.cpp @@ -8,6 +8,10 @@ #include +#if IS_WIN32() +#include +#endif + struct PatternToString { explicit PatternToString(BytePattern pattern) : pattern{ pattern } @@ -38,6 +42,6 @@ void PatternNotFoundHandler::operator()(BytePattern pattern) const { assert(false && "Pattern needs to be updated!"); #if IS_WIN32() - MessageBoxA(nullptr, StringBuilderStorage<300>{}.builder().put("Failed to find pattern:\n", PatternToString{ pattern }).cstring(), "Osiris", MB_OK | MB_ICONWARNING); + WindowsMessageBox{}.showWarning("Osiris", StringBuilderStorage<300>{}.builder().put("Failed to find pattern:\n", PatternToString{ pattern }).cstring()); #endif } diff --git a/Source/Interfaces/InterfaceFinderWithLog.h b/Source/Interfaces/InterfaceFinderWithLog.h index b296a7cbffa..70f0c3a6b0c 100644 --- a/Source/Interfaces/InterfaceFinderWithLog.h +++ b/Source/Interfaces/InterfaceFinderWithLog.h @@ -3,7 +3,8 @@ #include #if IS_WIN32() -#include +#include +#include #endif #include "InterfaceFinder.h" @@ -20,7 +21,7 @@ struct InterfaceFinderWithLog { return foundInterface; #if IS_WIN32() - MessageBoxA(nullptr, ("Failed to find " + std::string{ name } + " interface!").c_str(), "Osiris", MB_OK | MB_ICONERROR); + WindowsMessageBox{}.showError("Osiris", ("Failed to find " + std::string{ name } + " interface!").c_str()); #endif std::exit(EXIT_FAILURE); } diff --git a/Source/Osiris.vcxproj b/Source/Osiris.vcxproj index fe341949e3c..f8fefdd42fc 100644 --- a/Source/Osiris.vcxproj +++ b/Source/Osiris.vcxproj @@ -424,6 +424,7 @@ + diff --git a/Source/Osiris.vcxproj.filters b/Source/Osiris.vcxproj.filters index 2b4ba1ff402..4be965a3849 100644 --- a/Source/Osiris.vcxproj.filters +++ b/Source/Osiris.vcxproj.filters @@ -1222,6 +1222,9 @@ MemoryAllocation + + Platform\Windows + diff --git a/Source/Platform/Windows/WindowsMessageBox.h b/Source/Platform/Windows/WindowsMessageBox.h new file mode 100644 index 00000000000..c732ace60b5 --- /dev/null +++ b/Source/Platform/Windows/WindowsMessageBox.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "WindowsDynamicLibrary.h" + +class WindowsMessageBox { +public: + void showWarning(const char* title, const char* message) const noexcept + { + showMessageBox(title, message, MB_ICONWARNING); + } + + void showError(const char* title, const char* message) const noexcept + { + showMessageBox(title, message, MB_ICONERROR); + } + +private: + void showMessageBox(const char* title, const char* message, unsigned int extraFlags) const noexcept + { + if (messageBoxA) + messageBoxA(nullptr, message, title, MB_OK | extraFlags); + } + + decltype(&MessageBoxA) messageBoxA = WindowsDynamicLibrary{ "USER32.dll" }.getFunctionAddress("MessageBoxA").as(); +};