将C语言结构体转换为Delphi

3

我需要将以下结构体转换为Delphi。我对“保留”和“版本”成员中的“:4”值的含义存在疑问。看起来它会影响结构体的大小!有没有人有任何提示?

typedef struct _FSRTL_COMMON_FCB_HEADER {
  CSHORT        NodeTypeCode;
  CSHORT        NodeByteSize;
  UCHAR         Flags;
  UCHAR         IsFastIoPossible;
  UCHAR         Flags2;
  UCHAR         Reserved  :4;
  UCHAR         Version  :4;
  PERESOURCE    Resource;
  ...

位数。 - LU RD
1
记录一下,同时也标注一下 Pascal:Free Pascal 支持位域。 - Marco van de Voort
2个回答

7

正如评论中所说,这是一个位域,即一组比特位,它们共同形成一个字节、字或双字。

最简单的解决方案是:

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;

我在这篇文章中提出了一种不那么简单但更为通用的解决方案,并对其进行了解释:http://rvelthuis.de/articles/articles-convert.html#bitfields,Uli已经链接过了。


3

这些是位域。虽然Delphi不直接支持,但有解决方法,请参见此处和特别是此处


啊,谢谢,我只是想发布同样的链接(第二个)。<g> - Rudy Velthuis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接