Hello,
I need to interop from C# with a native method and pass an array of structs.
I have done it as follows:
[StructLayout(LayoutKind.Sequential,Pack=4)]
public struct ISD_STATION_STATE_TYPE
{... //struct fields};
[StructLayout(LayoutKind.Sequential,Pack=4)]
public struct ISD_TRACKER_DATA_TYPE{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst=ISD_MAX_STATIONS)]
public ISD_STATION_STATE_TYPE [] Station;
};
[DllImport("isense.dll")]
public static extern int ISD_GetData (int handle, ref ISD_TRACKER_DATA_TYPE
Data);
but when I call this function:
ISD_TRACKER_DATA_TYPE data= new ISD_TRACKER_DATA_TYPE();
ISD_GetData( handle, ref data );
I obtain a runtime System.TypeLoadException saying that
Can not marshal field Station of type ... : This type can not be marshaled
as a structure field.
Does anybody know where the problem is and how to solve it?
Many thanks in advance for your help
Javier
Mattias Sjögren - 26 Oct 2005 17:37 GMT
>[ MarshalAs( UnmanagedType.ByValArray, SizeConst=ISD_MAX_STATIONS)]
>public ISD_STATION_STATE_TYPE [] Station;
Here's your problem. Nested struct arrays isn't supported by the v1.x
marshaler.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Javier Jaen - 26 Oct 2005 17:56 GMT
yes Mattias this was the problem.
I solved it by making the structure flat (removing the array declaration)
thanks for your help.
> >[ MarshalAs( UnmanagedType.ByValArray, SizeConst=ISD_MAX_STATIONS)]
>>public ISD_STATION_STATE_TYPE [] Station;
[quoted text clipped - 3 lines]
>
> Mattias