C # StructLayout (layoutkind.sequential)]

Source: Internet
Author: User

A struct is made up of several members. There are two types of layouts
1.Sequential, sequential layout, e.g.
struct S1
{
int A;
int b;
}
So by default in memory is the first row A, then row b
That is, if you can take the address of a, and the address of B, then the length of an int type, 4 bytes

struct S1
{
int A;
int b;
}
This is the same as the previous one. Because the default memory arrangement is sequential, which is arranged in the order of the members.
2.Explicit, Precise layout
You need to set the location of each member with FieldOffset ()
This enables the functionality of a common body like C

struct S1
{
[FieldOffset (0)]
int A;
[FieldOffset (0)]
int b;
}
So that A and B have the same address in memory


The StructLayout feature supports three additional fields: CharSet, Pack, Size.
· CharSet defines how string members in a struct are arranged when the structure is passed to the DLL. Can be Unicode, ANSI, or Auto.
The default is auto, which in win NT/2000/XP indicates that strings are arranged in a Unicode string, and in win 95/98/me, they are arranged in ANSI strings.
· Pack defines the package size of the structure. Can be 1, 2, 4, 8, 16, 32, 64, 128, or special value 0. A special value of 0 indicates the default compression size for the current operating platform.

[StructLayout (layoutkind.sequential, CharSet = CharSet.Ansi)]
public struct List_open
{
public int Dwserverid;
public int dwlistid;
Public System.UInt16 wrecordsize;
Public System.UInt16 Wdummy;
public int dwfilesize;
public int dwtotalrecs;
Public Ns_prefetchlist Sprefetch;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 24)]
public string Szsrcmach;
[MarshalAs (UnmanagedType.ByValTStr, SizeConst = 24)]
public string Szsrccomp;
}

This example uses the Mashalas attribute , which is used to describe the marshaling format for a field, method, or parameter. Use it as the parameter prefix and specify the data type that the target requires.
For example, the following code marshals a string of two parameters to a Windows API function as a long pointer to a data type (LPSTR):
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
String Existingfile;
[MarshalAs (UNMANAGEDTYPE.LPSTR)]
String NewFile;
Note that when a struct is a parameter, it is generally preceded by a ref modifier, otherwise an error occurs: The object's reference does not specify an instance of the object.
[DllImport ("kernel32", entrypoint= "GetVersionEx")]
public static extern bool GetVersionEx2 (ref OSVersionInfo2 OSVI);

C # StructLayout (layoutkind.sequential)]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.