how to hooking d3dx library and change font and size

02/14/2024 22:06 sLowNight2#1
Hello, when I examined the exe I used with ida, the font was created with the "d3dxcreatefontindirecta" function. I tried to hook this function with the minhook library, but the client gave an error. I think my mistake is in calling the function.


My code;
Code:
// Hooks.h
#pragma once

#include "Core.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <d3dx9.h>
#include "MinHook.h"
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "kernel32.lib")

// Connect
typedef int (WINAPI* CONNECT)(SOCKET, const struct sockaddr*, int);
extern CONNECT oConnect;
int WINAPI HookedConnect(SOCKET s, const struct sockaddr* name, int namelen);

// CreateFileA
bool IsAllowedDirectory(const std::string& dir);
std::tuple<std::string, std::string, std::string>SplitPath(const std::string& filePath);
typedef HANDLE(WINAPI* tCreateFileA)(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
extern tCreateFileA oCreateFileA;
HANDLE WINAPI HookedCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
    LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
    DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);

// Font
typedef int(__thiscall* tFont)(char*);
extern tFont oFont;
BOOL hookedFont(char *thisPtr);

typedef HRESULT(WINAPI* tD3DXCreateFontIndirectA)(LPDIRECT3DDEVICE9, CONST D3DXFONT_DESCA*, LPD3DXFONT*);
extern tD3DXCreateFontIndirectA oD3DXCreateFontIndirectA;
HRESULT WINAPI HookedD3DXCreateFontIndirectA(LPDIRECT3DDEVICE9 pDevice, CONST D3DXFONT_DESCA* pDesc, LPD3DXFONT* ppFont);

// Declare the function to install and uninstall the hook
DWORD InstallHook();
DWORD UninstallHook();


// Hooks.cpp
#include "Hooks.h"

DWORD InstallHook()
{
    // Initialize MinHook
    if (MH_Initialize() != MH_OK) {
        // Handle initialization error
        return 1;
    }

    // Create a hook for the connect function
    if (MH_CreateHookApi(L"ws2_32.dll", "connect", &HookedConnect, reinterpret_cast<LPVOID*>(&oConnect)) != MH_OK) {
        // Handle hook creation error
        return 1;
    }

    // Create a hook for the CreateFileA function
    if (MH_CreateHookApi(L"kernel32.dll", "CreateFileA", &HookedCreateFileA, reinterpret_cast<LPVOID*>(&oCreateFileA)) != MH_OK) {
        // Handle hook creation error
        return 1;
    }

    // Create a hook for the Font function
    oFont = reinterpret_cast<tFont>(0x004BDF60); // Replace with the actual address
    if (MH_CreateHook(reinterpret_cast<void*>(oFont), &hookedFont, reinterpret_cast<void**>(&oFont)) != MH_OK) {
        // Handle hook creation error
        return 1;
    }

    if (MH_CreateHookApi(L"D3DX9_42.dll", "D3DXCreateFontIndirectA", &HookedD3DXCreateFontIndirectA, reinterpret_cast<LPVOID*>(&oD3DXCreateFontIndirectA)) != MH_OK) {
        // Handle hook creation error
        MessageBoxA(NULL, "MinHook Error", "MinHook Error", MB_OK);
        return 1;
    }


    // Enable the hook
    if (MH_EnableHook(MH_ALL_HOOKS) != MH_OK) {
        // Handle hook enable error
        return 1;
    }

    return 0;
}

DWORD UninstallHook()
{
    // Disable the hook
    if (MH_DisableHook(MH_ALL_HOOKS) != MH_OK) {
        // Handle hook disable error
        return 1;
    }

    // Uninitialize MinHook
    if (MH_Uninitialize() != MH_OK) {
        // Handle uninitialization error
        return 2;
    }

    return 0;
}

// CreateFileA.cpp
#include "Hooks.h"
#include <Shlwapi.h>

tFont oFont = nullptr;

BOOL hookedFont(char *thisPtr)
{
    std::string fontPath = "G01_GFONT\\gigassoft_12.ttf";

    if (!PathFileExistsA(fontPath.c_str())) {
        return 0;
    }
    else {
        if (!AddFontResourceA(fontPath.c_str())) {
            return 0;
        }
        else {
            return 1;
        }
    }
   
}

tD3DXCreateFontIndirectA oD3DXCreateFontIndirectA = nullptr;
HRESULT WINAPI HookedD3DXCreateFontIndirectA(LPDIRECT3DDEVICE9 pDevice, CONST D3DXFONT_DESCA* pDesc, LPD3DXFONT* ppFont) {

    // Yeni font parametreleri
    D3DXFONT_DESCA newFontDesc;
    ZeroMemory(&newFontDesc, sizeof(newFontDesc));
    newFontDesc.Height = 48;  // Punto boyutu
    newFontDesc.Width = 0;
    newFontDesc.Weight = FW_NORMAL;
    newFontDesc.MipLevels = 1;
    newFontDesc.Italic = FALSE;
    newFontDesc.CharSet = DEFAULT_CHARSET;
    newFontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
    newFontDesc.Quality = DEFAULT_QUALITY;
    newFontDesc.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
    strcpy_s(newFontDesc.FaceName, "Arial");  // Font adı

    HRESULT result = oD3DXCreateFontIndirectA(pDevice, &newFontDesc, ppFont);

    if (result >= 0)
    {
        MessageBoxA(NULL, "test", "test", NULL);
    }

    return result;
}
Thanks in advance for your help...
@[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...] @[Only registered and activated users can see links. Click Here To Register...]
08/19/2025 13:23 Mega Byte#2
More than font size would need to change the positions it renders and size of areas it might wrap too if any.

I suspect that you can hook d3d functions through patching the vtable.
The addresses are different you need a signature scan to find the device then get the vtable.
But in saying this I have not hooked the font stuff.

Good luck
08/19/2025 16:31 zahter31#3
Quote:
Originally Posted by Mega Byte View Post
More than font size would need to change the positions it renders and size of areas it might wrap too if any.

I suspect that you can hook d3d functions through patching the vtable.
The addresses are different you need a signature scan to find the device then get the vtable.
But in saying this I have not hooked the font stuff.

Good luck
Ohh good morning!
08/22/2025 18:56 xmetrix#4
[Only registered and activated users can see links. Click Here To Register...][Only registered and activated users can see links. Click Here To Register...]