4

Background:

Translating the IP_OPTION_INFORMATION32 and ICMP_ECHO_REPLY32 structures for 64-bit compiler I got stucked with data types to use there. Structure definitions from the reference:

The IP_OPTION_INFORMATION32 structure:

typedef struct _IP_OPTION_INFORMATION32 {
  UCHAR              Ttl;
  UCHAR              Tos;
  UCHAR              Flags;
  UCHAR              OptionsSize;
  UCHAR * POINTER_32 OptionsData;
} IP_OPTION_INFORMATION32, *PIP_OPTION_INFORMATION32;

I would translate this way (for Delphi XE2, 64-bit target platform). As you can see, I don't know what type to use for the OptionsData field of the structure:

IP_OPTION_INFORMATION32 = record
  Ttl: UCHAR;
  Tos: UCHAR;
  Flags: UCHAR;
  OptionsSize: UCHAR;
  OptionsData:       // what should I use here for UCHAR * POINTER_32 ?
end;

ICMP_ECHO_REPLY32 structure:

typedef struct icmp_echo_reply32 {
  IPAddr                         Address;
  ULONG                          Status;
  ULONG                          RoundTripTime;
  USHORT                         DataSize;
  USHORT                         Reserved;
  VOID * POINTER_32              Data;
  struct ip_option_information32  Options;
} ICMP_ECHO_REPLY32, *PICMP_ECHO_REPLY32;

For Delphi XE2 64-bit target platform I would write:

ICMP_ECHO_REPLY32 = record
  Address: TIPAddr;  // defined before
  Status: ULONG;
  RoundTripTime: ULONG;
  DataSize: USHORT;
  Reserved: USHORT;
  Data:              // what should I use here for VOID * POINTER_32 ?
  Options: IP_OPTION_INFORMATION32;
end;

Question:

How would you define the UCHAR * POINTER_32 and VOID * POINTER_32 types in Delphi for 64-bit platform target ? As far as I know, there is no 32-bit pointer type available for 64-bit platform target and I just don't like it to be defined e.g. as a Int32 type :-)

What is the most precise translation for the mentioned types ?

4

2 回答 2

4

另一个堆栈溢出问题POINTER_32中涵盖的问题:POINTER_32 - 它是什么,为什么?

当与在不同进程中定义的结构(具有 32 位指针)执行互操作时,您将使用它。

你没有__ptr32在 Delphi 中的等价物,所以除了将它声明为 32 位整数之外你别无选择。我会使用无符号类型。

于 2013-06-13T09:56:51.387 回答
0

在这种情况下UInt32(或 Cardinal、DWORD 等 - 任何无符号 32 位)都可以。您需要做的就是在结构(记录)的适当位置声明无符号 32 位。CPU 不会关心,编译器不会关心(如果需要,您可以将类型转换为指针)它实际上是否是指针。除非您对值和处理器的标志检查进行一些数学运算,否则即使有符号的 32 位也可以工作。从 C 到 Delphi 的结构转换中的另一件事:确保原始对齐是 4 个字节(每个项目 32 位),因为默认情况下使用相同的 4 个字节对齐(recordpacked record对齐)。否则,您可能会通过将记录应用于数据来遇到问题,例如

type
  Picmp_echo_reply32 = ^icmp_echo_reply32;
...
  data_size := Picmp_echo_reply32(@mybuf[some_offset]).DataSize;

或者

var
  icmp_echo: Picmp_echo_reply32;
...
  icmp_echo := @buf[offset];
  reserved := icmp_echo.Reserved;
于 2013-06-13T15:52:07.687 回答