Guest User

HOOK D3D

a guest
Jul 30th, 2018
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <d3d9.h>
  6. #include "../LIB/MinHook/include/MinHook.h"
  7. #include "../LIB/DXSDK/d3dx9.h"
  8.  
  9. #pragma comment(lib, "d3d9.lib")
  10. #pragma comment(lib, "winmm.lib")
  11. #if defined _M_X64
  12. #pragma comment(lib, "../LIB/DXSDK/x64/d3dx9.lib")
  13. #elif defined _M_IX86
  14. #pragma comment(lib, "../LIB/DXSDK/x86/d3dx9.lib")
  15. #endif
  16.  
  17. using namespace std;
  18.  
  19. typedef HRESULT(APIENTRY *SetStreamSource_t)(IDirect3DDevice9*, UINT, IDirect3DVertexBuffer9*, UINT, UINT);
  20. SetStreamSource_t SetStreamSource_orig = 0;
  21. typedef HRESULT(APIENTRY *SetTexture_t)(IDirect3DDevice9*, DWORD, IDirect3DBaseTexture9 *);
  22. SetTexture_t SetTexture_orig = 0;
  23. typedef HRESULT(APIENTRY* Present_t) (IDirect3DDevice9*, const RECT *, const RECT *, HWND, const RGNDATA *);
  24. Present_t Present_orig = 0;
  25. typedef HRESULT(APIENTRY *Reset_t)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*);
  26. Reset_t Reset_orig = 0;
  27.  
  28. HRESULT APIENTRY SetStreamSource_hook(LPDIRECT3DDEVICE9 pDevice, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT sStride)
  29. {
  30.  
  31.     return SetStreamSource_orig(pDevice, StreamNumber, pStreamData, OffsetInBytes, sStride);
  32. }
  33. HRESULT APIENTRY SetTexture_hook(LPDIRECT3DDEVICE9 pDevice, DWORD Sampler, IDirect3DBaseTexture9 *pTexture)
  34. {
  35.    
  36.     return SetTexture_orig(pDevice, Sampler, pTexture);
  37. }
  38. HRESULT APIENTRY Present_hook(LPDIRECT3DDEVICE9 pDevice, const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride, const RGNDATA *pDirtyRegion)
  39. {
  40.  
  41.     return Present_orig(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
  42. }
  43. HRESULT APIENTRY Reset_hook(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS *pPresentationParameters)
  44. {
  45.     HRESULT ResetReturn = Reset_orig(pDevice, pPresentationParameters);
  46.     return ResetReturn;
  47. }
  48.  
  49. DWORD WINAPI N7D3D(LPVOID lpParameter)
  50. {
  51.     beforehook();
  52.     HMODULE dDll = NULL;
  53.     while (!dDll)
  54.     {
  55.         dDll = GetModuleHandleA("d3d9.dll");
  56.         Sleep(100);
  57.     }
  58.     CloseHandle(dDll);
  59.  
  60.     IDirect3D9* d3d = NULL;
  61.     IDirect3DDevice9* d3ddev = NULL;
  62.  
  63.     HWND tmpWnd = CreateWindowA("BUTTON", "DX", WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, HMODULE(), NULL);
  64.     if (tmpWnd == NULL)
  65.     {
  66.         return 0;
  67.     }
  68.  
  69.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  70.     if (d3d == NULL)
  71.     {
  72.         DestroyWindow(tmpWnd);
  73.         return 0;
  74.     }
  75.  
  76.     D3DPRESENT_PARAMETERS d3dpp;
  77.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  78.     d3dpp.Windowed = TRUE;
  79.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  80.     d3dpp.hDeviceWindow = tmpWnd;
  81.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  82.  
  83.     HRESULT result = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, tmpWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
  84.     if (result != D3D_OK)
  85.     {
  86.         d3d->Release();
  87.         DestroyWindow(tmpWnd);
  88.         return 0;
  89.     }
  90.  
  91. #if defined _M_X64
  92.     DWORD64* dVtable = (DWORD64*)d3ddev;
  93.     dVtable = (DWORD64*)dVtable[0];
  94. #elif defined _M_IX86
  95.     DWORD* dVtable = (DWORD*)d3ddev;
  96.     dVtable = (DWORD*)dVtable[0];
  97. #endif
  98.  
  99.     if (MH_Initialize() != MH_OK) { return 1; }
  100.     if (MH_CreateHook((DWORD_PTR*)dVtable[17], &Present_hook, reinterpret_cast<void**>(&Present_orig)) != MH_OK) { return 1; }
  101.     if (MH_EnableHook((DWORD_PTR*)dVtable[17]) != MH_OK) { return 1; }
  102.     if (MH_CreateHook((DWORD_PTR*)dVtable[100], &SetStreamSource_hook, reinterpret_cast<void**>(&SetStreamSource_orig)) != MH_OK) { return 1; }
  103.     if (MH_EnableHook((DWORD_PTR*)dVtable[100]) != MH_OK) { return 1; }
  104.     if (MH_CreateHook((DWORD_PTR*)dVtable[65], &SetTexture_hook, reinterpret_cast<void**>(&SetTexture_orig)) != MH_OK) { return 1; }
  105.     if (MH_EnableHook((DWORD_PTR*)dVtable[65]) != MH_OK) { return 1; }
  106.     if (MH_CreateHook((DWORD_PTR*)dVtable[16], &Reset_hook, reinterpret_cast<void**>(&Reset_orig)) != MH_OK) { return 1; }
  107.     if (MH_EnableHook((DWORD_PTR*)dVtable[16]) != MH_OK) { return 1; }
  108.  
  109.     d3ddev->Release();
  110.     d3d->Release();
  111.     DestroyWindow(tmpWnd);
  112.  
  113.     return 1;
  114.     }
  115.  
  116. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  117. {
  118.     if (ul_reason_for_call == 1) {
  119.             DisableThreadLibraryCalls(hModule);
  120.             CreateThread(0, 0, N7D3D, 0, 0, 0);
  121.         }
  122.     return 1;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment