need help with a hook that works on demo app but not in game

c++ / delphi package - dll injection and api hooking
Post Reply
Mal23
Posts: 3
Joined: Wed Aug 11, 2004 12:59 am
Contact:

need help with a hook that works on demo app but not in game

Post by Mal23 »

Hello,
first let me thank you for your great package madshi :=)
i got a little problem.. im trying to make a program which can send keyboard input to the game DAoC, but the hook doesnt work since the game is running inside a dll (at least i think thats the reason, since it works fine on a few demo apps).
if you look at the code below.. none of the 2 msg boxes pop up so i think the problem starts here..
do i have to use any other api hooking method instead of madshi's to be able to hook in dlls?
if thats the case which one would be recommended?
some code examples would be nice

Code: Select all

var
 DirectInputCreateANext : function (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;
function DirectInputCreateACallback   (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;
begin
  Result := DirectInputCreateANext(hinst, dwVersion, ppDI, punkOuter);
  showmessage('ok');
  if (Result = 0) and (@CreateDeviceNext = nil) then begin
    if HookCode(GetInterfaceMethod(ppDI, 3), @CreateDeviceCallback, @CreateDeviceNext) = false then showmessage('cant hook2');
  end;
end;


begin
if HookAPI('DINPUT.dll', 'DirectInputCreateA', @DirectInputCreateACallback, @DirectInputCreateANext) = false then showmessage('cant hook1');
end.



update:
i also wrote a wrapper dll now which exports DirectInputCreateA DirectInputCreateW DirectInputCreateEX

Code: Select all

begin 

  @p1 := GetProcAddress(loadlibrary('dinpu.dll'), 'DirectInputCreateA');
  
  result := p1(hinst, dwVersion, ppDI, punkOuter);
  if (Result = 0) and (@CreateDeviceNext = nil) then begin

    if HookCode(GetInterfaceMethod(ppDI, 3), @CreateDeviceCallback, @CreateDeviceNext) = false then showmessage('cant hook2') else  showmessage('kk');
  end;
end;
exports   DirectInputCreateA;
doesnt work too... works with test apps but when loading game with this dll it just closes after login screen when dx gets initialized
any ideas what im doing wrong?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Are you hooking DirectInputCreateW and DirectInputCreateEx, too? What about DirectInput8Create in dinput8.dll?
Mal23
Posts: 3
Joined: Wed Aug 11, 2004 12:59 am
Contact:

Post by Mal23 »

Code: Select all

begin
if HookAPI('DINPUT.dll', 'DirectInputCreateEX', @DirectInputCreateEXCallback, @DirectInputCreateEXNext) = false then showmessage('cant hook1');
if HookAPI('DINPUT.dll', 'DirectInputCreateW', @DirectInputCreateWCallback, @DirectInputCreateWNext) = false then showmessage('cant hook2');
if HookAPI('DINPUT.dll', 'DirectInputCreateA', @DirectInputCreateACallback, @DirectInputCreateANext) = false then showmessage('cant hook3');
end.
yes im hooking all 3 functions, just copied my createA 2 times since i guessed they are only internally different or do i have to declare them different too?
dinput8 isnt used in that game, i tried it, doesnt work too.
and since i dont get the msg boxes 'cant hook' i think it doesnt catch the game at all since its running as game.dll !?
when doing the same without injecting but making a wrapper dll the game just crashes, or did i do something wrong when writing the wrapper dll?
never did that before.. here is my wrapper code for createEX

Code: Select all

function DirectInputCreateEX   (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;

var
p1: funcpoint;
begin  
 showmessage('ex');
  @p1 := GetProcAddress(loadlibrary('dinpu.dll'), 'DirectInputCreateEX');
  
  result := p1(hinst, dwVersion, ppDI, punkOuter);
  if (Result = 0) and (@CreateDeviceNext = nil) then begin
    if HookCode(GetInterfaceMethod(ppDI, 3), @CreateDeviceCallback, @CreateDeviceNext) = false then showmessage('cant hook2') else  showmessage('kk');
  end;
end;
exports   DirectInputCreateEX;
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

API names are case sensitive! It must be "DirectInputCreateEx" instead of "DirectInputCreateEX".
Mal23
Posts: 3
Joined: Wed Aug 11, 2004 12:59 am
Contact:

Post by Mal23 »

doh, thanks :)
it was createEx daoc is using... but now it crashes instead of just doing nothing (while i had EX wrong) with using your hooking method, it doesnt even show the msg box, just crashes with cant write to ram error when computing import tables or something, in delphi debugger i could see ntll.rtlComputeImportTableHash a few pages before the crash.
im not good in asm or debugging so dunno what that means but thought ill write it here in case that helps finding the problem.
ill test more later, but my code works in a dx demo app which calls CreateA and CreateW, the hooked functions show mymsg boxes, everything works fine there...

Code: Select all

var
 DirectInputCreateExNext : function (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;
function DirectInputCreateExCallback   (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;
begin
 // showmessage('Ex');
  Result := DirectInputCreateExNext(hinst, dwVersion, ppDI, punkOuter);

 // if (Result = 0) and (@CreateDeviceNext = nil) then begin
  //  if HookCode(GetInterfaceMethod(ppDI, 3), @CreateDeviceCallback, @CreateDeviceNext) = false then showmessage('cant hook2');
  //end;
end;

begin
if HookAPI('DINPUT.dll', 'DirectInputCreateEx', @DirectInputCreateExCallback, @DirectInputCreateExNext) = false then showmessage('cant hook1');
if HookAPI('DINPUT.dll', 'DirectInputCreateW', @DirectInputCreateWCallback, @DirectInputCreateWNext) = false then showmessage('cant hook2');
if HookAPI('DINPUT.dll', 'DirectInputCreateA', @DirectInputCreateACallback, @DirectInputCreateANext) = false then showmessage('cant hook3');
end.
thanks for trying to help


update:
just tried same with the wrapper dll i wrote, again.. works in demo app but in daoc it crashes with the same error msg when the login app loads game.dll
msgbox isnt shown

Code: Select all


function DirectInputCreateEx   (hinst: THandle;
                                    dwVersion: DWORD;
                                    out ppDI: IDirectInputA;
                                    punkOuter: IUnknown): HResult; stdcall;

var
p1: funker;
begin
 showmessage('ex');
  @p1 := GetProcAddress(loadlibrary('dinpu.dll'), 'DirectInputCreateEx');

  result := p1(hinst, dwVersion, ppDI, punkOuter);
  if (Result = 0) and (@CreateDeviceNext = nil) then begin
    showmessage('ok5');
    if HookCode(GetInterfaceMethod(ppDI, 3), @CreateDeviceCallback, @CreateDeviceNext) = false then showmessage('cant hook2') else  showmessage('kk');
  end;
end;
exports   DirectInputCreateEx;
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

According to some googling DirectInputCreateEx has 5 parameters, not 4.
Astaelan
Posts: 22
Joined: Wed Sep 22, 2004 7:08 pm

Post by Astaelan »

Maybe I'm missing something here, but isn't he trying to call LoadLibrary("dinpu.dll")? Unless dinpu.dll is his own DLL (which it might be, sorry if it is), then is he maybe mistyping the name of the DLL to load?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Yeah, good find, I missed that.
Post Reply