使用ctypes的Python中访问c_char_p_Array_256 [英] Access c_char_p_Array_256 in Python using ctypes
本文介绍了使用ctypes的Python中访问c_char_p_Array_256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个原生的Python桥梁,一些C code,它返回一个指针数组(结构的数组)。该结构包含了一些字符数组(字符串)。但我怎么可以从 c_char_p_Array_NNN
得到一个实际的Python字符串?
typedef结构td_Group
{
unsigned int类型GROUP_ID;
焦炭GROUPNAME [256];
烧焦DATE_CREATED [32];
烧焦DATE_MODIFIED [32];
无符号整型user_modified;
无符号整型user_created;
}集团;INT getGroups(LIBmanager *,**组);#############蟒蛇code以下:
类组(结构):
_fields_ = [(GROUP_ID,c_uint),
(组名,c_char_p * 256),
(DATE_CREATED,c_char_p * 32),
(DATE_MODIFIED,c_char_p * 32),
(user_modified,c_uint)
(user_created,c_uint)
高清somefunc():
myGroups = c_void_p()
数= libnativetest.getGroups(nativePointer,按地址(myGroups))
打印得到+ STR(计数)+群体!
myCastedGroups = CAST(myGroups,指针(集团*计))
在范围X(0,计数):
theGroup = CAST(myCastedGroups [X],指针(集团))
theGroupName = theGroup.contents.groupname
###现在我该怎样访问theGroupName以字符串形式?
#这是一个c_char_p_Array_256 presently
解决方案
的类型应该是 c_char * 256
,而不是 c_char_p * 256
,因为它是一个的char [256]
,而不是的char * [256]
。
string_at (theGroupName,sizeof的( theGroupName))
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
解决方案
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))
这篇关于使用ctypes的Python中访问c_char_p_Array_256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文