public static PinnedBuffer <byte> Serialize <T>(T instance) where T : struct { var pinnedBuffer = new PinnedBuffer <byte>(PinnedBuffer <T> .TypeSize); pinnedBuffer.Write(0uL, instance); return(pinnedBuffer); }
/// <summary> /// Gets IMAGE_NT_HEADERS structure from raw PE image /// </summary> /// <param name="rawFile">Raw exe or dll data</param> /// <returns>IMAGE_NT_HEADERS structure</returns> public static IMAGE_NT_HEADERS GetImageNtHeaders(byte[] rawFile) { using var FileData = PinnedBuffer.Create(rawFile); var dos_header = FileData.Read <IMAGE_DOS_HEADER>(0); var header = FileData.Read <IMAGE_NT_HEADERS>((ulong)dos_header.e_lfanew); if (header.Signature != 0x4550 || header.FileHeader.SizeOfOptionalHeader == 0) { throw new BadImageFormatException(); } return(header); }
public PinnedBuffer(PinnedBuffer existing, int offset) : base(ownsHandle: true) { GCHandle = GCHandle.Alloc(existing.GCHandle.Target, GCHandleType.Pinned); SetHandle(GCHandle.AddrOfPinnedObject() + existing.Offset + offset); }