C# StructLayout(LayoutKind.Sequential)]
2016-06-12 12:30
441 查看
结构体是由若干成员组成的.布局有两种 1.Sequential,顺序布局,比如 struct S1 { int a; int b; } 那么默认情况下在内存里是先排a,再排b 也就是如果能取到a的地址,和b的地址,则相差一个int类型的长度,4字节 [StructLayout(LayoutKind.Sequential)] struct S1 { int a; int b; } 这样和上一个是一样的.因为默认的内存排列就是Sequential,也就是按成员的先后顺序排列. 2.Explicit,精确布局 需要用FieldOffset()设置每个成员的位置 这样就可以实现类似c的公用体的功能 [StructLayout(LayoutKind.Explicit)] struct S1 { [FieldOffset(0)] int a; [FieldOffset(0)] int b; } 这样a和b在内存中地址相同
StructLayout特性支持三种附加字段:CharSet、Pack、Size。
· CharSet定义在结构中的字符串成员在结构被传给DLL时的排列方式。可以是Unicode、Ansi或Auto。
默认为Auto,在WIN NT/2000/XP中表示字符串按照Unicode字符串进行排列,在WIN 95/98/Me中则表示按照ANSI字符串进行排列。
· Pack定义了结构的封装大小。可以是1、2、4、8、16、32、64、128或特殊值0。特殊值0表示当前操作平台默认的压缩大小。
[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;
}
此例中用到MashalAs特性,它用于描述字段、方法或参数的封送处理格式。用它作为参数前缀并指定目标需要的数据类型。
例如,以下代码将两个参数作为数据类型长指针封送给 Windows API 函数的字符串 (LPStr):
[MarshalAs(UnmanagedType.LPStr)]
String existingfile;
[MarshalAs(UnmanagedType.LPStr)]
String newfile;
注意结构作为参数时候,一般前面要加上ref修饰符,否则会出现错误:对象的引用没有指定对象的实例。
[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]
public static extern bool GetVersionEx2( ref OSVersionInfo2 osvi );
相关文章推荐
- UITextField的基本使用
- Crank Storyboard Suite汽车仪表盘设计
- SecrueCRT 方向键不能使用
- easyui formatter实现超链接效果
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- Android UI-开源框架ImageLoader的完美例子
- UIViewContentMode各类型效果
- 浅谈如何实现easyui的datebox格式化
- POJ-1458 Common Subsequence
- UI各种效果库
- Android基础UI组件
- UILabel的行间距
- UIView 中的控件事件穿透 Passthrough 的实现
- javax.persistence.PersistenceException: [PersistenceUnit: cmf.server] Unable to build EntityManagerF
- android UI——分组+悬停 listview
- 卡特兰数变形之Unique Binary Search Trees II
- “could not build module UIKit”问题的解决
- 设置soapui 最大设置访问时间
- minigui杂项
- IOS Dev Intro - Key-Value Observing