I am trying to pass a structure into an unmanaged C function. This
structure contains an array of another structure. The internal structure
contains an array of integers. This nested structure is an in parameter
only. How can I do this? I have tried several ways of doing this and I
always get an interop exception. I doesn't seem to like the nested
structures/arrays. What is the right way to do this?
Here is an example of what I am trying to do...
[StructLayout(LayoutKind.Sequential, Pack=8, CharSet=CharSet.Ansi)]
internal struct INPUT {
public int iMode;
public int iNumBases;
[MarshalAs(UnmanagedType.LPArray, SizeConst=20)]
public nested_Base[] tBase;
public struct nested_BaseInfo {
public int iID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public int[] iNAIDs;
}
public int iNumCol;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public int[] iColIDs;
public float fSize;
};
[DllImport(m_strDLLName)]
private static extern int Foo( ref INPUT Input );
Mattias Sj?gren - 12 Apr 2004 14:28 GMT
David,
>How can I do this? I have tried several ways of doing this and I
>always get an interop exception. I doesn't seem to like the nested
>structures/arrays. What is the right way to do this?
Unfortunately this isn't supported. See
http://www.dotnetinterop.com/faq/?q=StructWithStructArray
for some possible workarounds.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
David Hoffer - 13 Apr 2004 00:33 GMT
Thanks for the info. I hope this can be added in future NET releases, it
seems like a common thing that would be easy to solve.
-dh
> David,
>
[quoted text clipped - 9 lines]
>
> Mattias
Mattias Sj?gren - 15 Apr 2004 13:36 GMT
David,
>Thanks for the info. I hope this can be added in future NET releases, it
>seems like a common thing that would be easy to solve.
Indeed, and in C# v2 you'll be able to do it like this if you don't
mind using unsafe code.
unsafe internal struct INPUT {
public int iMode;
public int iNumBases;
public fixed nested_Base tBase[20];
public struct nested_BaseInfo {
public int iID;
public fixed int iNAIDs[10];
}
public int iNumCol;
public fixed int iColIDs[10];
public float fSize;
};
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.