【问题标题】:unknown array length in python ctypespython ctypes中的未知数组长度
【发布时间】:2010-07-07 07:16:51
【问题描述】:

我正在使用 Python 中的 ctypes 调用 C 函数。它在库分配的内存中返回一个指向结构的指针(应用程序稍后调用另一个函数来释放它)。我无法弄清楚如何按摩函数调用以适应 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)。

【问题讨论】:

    标签: python arrays pointers ctypes winapi


    【解决方案1】:

    修改 Scott 的回答以删除 resize() 调用有效:

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

    【讨论】:

    • 你能解释一下这是如何使用的吗?我尝试调整大小但返回:(lu_list.contents).guid = customresize( (lu_list.contents).guid, (lu_list.contents).cnt)TypeError: incompatible types, GUID_Array_4 instance instead of GUID_Array_1 instance
    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 2019-12-01
    • 2010-10-14
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2015-04-20
    • 2017-07-15
    相关资源
    最近更新 更多