如何将结构中的C union转换为Delphi

时间:2010-09-14 08:36:00

标签: delphi

我需要帮助将此C类型声明转换为Delphi:

typedef struct _IO_STATUS_BLOCK {
  union {
    NTSTATUS Status;
    PVOID    Pointer_;
  } ;
  ULONG_PTR Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;

3 个答案:

答案 0 :(得分:9)

来自JCL中的Hardlinks.pas

type
  IO_STATUS_BLOCK = record
    case integer of
      0:
       (Status: NTSTATUS);
      1:
       (Pointer: Pointer;
        Information: ULONG); // 'Information' does not belong to the union!
  end;
  // PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;

答案 1 :(得分:5)

除了其他帖子,您可能还想阅读Pitfalls of Conversion。 Rudy的articles是有关Delphi / C / C ++互操作性的信息的金矿。

答案 2 :(得分:3)

C Union可以在记录中使用案例选择器进行翻译,这里的问题是Delphi在Case语句之后不允许任何内容。这可以通过嵌套的Case语句来解决,如下所示:

  _IO_STATUS_BLOCK = record
    case Integer of
      0: (
        case Integer of
        0: (
          Status: NTSTATUS);
        1: (
          Pointer_: Pointer);
      2: (Information: ULONG_PTR));
  end;
  IO_STATUS_BLOCK = _IO_STATUS_BLOCK;
  PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;

/编辑: 要使偏移正确(请参阅下面的注释),需要填充,修正后的版本如下。它在这里没有提供太多的优势,但如果更多的领域甚至工会跟在第一个联盟后面,那就更好了:

  _IO_STATUS_BLOCK = record
    case Integer of
      0: (
        case Integer of
          0: (Status: NTSTATUS);
          1: (Pointer_: Pointer));
      1: (
        Pad: DWORD;
        Information: ULONG_PTR);
  end;
  IO_STATUS_BLOCK = _IO_STATUS_BLOCK;
  PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;

/ EDIT2: 一个更好的版本看起来像这样:

  _IO_STATUS_BLOCK = record
    case Integer of
      0: (
        Status: NTSTATUS;
        Pointer_: Pointer);
      1: (
        case Padding: DWORD of
          0: (
            Information: ULONG_PTR));
  end;
  IO_STATUS_BLOCK = _IO_STATUS_BLOCK;
  PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;