gpt4 book ai didi

delphi - 如何在记录结构中声明并集?

转载 作者:行者123 更新时间:2023-12-03 14:51:32 26 4
gpt4 key购买 nike

我正在尝试定义 TWaveFormatExtensible 类型,但我不确定是否正确声明了 Samples 联合。以下是头文件 (Windows SDK 10.0.17763.0) 的原始声明:

typedef struct {
WAVEFORMATEX Format;
union {
WORD wValidBitsPerSample; /* bits of precision */
WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
WORD wReserved; /* If neither applies, set to zero. */
} Samples;
DWORD dwChannelMask; /* which channels are */
/* present in stream */
GUID SubFormat;
}

这就是我尝试过的:

type
TWAVEFORMATEX = record
wFormatTag: Word;
nChannels: LongWord;
nSamplesPerSec: Word;
nAvgBytesPerSec: LongWord;
nBlockAlign: Word;
wBitsPerSample: Word;
cbSize: Word;
end;

TWaveFormatExtensible = record
Format: TWAVEFORMATEX;
dwChannelMask: LongWord;
SubFormat: Integer;
case Word of
0: (wValidBitsPerSample: Word;);
1: (wSamplesPerBlock: Word;);
2: (wReserved: Word;);
end;

但这并不正确。如何在 Delphi 中的记录结构内声明联合?

最佳答案

结构体字段的顺序必须与原始 (C++) 声明中的顺序相同。但有一个问题:原始声明将 Samples 变体放在记录的中间,而这在 Delphi 中是不允许的。

您可以通过将变体部分声明为单独的记录,然后将该记录作为字段包含在最终结构中来解决此问题。

TWaveFormatExtensibleSamples = record
case Word of
0: (wValidBitsPerSample: Word;);
1: (wSamplesPerBlock: Word;);
2: (wReserved: Word;);
end;

然后构建最终的结构:

TWaveFormatExtensible = record
Format: TWAVEFORMATEX;
Samples: TWaveFormatExtensibleSamples;
dwChannelMask: DWORD;
SubFormat: TGUID;
end;
<小时/>

编辑:包含变体部分的记录的文档,状态:

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.

这涉及没有附带记录声明的变体部分。

但是,正如 Remy Lebeau 指出的那样,带有变体部分的记录可以直接在 TWaveFormatExtensible 声明中声明为结构的一部分,位于其他字段之间:

TWaveFormatExtensible = record
Format: TWAVEFORMATEX;
Samples: record
case Word of
0: (wValidBitsPerSample: Word;);
1: (wSamplesPerBlock: Word;);
2: (wReserved: Word;);
end;
dwChannelMask: DWORD;
SubFormat: TGUID;
end;

因此可以使用它以及单独声明的 TWaveFormatExtensibleSamples 记录。

关于delphi - 如何在记录结构中声明并集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58782529/

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