Skip to content

Commit

Permalink
Message Box Font Fix 1.1: Use built-in WindhawkUtils (#284)
Browse files Browse the repository at this point in the history
* Use built-in WindhawkUtils
  • Loading branch information
aubymori authored Sep 2, 2023
1 parent dd74a66 commit f4942e9
Showing 1 changed file with 13 additions and 98 deletions.
111 changes: 13 additions & 98 deletions mods/msg-box-font-fix.wh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @id msg-box-font-fix
// @name Message Box Font Fix
// @description Fixes the MessageBox font size in 1709+
// @version 1.0
// @version 1.1
// @author aubymori
// @github https://github.com/aubymori
// @include *
Expand Down Expand Up @@ -32,97 +32,7 @@ instead of just using `SystemParametersInfoW` like a normal person.*
*/
// ==/WindhawkModReadme==

#pragma region "WindhawkUtils"
#include <initializer_list>
namespace YukisCoffee::WindhawkUtils
{
struct SymbolHooks
{
PCWSTR symbolName;
void *hookFunction;
void **pOriginalFunction;
};

bool hookWithSymbols(HMODULE module, std::initializer_list<SymbolHooks> hooks, PCWSTR server = NULL)
{
WH_FIND_SYMBOL symbol;
HANDLE findSymbol;

if (!module)
{
Wh_Log(L"Loaded invalid module");
return false;
}

// These functions are terribly named, which is why I wrote this wrapper
// in the first place.
findSymbol = Wh_FindFirstSymbol(module, server, &symbol);

if (findSymbol)
{
do
{
for (SymbolHooks hook : hooks)
{
// If the symbol is unknown, then learn it.
if (!*hook.pOriginalFunction && 0 == wcscmp(symbol.symbol, hook.symbolName))
{
if (hook.hookFunction)
{
// This is unsafe if you make any typo at all.
Wh_SetFunctionHook(
symbol.address,
hook.hookFunction,
hook.pOriginalFunction
);

Wh_Log(
L"Installed hook for symbol %s at %p.",
hook.symbolName,
symbol.address
);
}
else
{
*hook.pOriginalFunction = symbol.address;

Wh_Log(
L"Found symbol %s for %p.",
hook.symbolName,
symbol.address
);
}
}
}
}
while (Wh_FindNextSymbol(findSymbol, &symbol));

Wh_FindCloseSymbol(findSymbol);
}
else
{
Wh_Log(L"Unable to find symbols for module.");
return false;
}

// If a requested symbol is not found at all, then error as such.
for (SymbolHooks hook : hooks)
{
if (!*hook.pOriginalFunction)
{
Wh_Log(
L"Original function for symbol hook %s not found.",
hook.symbolName
);

return false;
}
}

return true;
}
}
#pragma endregion // "WindhawkUtils"
#include <windhawk_utils.h>

typedef HFONT (*GetMessageBoxFontForDpi_t)(UINT);
GetMessageBoxFontForDpi_t GetMessageBoxFontForDpi_orig;
Expand Down Expand Up @@ -160,15 +70,20 @@ BOOL Wh_ModInit()
return FALSE;
}

bool bHookSuccess = YukisCoffee::WindhawkUtils::hookWithSymbols(
hUser32,
WindhawkUtils::SYMBOL_HOOK symbolHooks[] = {
{
{
L"struct HFONT__ * __cdecl GetMessageBoxFontForDpi(unsigned int)",
(void *)GetMessageBoxFontForDpi_hook,
(void **)&GetMessageBoxFontForDpi_orig
}
L"struct HFONT__ * __cdecl GetMessageBoxFontForDpi(unsigned int)"
},
(void **)&GetMessageBoxFontForDpi_orig,
(void *)GetMessageBoxFontForDpi_hook
}
};

bool bHookSuccess = HookSymbols(
hUser32,
symbolHooks,
ARRAYSIZE(symbolHooks)
);

if (!bHookSuccess)
Expand Down

0 comments on commit f4942e9

Please sign in to comment.