Hooking class methoods (DirectInput)

c++ / delphi package - dll injection and api hooking
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Hooking class methoods (DirectInput)

Post by ginsoaked »

I've used MS Detours a bit and I've looked at madcodehook and I was wondering something. I need to hook into DirectInput and I've seen that in madcodehook you can use HookAPI to get the DirectInput8Create() call and then inside there you can use HookCode to hook the CreateDevice() method of the DirectInput8 object created by DirectInput8Create(). Does anyone know if there is a way to do this kind of thing using Detours or does it not have that capability? If so, does anyone have a link or some code so I can see what it would do?

I know this isn't a Detours forum, but I just want to compare the methods and I thought some people here might have some useful info.

Thanks!
Last edited by ginsoaked on Tue Oct 19, 2004 12:19 pm, edited 1 time in total.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Theoretically Detours should be able to do the same thing, because it uses a similar (but less advanced) hooking method to madCodeHook. But I don't know how exactly it works in Detours, since I haven't looked at that for ages...
Sirmabus
Posts: 89
Joined: Fri May 28, 2004 6:20 pm

Post by Sirmabus »

madCodeHook works great to hook DirectInput.

It's very stable too. I've used peoples programs that hook in a bad way and have random crashes.

Use madCodeHook!
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Thanks... :D
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Post by ginsoaked »

Sirmabus, have you tried hooking DirectInput (and IDirectInput::CreateDevice()) using MadCodeHook and C++?
Or anyone else, for that matter?
I'm having some trouble with it. I can hook DirectInput8Create() fine, but my CreateDevice() hook never seems to get called.

This is what the HookCode() call looks like:
HookCode(GetInterfaceMethod((IDirectInput8*)DI, 3), DICreateDeviceCallback, (PVOID*) &DICreateDeviceNextHook);

And here's how the functions are defined:

HRESULT (*DICreateDeviceNextHook)(LPVOID Self, REFGUID rguid, LPDIRECTINPUTDEVICE *lplpDirectInputDevice, LPUNKNOWN pUnkOuter);
HRESULT DICreateDeviceCallback(LPVOID Self, REFGUID rguid, LPDIRECTINPUTDEVICE *lplpDirectInputDevice, LPUNKNOWN pUnkOuter);


Is there something special I need to do to hook a class method using HookCode()? (Aside from adding the Self parameter)

Any suggestions or ideas would be appreciated.

Thanks!
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Are you sure that 3 is the correct index? I've not hooked DirectInput myself yet. But as far as I remember there are at least 2 people who successfully hooked DirectInput with madCodeHook.
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Post by ginsoaked »

It's the 4th method listed so it should be 3, right?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

No, every interface has QueryInterface, AddRef and Release. So it's should be index 6.
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Post by ginsoaked »

When I said it was the 4th listed I was including those 3.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Ah well, then index 3 should be alright...
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

If HookCode returns TRUE, and your hook callback function is never called, and no crash occurs, either, then I can only think of one reason: Probably nobody is calling that function. Is that possible? Are you sure that the function is really called?
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Post by ginsoaked »

HookCode() is returning 0, does that mean there was an error? I wasn't able to find anything about what the return values mean or error reporting, etc.
Is there a way I can find out what happened (using GetLastError() or exceptions or something)?

Thanks.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

If HookCode returns 0, that definately indicates that hooking failed. You can check whether GetLastError reports something useful. Sometimes it does, sometimes not... :oops:
ginsoaked
Posts: 10
Joined: Thu Sep 30, 2004 12:46 am

Post by ginsoaked »

GetLastError() returns 87 which is "The parameter is incorrect."
And idea what it might be referring to?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Hmmmm... Not sure. Please log what "GetInterfaceMethod((IDirectInput8*)DI, 3)" returns.
Post Reply