gpt4 book ai didi

c# - 处理奇数场偏移

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:44:10 26 4
gpt4 key购买 nike

我正在尝试将以下 C++ 结构移植到 C#。

#pragma pack(push,1)
struct FatBootSectorStruct {
UCHAR BS_jmpBoot[3]; // 0
UCHAR BS_OEMName[8]; // 3
USHORT BPB_BytsPerSec; // 11
UCHAR BPB_SecPerClus; // 13
USHORT BPB_RsvdSecCnt; // 14
UCHAR BPB_NumFATs; // 16
USHORT BPB_RootEntCnt; // 17
USHORT BPB_TotSec16; // 19
UCHAR BPB_Media; // 21
USHORT BPB_FATSz16; // 22
USHORT BPB_SecPerTrk; // 24
USHORT BPB_NumHeads; // 26
ULONG BPB_HiddSec; // 28
ULONG BPB_TotSec32; // 32
union {
struct {
UCHAR BS_DrvNum; // 36
UCHAR BS_Reserved1; // 37
UCHAR BS_BootSig; // 38
ULONG BS_VolID; // 39
UCHAR BS_VolLab[11]; // 43
UCHAR BS_FilSysType[8]; // 54
UCHAR BS_Reserved2[448]; // 62
} Fat16;
struct {
ULONG BPB_FATSz32; // 36
USHORT BPB_ExtFlags; // 40
USHORT BPB_FSVer; // 42
ULONG BPB_RootClus; // 44
USHORT BPB_FSInfo; // 48
USHORT BPB_BkBootSec; // 50
UCHAR BPB_Reserved[12]; // 52
UCHAR BS_DrvNum; // 64
UCHAR BS_Reserved1; // 65
UCHAR BS_BootSig; // 66
ULONG BS_VolID; // 67
UCHAR BS_VolLab[11]; // 71
UCHAR BS_FilSysType[8]; // 82
UCHAR BPB_Reserved2[420]; // 90
} Fat32;
};
USHORT Signature; // 510
};

这是我的:

[StructLayout(LayoutKind.Explicit, Size = 512, Pack=1)]
internal struct FATBootSector
{
[FieldOffset(0)]
public byte[] BS_jmpBoot; // 0
[FieldOffset(3)]
public byte[] BS_OEMName; // 3
[FieldOffset(11)]
public ushort BPB_BytsPerSec; // 11
[FieldOffset(13)]
public byte BPB_SecPerClus; // 13
[FieldOffset(14)]
public ushort BPB_RsvdSecCnt; // 14
[FieldOffset(16)]
public byte BPB_NumFATs; // 16
[FieldOffset(17)]
public ushort BPB_RootEntCnt; // 17
[FieldOffset(19)]
public ushort BPB_TotSec16; // 19
[FieldOffset(21)]
public byte BPB_Media; // 21
[FieldOffset(22)]
public ushort BPB_FATSz16; // 22
[FieldOffset(24)]
public ushort BPB_SecPerTrk; // 24
[FieldOffset(26)]
public ushort BPB_NumHeads; // 26
[FieldOffset(28)]
public ulong BPB_HiddSec; // 28
[FieldOffset(32)]
public ulong BPB_TotSec32; // 32

// FAT16
[FieldOffset(36)]
public byte FAT16_BS_DrvNum; // 36
[FieldOffset(37)]
public byte FAT16_BS_Reserved1; // 37
[FieldOffset(38)]
public byte FAT16_BS_BootSig; // 38
[FieldOffset(39)]
public ulong FAT16_BS_VolID; // 39
[FieldOffset(43)]
public byte[] FAT16_BS_VolLab; // 43
[FieldOffset(54)]
public byte[] FAT16_BS_FilSysType; // 54
[FieldOffset(62)]
public byte[] FAT16_BS_Reserved2; // 62

// FAT32
[FieldOffset(36)]
public ulong FAT32_BPB_FATSz32; // 36
[FieldOffset(40)]
public ushort FAT32_BPB_ExtFlags; // 40
[FieldOffset(42)]
public ushort FAT32_BPB_FSVer; // 42
[FieldOffset(44)]
public ulong FAT32_BPB_RootClus; // 44
[FieldOffset(48)]
public ushort FAT32_BPB_FSInfo; // 48
[FieldOffset(50)]
public ushort FAT32_BPB_BkBootSec; // 50
[FieldOffset(52)]
public byte[] FAT32_BPB_Reserved; // 52
[FieldOffset(64)]
public byte FAT32_BS_DrvNum; // 64
[FieldOffset(65)]
public byte FAT32_BS_Reserved1; // 65
[FieldOffset(66)]
public byte FAT32_BS_BootSig; // 66
[FieldOffset(67)]
public byte FAT32_BS_VolID; // 67
[FieldOffset(71)]
public byte[] BS_VolLab; // 71
[FieldOffset(82)]
public byte[] FAT32_BS_FilSysType; // 82
[FieldOffset(90)]
public byte[] FAT32_BPB_Reserved2; // 90

[FieldOffset(510)]
public ushort Signature;

}

问题出在字段偏移量 0 和 3 上。当我尝试在 C# 中加载结构时,我得到 Could not load type 'FATBootSector' from assembly 'xxxx' because it contains an object field at offset 3 that that未正确对齐或被非对象字段重叠。。我遇到了this previous question on SO但这似乎没有帮助。

我也尝试过将其更改为如下所示的顺序结构:

[StructLayout(LayoutKind.Sequential, Size = 512, Pack=1, CharSet=CharSet.Ansi)]
internal struct FATBootSector
{

[MarshalAs(UnmanagedType.U1)]
public byte BS_jmpBoot0; // 0
[MarshalAs(UnmanagedType.U1)]
public byte BS_jmpBoot1; // 0
[MarshalAs(UnmanagedType.U1)]
public byte BS_jmpBoot2; // 0

[MarshalAs(UnmanagedType.LPStr, SizeConst=8)]
public string BS_OEMName; // 3
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_BytsPerSec; // 11
[MarshalAs(UnmanagedType.U1)]
public byte BPB_SecPerClus; // 13
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_RsvdSecCnt; // 14
[MarshalAs(UnmanagedType.U1)]
public byte BPB_NumFATs; // 16
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_RootEntCnt; // 17
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_TotSec16; // 19
[MarshalAs(UnmanagedType.U1)]
public byte BPB_Media; // 21
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_FATSz16; // 22
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_SecPerTrk; // 24
[MarshalAs(UnmanagedType.U2)]
public ushort BPB_NumHeads; // 26
[MarshalAs(UnmanagedType.U4)]
public ulong BPB_HiddSec; // 28
[MarshalAs(UnmanagedType.U4)]
public ulong BPB_TotSec32; // 32

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 474)]
public byte[] FAT1632Info;

[MarshalAs(UnmanagedType.U2)]
public ushort Signature;

}

我正在使用以下代码来获取结构:

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool ReadFile(IntPtr hFile, [Out] IntPtr lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, [In] ref System.Threading.NativeOverlapped lpOverlapped);

public bool GetPartitionDetails()
{
uint BytesRead;

IntPtr BootSectorPtr = Marshal.AllocHGlobal(512);
PInvoke.FATBootSector BootSector;

System.Threading.NativeOverlapped Overlapped = new System.Threading.NativeOverlapped();

bool ret = PInvoke.ReadFile(this.Handle, BootSectorPtr, (uint)512, out BytesRead, ref Overlapped);

BootSector = (PInvoke.FATBootSector)Marshal.PtrToStructure(BootSectorPtr, typeof(PInvoke.FATBootSector)); // causes access violation

return true;
}

我希望我不必这样做,但我能想到的唯一替代方法是使用 IntPtr 遍历内存。有什么想法吗?

最佳答案

这里有不少问题。首先,您在滥用 FieldOffset。当编译器无法为您布局结构时,您可以使用它。总是那是当你有 union 的时候。不要在这里使用 FieldOffset。当然不要预先指定尺寸。同样,让编译器来做。对照原生版本检查它。

然后你不正确地处理数组。您不需要 fixed,但您需要说明数组的长度。该结构的开头应如下所示:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct FATBootSector
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] BS_jmpBoot;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] BS_OEMName;
....
}

其余的问题都是由错误的类型转换引起的。 ULONG 在 C++ 中是一个 4 字节的无符号类型。这是 C# 中的 uint。您使用的是 8 字节宽的 ulong

这是至少可以避免访问冲突的结构版本:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct FATBootSector
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] BS_jmpBoot;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] BS_OEMName;
public ushort BPB_BytsPerSec;
public byte BPB_SecPerClus;
public ushort BPB_RsvdSecCnt;
public byte BPB_NumFATs;
public ushort BPB_RootEntCnt;
public ushort BPB_TotSec16;
public byte BPB_Media;
public ushort BPB_FATSz16;
public ushort BPB_SecPerTrk;
public ushort BPB_NumHeads;
public uint BPB_HiddSec;
public uint BPB_TotSec32;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 474)]
public byte[] FAT1632Info;
public ushort Signature;
}

要转换 FAT16/FAT32 union ,您可能需要使用 fixed。或者有两种 FATBootSector 类型。一个用于 FAT16,一个用于 FAT32。

关于c# - 处理奇数场偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com