Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #pragma comment(lib, "detours.lib")
- #include "stdafx.h"
- #include "detours.h"
- #include <iostream>
- #include <Windows.h>
- #include <Unknwnbase.h>
- #include <dinput.h>
- #include "d3d9.h"
- #include <string>
- #include "dx9hook.h"
- #include "Imgui/imgui.h"
- #include "Imgui/imconfig.h"
- #include "Imgui/imgui_impl_dx9.h"
- #include "Imgui/imgui_impl_win32.h"
- #include "Imgui/imgui_internal.h"
- #include "Imgui/imstb_rectpack.h"
- #include "Imgui/imstb_textedit.h"
- #include "Imgui/imstb_truetype.h"
- using namespace std;
- bool DX9HookSuccess = false;
- void Hook_D3D9_Direct3DCreate9();
- void Hook_D3D9_CreateDevice();
- void Hook_D3D9_EndScene();
- bool DetourHookFunction(PVOID *ppPointer, PVOID pDetour)
- {
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- if (DetourAttach(ppPointer, pDetour))
- return false;
- if (DetourTransactionCommit() != NO_ERROR)
- return false;
- return true;
- }
- BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- AllocConsole(); // enables the console
- freopen("CONOUT$", "w", stdout);
- Hook_D3D9_Direct3DCreate9();
- break;
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- // Hook D3D9 Direct3DCreate9
- void Hook_D3D9_Direct3DCreate9()
- {
- IMGUI_CHECKVERSION();
- ImGui::CreateContext();
- ImGuiIO& io = ImGui::GetIO(); (void)io;
- HMODULE hD3D9 = GetModuleHandleA("d3d9.dll");
- if (hD3D9)
- {
- PBYTE pDirect3DCreate9 = (PBYTE)GetProcAddress(hD3D9, "Direct3DCreate9");
- if (pDirect3DCreate9)
- {
- printf("Found Direct3DCreate9 at addr 0x%X\n", (int)pDirect3DCreate9);
- Direct3DCreate9_Original = (Direct3DCreate9_t)(pDirect3DCreate9);
- if (DetourHookFunction(&(PVOID&)Direct3DCreate9_Original, Direct3DCreate9_Hook))
- {
- printf("Hooked Direct3DCreate9\n");
- return;
- }
- }
- }
- printf("Failed to hook Direct3DCreate9\n");
- }
- IDirect3D9* WINAPI Direct3DCreate9_Hook(UINT sdkVers)
- {
- printf("Feed a fake IDirect3D9(Version:%d)\n", sdkVers);
- IDirect3D9 *legit = Direct3DCreate9_Original(sdkVers);
- int* pVTable = (int*)(*(int*)legit);
- D3D9_CreateDevice_Original = (D3D9_CreateDevice_t)pVTable[16];
- Hook_D3D9_CreateDevice();
- return legit;
- }
- // Hook D3D9 CreateDevice
- void Hook_D3D9_CreateDevice()
- {
- if (DetourHookFunction(&(PVOID&)D3D9_CreateDevice_Original, D3D9_CreateDevice_Hook))
- {
- printf("Hooked D3D9_CreateDevice\n");
- }
- else
- {
- printf("Failed to hook D3D9_CreateDevice\n");
- }
- }
- HRESULT WINAPI D3D9_CreateDevice_Hook(IDirect3D9* pD3D9, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS * pPresentationParameters, IDirect3DDevice9 ** ppReturnedDeviceInterface)
- {
- ImGui_ImplWin32_Init(pPresentationParameters->hDeviceWindow);
- HRESULT hres = D3D9_CreateDevice_Original(pD3D9, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
- d3d9_device = *ppReturnedDeviceInterface;
- printf("Got a D3D9 DeviceInterface\n");
- int* pVTable = (int*)(*(int*)d3d9_device);
- EndScene_Original = (EndScene_t)pVTable[42];
- Hook_D3D9_EndScene();
- return hres;
- }
- // Hook D3D9 EndScene
- void Hook_D3D9_EndScene()
- {
- if (DetourHookFunction(&(PVOID&)EndScene_Original, EndScene_Hook))
- {
- printf("Hooked EndScene\n");
- }
- else
- {
- printf("Failed to hook EndScene\n");
- }
- }
- HRESULT WINAPI EndScene_Hook(IDirect3DDevice9* pDevice)
- {
- printf("wassup");
- ImGui_ImplDX9_Init(pDevice);
- bool bShowWindow = TRUE;
- ImGui_ImplDX9_NewFrame();
- ImGui_ImplWin32_NewFrame();
- ImGui::NewFrame();
- ImGui::ShowDemoWindow(&bShowWindow);
- ImGui::EndFrame();
- ImGui::Render();
- ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
- D3DRECT testRect = { 0, 0, 400, 80 };
- pDevice->Clear(1, &testRect, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 0, 0);
- HRESULT hres = EndScene_Original(pDevice);
- return hres;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement