gpt4 book ai didi

c# - C# P/Invoke 中的嵌套结构

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:25 24 4
gpt4 key购买 nike

我正在尝试调用具有以下结构的非托管 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 是错误的(看起来像一个内存地址),其他一切都是零。

感谢任何帮助。我做了很多将结构传递给 unmanages 代码的工作,但从来没有这么复杂。

最佳答案

ppr_object_list_type 包含指向 ppr_object_type指针,而不是实际的 ppr_object_type 值。

您需要将 ObjectInfo 更改为

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

要访问 ObjectType 值,您需要使用 Marshal 中的方法类。

关于c# - C# P/Invoke 中的嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2066117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com