Guest User

Untitled

a guest
Oct 17th, 2014
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.03 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <time.h>
  4. #include <d3d9.h>
  5. #include <d3dx9.h>
  6. #include "Functions.h"
  7.  
  8. #undef MessageBox
  9. #undef DrawText
  10. //---------------------------------------------------------------------------
  11. //the basic includes for OSHGui
  12. #include <OSHGui.hpp>
  13. #include "Input/WindowsMessage.hpp"
  14. #include "Drawing/Direct3D9/Direct3D9Renderer.hpp"
  15. //---------------------------------------------------------------------------
  16. //optional: make the namespace available
  17. using namespace OSHGui;
  18. using namespace OSHGui::Drawing;
  19. //---------------------------------------------------------------------------
  20. Input::WindowsMessage input;
  21. HHOOK hookHandle;
  22. //-----------------------------------
  23. void InitializeOSHGui(LPDIRECT3DDEVICE9 device)
  24. {
  25.     //1. create our renderer, Direct3D9 in this case
  26.     auto renderer = std::unique_ptr<Direct3D9Renderer>(new Direct3D9Renderer(device));
  27.  
  28.     //2. and initialize the OSHGui with it
  29.     Application::Initialize(std::move(renderer));
  30.  
  31.     //or 1.+2.
  32.     //Application::Initialize(std::unique_ptr<Direct3D9Renderer>(new Direct3D9Renderer(device)));
  33.  
  34.     //now we have a valid OSHGui instance, so lets grab it
  35.     auto &app = Application::Instance();
  36.  
  37.     //3. create a font which will be used by the OSHGui
  38.     auto font = FontManager::LoadFont("Arial", 8.0f, false); //Arial, 8PT, no anti-aliasing
  39.     app.SetDefaultFont(font);
  40.  
  41.     //4. create our form
  42.     auto form = std::make_shared<Form>();
  43.  
  44.     //5. set this form as our mainform
  45.     app.Run(form);
  46.  
  47.     //optional: enable the OSHGui drawing
  48.     app.Enable();
  49.  
  50.     //optional: register a Hotkey with which we can toggle the OSHGui drawing
  51.     app.RegisterHotkey(Hotkey(Key::Insert, []
  52.     {
  53.         Application::Instance().Toggle();
  54.     }));
  55. }
  56. //---------------------------------------------------------------------------
  57. void Render(LPDIRECT3DDEVICE9 device)
  58. {
  59.     device->Clear(0, 0, D3DCLEAR_TARGET, 0x0, 1.0f, 0);
  60.     device->BeginScene();
  61.  
  62.     auto &renderer = Application::Instance().GetRenderer();
  63.  
  64.     //1. let our renderer begin its work
  65.     renderer.BeginRendering();
  66.  
  67.     //2. render the OSHGui
  68.     Application::Instance().Render();
  69.  
  70.     //3. end the rendering
  71.     renderer.EndRendering();
  72.  
  73.     device->EndScene();
  74.     device->Present(0, 0, 0, 0);
  75. }
  76. ////---------------------------------------------------------------------------
  77. class Win32Window
  78. {
  79. public:
  80.     Win32Window(HINSTANCE instance)
  81.     {
  82.         ZeroMemory(&wc, sizeof(wc));
  83.         wc.style = CS_CLASSDC;
  84.         wc.lpfnWndProc = DefWindowProc;
  85.         wc.hInstance = instance;
  86.         wc.lpszClassName = L"D3D";
  87.         wc.hCursor = LoadCursor(0, IDC_ARROW);
  88.  
  89.         RegisterClass(&wc);
  90.  
  91.         RECT windowSize = { 0, 0, 1280, 720 };
  92.         AdjustWindowRect(&windowSize, WS_OVERLAPPEDWINDOW, false);
  93.         windowSize.right = std::abs(windowSize.right) + std::abs(windowSize.left);
  94.         windowSize.bottom = std::abs(windowSize.bottom) + std::abs(windowSize.top);
  95.  
  96.         Window = CreateWindowA("D3D", "D3D loser", WS_OVERLAPPEDWINDOW, 0, 100, windowSize.right, windowSize.bottom, GetDesktopWindow(), 0, wc.hInstance, 0);
  97.     }
  98.     ~Win32Window()
  99.     {
  100.         if (wc.hInstance)
  101.         {
  102.             UnregisterClass(L"D3D", wc.hInstance);
  103.         }
  104.     }
  105.  
  106.     HWND Window;
  107.  
  108. private:
  109.     WNDCLASS wc;
  110. };
  111. //---------------------------------------------------------------------------
  112. //wrapper to setup D3D9
  113. //---------------------------------------------------------------------------
  114. class Direct3DDevice
  115. {
  116. public:
  117.     Direct3DDevice(HWND hwnd) : D3D(nullptr), Device(nullptr)
  118.     {
  119.         if (!(D3D = Direct3DCreate9(D3D_SDK_VERSION)))
  120.         {
  121.             throw std::runtime_error("can't create Direct3D9");
  122.         }
  123.  
  124.         RECT rect;
  125.         GetClientRect(hwnd, &rect);
  126.  
  127.         D3DPRESENT_PARAMETERS pp;
  128.         ZeroMemory(&pp, sizeof(pp));
  129.         pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  130.         pp.BackBufferFormat = D3DFMT_X8R8G8B8;
  131.         pp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  132.         pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  133.         pp.Windowed = TRUE;
  134.         pp.BackBufferWidth = rect.right;
  135.         pp.BackBufferHeight = rect.bottom;
  136.  
  137.         if (FAILED(D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &Device)))
  138.         {
  139.             D3D->Release();
  140.  
  141.             throw std::runtime_error("can't create device");
  142.         }
  143.  
  144.         D3DXMATRIX projection;
  145.         D3DXMatrixPerspectiveFovLH(&projection, D3DXToRadian(45.0f), 584.0f / 562.0f, 1.0f, 100.0f);
  146.         Device->SetTransform(D3DTS_PROJECTION, &projection);
  147.     }
  148.     ~Direct3DDevice()
  149.     {
  150.         if (Device)
  151.         {
  152.             Device->Release();
  153.         }
  154.         if (D3D)
  155.         {
  156.             D3D->Release();
  157.         }
  158.     }
  159.  
  160.     LPDIRECT3DDEVICE9 Device;
  161.  
  162. private:
  163.     LPDIRECT3D9 D3D;
  164. };
  165. //---------------------------------------------------------------------------
  166.  
  167.  
  168. const D3DCOLOR txtGreen = D3DCOLOR_ARGB(255, 0, 255, 0);
  169. HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice);
  170. typedef HRESULT(__stdcall* EndScene_t)(LPDIRECT3DDEVICE9);
  171. EndScene_t pEndScene;
  172. DWORD WINAPI HookThread();
  173.  
  174. HMODULE hModD3D9 = NULL;
  175. FARPROC dwEndScene = NULL;
  176. HANDLE tmpHandle = NULL;
  177. DWORD* VTableStart = NULL;
  178. DWORD tempadd = NULL;
  179.  
  180.  
  181.  
  182. BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD Reason,LPVOID Reserved)
  183. {
  184.     switch(Reason)
  185.     {
  186.     case DLL_PROCESS_ATTACH:
  187.         add_log("==========LOG START==========");
  188.         add_log("DLL Attached");
  189.         add_log("Creating Thread...");
  190.         tmpHandle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&HookThread, 0, 0, 0);
  191.         if (!tmpHandle)
  192.         {
  193.             add_log("ThreadCreation Failed!");
  194.         }
  195.         break;
  196.     case DLL_PROCESS_DETACH:
  197.         add_log("DLL Detached");
  198.         add_log("==========LOG END==========\n\n\n");
  199.         break;
  200.     }
  201.     return 1;
  202. }
  203.  
  204.  
  205. LRESULT CALLBACK MessageHook(int code, WPARAM wParam, LPARAM lParam)
  206. {
  207.     if (lParam &0x80000000 || lParam &0x40000000)
  208.     {
  209.         return CallNextHookEx(hookHandle, code, wParam, lParam);
  210.     }
  211.     if (code == HC_ACTION)
  212.     {
  213.         if (input.ProcessMessage((LPMSG)lParam))
  214.         {
  215.             return TRUE;
  216.         }
  217.     }
  218.     return CallNextHookEx(hookHandle, code, wParam, lParam);
  219. }
  220.  
  221.  
  222.  
  223. DWORD WINAPI HookThread(void)
  224. {
  225.     add_log("Thread Created");
  226.     while (!hModD3D9)
  227.     {
  228.         add_log("Searching d3d9.dll...");
  229.         hModD3D9 = GetModuleHandle(L"d3d9.dll");
  230.         Sleep(100);
  231.     }
  232.     add_log("Found d3d9.dll: %x !", hModD3D9);
  233.     tempadd = dwFindPattern((DWORD)hModD3D9, 0x128000,
  234.         (PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86", "xx????xx????xx");
  235.     VTableStart = (DWORD*) *(DWORD*)(tempadd+2);
  236.     dwEndScene = (FARPROC) VTableStart[42];
  237.     pEndScene = (EndScene_t) DetourFunc((PBYTE) dwEndScene, (PBYTE)hkEndScene, 5);
  238.  
  239.    
  240.    
  241.  
  242.  
  243.     //neu----
  244.     hookHandle = SetWindowsHookEx(WH_GETMESSAGE, MessageHook, NULL, GetCurrentThreadId());
  245.    
  246.     //Hier der Code-----------------------------???----------------------
  247.     //Direct3DDevice device(window.Window);
  248.  
  249.     //InitializeOSHGui(device.Device);
  250.  
  251.     //ShowWindow(window.Window, SW_SHOWDEFAULT);
  252.     //UpdateWindow(window.Window);
  253.  
  254.     //MSG msg;
  255.     //ZeroMemory(&msg, sizeof(msg));
  256.     //while (true)
  257.     //{
  258.     //  if (!IsWindowVisible(window.Window))
  259.     //  {
  260.     //      break;
  261.     //  }
  262.  
  263.     //  if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  264.     //  {
  265.     //      //let the OSHGui handle the input
  266.     //      if (!input.ProcessMessage(&msg))
  267.     //      {
  268.     //          if (msg.message == WM_QUIT)
  269.     //          {
  270.     //              break;
  271.     //          }
  272.  
  273.     //          TranslateMessage(&msg);
  274.     //          DispatchMessage(&msg);
  275.     //      }
  276.     //  }
  277.     //  else
  278.     //  {
  279.     //      Render(device.Device);
  280.     //  }
  281.     //}
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.     while (true)
  289.     {
  290.         Sleep(500);
  291.     }
  292.     return 0;
  293. }
  294.  
  295.  
  296. HRESULT __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
  297. {
  298.     Render(pDevice);
  299.  
  300.     //Hier der Code-----------------------------???----------------------
  301.     //Direct3DDevice device(window.Window);
  302.  
  303.     //InitializeOSHGui(device.Device);
  304.  
  305.     //ShowWindow(window.Window, SW_SHOWDEFAULT);
  306.     //UpdateWindow(window.Window);
  307.  
  308.     //MSG msg;
  309.     //ZeroMemory(&msg, sizeof(msg));
  310.     //while (true)
  311.     //{
  312.     //  if (!IsWindowVisible(window.Window))
  313.     //  {
  314.     //      break;
  315.     //  }
  316.  
  317.     //  if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  318.     //  {
  319.     //      //let the OSHGui handle the input
  320.     //      if (!input.ProcessMessage(&msg))
  321.     //      {
  322.     //          if (msg.message == WM_QUIT)
  323.     //          {
  324.     //              break;
  325.     //          }
  326.  
  327.     //          TranslateMessage(&msg);
  328.     //          DispatchMessage(&msg);
  329.     //      }
  330.     //  }
  331.     //  else
  332.     //  {
  333.     //      Render(device.Device);
  334.     //  }
  335.     //}
  336.  
  337.  
  338.     return pEndScene(pDevice);
  339. }
Advertisement
Add Comment
Please, Sign In to add comment