我有以下问题:
C++代码:
typedef struct
{
double x;
double y;
double z;
} XYZ;
double Sum(XYZ xyz)
{
return xyz.x +xyz.y + xyz.z;
}
C#代码:
[StructLayout(LayoutKind.Sequential)]
public class XYZ
{
public double x;
public double y;
public double z;
}
[DllImport("MyUnmanaged.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Sum([MarshalAs(UnmanagedType.LPStruct)] XYZ xyz);
当我按照 C# main 运行时:
XYZ1 xyz1 = new XYZ1 { x = 1f, y = 1f, z = 1f};
var x = MarchingCubesWrapper.Sum(xyz1);
我看到结构值没有很好地传递到非托管环境这里是屏幕截图:
你能告诉我问题是什么吗?
最佳答案
您需要将 XYZ
声明为 struct
而不是 class
。然后它应该正确编码。
关于c# - 从托管代码传递到 native 代码时结构字段值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115316/