python - 将字节数组传递给接受空指针的 C++ 函数

标签 python boost

我正在使用 boost::python 导出一些需要 void* 的 C++ 函数。他们在内部将其解释为原始内存(字节数组)。想想一些非常特殊用途的设备的读取写入

如何将 Python bytearray 传递给这样的函数?

我试过使用 ctypes.c_void_p.from_buffer(mybytearray) 但这与函数的签名不匹配。

这是最小的例子:

#include <boost/python.hpp>

void fun_voidp(void*) {}

BOOST_PYTHON_MODULE(tryit) {
    using namespace boost::python;
    def("fun_voidp", fun_voidp);
}

在 python 方面:

import tryit
import ctypes
b = bytearray(100)
tryit.fun_voidp(b) # fail
tryit.fun_voidp(ctypes.c_void_p.from_buffer(b)) # fail
tryit.fun_voidp(ctypes.c_void_p.from_buffer(b).value) # fail

最佳答案

我非常希望修复函数原型(prototype)以采用 char* 并与最后的 python 行一起使用。

没有理由在 C++ 中使用 void*。我知道 API 是否支持,但用您自己的函数包装它应该不难。

关于python - 将字节数组传递给接受空指针的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171507/

相关文章:

python - 当我运行我的套接字程序时,得到 : `import: command not found`

c++ - 如何在 boost 中正确使用捆绑属性?

c++ - Windows 上信号处理程序的异步安全写入函数

c++ - BOOST_FILESYSTEM_VERSION 2 path.native_file_string() 的 BOOST_FILESYSTEM_VERSION 3 模拟是什么?

c++ - ImportError :/usr/lib/libboost_python. so.1.54.0: undefined symbol: PyClass_Type

python - 我的素性测试忽略了一个条件。我究竟做错了什么?

python - Virtualenv 没有名为 zlib 的模块

Python 正则表达式 : Check for match and capture groups

python - 更好的算法来随机播放(或交错)多个不同长度的列表

c++ - boost ASIO : do we need to keep using async_read and write inside a function called from async_read?