[英]Passing a byte array to a C++ function that accepts a void pointer
I'm using boost::python to export some C++ functions that expect a void*
. 我正在使用boost :: python导出一些期望
void*
C ++函数。 They interpret it as raw memory (an array of bytes) internally. 它们在内部将其解释为原始内存(字节数组)。 Think
read
and write
for some very special purpose device. 想
read
并write
了一些非常特殊用途的设备。
How do I pass a Python bytearray
to such a function? 如何将Python
bytearray
传递给这样的函数?
I have tried using ctypes.c_void_p.from_buffer(mybytearray)
but this doesn't match the signature of the function. 我尝试过使用
ctypes.c_void_p.from_buffer(mybytearray)
但这与函数的签名不匹配。
Here's the minimal example: 这是最小的例子:
#include <boost/python.hpp>
void fun_voidp(void*) {}
BOOST_PYTHON_MODULE(tryit) {
using namespace boost::python;
def("fun_voidp", fun_voidp);
}
And at the python side: 在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
I'd very much fix the function prototype to take char*
and go with the last python line. 我非常修复函数原型以获取
char*
并使用最后一条python线。
There's no reason to use void*
in C++. 没有理由在C ++中使用
void*
。 I understand if the API does, but it shouldn't be hard to wrap it with your own function. 我理解API是否存在,但用自己的函数包装起来应该不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.