C#命令获取struct offset?

栏目: ASP.NET · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/2120367/c-sharp-command-to-get-struct-offset

假设我有一个像这样的C#结构:

[StructLayout(LayoutKind.Explicit)]
struct IMAGE_DOS_HEADER {
    [FieldOffset(60)] public int e_lfanew;
}

现在假设我从文件中读取数据,如下所示:

byte[] data = new byte[4096];
FileStream f = new FileInfo(filename).Open(FileMode.Open, FileAccess.Read);
int n = f.Read(data, 0, 4096);

现在我想测试n以确保我已经读取了足够的字节来获取e_lfanew的值.有没有什么方法可以获得值60(FieldOffset)而无需重新输入?我正在寻找这样的东西:

if (n >= offsetof(IMAGE_DOS_HEADER.e_lfanew) + sizeof(int)) {
    ...
}

有没有这样的命令?在我的实际代码中,我必须进行其中几项测试,并且通过在结构中添加先前字段或通过从FieldOffset属性中复制值来手动输入数字似乎很乏味且容易出错.有没有更好的办法?

使用Marshal.OffsetOf:
Marshal.OffsetOf(typeof(IMAGE_DOS_HEADER), "e_lfanew")

翻译自:https://stackoverflow.com/questions/2120367/c-sharp-command-to-get-struct-offset


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具