17.7 Mapping a Struct to a Binary Format

You can use the LayoutKind.Sequential argument with the StructLayout attribute to tell .NET to ensure that struct fields are laid out in the order in which they are defined. You can use this to create a struct that maps to a binary format, such as a GIF image. If you use this in conjunction with Win32 APIs for mapping a file into memory, you can define such a struct, load a file into memory, and superimpose the struct over that region of memory.

The next example features a struct, Gif89a, whose fields map directly to the layout of a GIF image. Because you can't define a fixed-length array in a C# struct, use a sequence of three bytes to hold the GIF signature ("GIF") and the version ("89a"). The Signature and Version properties each have an accessor method that turns these bytes into a string. The Dump( ) method displays the version and the dimensions of the GIF.

The Main( ) method uses three functions defined in kernel32.dll to map a GIF file into memory. Then, it casts that memory region (starting at baseAddress) to a Gif89a. This maps the struct onto the binary file format, eliminating the need to write code that parses each individual field of the GIF header.

// GIFInspects.cs - compile with /unsafe
using System;
using System.IO;
using System.Runtime.InteropServices;
  
[StructLayout(LayoutKind.Sequential)]
public struct Gif89a {
  byte sig0, sig1, sig2; // Signature
  byte ver0, ver1, ver2; // Version
  public ushort ScreenWidth;
  public ushort ScreenHeight;
  // other members of GIF89a header removed for brevity
  public string Signature { // concatenate the bytes to get a string
    get {
      char[ ] c = {(char) sig0, (char) sig1, (char) sig2};
      return new String(c);
    }
  }
  public string Version {
    get {
      char[ ] c = {(char) ver0, (char) ver1, (char) ver2};
      return new String(c);
    }
  }
  public void Dump( ) {
    Console.WriteLine("Image size: {0}x{1}", ScreenWidth, ScreenHeight);
    Console.WriteLine("GIF Type: {0}{1}", Signature, Version);
  }
}
  
public class GIFInspect {
  const uint PAGE_READONLY = 0x02; // from winnt.h
  const uint FILE_MAP_READ = 0x04; // from WinBASE.h
  [DllImport("kernel32.dll")]
  private static extern int CreateFileMapping(IntPtr hFile,
                                              int lpAttributes,
                                              uint flProtect,
                                              uint dwMaximumSizeHigh,
                                              uint dwMaximumSizeLow,
                                              string lpName);
  [DllImport("kernel32.dll")]
  private static extern uint MapViewOfFile(int hFileMappingObject,
                                           uint dwDesiredAccess,
                                           uint dwFileOffsetHigh,
                                           uint dwFileOffsetLow,
                                           uint dwNumberOfBytesToMap);
  [DllImport("kernel32.dll")]
  private static extern int CloseHandle(int hObject);
  
  public static void Main(string[ ] args) {
    // Open the file
    FileStream fs = new FileStream(args[0],
                                  FileMode.Open,
                                  FileAccess.Read,
                                  FileShare.Read);
    // Map the file into memory
    int hSection = CreateFileMapping(fs.Handle, 0, PAGE_READONLY,
                                     0, 0, null);
    uint baseAddress = MapViewOfFile(hSection, FILE_MAP_READ,
                                     0, 0, 0);
    // Cast the memory at baseAddress to a Gif89a
    unsafe {
      Gif89a* ptr = (Gif89a*) baseAddress;
      ptr->Dump( ); // display information about the GIF
    }
    fs.Close( );
    CloseHandle(hSection);
  }
}


    Part II: Programming with the .NET Framework
    Part IV: API Quick Reference
     
    ASPTreeView.com
     
    Evaluation has ЪЪХЩ№ЧЧexpired.
    Info...