您的位置:首页 > 编程语言 > C#

C#返回byte[]给dll的方法,寻求帮助

2017-01-19 07:12 423 查看
谁能帮下我。我是新手中的新手,第一次用C++,目前只想解决这个问题

我现在的需求是,DLL里调用了C#的委托函数,委托函数返回字节数组给DLL,DLL再接下去处理

这个C#的委托函数的代码
static IntPtr sendDataFun(IntPtr buf, int len)

 {

       byte[] buffer = new byte[len];

       Marshal.Copy(buf, buffer, 0, len);

      //此处是各种处理。。省略

       

      //获取buffer字节数组的内存地址

      IntPtr pin = GCHandle.ToIntPtr(GCHandle.Alloc(buffer));

      return pin;//把内存地址返回给DLL

}


这里是C++ 写的DLL代码
//这行前面的int 类型应该改成什么才能接收C#返回IntPtr类型

typedef int (WINAPI *mhook_func)(char* buf, int len);//这个结构就是下面_msend函数的

int WINAPI send(const char *buf, int len)

{

   char *temp = new char[len];

    memcpy_s(temp, len, buf, len);

 

    //_msend就是C#的委托函数(sendDataFun),先将temp和len发送给C#的处理,再返回字节数组,再转成 char *

    char * aa = _msend(temp, len);//这里要怎么做才能把C#返回的IntPtr指针内容读取并转成char * 

    //重点在这一部分。怎么实现我要的功能

     

    //第一个参数aa就是上面上行从C#返回的字节数组的char *

    int ret = g_trueSend(aa, len);

    delete temp;

}


谢谢大家,我对C++不了解,希望能好人做到底,在我的基础上改代码,给提示我,我也不懂的,免得若大家烦。

谢谢谢谢谢谢!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: