C++的回调函数中有一个参数是,是返回一个字符串,原则如下: 
typedef   void   (*TDataEvent)(char   *AData   ,int   ALen); 
其中char   *AData是从DLL中返回一个字符串,串的内存已经在DLL中分配了 下面中我在C#中定义的委托 
public   delegate   void   TDataEvent(Byte[]   AData,   int   ALen); 下面是回调函数的设置代码: 
Event   =   new   clReceivelDllPoxy.TDataEvent(getDate); 
ReceDllPoxy.AddServer(1024,   Event,   2); 
其中   Event是上面委托的实例,我定义成一个成员这样就不会被自己释放 下面是C#中回调函数的实现 
public   void   getDate(byte[]   AData,   int   ALen) 

//为什么这里AData的维数只有1,也就是说只收到一个字节????

解决方案 »

  1.   

    In c++, char  *AData does not have any length information in itself.
    How can you count on C# to give you the AData.Length information?So, You need to do it yourself:public  void  GetDate(IntPtr  pData,  int  length) 

      string data = Marshal.PtrToStringAnsi(pData, length );
    }
      

  2.   

    gomoku 说得有道理,那么怎么将C++中的char*转换成.Net的byte[]而不是string呢?因为我这的char*是字节数组,不是字符串。
      

  3.   

    byte[] bytes = new byte[length];
    Marshal.Copy(pData, bytes, 0, bytes.Length);   // remember it is a copy