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 / August 2003

Tip: Looking for answers? Try searching our database.

Marshalling Nested Structures

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
news.microsoft.com - 25 Aug 2003 22:04 GMT
Hi there,

   I have a wiered scenario. I have two structures defined this way in
unmanaged dll.

struct A{
   char * name;
   int ID;
};

struct B{
   struct A* record;
};

Then I have a function in the dll that consumes a pointer to struct B. like
this.

  int InitRecord(struct B* data);

I declared the method as below in C#.

[StructLayout(LayoutKind.Sequential)]
 public struct ManagedStructA{

   public System.String name;
   public System.Int16 ID;
};

[StructLayout(LayoutKind.Sequential)]
 public struct ManagedStructB{

public System.IntPtr ManagedStructA;

};

[DllImport("MyCDll.Dll")]
public static extern System.Int16 InitRecord(ref ManagedStructB
managedData);

My question is how can i convert the ManagedStructA object to an IntPtr. Or
is there anyother way I can pass ManagedStructA to ManagedStructB so it will
be passed as a pointer internally. I tried using this declaration but it
throws exception saying "Marshaling types to LPStruct is not supported on
structure field"

[StructLayout(LayoutKind.Sequential)]
 public struct ManagedStructB{
[MarshalAs(UnmanagedType.LPStruct)]
public System.IntPtr ManagedStructA;

};

Thanks for help.
David Stucki [MS] - 25 Aug 2003 23:07 GMT
Use Marshal.PtrToStructure and Marshal.StructureToPtr.  If you're
allocating the memory that the IntPtr points to you'll also need to use
Marshal.Alloc* and the corresponding Marshal.Free*.

David Stucki
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

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.