2011-11-29 72 views
0

我有一個本地python橋到一些C代碼,它返回一個指向數組(結構數組)的指針。該結構包含一些字符數組(字符串)。但是我怎樣才能從c_char_p_Array_NNN得到一個真正的Python字符串?在Python中使用ctypes訪問c_char_p_Array_256

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

的類型應該是c_char*256,不c_char_p*256,因爲它是一個char[256],而不是一個char *[256]

string_at (theGroupName, sizeof(theGroupName))