fork download
  1. /*
  2. * RA3INFO | 'Red Alert 3' By Gamer_Z/Grasmanek94 | http://g...content-available-to-author-only...e.com/
  3. */
  4. #include <main.h>
  5. #include <windows.h>
  6. #include <d3d9.h>
  7. #include <assert.h>
  8.  
  9. class VirtualMethodHook
  10. {
  11. public:
  12. // Types.
  13. typedef void (*func_ptr_t)();
  14. typedef struct {
  15. union {
  16. func_ptr_t function;
  17. ptrdiff_t offset;
  18. };
  19. ptrdiff_t delta;
  20. } method_ptr_t;
  21. typedef struct {
  22. func_ptr_t func[1];
  23. } vtable_t;
  24. typedef struct {
  25. vtable_t * vtable_ptr;
  26. } class_instance_t;
  27. private:
  28. // Hook a vtable virtual method and replace its entry with another function.
  29. static func_ptr_t _HookMethod(vtable_t * hookedVTable, method_ptr_t * oldMethod, func_ptr_t newFunction)
  30. {
  31. // We want the offset to be odd, indicating that this method pointer is an offset into a vtable.
  32. if (!(oldMethod->offset & 0x1)) {
  33. return NULL;
  34. }
  35. // Get the index into the vtable.
  36. assert(!((oldMethod->offset - 1) % sizeof(func_ptr_t)));
  37. size_t hookedIndex = (oldMethod->offset - 1) / sizeof(func_ptr_t);
  38. // Replace the hooked method and return the original function pointer.
  39. func_ptr_t original = hookedVTable->func[hookedIndex];
  40. hookedVTable->func[hookedIndex] = newFunction;
  41. return original;
  42. }
  43. public:
  44. // Hook a virtual method with a function pointer, returning the original function pointer.
  45. #if __cplusplus >= 201103L
  46. template <class _HookedClass, typename _Ret, typename ..._Args>
  47. static _Ret (*HookMethod(_HookedClass * instance, _Ret (_HookedClass::*oldMethod)(_Args...), _Ret (*newFunction)(_HookedClass *, _Args...)))(_HookedClass *, _Args...)
  48. #else
  49. template <class _HookedClass, typename _Method, typename _Func>
  50. static _Func HookMethod(_HookedClass * instance, _Method oldMethod, _Func newFunction)
  51. #endif
  52. {
  53. assert(sizeof(oldMethod) == sizeof(method_ptr_t));
  54. // Get the vtable for the instance.
  55. vtable_t * vtable = ((class_instance_t *) instance)->vtable_ptr;
  56. if (!vtable) {
  57. // This class has no virtual methods.
  58. return NULL;
  59. }
  60. // Replace the function pointer in the virtual method.
  61. return
  62. #if __cplusplus >= 201103L
  63. (_Ret (*)(_HookedClass *, _Args...))
  64. #else
  65. (_Func)
  66. #endif
  67. _HookMethod(vtable, (method_ptr_t *) &oldMethod, (func_ptr_t) newFunction);
  68. }
  69. // Unhook (restore) a virtual method from the original.
  70. #if __cplusplus >= 201103L
  71. template <class _HookedClass, typename _Ret, typename ..._Args>
  72. static bool UnhookMethod(_HookedClass * instance, _Ret (_HookedClass::*hookedMethod)(_Args...), _Ret (*originalFunction)(_HookedClass *, _Args...))
  73. #else
  74. template <class _HookedClass, typename _Method, typename _Func>
  75. static bool UnhookMethod(_HookedClass * instance, _Method hookedMethod, _Func originalFunction)
  76. #endif
  77. {
  78. // Hook in reverse.
  79. return (bool) HookMethod(instance, hookedMethod, originalFunction);
  80. }
  81. };
  82.  
  83. HMODULE gl_hOriginalDll = NULL;
  84. IDirect3DDevice9 * device = NULL;
  85. IDirect3D9 *pIDirect3D9_orig;
  86.  
  87. typedef HRESULT(_stdcall * orig_CreateDevice)(UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface);
  88. orig_CreateDevice OCD;
  89.  
  90. VirtualMethodHook * OriginalCreateDevice;
  91.  
  92. void LoadOriginalDll(void)
  93. {
  94. char buffer[MAX_PATH];
  95. ::GetSystemDirectory(buffer,MAX_PATH);
  96. strcat(buffer,"\\d3d9.dll");
  97. if (!gl_hOriginalDll) gl_hOriginalDll = ::LoadLibrary(buffer);
  98. if (!gl_hOriginalDll)
  99. {
  100. OutputDebugString("PROXYDLL: Original d3d9.dll not loaded ERROR ****\r\n");
  101. ::ExitProcess(0);
  102. }
  103. }
  104.  
  105. HRESULT WINAPI myCreateDevice(UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface)
  106. {
  107. HRESULT hres = OCD( Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
  108.  
  109. return hres;
  110. }
  111.  
  112. IDirect3D9* WINAPI Direct3DCreate9(UINT SDKVersion)
  113. {
  114. if (!gl_hOriginalDll) LoadOriginalDll();
  115.  
  116. typedef IDirect3D9 *(WINAPI* D3D9_Type)(UINT SDKVersion);
  117. D3D9_Type D3DCreate9_fn = (D3D9_Type) GetProcAddress( gl_hOriginalDll, "Direct3DCreate9");
  118.  
  119. if (!D3DCreate9_fn)
  120. {
  121. OutputDebugString("PROXYDLL: Pointer to original D3DCreate9 function not received ERROR ****\r\n");
  122. ::ExitProcess(0);
  123. }
  124.  
  125. pIDirect3D9_orig = D3DCreate9_fn(SDKVersion);
  126. OriginalCreateDevice = new VirtualMethodHook();
  127. OCD = OriginalCreateDevice->HookMethod(pIDirect3D9_orig,&IDirect3D9::CreateDevice,myCreateDevice);
  128. //OCD = IDirect3D9::CreateDevice;
  129. //*pIDirect3D9_orig->CreateDevice = &IDirect3D9::CreateDevice;
  130. return (pIDirect3D9_orig);
  131. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty