【发布时间】:2023-02-23 20:24:32
【问题描述】:
我正在尝试在 Rust 中创建一个动态的 LOGPALETTE 结构。这个结构的最后一个字段是名义上声明为 1 个元素的数组,但实际上它可以是任意数量的元素的数组。我们在堆中分配结构时指定元素的数量。
这就是我在 C 中的做法:
PALETTEENTRY entry = {0};
LOGPALETTE* pLogPal = (LOGPALETTE*)malloc(
sizeof(LOGPALETTE) + 2 * sizeof(PALETTEENTRY) // room for 2 elements
);
pLogPal->palNumEntries = 2; // inform we have 2 elements
pLogPal->palPalEntry[0] = entry; // fill the 2 elements
pLogPal->palPalEntry[1] = entry;
// use pLogPal...
free(pLogPal);
考虑到 LOGPALETTE 和 PALETTEENTRY 声明,我如何在 Rust 中编写这个?
【问题讨论】:
-
Nitpick:使用官方的
windows和windows-syscrates。 -
可能首先需要将地址the bug in the C code,然后
mem::transmute转化为 Rust 代码。