Advertisement
iJorgePc

error imgui

May 12th, 2024
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.89 KB | Source Code | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <imgui.h>
  4. #include <imgui_impl_dx9.h>
  5. #include <imgui_impl_win32.h>
  6. #include "Hook.h"
  7.  
  8. bool showMenu = true;
  9. bool initImGui = false;
  10.  
  11. void InputHandler() {
  12.     for (int i = 0; i < 5; i++) {
  13.         ImGui::GetIO().MouseDown[i] = false;
  14.     }
  15.  
  16.     int Button = -1;
  17.     if (GetAsyncKeyState(VK_LBUTTON)) {
  18.         Button = 0;
  19.     }
  20.  
  21.     if (Button != -1) {
  22.         ImGui::GetIO().MouseDown[Button] = true;
  23.     }
  24. }
  25.  
  26. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  27. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  28.     if (showMenu && ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) {
  29.         return true;
  30.     }
  31.     return CallWindowProc(Process.TargetWndProc, hWnd, msg, wParam, lParam);
  32. }
  33.  
  34. HRESULT APIENTRY hkEndScene(IDirect3DDevice9* pDevice) {
  35.     if (pDevice == NULL)
  36.         return oEndScene(pDevice);
  37.  
  38.  
  39.     if (!initImGui) {
  40.         nDevice = pDevice;
  41.         IMGUI_CHECKVERSION();
  42.         ImGui::CreateContext();
  43.  
  44.         D3DDEVICE_CREATION_PARAMETERS pParameters;
  45.         pDevice->GetCreationParameters(&pParameters);
  46.         Process.TargetHwnd = pParameters.hFocusWindow;
  47.  
  48.         if (Process.TargetHwnd != NULL) {
  49.             Process.TargetWndProc = (WNDPROC)SetWindowLongPtr(Process.TargetHwnd, GWLP_WNDPROC, (LONG_PTR)WndProc);
  50.             ImGui_ImplWin32_Init(Process.TargetHwnd);
  51.             ImGui_ImplDX9_Init(pDevice);
  52.             ImGui::GetIO().ImeWindowHandle = Process.TargetHwnd;
  53.             ImGui::GetIO().IniFilename = NULL;
  54.             ImGui::GetIO().LogFilename = NULL;
  55.             initImGui = true;
  56.             pac = 1;
  57.         }  
  58.     }
  59.  
  60.     if (nDevice != pDevice)
  61.     {
  62.         nDevice = pDevice;
  63.         ImGui_ImplDX9_Init(pDevice);
  64.     }
  65.  
  66.     ImGui_ImplDX9_NewFrame();
  67.     ImGui_ImplWin32_NewFrame();
  68.     ImGui::NewFrame();
  69.  
  70.     if (GetAsyncKeyState(VK_INSERT) & 1)
  71.         showMenu = !showMenu;
  72.  
  73.     ImGui::GetIO().MouseDrawCursor = showMenu;
  74.     if (showMenu == true) {
  75.         InputHandler();
  76.         ImGui::Begin("windowsx", nullptr);
  77.  
  78.         ImGui::Text("aea11");
  79.  
  80.         ImGui::End();
  81.     }
  82.     ImGui::EndFrame();
  83.     ImGui::Render();
  84.     ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
  85.     return oEndScene(pDevice);
  86. }
  87.  
  88. HRESULT APIENTRY hkPresent(IDirect3DDevice9* pDevice, const RECT* pSourceRect, const RECT* pDestRect, HWND hDestWindowOverride, const RGNDATA* pDirtyRegion) {
  89.     if (pDevice == NULL)
  90.         return oPresent(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
  91.  
  92.     return oPresent(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
  93. }
  94.  
  95. HRESULT APIENTRY hkDrawIndexedPrimitive(IDirect3DDevice9* pDevice, D3DPRIMITIVETYPE Type, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
  96.     if (pDevice == NULL)
  97.         return oDrawIndexedPrimitive(pDevice, Type, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
  98.  
  99.     return oDrawIndexedPrimitive(pDevice, Type, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
  100. }
  101.  
  102. HRESULT APIENTRY hkDrawPrimitive(IDirect3DDevice9* pDevice, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
  103.     if (pDevice == NULL)
  104.         return oDrawPrimitive(pDevice, PrimitiveType, StartVertex, PrimitiveCount);
  105.  
  106.     return oDrawPrimitive(pDevice, PrimitiveType, StartVertex, PrimitiveCount);
  107. }
  108.  
  109. HRESULT APIENTRY hkSetTexture(IDirect3DDevice9* pDevice, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
  110.     if (pDevice == NULL)
  111.         return oSetTexture(pDevice, Stage, pTexture);
  112.  
  113.     return oSetTexture(pDevice, Stage, pTexture);
  114. }
  115.  
  116. HRESULT APIENTRY hkReset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters) {
  117.     if (pDevice == NULL)
  118.         return oReset(pDevice, pPresentationParameters);
  119.  
  120.     ImGui_ImplDX9_InvalidateDeviceObjects();
  121.     auto ResetReturn = oReset(pDevice, pPresentationParameters);
  122.     ImGui_ImplDX9_CreateDeviceObjects();
  123.     return oReset(pDevice, pPresentationParameters);
  124. }
  125.  
  126. HRESULT APIENTRY hkSetStreamSource(IDirect3DDevice9* pDevice, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
  127.     if (pDevice == NULL)
  128.         return oSetStreamSource(pDevice, StreamNumber, pStreamData, OffsetInBytes, Stride);
  129.  
  130.     return oSetStreamSource(pDevice, StreamNumber, pStreamData, OffsetInBytes, Stride);
  131. }
  132.  
  133. HRESULT APIENTRY hkSetVertexDeclaration(IDirect3DDevice9* pDevice, IDirect3DVertexDeclaration9* pDecl) {
  134.     if (pDevice == NULL)
  135.         return oSetVertexDeclaration(pDevice, pDecl);
  136.  
  137.     return oSetVertexDeclaration(pDevice, pDecl);
  138. }
  139.  
  140. HRESULT APIENTRY hkSetVertexShaderConstantF(IDirect3DDevice9* pDevice, UINT StartRegister, const float* pConstantData, UINT Vector4fCount) {
  141.     if (pDevice == NULL)
  142.         return oSetVertexShaderConstantF(pDevice, StartRegister, pConstantData, Vector4fCount);
  143.  
  144.     return oSetVertexShaderConstantF(pDevice, StartRegister, pConstantData, Vector4fCount);
  145. }
  146.  
  147. HRESULT APIENTRY hkSetVertexShader(IDirect3DDevice9* pDevice, IDirect3DVertexShader9* pShader) {
  148.     if (pDevice == NULL)
  149.         return oSetVertexShader(pDevice, pShader);
  150.  
  151.     return oSetVertexShader(pDevice, pShader);
  152. }
  153.  
  154. HRESULT APIENTRY hkSetPixelShader(IDirect3DDevice9* pDevice, IDirect3DPixelShader9* pShader) {
  155.     if (pDevice == NULL)
  156.         return oSetPixelShader(pDevice, pShader);
  157.  
  158.     return oSetPixelShader(pDevice, pShader);
  159. }
  160.  
  161. DWORD WINAPI InitHook(LPVOID lpParameter) {
  162.     if (checktDirectXVersion(DirectXVersion.D3D9) == false) {
  163.         return FALSE;
  164.     }
  165.  
  166.     HWND WindowHwnd = CreateWindow("BUTTON", "DX", WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, Process.hModule, NULL);
  167.     if (WindowHwnd == NULL) {
  168.         return FALSE;
  169.     }
  170.  
  171.     IDirect3D9* IDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
  172.     if (IDirect3D9 == NULL) {
  173.         DestroyWindow(WindowHwnd);
  174.         return FALSE;
  175.     }
  176.  
  177.     D3DPRESENT_PARAMETERS pParameters;
  178.     ZeroMemory(&pParameters, sizeof(pParameters));
  179.     pParameters.Windowed = TRUE;
  180.     pParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
  181.     pParameters.hDeviceWindow = WindowHwnd;
  182.     pParameters.BackBufferFormat = D3DFMT_UNKNOWN;
  183.  
  184.     IDirect3DDevice9* pDevice;
  185.     if (IDirect3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WindowHwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pParameters, &pDevice) != D3D_OK) {
  186.         IDirect3D9->Release();
  187.         DestroyWindow(WindowHwnd);
  188.         return FALSE;
  189.     }
  190.  
  191.     if (pDevice == NULL) {
  192.         IDirect3D9->Release();
  193.         DestroyWindow(WindowHwnd);
  194.         return FALSE;
  195.     }
  196.  
  197. #if defined _M_X64
  198.     DWORD64* DVTable = (DWORD64*)pDevice;
  199.     DVTable = (DWORD64*)DVTable[0];
  200. #elif defined _M_IX86
  201.     DWORD* DVTable = (DWORD*)pDevice;
  202.     DVTable = (DWORD*)DVTable[0];
  203. #endif
  204.  
  205.     oEndScene = (EndScene)DVTable[42];
  206.     oPresent = (Present)DVTable[17];
  207.     oDrawIndexedPrimitive = (DrawIndexedPrimitive)DVTable[82];
  208.     oDrawPrimitive = (DrawPrimitive)DVTable[81];
  209.     oSetTexture = (SetTexture)DVTable[65];
  210.     oReset = (Reset)DVTable[16];
  211.     oSetStreamSource = (SetStreamSource)DVTable[100];
  212.     oSetVertexDeclaration = (SetVertexDeclaration)DVTable[87];
  213.     oSetVertexShaderConstantF = (SetVertexShaderConstantF)DVTable[94];
  214.     oSetVertexShader = (SetVertexShader)DVTable[92];
  215.     oSetPixelShader = (SetPixelShader)DVTable[107];
  216.  
  217.     DetourTransactionBegin();
  218.     DetourUpdateThread(GetCurrentThread());
  219.     DetourAttach(&(LPVOID&)oEndScene, (PBYTE)hkEndScene);
  220.     DetourAttach(&(LPVOID&)oPresent, (PBYTE)hkPresent);
  221.     DetourAttach(&(LPVOID&)oDrawIndexedPrimitive, (PBYTE)hkDrawIndexedPrimitive);
  222.     DetourAttach(&(LPVOID&)oDrawPrimitive, (PBYTE)hkDrawPrimitive);
  223.     DetourAttach(&(LPVOID&)oSetTexture, (PBYTE)hkSetTexture);
  224.     DetourAttach(&(LPVOID&)oReset, (PBYTE)hkReset);
  225.     DetourAttach(&(LPVOID&)oSetStreamSource, (PBYTE)hkSetStreamSource);
  226.     DetourAttach(&(LPVOID&)oSetVertexDeclaration, (PBYTE)hkSetVertexDeclaration);
  227.     DetourAttach(&(LPVOID&)oSetVertexShaderConstantF, (PBYTE)hkSetVertexShaderConstantF);
  228.     DetourAttach(&(LPVOID&)oSetVertexShader, (PBYTE)hkSetVertexShader);
  229.     DetourAttach(&(LPVOID&)oSetPixelShader, (PBYTE)hkSetPixelShader);
  230.     DetourTransactionCommit();
  231.  
  232.     pDevice->Release();
  233.     IDirect3D9->Release();
  234.     DestroyWindow(WindowHwnd);
  235.     return TRUE;
  236. }
  237.  
  238. BOOL WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved) {
  239.     switch (fdwReason) {
  240.     case DLL_PROCESS_ATTACH:
  241.         Process.hModule = hModule;
  242.         DisableThreadLibraryCalls(hModule);
  243.         CreateThread(0, 0, InitHook, 0, 0, NULL);
  244.         break;
  245.     case DLL_PROCESS_DETACH:
  246.         ImGui_ImplDX9_Shutdown();
  247.         ImGui_ImplWin32_Shutdown();
  248.         ImGui::DestroyContext();
  249.         FreeLibraryAndExitThread(hModule, 0);
  250.         break;
  251.     }
  252.     return TRUE;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement