我在 Windows 上运行的 C++ 程序中有一个结构,我想使用 ctypes 通过 Python 中的共享内存来访问该结构。例如:
#define MAX_ENTITIES 30
struct State
{
double x;
double y;
double z;
};
struct Stat
{
unsigned int numAvailable;
unsigned int numUsed;
};
struct TransferData
{
double exLg;
float other;
unsigned int more;
int more2;
unsigned char next;
bool statusReady;
Stat status;
State entities[MAX_ENTITIES];
};
As:
import ctypes
MAX_ENTITIES = 30
class State(ctypes.Structure):
_fields_ = [
('x', ctypes.c_double),
('y', ctypes.c_double),
('z', ctypes.c_double)
]
class Stat(ctypes.Structure):
_fields_ = [
('numAvailable', ctypes.c_uint),
('numUsed', ctypes.c_uint)
]
class TransferData(ctypes.Structure):
_fields_ = [
('exLg', ctypes.c_double),
('other', ctypes.c_float),
('more', ctypes.c_uint),
('more2', ctypes.c_int),
('next', ctypes.c_ubyte),
('statusReady', ctypes.c_bool),
('status', Stat),
('entities', State * MAX_ENTITIES)
]
我希望:
shmem = mmap.mmap(-1, ctypes.sizeof(TransferData.TransferData),
"TransferDataSHMEM")
data = TransferData.from_buffer(shmem)
将使数据成为 C++ 端所表示内容的共享内存镜像,但它全部为零。