1

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

1 Answer 1

2

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))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.