Hook member functions/constructors

Hi, can somebody show an example of how to hook member functions and constructors?

This is what I have tried, but the program crashes.
typedef void (*ConstructorFunc)();
static ConstructorFunc origConstructor;

static void myConstructor()
{
origConstructor(); // how to call the original constructor?
}

progConstructor = mHooks.createHookByName("some.dll", "", &myConstructor);

Thanks for any help!

Sorry, the code got fucked

Sorry, the code got fucked up. Here's it again:

typedef void (*ConstructorFunc)();
static ConstructorFunc origConstructor;

static void myConstructor()
{
// this function gets called correctly
origConstructor(); // ...but how to call the original constructor?
}

progConstructor = mHooks.createHookByName("some.dll", "(mangled constructor signature)", &myConstructor);

Hook member functions/constructors

You can only hook C-style functions safely. C++ constructors/member functions are special, since these are called with a special (compiler dependent) calling convention. See e.g. CP article or google for c++ ABI / calling convention.

On assembly level this means that e.g. the MS compiler will pass the this-pointer to a given object in the ECX register.
However, NCodeHook doesn't know about this, because it would need to perform considerable program analysis to find out.

So there is no easy way to achieve what you want to do.
A workaround could be to wrap your C++ objects/methods into a C-style interface dll and hook those functions.
Hope that helps

Thanks for your reply. Alas,

Thanks for your reply. Alas, a C-style wrapper is not an option.

I solved it by using a _declspec(naked) function and inline asm code to call the original constructor. I haven't tried yet to call a constructor with arguments, or other member functions, but this one does what is required so far.