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 / September 2006

Tip: Looking for answers? Try searching our database.

MarshalAs attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian M - 13 Sep 2006 03:40 GMT
I am wondering if every single field of a struct needs to be completely
attributed or if the runtime will marshall standard types "automagically"...

This would be the PInvok'd api call:
[DllImport("mywork.dll", CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi)]
public static extern int GetSomeData(ref MYSTRUCT myStruct);

For example, given this struct:

[StructLayout(LayoutKind.Sequential,Pack=8)]
public struct MYSTRUCT
{
   [MarshalAs(UnmanagedType.I4)]
   public int    iFieldOne;

   [MarshalAs(UnmanagedType.U4)]
   public uint dwFieldTwo;

   [MarshalAs(UnmanagedType.U2)]
   public short  wFieldThree;

   [MarshalAs(UnmanagedType.Bool)]
   public bool bFieldFour;

}

would this be functionally the same as:

[StructLayout(LayoutKind.Sequential,Pack=8)]
public struct MYSTRUCT
{
   public int       iFieldOne;
   public uint     dwFieldTwo;
   public short   wFieldThree;
   public bool    bFieldFour;
}

Thanks in advance,
Brian Manlove
prakash.kgm@gmail.com - 14 Sep 2006 06:59 GMT
There is no need to give the MarshalAs() attribute for blittable types.

http://msdn2.microsoft.com/en-us/library/75dwhxf7.aspx

Regards
Prakash.

> I am wondering if every single field of a struct needs to be completely
> attributed or if the runtime will marshall standard types "automagically"...
[quoted text clipped - 36 lines]
> Thanks in advance,
> Brian Manlove

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.