Sto riscontrando qualche problema nel marshalling di un puntatore a un array di stringhe. Sembra innocuo come questo:Puntatore Marshalling a una matrice di stringhe
typedef struct
{
char* listOfStrings[100];
} UnmanagedStruct;
Questo in realtà è inserito all'interno di un altro struttura come questa:
typedef struct
{
UnmanagedStruct umgdStruct;
} Outerstruct;
codice non gestito richiama in codice gestito e restituisce Outerstruct come IntPtr con la memoria allocata e valori compilato .
mondo Gestito:
[StructLayout(LayoutKind.Sequential)]
public struct UnmanagedStruct
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr, SizeConst=100)]
public string[] listOfStrings;
}
[StructLayout(LayoutKind.Sequential)]
public struct Outerstruct
{
public UnmanagedStruct ums;
}
public void CallbackFromUnmanagedLayer(IntPtr outerStruct)
{
Outerstruct os = Marshal.PtrToStructure(outerStruct, typeof(Outerstruct));
// The above line FAILS! it throws an exception complaining it cannot marshal listOfStrings field in the inner struct and that its managed representation is incorrect!
}
Se cambio listOfStrings in modo che sia semplicemente un IntPtr, Marshal.PtrToStructure funziona ma ora non riesco a estrarre listOfStrings ed estrarre le stringhe una alla volta.
ByValArray == array sul posto, LPArray == un puntatore a un array. Sebbene SizeConst debba ancora funzionare con un LPArray, quindi l'errore nel marshalling era un po 'strano. –
Oh, dovrebbe funzionare anche se hai una stringa pubblica [] listOfStrings, è il ByValArray che fa la differenza credo. –