其他信息: 对 PInvoke 函数“Server!Server.CSocket1::Send”的调用导致堆栈不对称。
[DllImport("socketdll2.dll", CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
public static extern int Send(int key, byte[] data);
函数调用约定是否一致。cdecl
然后就是传进去的参数类型是否匹配
如果我记的没错的话,C#和dll对接时候,byte[]是不能被自动转换成byte*的,需要分配非托管内存来处理。一般要采用类似以下代码:
[DllImport("socketdll2.dll", CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
public static extern int Send(int key, IntPtr data);
byte[] data = .... // 要传递的数据
// 分配非托管内存
IntPtr ptr = Marshal.AllocHGlobal(data.Length);
// 将数据复制到非托管内存
for(int i = 0; i < data.Length; i ++)
Marsha.WriteByte(ptr, i, data[i])
// 调用api
Send(key, ptr);
// 释放非托管内存
FreeHGlobal(ptr);