Hooking Constructors

May 9, 2013 at 7:47 AM
Edited May 11, 2013 at 4:52 AM
Hi,

I'm trying to hook into a target application that was written in the Qt framework, so the functions I am hooking are object methods (as opposed to static functions).

So, a random example is:

qint64 QIODevice::read ( char * data, qint64 maxSize )

I can hook something of this form, and it works fine. My delegate looks like:

delegate Int64 DDeviceRead(IntPtr Device, IntPtr data, Int64 maxSize)

(The "IntPtr Device" as the first argument is how the "thiscall" calling convention knows the object it is operating on)

Generally, I take the IntPtr Device, and pass it to a C++/CLI wrapper, which calls the function natively, and just return the result back to C#, and then back to the target application.

There are two situations that I cannot get working:
  1. Any object constructors. So, something like "QIODevice::QIODevice ( QObject * parent )" is tricky, because firstly I'm not sure of the correct delegate signature, and secondly, once I create a new object instance in my C++/CLI code, I try to pass the object reference back to C# and then back to the target application, which invariably crashes. If anyone has a working example of calling a constructor in C++/CLI and passing that reference back to the target application, please let me know.
  2. Any method that returns a complex object rather than a simple type. For example, "QByteArray QIODevice::read ( qint64 maxSize )" returns a QByteArray, which I'm guessing I need to return as an IntPtr. (Yes, I could try to return something similar to byte[], but this is only an example - many of the functions return objects that don't map easily to simple types). Again, if anyone knows how I can pass such a return value from C++/CLI back to C# and back to the target application without it crashing, I would love you forever ...
Just so you know, C++/CLI is very new to me, so maybe I'm stuffing up pointers and references or something like that. I can also post sample code if anyone asks for it.

Ta!