سؤال

أحاول الاتصال بـ 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 القيم ، ستحتاج إلى استخدام الأساليب في مارشال صف دراسي.

نصائح أخرى

يجب أن ينجح هذا إذا كنت تهتم فقط بالعنصر الأول:

public struct ObjectInfo
{
    public int numObjects;
    [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
    public ObjectType[] objListPointer;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top