【问题标题】:How to pass an array of structs to a DLL function using cpython?如何使用 cpython 将结构数组传递给 DLL 函数?
【发布时间】:2019-12-29 22:05:51
【问题描述】:
#include <stdio.h>

struct struct_data {
    int price;
    char list[255];
    int count;
};

__declspec(dllexport) void __stdcall testcall(struct struct_data *array_data) {

    int i = 0;
    while(array_data[i].price != 0) {           // array_data is guaranteed to have a zero somewhere inside memory allocated for the array
        printf("%d\n", array_data[i].price);
        i++;
    }

}

我尝试使用 cpython 从 Python 调用它,如下所示:

# C Backend imports
import os
import sys
import ctypes

# load DLL
dll = ctypes.WinDLL('C:\\Users\\user\Documents\\Pelles C Projects\\MarketlibDLL\\MarketlibDLL.dll')

class struct_data(ctypes.Structure):
    _fields_ = [
        ('price', ctypes.c_int),
        ('list', ctypes.c_char * 255),
        ('count', ctypes.c_int),
    ]

d = struct_data() * 100    # I try to do:   struct struct_data d[100]; 
d[0].price = 100
d[0].list = "ABC"
d[0].count = 3
d[1].price = 0

dll.testcall.argtypes = [ctypes.POINTER(struct_data)]
dll.testcall(d)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-36161e840b5f> in <module>
----> 1 d = struct_data() * 100
      2 d[0].price = 100
      3 d[0].list = "ABC"
      4 d[0].count = 3
      5 d[1].price = 0

TypeError: unsupported operand type(s) for *: 'struct_data' and 'int'

【问题讨论】:

  • @golobich 查看已编辑的答案,我正在尝试创建一个 100 项的 struct_data 数组,我可以将其传递给 C 函数

标签: python c python-3.x ctypes cpython


【解决方案1】:

[Python 3.Docs]: ctypes - A foreign function library for Python 包含所有必需的信息(Arrays 部分包含一个完美映射当前场景的示例)。

修改你的代码(为了可读性分成两行):

struct_data_array_100 = struct_data * 100  # !!! This is a type (array consisting of 100 struct_data elements) !!!
d = struct_data_array_100()  # Instantiate the array type
#d[0].price ...
# ...

【讨论】:

    【解决方案2】:

    如果您想要一个包含 100 个 struct_data 的列表,您需要将列表相乘而不是数据:

    d = [struct_data()] * 100
    

    这和写法一样:

    d = [struct_data() for _ in range(100)]
    

    【讨论】:

    • 我想要一个包含 100 个 struct_data 对象的 C 数组,这与列表不同。我终于知道我必须这样做:d = (struct_data * 100)()
    猜你喜欢
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多