我试图调用到非托管DLL,它具有以下结构:

typedef struct
    {
      int num_objects;
      ppr_object_type *objects;
    } ppr_object_list_type;
ppr_coordinate_type;

typedef struct
{
  int model_id;
  ppr_coordinate_type position;
  float scale_factor;
  float size;
  ppr_rotation_type rotation;
  int nominal_width;
  int nominal_height;
  float confidence;
  int num_landmarks;
  ppr_landmark_type *landmarks;
} ppr_object_type;

typedef struct
{
  float x;
  float y;
} 

typedef struct
{
  float yaw;
  float pitch;
  float roll;
  ppr_precision_type precision;
} ppr_rotation_type;

这是我使用C#的侧什么:

[StructLayout(LayoutKind.Sequential)]
    public struct ObjectInfo
    {
        public int numObjects;
        public ObjectType objListPointer;
    }

[StructLayout(LayoutKind.Sequential)]
    public struct ObjectType
    {
        int model_id;
        Coordinate position;
        float scale_factor;
        float size;
        Rotation rotation;
        int nominal_width;
        int nominal_height;
        float confidence;
        int num_landmarks;
        IntPtr landmarks;

    }
    [StructLayout(LayoutKind.Sequential)]
    public struct Coordinate
    {
        float x;
        float y;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct Rotation
    {
        float yaw;
        float pitch;
        float roll;
        int precision;
    }

我正在呼叫被这样指定的:

ppr_error_type ppr_detect_objects (ppr_context_type context,
                                   ppr_image_type image,
                                   ppr_object_list_type *object_list);

我的C#调用如下:

ObjectInfo info = new ObjectInfo();
int objOK = ppr_detect_objects(context, imagePtr, ref info);

我知道ppr_object_list_type期待填补对象的数组。我知道C#与嵌套对象的任意波形数组烦恼。我在想,我这样做的方式将返回只是第一个(这是我所关心的)。

然而,当我把它这样“num_objects”填入正确与值1。MODEL_ID是错误的(看起来像一个内存地址),其他一切都是零。

任何帮助理解。我已经做了很多工作,传递结构,以取消管理代码,但从来没有任何远程这个复杂的。

有帮助吗?

解决方案

ppr_object_list_type包含指针ppr_object_type,并非实际ppr_object_type值。

您需要更改ObjectInfo

[StructLayout(LayoutKind.Sequential)]
public struct ObjectInfo
{
    public int numObjects;
    public IntPtr objListPointer;
}

要访问ObjectType值,你需要使用的方法中的元帅类。

其他提示

这应该工作,如果你只关心第一个项目:

public struct ObjectInfo
{
    public int numObjects;
    [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
    public ObjectType[] objListPointer;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top