2010-07-07 47 views
2

我使用Python中的​​调用C函数。它在由库分配的内存中返回一个指向结构的指针(应用程序调用另一个函数以稍后释放它)。我无法弄清楚如何按下函数调用以适应ctypes。该结构是这样的:python ctypes中的未知数组长度

struct WLAN_INTERFACE_INFO_LIST { 
    DWORD    dwNumberOfItems; 
[...] 
    WLAN_INTERFACE_INFO InterfaceInfo[]; 
} 

我一直在使用一个子类的结构看起来像这样:

class WLAN_INTERFACE_INFO_LIST(Structure): 
    _fields_ = [ 
     ("NumberOfItems", DWORD), 
     [...] 
     ("InterfaceInfo", WLAN_INTERFACE_INFO * 1) 
     ] 

我怎么能告诉ctypes的让我访问InterfaceInfo阵列中第n项?

我不能使用Scott's excellent customresize() function,因为我没有内存(Memory cannot be resized because this object doesn't own it)。

回答

3

修改斯科特的答案删除resize()呼叫工作:

def customresize(array, new_size): 
    return (array._type_*new_size).from_address(addressof(array)) 
+0

你能解释一下这是怎么使用?我尝试调整大小,但返回了:'(lu_list.contents).guid = customresize((lu_list.contents).guid,(lu_list.contents).cnt)' 'TypeError:不兼容的类型,GUID_Array_4实例而不是GUID_Array_1实例' – Awalias 2012-11-14 10:54:35