我需要将下面的结构转换为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;
...
答案 0 :(得分:6)
正如评论已经说过的,这是一个位域,即一组位,它们一起形成一个字节,字或双字。
最简单的解决方案是:
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已经链接到了。
答案 1 :(得分:3)