[英]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_header
和ext4_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;
謝謝!
以與C ++代碼相同的方式投射hdr
。 它強制轉換為指向八位位組的指針,以便指針算術將偏移量視為八位位組值。 在德爾福:
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)之后結果將指向何處? hdr
之后是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.