Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. // dllmain.cpp : Defines the entry point for the DLL application.
  2. #pragma comment(lib, "d3d9.lib")
  3. #pragma comment(lib, "d3dx9.lib")
  4. #pragma comment(lib, "detours.lib")
  5.  
  6. #include <Windows.h>
  7. #include <iostream>
  8. #include <string>
  9. #include <Detours.h>
  10. #include <imgui.h>
  11. #include "imgui.h"
  12. #include "imgui_impl_dx9.h"
  13. #include "imgui_impl_win32.h"
  14. #include <d3d9.h>
  15. #include <d3dx9.h>
  16.  
  17. typedef HRESULT(_stdcall* EndScene)(LPDIRECT3DDEVICE9 pDevice);
  18. HRESULT _stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice);
  19. EndScene oEndScene;
  20. using fReset_t = HRESULT(__stdcall*)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*);
  21. fReset_t oReset;
  22.  
  23. LPDIRECT3DDEVICE9    g_pd3dDevice = NULL;
  24. IDirect3D9 *         g_pD3D = NULL;
  25. HWND                 window = NULL;
  26. WNDPROC              wndproc_orig = NULL;
  27.  
  28. bool show_menu = false;
  29. bool chams = true;
  30.  
  31. BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam)
  32. {
  33.     DWORD wndProcID;
  34.     GetWindowThreadProcessId(handle, &wndProcID);
  35.  
  36.     if (GetCurrentProcessId() != wndProcID)
  37.     {
  38.         return TRUE;
  39.     }
  40.  
  41.     window = handle;
  42.     return FALSE;
  43. }
  44.  
  45. HWND GetProcessWindow()
  46. {
  47.     EnumWindows(EnumWindowsCallback, NULL);
  48.     return window;
  49. }
  50.  
  51. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  52. LRESULT CALLBACK WndProc(const HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  53. {
  54.     if (show_menu && ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  55.     {
  56.         return true;
  57.     }
  58.     return CallWindowProc(wndproc_orig, hWnd, msg, wParam, lParam);
  59. }
  60.  
  61. bool GetD3D9Device(void ** pTable, size_t size)
  62. {
  63.     if (!pTable)
  64.     {
  65.         return false;
  66.     }
  67.  
  68.     g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
  69.  
  70.     if (!g_pD3D)
  71.     {
  72.         return false;
  73.     }
  74.  
  75.     D3DPRESENT_PARAMETERS d3dpp = {};
  76.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  77.     d3dpp.hDeviceWindow = GetProcessWindow();
  78.     d3dpp.Windowed = true;
  79.  
  80.     g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dpp.hDeviceWindow,
  81.         D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
  82.  
  83.     if (!g_pd3dDevice)
  84.     {
  85.         g_pD3D->Release();
  86.         return false;
  87.     }
  88.  
  89.     memcpy(pTable, *reinterpret_cast<void***>(g_pd3dDevice), size);
  90.     g_pd3dDevice->Release();
  91.     g_pD3D->Release();
  92.     return true;
  93. }
  94.  
  95. void CleanupDeviceD3D()
  96. {
  97.     if (g_pd3dDevice)
  98.     {
  99.         g_pd3dDevice->Release();
  100.         g_pd3dDevice = NULL;
  101.     }
  102.  
  103.     if (g_pD3D)
  104.     {
  105.         g_pD3D->Release();
  106.         g_pD3D = NULL;
  107.     }
  108. }
  109.  
  110. HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
  111. {
  112.     static bool init = false;
  113.  
  114.     if (GetAsyncKeyState(VK_INSERT) & 1)
  115.     {
  116.         show_menu = !show_menu;
  117.     }
  118.  
  119.     if (!init)
  120.     {
  121.         wndproc_orig = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)WndProc);
  122.  
  123.         ImGui::CreateContext();
  124.         ImGuiIO& io = ImGui::GetIO();
  125.  
  126.         ImGui::StyleColorsLight();
  127.  
  128.         ImGui_ImplWin32_Init(window);
  129.         ImGui_ImplDX9_Init(pDevice);
  130.  
  131.         init = true;
  132.     }
  133.  
  134.     if (init)
  135.     {
  136.         if (show_menu)
  137.         {
  138.             ImGui_ImplDX9_NewFrame();
  139.             ImGui_ImplWin32_NewFrame();
  140.             ImGui::NewFrame();
  141.  
  142.             /* ImGui::Begin("Menu", &show_menu);
  143.             {
  144.                 ImGui::Checkbox("Chams", &chams);
  145.             }
  146.             */
  147.  
  148.             ImGui::Text("Hello, world!");
  149.  
  150.             ImGui::EndFrame();
  151.             ImGui::Render();
  152.             ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  153.         }
  154.     }
  155.  
  156.     return oEndScene(pDevice);
  157. }
  158.  
  159. HRESULT __stdcall hkReset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters)  // 16
  160. {
  161.     ImGui_ImplDX9_InvalidateDeviceObjects();
  162.     ImGui_ImplDX9_CreateDeviceObjects();
  163.  
  164.     HRESULT Return = oReset(pDevice, pPresentationParameters);
  165.  
  166.     return Return;
  167. }
  168.  
  169. DWORD WINAPI mainThread(PVOID base)
  170. {
  171.     void* d3d9Device[119];
  172.  
  173.     if (GetD3D9Device(d3d9Device, sizeof(d3d9Device)))
  174.     {
  175.         oEndScene = (EndScene)Detours::X86::DetourFunction((Detours::uint8_t*)d3d9Device[42], (Detours::uint8_t*)hkEndScene);
  176.         oReset = (fReset_t)Detours::X86::DetourFunction((Detours::uint8_t*)d3d9Device[16], (Detours::uint8_t*)hkReset);
  177.  
  178.         while (true)
  179.         {
  180.             if (GetAsyncKeyState(VK_F10))
  181.             {
  182.                 CleanupDeviceD3D();
  183.                 FreeLibraryAndExitThread(static_cast<HMODULE>(base), 1);
  184.             }
  185.         }
  186.     }
  187.    
  188.     FreeLibraryAndExitThread(static_cast<HMODULE>(base), 1);
  189. }
  190.  
  191. BOOL APIENTRY DllMain( HMODULE hModule,
  192.                        DWORD  ul_reason_for_call,
  193.                        LPVOID lpReserved
  194.                      )
  195. {
  196.     switch (ul_reason_for_call)
  197.     {
  198.     case DLL_PROCESS_ATTACH:
  199.         CreateThread(nullptr, NULL, mainThread, hModule, NULL, nullptr);
  200.     case DLL_THREAD_ATTACH:
  201.     case DLL_THREAD_DETACH:
  202.     case DLL_PROCESS_DETACH:
  203.         break;
  204.     }
  205.     return TRUE;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement