ctypes 将 Python 布尔数组转换为 C++ 布尔数组以传递给函数

问题描述 投票:0回答:1

我有一个 C++ 函数,它接受参数

(const BOOL channels[])
,这是在 C++ 中调用它时传递的变量:

bool[] achannel = new bool[16];
achannel[0] = true;

我想知道如何使用 ctypes 将其转换为 Python,以便从 Python 应用程序成功调用该函数。

我试过这个:

achannels = [None] * 16
achannels[0] = True

但最终出现此错误:

OSError: exception: access violation reading 0x0000000000000039
python c++ ctypes
1个回答
0
投票

const BOOL channels[]
是指向 const BOOL
指针
。您要传递的参数是一个指向 bool 的指针,它恰好是 16 个 bool 数组的第一个元素。

您可以将所需的论点构建为

arr_type = ctypes.c_bool * 16
achannels = arr_type(True, *[False]*15)

achannels_ptr = c_types.pointer(value)
© www.soinside.com 2019 - 2024. All rights reserved.