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 / July 2004

Tip: Looking for answers? Try searching our database.

How to marshal a struct

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas - 23 Jul 2004 23:01 GMT
Hi,

I am having difficulty accesing the member data in a  struct.

This what I am doing.

In unmanaged code
typedef struct {
char name[20];
int id;
} person;

In C#

[StructLayout(LayoutKind.Sequential)]
public class person
{
[UnmanagedType.ByValTStr, SizeConst=20)]
public string name;
public int id;
}

     [DllImport("TestDLL.dll")]
     public static extern void SetStructData(
           [MarshalAs(UnmanagedType.LPStruct)]
           person pp );

When I pass the struct to unmanaged code, the data is all junk.

Can someone tell me what I am doing wrong.

Thanks.
Suresh - 23 Jul 2004 23:09 GMT
Specify the character set and try

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct person
{
         [UnmanagedType.ByValTStr, SizeConst=20)]
         public string name;
         public int id;
}

> Hi,
>
[quoted text clipped - 28 lines]
>
> Thanks.
BMermuys - 24 Jul 2004 02:00 GMT
Hi,

> Hi,
>
[quoted text clipped - 17 lines]
> public int id;
> }

Using struct:
---

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct person
{
   [UnmanagedType.ByValTStr, SizeConst=20)]
   public string name;
   public int id;
}

[DllImport("TestDLL.dll")]
public static extern void SetStructData( ref person pp );    // you can see
changes made to struct

Using class:
---

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public class person
{
   [UnmanagedType.ByValTStr, SizeConst=20)]
   public string name;
   public int id;
}

[DllImport("TestDLL.dll")]
public static extern void SetStructData( [In,Out] person pp );  // here
[In,Out] is required if you want to see changes!

HTH,
greetings

> When I pass the struct to unmanaged code, the data is all junk.
>
> Can someone tell me what I am doing wrong.
>
> Thanks.
Thomas - 26 Jul 2004 11:24 GMT
Thanks for your reply.

It did not work. I just cannot figure out why char[] is all junk data.

> Hi,
>
[quoted text clipped - 58 lines]
> >
> > Thanks.
BMermuys - 26 Jul 2004 12:25 GMT
> Thanks for your reply.

> > > [StructLayout(LayoutKind.Sequential)]
> > > public class person
[quoted text clipped - 3 lines]
> > > public int id;
> > > }

> It did not work. I just cannot figure out why char[] is all junk data.

The marshalling of such a simple struct itself is no problem, I use it all
the time.
Something else must be wrong, maybe you're translating the wrong struct or
the function isn't working properly.

HTH,
greetings

> > Hi,
> >
[quoted text clipped - 58 lines]
> > >
> > > Thanks.

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.