My first hook-Project

c++ / delphi package - dll injection and api hooking
Post Reply
Sunshine
Posts: 2
Joined: Wed May 25, 2005 9:15 pm

My first hook-Project

Post by Sunshine »

Hi all!

I just doing my first steps with madCodeHook and want to write a own small application. Inside this project I want to convert an existing and functioning Hook-DLL from C to Delphi. But there is an failure in my code, which I can't find. The target application which uses the hooked-DLL crashes imedeatly, whenn I start my own application. Here is the code of original hook-library

Code: Select all

void*	g_pDataBlockToMessage = NULL;

// ?DataBlockToMessage@@YAPAVMessage_t@@IPAX@Z
// is named DataBlockToMessage, returns a Message_t*,
// and takes an int and a void* as parameters.

//Message_t is actually a unknown class 
// But just to define a pointer on it,
// we don't need to know what it is.
typedef void Message_t;

Message_t* ( * pOriginalDataBlockToMessage )( int _Size, void* _pDataBlock );

Message_t* DataBlockToMessageHook( int _Size, void* _pDataBlock )
{
               /// Do something with the data.......  

	return pOriginalDataBlockToMessage( _Size, _pDataBlock );
}
In my own delphi main-application if have the following code:

Code: Select all

sucess:=InjectLibrary(ALL_SESSIONS or SYSTEM_PROCESSES, 'Hook.dll');
And in my hook.dll is the following code

Code: Select all

OriginalDataBlockToMessage : function (size: Integer; datablock: Pointer) :Pointer; cdecl;


function DataBlockToMessageHook(size : Integer; datablock: Pointer) :Pointer; stdcall;
begin
// Do something with the data..... (e.g. write to file)

result:=OriginalDataBlockToMessage(size,datablock);

end;

begin
HookAPI('path_to_dll','?DataBlockToMessage@@YAPAVMessage_t@@IPAX@Z',@DataBlockToMessageHook,@OriginalDataBlockToMessage);
end.
Thanks for any help in advance!
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

OriginalDataBlockToMessage : function (size: Integer; datablock: Pointer) :Pointer; cdecl;
function DataBlockToMessageHook(size : Integer; datablock: Pointer) :Pointer; stdcall;

Use either cdecl or stdcall, but mixing it like that is crazy!
Sunshine
Posts: 2
Joined: Wed May 25, 2005 9:15 pm

Post by Sunshine »

Thx, you made my day! Now it works :)
Post Reply