3

I need to convert the structure below to delphi. I am in doubt about what this ":4" value means in the "reserved" and "version" members. It looks like it interferes in the size of the structure! Anyone with any tips?

typedef struct _FSRTL_COMMON_FCB_HEADER {
  CSHORT        NodeTypeCode;
  CSHORT        NodeByteSize;
  UCHAR         Flags;
  UCHAR         IsFastIoPossible;
  UCHAR         Flags2;
  UCHAR         Reserved  :4;
  UCHAR         Version  :4;
  PERESOURCE    Resource;
  ...
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Carl
  • 111
  • 9

2 Answers2

7

As the comments already said, this is a bitfield, i.e. a set of bits that together form a byte, word or dword.

The simplest solution is:

type
  _FSRTL_COMMON_FCB_HEADER = record
  private
    function GetVersion: Byte;
    procedure SetVersion(Value: Byte);
  public
    NodeTypeCode: Word;
    ...
    ReservedVersion: Byte; // low 4 bits: reserved
                           // top 4 bits: version 
    // all other fields here

    property Version: Byte read GetVersion write SetVersion;
    // Reserved doesn't need an extra property. It is not used.
  end;

  ...

implementation

function _FSRTL_COMMON_FCB_HEADER.GetVersion: Byte;    
begin
  Result := (ReservedVersion and $F0) shr 4;
end;

procedure _FSRTL_COMMON_FCB_HEADER.SetVersion(Value: Byte);
begin
  ReservedVersion := (Value and $0F) shl 4;
end;

A less simple (but more general) solution and an explanation can be found in my article: http://rvelthuis.de/articles/articles-convert.html#bitfields, to which Uli already linked.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
3

These are bitfields. They aren't directly supported by Delphi but there are workarounds, see here and especially here.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83