SHARE
TWEET

Untitled

a guest Jan 14th, 2020 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  21. LPDIRECT3DDEVICE9    g_pd3dDevice = NULL;
  22. IDirect3D9 *         g_pD3D = NULL;
  23. HWND                 window = NULL;
  24. WNDPROC              wndproc_orig = NULL;
  25.  
  26. bool show_menu = false;
  27. bool chams = true;
  28.  
  29. BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam)
  30. {
  31.     DWORD wndProcID;
  32.     GetWindowThreadProcessId(handle, &wndProcID);
  33.  
  34.     if (GetCurrentProcessId() != wndProcID)
  35.     {
  36.         return TRUE;
  37.     }
  38.  
  39.     window = handle;
  40.     return FALSE;
  41. }
  42.  
  43. HWND GetProcessWindow()
  44. {
  45.     EnumWindows(EnumWindowsCallback, NULL);
  46.     return window;
  47. }
  48.  
  49. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  50. LRESULT __stdcall WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  51. {
  52.     if (show_menu && ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  53.     {
  54.         return true;
  55.     }
  56.     return CallWindowProc(wndproc_orig, hWnd, msg, wParam, lParam);
  57. }
  58.  
  59. bool GetD3D9Device(void ** pTable, size_t size)
  60. {
  61.     if (!pTable)
  62.     {
  63.         return false;
  64.     }
  65.  
  66.     g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
  67.  
  68.     if (!g_pD3D)
  69.     {
  70.         return false;
  71.     }
  72.  
  73.     D3DPRESENT_PARAMETERS d3dpp = {};
  74.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  75.     d3dpp.hDeviceWindow = GetProcessWindow();
  76.     d3dpp.Windowed = true;
  77.  
  78.     g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dpp.hDeviceWindow,
  79.         D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice);
  80.  
  81.     if (!g_pd3dDevice)
  82.     {
  83.         g_pD3D->Release();
  84.         return false;
  85.     }
  86.  
  87.     memcpy(pTable, *reinterpret_cast<void***>(g_pd3dDevice), size);
  88.     g_pd3dDevice->Release();
  89.     g_pD3D->Release();
  90.     return true;
  91. }
  92.  
  93. void CleanupDeviceD3D()
  94. {
  95.     if (g_pd3dDevice)
  96.     {
  97.         g_pd3dDevice->Release();
  98.         g_pd3dDevice = NULL;
  99.     }
  100.  
  101.     if (g_pD3D)
  102.     {
  103.         g_pD3D->Release();
  104.         g_pD3D = NULL;
  105.     }
  106. }
  107.  
  108. HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
  109. {
  110.     static bool init = false;
  111.  
  112.     if (GetAsyncKeyState(VK_INSERT) & 1)
  113.     {
  114.         show_menu = !show_menu;
  115.     }
  116.  
  117.     if (!init)
  118.     {
  119.         wndproc_orig = (WNDPROC)SetWindowLongPtr(window, GWL_WNDPROC, (LONG_PTR)WndProc);
  120.  
  121.         ImGui::CreateContext();
  122.         ImGuiIO& io = ImGui::GetIO();
  123.  
  124.         ImGui::StyleColorsDark();
  125.  
  126.         ImGui_ImplWin32_Init(window);
  127.         ImGui_ImplDX9_Init(pDevice);
  128.  
  129.         init = true;
  130.     }
  131.  
  132.     if (init)
  133.     {
  134.         if (show_menu)
  135.         {
  136.             ImGui_ImplDX9_NewFrame();
  137.             ImGui_ImplWin32_NewFrame();
  138.             ImGui::NewFrame();
  139.  
  140.             /* ImGui::Begin("Menu", &show_menu);
  141.             {
  142.                 ImGui::Checkbox("Chams", &chams);
  143.             }
  144.             */
  145.  
  146.             ImGui::Text("Hello, world!");
  147.  
  148.             ImGui::Render();
  149.             ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  150.         }
  151.     }
  152.  
  153.     return oEndScene(pDevice);
  154. }
  155.  
  156. DWORD WINAPI mainThread(PVOID base)
  157. {
  158.     void* d3d9Device[119];
  159.  
  160.     if (GetD3D9Device(d3d9Device, sizeof(d3d9Device)))
  161.     {
  162.         oEndScene = (EndScene)Detours::X86::DetourFunction((Detours::uint8_t*)d3d9Device[42], (Detours::uint8_t*)hkEndScene);
  163.  
  164.         while (true)
  165.         {
  166.             if (GetAsyncKeyState(VK_F10))
  167.             {
  168.                 CleanupDeviceD3D();
  169.                 FreeLibraryAndExitThread(static_cast<HMODULE>(base), 1);
  170.             }
  171.         }
  172.     }
  173.    
  174.     FreeLibraryAndExitThread(static_cast<HMODULE>(base), 1);
  175. }
  176.  
  177. BOOL APIENTRY DllMain( HMODULE hModule,
  178.                        DWORD  ul_reason_for_call,
  179.                        LPVOID lpReserved
  180.                      )
  181. {
  182.     switch (ul_reason_for_call)
  183.     {
  184.     case DLL_PROCESS_ATTACH:
  185.         CreateThread(nullptr, NULL, mainThread, hModule, NULL, nullptr);
  186.     case DLL_THREAD_ATTACH:
  187.     case DLL_THREAD_DETACH:
  188.     case DLL_PROCESS_DETACH:
  189.         break;
  190.     }
  191.     return TRUE;
  192. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top