C++ to Delphi - 结构和指针加法

C++ to Delphi - Struct and pointer addition

我用 C++ 定义了这个宏;

    #define EXT_FIRST_EXTENT(__hdr__) \
((struct ext4_extent *) (((char *) (__hdr__)) +         \
                         sizeof(struct ext4_extent_header)))

其中 ext4_extent_header 是一个结构;

    typedef struct ext4_extent_header {
    uint16_t  eh_magic;       /* probably will support different formats */
    uint16_t  eh_entries;     /* number of valid entries */
    uint16_t  eh_max;         /* capacity of store in entries */
    uint16_t  eh_depth;       /* has tree real underlying blocks? */
    uint32_t  eh_generation;  /* generation of the tree */
}__attribute__ ((__packed__)) EXT4_EXTENT_HEADER;

而ext4_extent也是一个结构;

typedef struct ext4_extent {
    uint32_t ee_block; /* first logical block extent covers */
    uint16_t ee_len; /* number of blocks covered by extent */
    uint16_t ee_start_hi; /* high 16 bits of physical block */
    uint32_t ee_start_lo; /* low 32 bits of physical block */
} __attribute__ ((__packed__)) EXT4_EXTENT;

这是我在Delphi;

中尝试写的
function Ext2Partition.EXT_First_Extent(hdr: PExt4_extent_header):PExt4_ExtEnt;
begin
  Result := PExt4_ExtEnt(hdr + sizeof(ext4_extent_header));
end;

但是编译器告诉我运算符不适用于此操作数类型。

这是我为 ext4_extent_headerext4_extent 转换为 Delphi 记录的 c++ 结构;

Type
  PExt4_extent_header = ^Ext4_extent_header;
  Ext4_extent_header = REcord
    eh_magic : Word;
    eh_entries : Word;
    eh_max : Word;
    eh_depth : Word;
    eh_generation : Cardinal;
  End;

Type
  PExt4_ExtEnt = ^Ext4_ExtEnt;
  Ext4_ExtEnt = Record
    ee_block : Cardinal;
    ee_len : Word;
    ee_start_hi : Word;
    ee_start_low : Cardinal;
  End;

谢谢!

Cast hdr 与 C++ 代码的方式相同。它将指针强制转换为八位字节,以便指针算术将偏移量视为八位字节值。在 Delphi:

Result := PExt4_ExtEnt(PAnsiChar(hdr) + sizeof(ext4_extent_header));

您可以启用指针运算并使其更简单:

{$POINTERMATH ON}
....
Result := hdr + 1;

您的代码中可能还有其他问题。如果 hdr 真的是 PExt4_ExtEnt 那么 C++ 就不需要宏了。它可以只写 hdr + 1。所以我怀疑您需要更深入地研究 C++ 代码才能找出 hdr 到底是什么。


另请注意,C++ 代码指定这些记录是打包的。在 Delphi 代码中使用 packed record 进行匹配。

1) 将函数更改为

function Ext2Partition.EXT_First_Extent(hdr: PExt4_extent_header):PExt4_ExtEnt;
begin
  Inc(hdr, 1); // It will increase _local copy_ of the hdr parameter to size of Ext4_extent_header, not to 1 byte. Keep in mind such behavior 
  Result := hdr;
end;

2) Result之后会指向哪里? hdr 之后是什么?