Skip to content

Commit

Permalink
Add WindowsMessageBox class
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Jul 25, 2023
1 parent 4f281c8 commit 70f42c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Source/Helpers/PatternNotFoundHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <Utils/StringBuilder.h>

#if IS_WIN32()
#include <Platform/Windows/WindowsMessageBox.h>
#endif

struct PatternToString {
explicit PatternToString(BytePattern pattern)
: pattern{ pattern }
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions Source/Interfaces/InterfaceFinderWithLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <Platform/Macros/IsPlatform.h>

#if IS_WIN32()
#include <Windows.h>
#include <string>
#include <Platform/Windows/WindowsMessageBox.h>
#endif

#include "InterfaceFinder.h"
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Source/Osiris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@
<ClInclude Include="Platform\TypeInfoPrecedingVmt.h" />
<ClInclude Include="Platform\Windows\WindowsDynamicLibrary.h" />
<ClInclude Include="Platform\Windows\PebLdr.h" />
<ClInclude Include="Platform\Windows\WindowsMessageBox.h" />
<ClInclude Include="Platform\Windows\WindowsPlatformApi.h" />
<ClInclude Include="Platform\Windows\PortableExecutable.h" />
<ClInclude Include="Platform\Windows\Win.h" />
Expand Down
3 changes: 3 additions & 0 deletions Source/Osiris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,9 @@
<ClInclude Include="MemoryAllocation\FreeMemoryRegionList.h">
<Filter>MemoryAllocation</Filter>
</ClInclude>
<ClInclude Include="Platform\Windows\WindowsMessageBox.h">
<Filter>Platform\Windows</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="Resources\Shaders\default_vs.hlsl">
Expand Down
27 changes: 27 additions & 0 deletions Source/Platform/Windows/WindowsMessageBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <Windows.h>

#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<decltype(&MessageBoxA)>();
};

0 comments on commit 70f42c6

Please sign in to comment.