C # llama a la función C que devuelve la estructura con una matriz de caracteres de tamaño fijo
Por lo tanto, ha habido muchas variantes de esta pregunta, y después de mirar varias, todavía no puedo entenderlo.
Este es el código C:
<code>typedef struct { unsigned long Identifier; char Name[128]; } Frame; Frame GetFrame(int index); </code>
Este es el código C #:
<code>struct Frame { public ulong Identifier; [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 128)] public char[] Name; } [DllImport("XNETDB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private static extern Frame GetFrame(int index); </code>
Este es el último intento que probé en C #, y parece bastante lógico, pero aparece el error "La firma del método no es compatible con PInvoke". Entonces, estoy un poco perdido sobre qué intentar a continuación. Cualquier ayuda es apreciada.
Gracias kevin
Actualizado Kevin agregó esto como una edición a mi respuesta.
En su lugar debería cambiar mi código C:
<code>void GetFrame(int index, Frame * f); </code>
y usar en su lugar para C #:
<code>struct Frame { public uint Identifier; [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)] public string Name; } [DllImport("XNETDB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private static extern void GetFrame(int index, ref Frame f); </code>