Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #pragma comment(lib, "detours.lib")
  3. #include "stdafx.h"
  4. #include "detours.h"
  5. #include <iostream>
  6. #include <Windows.h>
  7. #include <Unknwnbase.h>
  8. #include <dinput.h>
  9. #include "d3d9.h"
  10. #include <string>
  11. #include "dx9hook.h"
  12. #include "Imgui/imgui.h"
  13. #include "Imgui/imconfig.h"
  14. #include "Imgui/imgui_impl_dx9.h"
  15. #include "Imgui/imgui_impl_win32.h"
  16. #include "Imgui/imgui_internal.h"
  17. #include "Imgui/imstb_rectpack.h"
  18. #include "Imgui/imstb_textedit.h"
  19. #include "Imgui/imstb_truetype.h"
  20.  
  21. using namespace std;
  22. bool DX9HookSuccess = false;
  23.  
  24. void Hook_D3D9_Direct3DCreate9();
  25. void Hook_D3D9_CreateDevice();
  26. void Hook_D3D9_EndScene();
  27.  
  28. bool DetourHookFunction(PVOID *ppPointer, PVOID pDetour)
  29. {
  30. DetourTransactionBegin();
  31. DetourUpdateThread(GetCurrentThread());
  32. if (DetourAttach(ppPointer, pDetour))
  33. return false;
  34. if (DetourTransactionCommit() != NO_ERROR)
  35. return false;
  36. return true;
  37. }
  38.  
  39.  
  40. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  41. {
  42. switch (ul_reason_for_call)
  43. {
  44. case DLL_PROCESS_ATTACH:
  45. AllocConsole(); // enables the console
  46. freopen("CONOUT$", "w", stdout);
  47. Hook_D3D9_Direct3DCreate9();
  48. break;
  49. case DLL_THREAD_ATTACH:
  50. case DLL_THREAD_DETACH:
  51. case DLL_PROCESS_DETACH:
  52. break;
  53. }
  54. return TRUE;
  55. }
  56.  
  57. // Hook D3D9 Direct3DCreate9
  58. void Hook_D3D9_Direct3DCreate9()
  59. {
  60. IMGUI_CHECKVERSION();
  61. ImGui::CreateContext();
  62. ImGuiIO& io = ImGui::GetIO(); (void)io;
  63.  
  64. HMODULE hD3D9 = GetModuleHandleA("d3d9.dll");
  65. if (hD3D9)
  66. {
  67. PBYTE pDirect3DCreate9 = (PBYTE)GetProcAddress(hD3D9, "Direct3DCreate9");
  68. if (pDirect3DCreate9)
  69. {
  70. printf("Found Direct3DCreate9 at addr 0x%X\n", (int)pDirect3DCreate9);
  71. Direct3DCreate9_Original = (Direct3DCreate9_t)(pDirect3DCreate9);
  72. if (DetourHookFunction(&(PVOID&)Direct3DCreate9_Original, Direct3DCreate9_Hook))
  73. {
  74. printf("Hooked Direct3DCreate9\n");
  75. return;
  76. }
  77. }
  78. }
  79. printf("Failed to hook Direct3DCreate9\n");
  80. }
  81.  
  82. IDirect3D9* WINAPI Direct3DCreate9_Hook(UINT sdkVers)
  83. {
  84. printf("Feed a fake IDirect3D9(Version:%d)\n", sdkVers);
  85. IDirect3D9 *legit = Direct3DCreate9_Original(sdkVers);
  86. int* pVTable = (int*)(*(int*)legit);
  87. D3D9_CreateDevice_Original = (D3D9_CreateDevice_t)pVTable[16];
  88. Hook_D3D9_CreateDevice();
  89. return legit;
  90. }
  91.  
  92. // Hook D3D9 CreateDevice
  93. void Hook_D3D9_CreateDevice()
  94. {
  95. if (DetourHookFunction(&(PVOID&)D3D9_CreateDevice_Original, D3D9_CreateDevice_Hook))
  96. {
  97. printf("Hooked D3D9_CreateDevice\n");
  98. }
  99. else
  100. {
  101. printf("Failed to hook D3D9_CreateDevice\n");
  102. }
  103. }
  104.  
  105. HRESULT WINAPI D3D9_CreateDevice_Hook(IDirect3D9* pD3D9, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS * pPresentationParameters, IDirect3DDevice9 ** ppReturnedDeviceInterface)
  106. {
  107. ImGui_ImplWin32_Init(pPresentationParameters->hDeviceWindow);
  108.  
  109. HRESULT hres = D3D9_CreateDevice_Original(pD3D9, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
  110. d3d9_device = *ppReturnedDeviceInterface;
  111. printf("Got a D3D9 DeviceInterface\n");
  112. int* pVTable = (int*)(*(int*)d3d9_device);
  113. EndScene_Original = (EndScene_t)pVTable[42];
  114. Hook_D3D9_EndScene();
  115. return hres;
  116. }
  117.  
  118. // Hook D3D9 EndScene
  119. void Hook_D3D9_EndScene()
  120. {
  121. if (DetourHookFunction(&(PVOID&)EndScene_Original, EndScene_Hook))
  122. {
  123. printf("Hooked EndScene\n");
  124. }
  125. else
  126. {
  127. printf("Failed to hook EndScene\n");
  128. }
  129. }
  130.  
  131. HRESULT WINAPI EndScene_Hook(IDirect3DDevice9* pDevice)
  132. {
  133. printf("wassup");
  134. ImGui_ImplDX9_Init(pDevice);
  135.  
  136. bool bShowWindow = TRUE;
  137.  
  138. ImGui_ImplDX9_NewFrame();
  139. ImGui_ImplWin32_NewFrame();
  140. ImGui::NewFrame();
  141. ImGui::ShowDemoWindow(&bShowWindow);
  142. ImGui::EndFrame();
  143.  
  144. ImGui::Render();
  145. ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  146.  
  147. D3DRECT testRect = { 0, 0, 400, 80 };
  148. pDevice->Clear(1, &testRect, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 0, 0);
  149.  
  150. HRESULT hres = EndScene_Original(pDevice);
  151. return hres;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement