Carl
Carl

Reputation: 131

Convert C structure to delphi

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;
  ...

Upvotes: 3

Views: 178

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28846

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.

Upvotes: 7

Uli Gerhardt
Uli Gerhardt

Reputation: 14001

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

Upvotes: 3

Related Questions