byte[] newm = new byte[retVal];
int t = 1234;
GCHandle h = GCHandle.Alloc(t, GCHandleType.Pinned);
IntPtr p = h.AddrOfPinnedObject();
Marshal.Copy(p, newm, 0, retVal);
------解决方案-------------------- funcTest2(ref IntPtr data, ref int retVal)
参数对应C++原型是unsigned char *&ucData, int *dataLen
感觉你写的有问题 长度是个指针 你不应该直接用指针的值来初始化数组大小byte[] newm = new byte[retVal];
你应该先得到大小dataLen
private int funcTest2(ref IntPtr data, IntPtr retVal)
{
int[] dataLen = new int[1];
Marshal.Copy(retVal, dataLen , 0, 1);//先计算dataLen
byte[] newm = new byte[dataLen[0]];
Marshal.Copy(data, newm, 0, dataLen[0]);
}