python - 使用 ctypes 在 Python 中访问 c​​_char_p_Array_256

标签 python c ctypes

我有一个连接到一些 C 代码的 native python 桥,它返回一个指向数组(结构数组)的指针。该结构体包含一些字符数组(字符串)。但是如何从 c_char_p_Array_NNN 获取实际的 Python 字符串呢?

typedef struct td_Group
{
    unsigned int group_id;
    char groupname[256];
    char date_created[32];
    char date_modified[32];
    unsigned int user_modified;
    unsigned int user_created;
} Group;

int getGroups(LIBmanager *, Group **);

############# python code below: 
class Group(Structure):
    _fields_ = [("group_id", c_uint),
                ("groupname", c_char_p*256),
                ("date_created", c_char_p*32),
                ("date_modified", c_char_p*32),
                ("user_modified", c_uint),
                ("user_created", c_uint)]


def somefunc():
    myGroups = c_void_p()
    count = libnativetest.getGroups( nativePointer, byref(myGroups) )
    print "got " + str(count) + " groups!!"
    myCastedGroups = cast( myGroups, POINTER(Group*count) )
    for x in range(0,count):
        theGroup = cast( myCastedGroups[x], POINTER(Group) )
        theGroupName = theGroup.contents.groupname 
        ### Now how do I access theGroupName as a python string?
        # it is a c_char_p_Array_256 presently

最佳答案

类型应为 c_char*256 ,不是c_char_p*256 ,因为它是 char[256] ,不是char *[256] .

<a href="http://docs.python.org/library/ctypes.html#ctypes.string_at" rel="noreferrer noopener nofollow">string_at</a>(theGroupName, sizeof(theGroupName))

关于python - 使用 ctypes 在 Python 中访问 c​​_char_p_Array_256,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8314275/

相关文章:

python - 传递标准 :vector from C++ to Python via Ctypes: getting nonsensical values

python - django/celery - celery 状态 : Error: No nodes replied within time constraint

python - 如何在检测到的矩形中上下移动cv2.line来模拟Python中的人脸扫描?

python - Python简介,导致函数打印true

python - 来自 scipy.optimize 的 python 中 curve_fit 和 leastsq 之间的区别

c - 无法知道段错误的原因

c - 指针的后缀一元递增操作

python - PyOpenGL 的 "context specific data"是什么?

c - RaiseError (PERL, DBI) 相当于 unixODBC C API?

python - 如何在 Python 中将 DWORD 转换为字符串