如何将 C 联合翻译成 Delphi? [英] How do I translate a C union into Delphi?

查看:27
本文介绍了如何将 C 联合翻译成 Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct _FILE_OBJECTID_INFORMATION {
    LONGLONG FileReference;
    UCHAR ObjectId[16];
    union {
        struct {
            UCHAR BirthVolumeId[16];
            UCHAR BirthObjectId[16];
            UCHAR DomainId[16];
        } DUMMYSTRUCTNAME;
        UCHAR ExtendedInfo[48];
    } DUMMYUNIONNAME;
} FILE_OBJECTID_INFORMATION, *PFILE_OBJECTID_INFORMATION;

如何将这样的联合翻译成 Delphi?

How to translate such a union to Delphi?

推荐答案

C union 的 Pascal 等价物被称为 变体记录.

The Pascal equivalent of a C union is known as a variant record.

记录类型可以有一个变体部分,看起来像一个案例陈述.变体部分必须跟在记录中的其他字段之后声明.

A record type can have a variant part, which looks like a case statement. The variant part must follow the other fields in the record declaration.

要声明带有变体部分的记录类型,请使用以下内容语法:

To declare a record type with a variant part, use the following syntax:

type recordTypeName = record
  fieldList1: type1;
   ...
  fieldListn: typen;
case tag: ordinalType of
  constantList1: (variant1);
   ...
  constantListn: (variantn);
end;

声明的第一部分 - 直到保留字 case - 是与标准记录类型相同.剩下的声明 - 从 case 到可选的最后一个分号 - 被调用变体部分.在变体部分,

The first part of the declaration - up to the reserved word case - is the same as that of a standard record type. The remainder of the declaration - from case to the optional final semicolon - is called the variant part. In the variant part,

  • tag 是可选的,可以是任何有效的标识符.如果省略 tag,也可以省略后面的冒号 (:).
  • ordinalType 表示序数类型.
  • 每个 constantList 都是一个常量,表示 ordinalType 类型的值,或者是一个逗号分隔的此类常量列表.没有价值可以在组合的 constantLists 中出现多次.
  • 每个 variant 都是一个以分号分隔的声明列表,类似于 fieldList: type 结构的主要部分记录类型.也就是说,一个变体具有以下形式:

  • tag is optional and can be any valid identifier. If you omit tag, omit the colon (:) after it as well.
  • ordinalType denotes an ordinal type.
  • Each constantList is a constant denoting a value of type ordinalType, or a comma-delimited list of such constants. No value can be represented more than once in the combined constantLists.
  • Each variant is a semicolon-delimited list of declarations resembling the fieldList: type constructions in the main part of the record type. That is, a variant has the form:

fieldList1:type1;...fieldListn: typen;

fieldList1: type1; ... fieldListn: typen;

其中每个 fieldList 是一个有效的标识符或逗号分隔的列表标识符,每种类型表示一种类型,最后的分号是选修的.类型不能是长字符串、动态数组、变体(即 Variant 类型)或接口,也不能被结构化包含长字符串、动态数组、变体或接口;但它们可以是指向这些类型的指针.

where each fieldList is a valid identifier or comma-delimited list of identifiers, each type denotes a type, and the final semicolon is optional. The types must not be long strings, dynamic arrays, variants (that is, Variant types), or interfaces, nor can they be structured types that contain long strings, dynamic arrays, variants, or interfaces; but they can be pointers to these types.

带有变体部分的记录在语法上很复杂,但是语义上看似简单.记录的变体部分包含共享内存中相同空间的几个变体.你可以阅读或随时写入任何变体的任何字段;但如果你写信给一个变体中的字段,然后到另一个变体中的一个字段,您可以覆盖自己的数据.标签,如果有的话,作为的非变体部分中的额外字段(ordinalType 类型)记录.

Records with variant parts are complicated syntactically but deceptively simple semantically. The variant part of a record contains several variants which share the same space in memory. You can read or write to any field of any variant at any time; but if you write to a field in one variant and then to a field in another variant, you may be overwriting your own data. The tag, if there is one, functions as an extra field (of type ordinalType) in the non-variant part of the record.

<小时>

至于其余的,就很常规了:LONGLONG 是一个 64 位整数,UCHARunsigned char,或 Delphi 中的 AnsiChar.


As for the rest, it's pretty routine: LONGLONG is a 64 bit integer, and UCHAR is unsigned char, or AnsiChar in Delphi.

type
  TFileObjectIDInformation = record
    FileReference: Int64;
    ObjectID: array[0..15] of AnsiChar;
    case Integer of
    0:
      (
        BirthVolumeId: array[0..15] of AnsiChar;
        BirthObjectId: array[0..15] of AnsiChar;
        DomainId: array[0..15] of AnsiChar;
      );
    1:
      (ExtendedInfo: array[0..47] of AnsiChar);
  end;

Byte 可能比 AnsiChar 更合适.这当然有点难说,因为 C 与 Pascal 不同,没有 ByteAnsiChar 的单独类型.但是在我看来,这些数组好像可以作为文本读取,所以我猜测 AnsiChar 会更合适.

It's possible that Byte may be more appropriate than AnsiChar. It's a bit hard to tell of course because C, unlike Pascal, doesn't have separate types for Byte and AnsiChar. But these arrays look to me as though they would be read as text so my guess is that AnsiChar would be more appropriate.

这篇关于如何将 C 联合翻译成 Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆