c++ - 从 C++ 到 Delphi 的类型转换

标签 c++ delphi

我如何将其转换为 Delphi?

typedef struct ext2dirent {
    EXT2_DIR_ENTRY *next;
    EXT2_DIR_ENTRY *dirbuf;
    Ext2File *parent;
    lloff_t read_bytes;     // Bytes already read
    lloff_t next_block;
} EXT2DIRENT;

typedef struct tagEXT2_DIR_ENTRY {
    uint32_t    inode;          /* Inode number */
    uint16_t    rec_len;        /* Directory entry length */
    uint8_t     name_len;       /* Name length */
    uint8_t filetype;       /* File type */
    char    name[EXT2_NAME_LEN];    /* File name */
} __attribute__ ((__packed__)) EXT2_DIR_ENTRY;


EXT2DIRENT *dirent;

int blocksize = 4096;

dirent->dirbuf = (EXT2_DIR_ENTRY *) new char[blocksize]; //<-- This line

我想到了做这样的事情;

Type
  PExt2_Dir_Entry = ^Ext2_Dir_Entry;
  Ext2_Dir_Entry = packed Record
    inode: Cardinal;
    rec_len : Word;
    name_len : Byte;
    filetype : Byte;
    name : Array[0..EXT2_NAME_LEN-1] of AnsiChar;
  End;

    var
      temp : array of AnsiChar;

      if dir = NIL then
        Result := nil;

        SetLength(temp,self.block_size-1);
        dir.dirbuf := PExt2_Dir_Entry(@temp);

但是,我在 dir.dirbuf 中没有得到预期的结果。我不明白 new char 特性在 C++ 中的作用。但我认为这可能与我的失败有关。

最佳答案

带有 new 的 C++ 代码行分配 blocksize 字节的内存并将指针转换为所需的类型。
您的 @temp 是指向指针的指针。这不是我们想要的。
在 Delphi 中你可以这样做:

GetMem(dir.dirbuf, blocksize);

从上面的代码中不清楚,为什么分配大小是 4096 而不是 SizeOf(Ext2_Dir_Entry)

关于c++ - 从 C++ 到 Delphi 的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378305/

相关文章:

c++ - 使用 Win32 将 GDI 绘图大小缩放到窗口大小

Delphi:启动应用程序的快捷方式在哪里?

c++ - std::remove_if 无法正常工作

c++ - 从复杂(嵌套)mpl 序列转换而来的 boost::fusion::result_of::as_set(或 as_vector)

delphi - 如何在 TDBGrid 中显示富文本?

delphi - TStringGrid 的 OnColumnChanged 和 OnRowChanged 事件

delphi - 在Delphi中获取发送方组件的数组索引

delphi - int64 上的 SHR 未返回预期结果

c++ - 从输入中添加连续整数(从 Python 翻译成 C++)

c++ - Qt 所见即所得 - 字体大小问题