Pregunta

Estoy tratando de poner en un archivo DLL no administrado que tiene la siguiente estructura:

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;

Esto es lo que estoy utilizando en el lado 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;
    }

La llamada que estoy haciendo se especifica como esto:

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

Mi C # llamada es el siguiente:

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

Yo sé que el ppr_object_list_type está a la espera de llenar una matriz de objetos. Y sé C # tiene problemas con matrices arbitray de objetos anidados. Estaba pensando que la forma en que estoy haciendo que volvería sólo el primero (que es todo lo que importa).

Sin embargo, cuando llamo de esta manera "núm_objetos" se rellena correctamente con un valor de 1. La model_id está mal (se parece a una dirección de memoria) y todo lo demás es ceros.

Cualquier ayuda es apreciada. He hecho un montón de estructuras de paso de trabajo para código unmanages, pero nunca nada remotamente este complejo.

¿Fue útil?

Solución

ppr_object_list_type contiene un puntero a un ppr_object_type, no un valor ppr_object_type real.

Es necesario cambiar ObjectInfo a

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

Para acceder a los valores ObjectType, tendrá que utilizar los métodos en el clase Marshal .

Otros consejos

Esto debería funcionar si sólo se preocupan por el primer elemento:

public struct ObjectInfo
{
    public int numObjects;
    [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
    public ObjectType[] objListPointer;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top