Structlayout features, and mashalas

Source: Internet
Author: User

The Common Language Runtime library uses structlayOutattRibUteControls the data fields of a class or structure in the managed memoryLogical layout, that is, class or structureYou need to pressSort in some way. For exampleIf you want to pass the class to the unmanagedCode, The explicit control class layout isImportant. In its constructor
The layoutkind value initializes a new instance of the structlayoutattribute class. Layoutkind. Sequential is used to force members to be laid out in the order they appear.

The structlayout feature allows us to control the arrangement of elements in the structure statement block in the memory, and how the runtime arranges these elements when these elements are passed to the external DLL.

 

 

You can use system. runtime. interopservices. structlayout
Features precisely control the location of each structure member.
System. runtime. interopservices. structlayout
The allowed values include structlayout. Auto structlayout. sequential.
Structlayout. explicit.

1. Sequential: sequential layout, such
Struct
S1
{
Int;
Int B;
}
By default, A is first arranged in the memory and then B.
That is, if you can get the address of a, and the address of B, the difference is an int type length, 4 bytes
[Structlayout (layoutkind. Sequential)]

Struct S1
{
Int;
Int
B;
}
This is the same as the previous one, because the default memory arrangement is sequential, that is, sequential arrangement by members.
2. explicit, precise Layout
You need to use fieldoffset () to set the location of each member.
In this way, the function of a public body similar to C can be implemented.
[Structlayout (layoutkind. explicit)]

Struct S1
{
[Fieldoffset (0)]
Int;
[Fieldoffset (0)]
Int B;
}
In this way, A and B are in the same memory address.

 

 

 Structlayout supports three additional fields: charset, pack, and size.
·
Charset defines the arrangement of string members in the structure when the structure is passed to the DLL. It can be UNICODE, ANSI, or auto.
The default value is auto.
In NT/2000/XP, strings are arranged by Unicode strings, and in Win 95/98/Me, strings are arranged by ANSI strings.
·
Pack defines the encapsulation size of the structure. It can be 1, 2, 4, 8, 16, 32, 64, 128, or a special value of 0. The special value 0 indicates the default compression size of 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;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 24)]
Public String szsrcmach;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 24)]
Public String szsrccomp;
}

In this exampleMashalas features, UsedThe sending format of the description field, method, or Parameter.Use it as the parameter prefix and specify the data type required by the target.
For example, the following code uses two parameters as the long pointer of the Data Type to block the string (lpstr) sent to the Windows API function ):
[Financialas (unmanagedtype. lpstr)]
String existingfile;
[Financialas (unmanagedtype. lpstr)]
String newfile;

[Dllimport ("coredll. dll")]
[Return: financialas (unmanagedtype. bool)]
Public static extern bool readmsgqueue (intptr hmsgq, out power_broadcast broadcast, uint cbbuffersize, out uint lpnumberofbytesread, uint

 

Dwtimeout, out uint pdwflags); note that when the structure is used as a parameter, the ref modifier must be added before. Otherwise, an error occurs: the reference of the object does not include an instance of the specified object.
[Dllimport ("Kernel32", entrypoint = "getversionex")]
Public static extern bool getversionex2 (ref osversioninfo2 osvi );

Transferred from:

Http://www.cnblogs.com/JessieDong/archive/2009/07/21/1527553.html

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.