Stack Overflow
您好,
我正在编写一个C#应用程序来嗅探使用Qt编写的应用程序的网络流量。据我所知,目标应用程序使用:
class QByteArray _ thiscall QIODevice :: read( _int64)
读取数据。我的C#应用程序使用EasyHook库拦截对QIODevice :: read()的任何调用,然后将参数传递给尝试调用本机方法的C ++ / CLI对象。本机方法返回一个QByteArray,然后将其传递回C#,然后将其传递回目标应用程序。
总结:
Target App => Easyhook / C#=> C ++ / CLI =>原生方法=> C ++ / CLI => Easyhook / C#=>目标应用
当拦截方法的返回类型是本机对象(例如上面的QByteArray)时,该架构似乎完美地工作。
我的C#代码如下所示:
// //////////////////////////////////
// public: class QByteArray __thiscall QIODevice::read(__int64)
// //////////////////////////////////
[UnmanagedFunctionPointer(CallingConvention.ThisCall, CharSet = CharSet.Unicode, SetLastError = true)]
delegate IntPtr DDeviceRead(IntPtr Device, Int64 maxSize);
// Intercept all calls to Device::Read()
public IntPtr DeviceRead_Intercepted(IntPtr Device, Int64 maxSize)
{
// Call the method from C++/CLI
IODeviceWrapper qtDevice = new IODeviceWrapper(Device);
return qtDevice.Read(maxSize);
}
我的C ++ / CLI代码如下所示:
IntPtr IODeviceWrapper::Read(Int64 maxSize)
{
QByteArray * retArray = &Native->read(maxSize);
return static_cast<IntPtr>(retArray);
}
C ++不是我喜欢的语言 - 所以像指针和对象引用之类的东西有时会让人感到困惑。我尝试了很多变化,但似乎无法理解它。
本机Qt read()函数我假设返回一个新分配的QByteArray的地址,该变量接受,因此我的C ++ / CLI代码需要将该地址一直传递回目标应用程序。< / p>
这是怎么做到的?