开发者

Invalid Struct field values when passing it from managed to native code

开发者 https://www.devze.com 2023-03-29 17:39 出处:网络
I have a following problem: C++ code: typedef struct { double x; double y; double z; } XYZ; double Sum(XYZ xyz)

I have a following problem:

C++ code:

typedef struct
{
   double x;
   double y;
   double z;
} XYZ;

double Sum(XYZ xyz)
{
  return xyz.x +xyz.y + xyz.z;
}

C# code:

[StructLayout(LayoutKind.Sequential)]
public class XYZ
{
    public double x;
    public double y;
    public double z;
}
[D开发者_运维百科llImport("MyUnmanaged.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Sum([MarshalAs(UnmanagedType.LPStruct)] XYZ xyz);

When I run following C# main:

XYZ1 xyz1 = new XYZ1 { x = 1f, y = 1f, z = 1f};
var x = MarchingCubesWrapper.Sum(xyz1);

I see that struct values did not passed to unmanaged environment well here is screenshot:

Invalid Struct field values when passing it from managed to native code

Could you tell me please what is the problem?


You need to declare XYZ as a struct rather than a class. It should then marshal correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消