Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Hooking a class function?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
legoblock
How do I cheat?
Reputation: 0

Joined: 01 Jun 2011
Posts: 9

PostPosted: Thu Jun 02, 2011 6:36 pm    Post subject: Hooking a class function? Reply with quote

Specifically I would like to hook IDirect3DDevice9::BeginScene.

Here is (basically) the code I have so far, using Microsoft Detours.

Code:
#include "RenderHook.h"
#include "DetourManager.h"

HRESULT (WINAPI IDirect3DDevice9::* _BeginScene)() = &IDirect3DDevice9::BeginScene;

HRESULT __fastcall BeginSceneHook(IDirect3DDevice9* This)
{
  UNREFERENCED_PARAMETER(This);

  // return success without actually beginning scene to stop rendering (?)
  return D3D_OK;
}

BOOL HookRendering(__in BOOL bSet)
{
  return ((bSet ? AttachDetour : DetachDetour)(reinterpret_cast<PVOID*>(_BeginScene), BeginSceneHook));
}


So apparently I can't cast pointers to class functions to any other type of pointer (e.g. void*, etc.) so I can't really hook it at all. Is there another method altogether of doing this? I've heard classes are really just pointers to their methods, is there a way of changing that pointer to point to my function?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 02, 2011 7:53 pm    Post subject: Reply with quote

that cast is undefined behavior i think.

the easy way is to just create a non member function as a wrapper and pass that.
Back to top
View user's profile Send private message
legoblock
How do I cheat?
Reputation: 0

Joined: 01 Jun 2011
Posts: 9

PostPosted: Thu Jun 02, 2011 8:51 pm    Post subject: Reply with quote

But then how can I hook it?
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 02, 2011 9:00 pm    Post subject: Reply with quote

wrap the member function in another function and pass the instance of the class to your function wrapper function and use that as the function that you pass to the detour.

snort.

i've never used the detours library so i'm running on the assumption that you'll need to do something similar to using a member function for CreateThread()
Back to top
View user's profile Send private message
legoblock
How do I cheat?
Reputation: 0

Joined: 01 Jun 2011
Posts: 9

PostPosted: Thu Jun 02, 2011 9:41 pm    Post subject: Reply with quote

No like, I have to pass the address of the function I want to hook. If I do that, won't my wrapper function get hooked instead?

EDIT: Apparently I needed to use the class' vtable. It's all good, thanks! (can't post the link that helped me yet)
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Fri Jun 03, 2011 1:20 pm    Post subject: Reply with quote

Keep in mind Direct3D API uses __stdcall for almost all of its definitions, you need to follow their conventions or you will run into issues.

If you want to hook just a single specific function, obtain the vtable pointer for the device and overwrite the vtables pointer for that function at the proper offset. If you still need more assistance, there are quiet a few threads over on GameDeception that cover this. I would recommend not posting for help there though as you will land up just getting flamed.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites