如何将C联盟转换为Delphi? [英] How do I translate a C union into Delphi?

查看:103
本文介绍了如何将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?

推荐答案

Pascal等价于C union 被称为变体记录

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


记录类型可以有一个变体部分,看起来像一个 case
语句。变量部分必须遵循记录
声明中的其他字段。

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;

声明的第一部分 - 直到保留字案例
与标准记录类型相同。
声明的其余部分从案例到可选的最终分号 - 称为
变体部分。在变体部分,

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,


  • 标签是可选的,可以是任何有效的标识符。如果您省略了标签,请忽略冒号(:)。

  • 表示序号类型。 >
  • 每个常量列表是一个常量,表示类型为 ordinalType 的值,或者这种常量的逗号分隔列表。

  • 每个变体是一个分号,而不是
    的值可以在多个常量列表中被多次表示。在
    记录类型的主要部分中,类似于 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类型)或接口,也不能被构造为包含长字符串,动态数组,变体或$ b的
类型$ b接口;但是它们可以是这些类型的指针。

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.

具有变体部分的记录在语法上非常复杂,但b $ b在语义上是欺骗性的。记录的变体部分包含
在内存中共享相同空间的几种变体。您可以随时阅读或
写入任何变体的任何字段;但是如果您在一个变体中写入
字段,然后写入另一个变体中的字段,则
可能会覆盖您自己的数据。标签(如果有的话)在
记录的非变体部分中作为
的额外字段(类型为 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位整数, UCHAR unsigned char AnsiChar 在Delphi中。


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;

可能字节可能更合适比 AnsiChar 。这当然有点难以说明,因为C不像Pascal,没有单独的类型为字节 AnsiChar 。但是这些数组看起来好像他们会被看作是文本,所以我的猜测是 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天全站免登陆