Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

How to marshall an array of nested structures?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Hoffer - 05 Apr 2004 22:42 GMT
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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.