views:

41

answers:

1

I'm calling a C function using ctypes from Python. It returns a pointer to a struct, in memory allocated by the library (the application calls another function to free it later). I'm having trouble figuring out how to massage the function call to fit with ctypes. The struct looks like:

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

I've been using a Structure subclass that looks like this:

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

How can I tell ctypes to let me access the nth item of the InterfaceInfo array?

I cannot use Scott's excellent customresize() function because I don't own the memory (Memory cannot be resized because this object doesn't own it).

+1  A: 

Modifying Scott's answer to remove the resize() call worked:

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