简体   繁体   English

Python中的ctypes空指针数组

[英]ctypes void pointer array in Python

everyone, 大家,

now I am using the model of ctypes in python to introduce a .dll file. 现在,我在python中使用ctypes模型引入.dll文件。 In my source code I have a function in c++ as followings 在我的源代码中,我在c ++中有一个函数,如下所示

bool getFrameFromStream(char* streamName, const void* framebuffer, int frame_number, int overwriteMaxPixelVal=-1)

where framebuffer is a pointer pointing to an array of pixel data of flexible types, eg char, Uint16, etc. Since I am a newbee for python, I used ctypes.create_stream_buffer, namely 其中framebuffer是指向灵活类型的像素数据数组的指针,例如char,Uint16等。由于我是python的新手,所以我使用了ctypes.create_stream_buffer,即

framebuffer = ctypes.create_string_buffer(1000000)
ctypes.cast(framebuffer, ctypes.c_void_p)

My questions is if the usage promises all the elements in the buffer can be of any type? 我的问题是用法是否保证缓冲区中的所有元素可以是任何类型? Or just first one element is flexible while the others should still be char? 或者只是第一个元素是灵活的,而其他元素仍应为char? If so, what should I do if I want to transfer a suitable variable. 如果是这样,如果我要传输合适的变量,该怎么办。

Many Thanks 非常感谢

If you have a buffer and you want to convert to different types, you should use a struct . 如果您有缓冲区,并且想要转换为其他类型,则应使用struct For example, if you have: 例如,如果您有:

struct FrameBuffer {
    int a;
    int b;
    double d;
    float g;
}

Then if you have the string that represents this stack in a variable my_data in Python, you could unpack it with the call: 然后,如果您在Python的变量my_data中具有表示此堆栈的字符串,则可以通过调用将其解压缩:

import struct
# ...
mvars = struct.unpack("iidf",my_data);

myvars will be a tuple of the data unpacked from there. myvars将是从那里解压缩的数据的元组。

Check the examples in the link I gave you. 查看我给您的链接中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM