Frage

Ich versuche, eine nicht verwaltete DLL mit der folgenden Struktur zu rufen:

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;

Das benutze ich auf der C# Seite:

[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;
    }

Der Anruf, den ich mache, wird so angegeben:

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

Mein C# Call sieht so aus:

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

Ich weiß, dass das ppr_object_list_type erwartet, ein Array von Objekten auszufüllen. Und ich weiß, dass C# Probleme mit Arbitray -Arrays verschachtelter Objekte hat. Ich dachte, dass die Art und Weise, wie ich es tue, nur den ersten zurückgeben würde (was mir alles interessiert).

Wenn ich es jedoch so nenne, wird "num_objects" mit einem Wert von 1. korrekt ausgefüllt. Das Modell_ID ist falsch (sieht aus wie eine Speicheradresse) und alles andere ist Nullen.

Jede Hilfe wird geschätzt. Ich habe viele Arbeiten geleistet, um Strukturen zu verabschieden, um Code zu entfernen, aber nie etwas entfernt von diesem Komplex.

War es hilfreich?

Lösung

ppr_object_list_type enthält ein Zeiger zu einem ppr_object_type, keine tatsächliche ppr_object_type Wert.

Du musst dich ändern ObjectInfo zu

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

Zugreifen auf die ObjectType Werte müssen Sie die Methoden in der verwenden Marschall Klasse.

Andere Tipps

Dies sollte funktionieren, wenn Sie sich nur um den ersten Artikel kümmern:

public struct ObjectInfo
{
    public int numObjects;
    [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
    public ObjectType[] objListPointer;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top