HookAPI crashes without calling InitializeMadCHook

c++ / delphi package - dll injection and api hooking
Post Reply
_NN_
Posts: 55
Joined: Mon Jan 21, 2013 4:00 pm

HookAPI crashes without calling InitializeMadCHook

Post by _NN_ »

Is it possible to just fail and return FALSE instead of producing Access Violation when HookAPI is called without InitializeMadCHook ?
madshi
Site Admin
Posts: 10376
Joined: Sun Mar 21, 2004 5:25 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by madshi »

It's difficult, especially when using the Delphi LIB, because if you don't call InitializeMadCHook(), the whole Delphi RTL is not even initialized at all.

It would not be difficult to make HookAPI() not crash, it's just one API, but I'd have to add the same check to each and every exported function, and in Delphi that's a lot more than madCHook.h contains. So a lot of work and added code, just to protect against incorrect usage? Not sure if I like that...
_NN_
Posts: 55
Joined: Mon Jan 21, 2013 4:00 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by _NN_ »

You can add it inline in the header if you don't want to touch Delphi source.
I understand it is incorrect usage, but I spent several minutes trying to understand why I have crash in some irrelevant address.
madshi
Site Admin
Posts: 10376
Joined: Sun Mar 21, 2004 5:25 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by madshi »

Well, FWIW, I always recommend to use one of the demo projects as the starting point for your own projects. That way you already have the basic framework set up correctly, including compiler and linker settings etc.
_NN_
Posts: 55
Joined: Mon Jan 21, 2013 4:00 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by _NN_ »

This discussion goes to direction who is responsible to check the caller or the callee.
My opinion that callee will do as much checks as possible, while you say it is not really necessary.

I already made a class which does this automatically, so I won't forget :)
Btw, perhaps you need some C++ API on top of your header which will take care of all this initializations.
Is it something that you would accept as patch ?
madshi
Site Admin
Posts: 10376
Joined: Sun Mar 21, 2004 5:25 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by madshi »

I'm not sure if I'd like a C++ API. I like a simple and short API. And if I distribute your C++ API, I also become responsible for maintaining it and fixing bugs in it. And if I add or change APIs, I also have to do all that in the C++ API files in addition to the current C header.

Anyway, how complicated is your C++ API? Does it fit into a single e.g. *.h file?
_NN_
Posts: 55
Joined: Mon Jan 21, 2013 4:00 pm

Re: HookAPI crashes without calling InitializeMadCHook

Post by _NN_ »

Currently I removed all wrappers since the code is very simple.
Maybe in the future I'll do wrapping which will initialize madCodeHook automatically on first call if wasn't initialized before or smth like that.
Post Reply