Explanation of [StructLayout (LayoutKind.Sequential, Charset=charset.ansi)]

Source: Internet
Author: User

[StructLayout (LayoutKind.Sequential, Charset=charset.ansi)]

This is a definition of a C/C + + DLL that refers to the way in which a struct is defined, primarily for in-memory sorting, LayoutKind has two properties sequential and explicit

Sequential means sequential storage, in which the data in the structure is stored sequentially in memory explicit represents an exact layout, and the location of each member needs to be set with FieldOffset ().

In order to prepare with unmanaged pointers, knowing what that means, C # 's CLR provides a more flexible way of automating management, so it's optional for C #.

Charset=charset.ansi indicates the encoding method

http://blog.csdn.net/masterft/article/details/1699009

Http://www.cnblogs.com/lonelyDog/archive/2012/02/02/2335432.html

Http://www.cnblogs.com/namek/archive/2010/08/26/1808773.html

1.Sequential, sequential layout, such as
struct S1
{
  int A;
  INT B;
}
So by default in memory is the first row A, then 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
[StructLayout (layoutkind.sequential)]  
struct S1
{
  int A;
  INT B;
}
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
need to set the position of each member with FieldOffset ()
This enables the functionality of a common body like C
[StructLayout (layoutkind.explicit)] 
struct S1
{
  [FieldOffset (0)]
  int A;
  [FieldOffset (0)]
  int b;
}
Such A and B have the same address in memory  
    
   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.      
  default is auto, in win   NT/2000/XP the string is arranged in a Unicode string, in Win   95/98/ The Me is arranged in an ANSI string.       the
  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 =)]
public string Szsrcmach;
[MarshalAs (unmanagedtype.byvaltstr, SizeConst =)]
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);

Conclusion:
By default (layoutkind.sequential), the CLR handles the layout of a struct in the same way as it does by default in C + +, which is aligned according to the member that occupies the most space in the structure (Align );
With layoutkind.explicit , theCLR does not make any memory alignment (Align)to the struct, and we have to be careful that it is FieldOffset;
with Layoutkind.auto , the CLR adjusts the order of the fields in the struct so that the instance occupies as little memory as possible and makes 4byte of memory Alignment (Align).

The last one is a more detailed description of the structure alignment and memory layout of the platform call

Second: In special cases, C + + code uses #pragma pack (n) to change the boundary alignment. Here to use C # to align with [StructLayout (layoutkind.sequential, Pack = N)], otherwise an error occurs.

Http://www.cnblogs.com/namek/archive/2010/08/26/1808773.html

Explanation of [StructLayout (LayoutKind.Sequential, Charset=charset.ansi)]

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.