 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Fri Mar 25, 2011 11:39 am Post subject: [Solved] CreateDevice hook crash |
|
|
Below is the function that is jumped to from IDirect3D9::CreateDevice.
If I attempt to access any of the args or get the return value of "oCreateDevice", then the program crashes. However if I don't access them, then it works fine.
I had the same problem when I forgot to add __stdcall for the Direct3D9Create hook. But that works fine now.
Code: |
typedef HRESULT(WINAPI *r_createDevice)(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS *pPresentationParameters, IDirect3DDevice9 **ppReturnedDeviceInterface);
r_createDevice ocreateDevice;
HRESULT WINAPI CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS *pPresentationParameters, IDirect3DDevice9 **ppReturnedDeviceInterface) {
output("d3d->CreateDevice");
return ocreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);;
}
|
Last edited by 661089799107 on Sat Mar 26, 2011 10:10 am; edited 1 time in total |
|
Back to top |
|
 |
sloppy Expert Cheater
Reputation: 0
Joined: 17 Aug 2008 Posts: 123
|
Posted: Fri Mar 25, 2011 4:48 pm Post subject: |
|
|
Your declaration looks fine to me.. but without implementation details of the hook it is difficult to offer any helpful advice.
|
|
Back to top |
|
 |
Innovation Grandmaster Cheater
Reputation: 12
Joined: 14 Aug 2008 Posts: 617
|
Posted: Fri Mar 25, 2011 4:50 pm Post subject: |
|
|
What makes you assume that IDirect3D9::CreateDevice uses the stdcall calling convention?
|
|
Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Fri Mar 25, 2011 5:32 pm Post subject: |
|
|
Innovation wrote: | What makes you assume that IDirect3D9::CreateDevice uses the stdcall calling convention? |
I'm not sure which one it is. I've tried using cdecl/stdcall/thiscall, but it still crashes (if I try to access an arg/return value).
CreateDevice uses "RETN 1C" to return - so it would have to be thiscall/stdcall(?)
sloppy wrote: | Your declaration looks fine to me.. but without implementation details of the hook it is difficult to offer any helpful advice. |
CreateDevice hook:
Code: |
IDirect3D9* __stdcall Direct3DCreate9(UINT SDKVersion) {
IDirect3D9* retVal;
unsigned int createDeviceAddress;
output("Direct3DCreate9");
__asm {
push SDKVersion
call ocreate
mov retVal, eax
push edi
mov edi, eax
// EAX = the value EDI is pointing to (4FDD1A98)
mov eax, dword ptr ds:[edi]
// EDI = [4FDD1A98 + 40] = 4FE51670
mov edi, dword ptr ds:[eax+40h]
// EDI = 4FEE51670 (d3d9.CreateDevice)
mov createDeviceAddress, edi
pop edi
}
if(!ocreateDevice)
ocreateDevice = static_cast<r_createDevice>(setHook(reinterpret_cast<BYTE*>(createDeviceAddress), reinterpret_cast<BYTE*>(&CreateDevice)));
return retVal;
}
|
Direct3DCreate9 hook: (inside DllMain)
Code: |
ocreate = static_cast<r_D3DCreate>(setHook(reinterpret_cast<BYTE*>(GetProcAddress(GetModuleHandle("d3d9.dll"), "Direct3DCreate9")), reinterpret_cast<BYTE*>(&Direct3DCreate9)));
|
Function that replaces opcodes with a jump:
Code: |
void* setHook(BYTE *src, const BYTE *dst, int len=5) {
BYTE *jmp = (BYTE*)malloc(len+5);
DWORD dwback;
VirtualProtect(src, len, PAGE_READWRITE, &dwback);
memcpy(jmp, src, len); jmp += len;
jmp[0] = 0xE9;
*(DWORD*)(jmp+1) = (DWORD)(src+len - jmp) - 5;
src[0] = 0xE9;
*(DWORD*)(src+1) = (DWORD)(dst - src) - 5;
VirtualProtect(src, len, dwback, &dwback);
return (jmp-len);
}
|
Last edited by 661089799107 on Sat Mar 26, 2011 1:26 pm; edited 1 time in total |
|
Back to top |
|
 |
sponge I'm a spammer
Reputation: 1
Joined: 07 Nov 2006 Posts: 6009
|
Posted: Fri Mar 25, 2011 9:04 pm Post subject: |
|
|
Code: | STDMETHOD(CreateDevice)(THIS_ UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, struct IDirect3DDevice9** ppReturnedDeviceInterface) PURE; |
Code: | #define STDMETHOD(method) virtual HRESULT STDMETHODCALLTYPE method
#define STDMETHODCALLTYPE __stdcall |
Code: | #define THIS_ INTERFACE *This,
#define INTERFACE IDirect3D9 |
Sneaky macros in my SDK?!?!
Macro deobfuscation:
Code: | virtual HRESULT __stdcall CreateDevice(IDirect3D9 *This, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, struct IDirect3DDevice9** ppReturnedDeviceInterface) = 0; |
COM Interface methods all have a hidden first parameter, the interface pointer.
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Sat Mar 26, 2011 5:27 am Post subject: |
|
|
@Bill87: Why are you using inline asm? There isn't any reason to use it. Along with that you are probably crashing because you aren't respecting the registers previous values and just overwriting them.
Cast your pointer to the definition of Direct3DCreate9:
Code: | typedef IDirect3D9* (__stdcall oD3DCreate9)( UINT );
oD3DCreate9 ocreate = 0x12345678; // Your addr here etc. |
And just call it normally:
Code: | IDirect3D9* pD3D9 = ocreate( SDKVersion ); |
_________________
- Retired. |
|
Back to top |
|
 |
661089799107 Expert Cheater
Reputation: 3
Joined: 25 Jan 2009 Posts: 186
|
Posted: Sat Mar 26, 2011 10:08 am Post subject: |
|
|
sponge wrote: |
Macro deobfuscation:
Code: | virtual HRESULT __stdcall CreateDevice(IDirect3D9 *This, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, struct IDirect3DDevice9** ppReturnedDeviceInterface) = 0; |
COM Interface methods all have a hidden first parameter, the interface pointer. |
I added the "hidden parameter", and now it works
Thanks everyone
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|