C char数组到Python列表

C char array to Python List

我有一个C函数如下,

int ldv_read( int handle, void* msg_p, unsigned length )

我需要将指针作为 msg_p 传递到 C 代码中,并能够在 Python 中打印数据。如果我在 C/C++ 中执行此操作,我将按如下方式执行此操作,

char buf[ 64 ];
buf[0] = 0x22;//This is needed to prepare for something else.
buf[1] = 0x0f;
buf[2] = 0x61;
pldv_read( handle, buf, sizeof( buf ) );
//iterate buf and print the data

如何在 Python 中执行此操作?这是我目前在 Python 中的内容,但如果我的 C 代码尝试写入 buf,这似乎会崩溃。我正在使用 Win7。

from ctypes import*
ldv_open = windll.wldv32.ldv_open
ldv_open.restype = c_int
ldv_open.argtypes = [c_char_p]
deviceName = ('LDV/\\.\Lonusb8-4')
handle = ldv_open(deviceName)

buf = c_char * 1024 #  <----clearly, this isn't a list, don't know how to create a char list in Python.
ldv_read = windll.wldv32.ldv_read
ldv_read.restype = c_int
ldv_read.argtypes = [c_int,c_void_p,c_uint]
res = ldv_read( handle, id(buf), len(buf) )
#how do I print my buf here?

使用create_string_buffer:

buf = create_string_buffer("\x22\x0F\x61", 64)
ldv_read = windll.wldv32.ldv_read
ldv_read.restype = c_int
ldv_read.argtypes = [c_int,c_void_p,c_uint]
res = ldv_read( handle, buf, len(buf) )
print(buf.raw)

它崩溃的原因是 id returns 保存字符串 .

的内部 PyObject 的内存地址