gpt4 book ai didi

delphi - 如何将 C 联合转换为 Delphi?

转载 作者:行者123 更新时间:2023-11-30 17:21:07 25 4
gpt4 key购买 nike

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?

最佳答案

C union 的 Pascal 等价物称为 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;

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 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;

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.

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

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 会更合适。

关于delphi - 如何将 C 联合转换为 Delphi?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com