Home / Forums / DirectX 11 Present Hook with Dear ImGui - Clean Overlay Guide

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

DirectX 11 Present Hook with Dear ImGui - Clean Overlay Guide

VectorByte
Graphics & DirectX Dev
VIP
Rep: 420
Join Date: Aug 2022
Posts: 39
Thanks: 115
2y ago · Jul 18, 2024 7:40 PM
#1
Here is a reference snippet for hooking
CPP
IDXGISwapChain::Present
to draw Dear ImGui overlays seamlessly on top of any DirectX 11 game:

CPP
#include <d3d11.h>
#include "imgui.h"
#include "imgui_impl_win32.h"
#include "imgui_impl_dx11.h"

typedef HRESULT(__stdcall* Present_t)(IDXGISwapChain*, UINT, UINT);
Present_t oPresent = nullptr;

ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
ID3D11RenderTargetView* g_mainRenderTargetView = nullptr;
bool g_bInitialized = false;

HRESULT __stdcall Hooked_Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) {
    if (!g_bInitialized) {
        if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&g_pd3dDevice))) {
            g_pd3dDevice->GetImmediateContext(&g_pd3dDeviceContext);
            
            ID3D11Texture2D* pBackBuffer;
            pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
            g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
            pBackBuffer->Release();
            
            DXGI_SWAP_CHAIN_DESC sd;
            pSwapChain->GetDesc(&sd);
            
            ImGui::CreateContext();
            ImGui_ImplWin32_Init(sd.OutputWindow);
            ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
            g_bInitialized = true;
        }
    }
    
    // Render Loop
    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();
    
    // Draw Custom ESP Overlay here
    ImDrawList* bg = ImGui::GetBackgroundDrawList();
    bg->AddText(ImVec2(20, 20), IM_COL32(0, 255, 255, 255), "[UnreliableCode] DX11 Overlay Active");
    
    ImGui::Render();
    g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
    
    return oPresent(pSwapChain, SyncInterval, Flags);
}


Always release your
CPP
g_mainRenderTargetView
on ResizeBuffers if the game window resolution changes!
VectorByte | DirectX 11/12 Hooking & ImGui Overlays
Quote
Math is the language of game engines.
The following users thanked VectorByte for this post:
RenderMaster
ImGui UI Designer
VIP
Rep: 310
Join Date: Sep 2023
Posts: 32
Thanks: 82
2y ago · Jul 18, 2024 9:05 PM
#2
Solid implementation! For maximum FPS performance with large entity counts, batching your text renders or using
CPP
ImDrawList::AddDrawCmd
helps minimize draw call overhead.
RenderMaster | Clean UI Design & Dear ImGui widgets
DarkLogic
Game Logic & SDK Developer
MEMBER
Rep: 180
Join Date: Jun 2024
Posts: 28
Thanks: 41
2y ago · Jul 19, 2024 10:15 AM
#3
Appreciate the clean code share @VectorByte. Works out of the box with standard VMT hooks or MinHook.
DarkLogic | Virtual method tables & SDKs