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

发布于 2024-12-18 14:54:09 字数 1192 浏览 0 评论 0原文

我有一个连接到一些 C 代码的本机 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

I have a native python bridge to some C code, which returns a pointer to an array (array of structs). The struct contains some character arrays (strings). But how can I get from a c_char_p_Array_NNN to an actual Python string?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-12-25 14:54:09

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

string_at(theGroupName, sizeof(theGroupName))

The type should be c_char*256, not c_char_p*256, because it's a char[256], not a char *[256].

string_at(theGroupName, sizeof(theGroupName))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文