c++
struct ClientInfo
{
char adress[4];
};
extern "C" __declspec(dllexport) ClientInfo Test()
{
ClientInfo c;
strcpy_s(c.adress, "1234");
return c;
}
c#
[DllImport("ManagerAPI.dll")]
public static extern ClientInfo Test();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ClientInfo
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =4)]
public string address;
}
ClientInfo c =Test();
Console.WriteLine(c.address);
调用的时候老是报
方法类型签名与pinvoke不兼容
c++ 中的char[] 到底对应着c#中的什么类型。 求助
strcpy_s(c.adress, "1234");
return c;
这代码有两个问题,一个是你的c是定义在堆栈上的,怎么能用它返回呢?另一个,你的char只有4个元素,是不够复制1234的,还有\0
string需要转换一下,MarshalAs(UnmanagedType.LPStr)]string,参考这个写https://blog.csdn.net/qq385105501/article/details/82253725