Ctypes DLL调用ArgumentError with c\u char\u Array

2024-04-25 04:07:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为C项目编写Python DLL包装器

重要的原始C代码:

  1. char IOmap[4096];
  2. int ec_config_overlap(uint8 usetable, void *pIOmap);

Python包装器

  1. IOMap = ctypes.POINTER(ctypes.c_char * 4096)
  2. c_ec_config_overlap = ethercat.ec_config_overlap c_ec_config_overlap.argtypes = [ctypes.c_unit8, IOMap] c_ec_config_overlap.restype = ctypes.c_int

当我试图用Python定义函数时

def ec_config_overlap(usetable, PIOMap): return c_ec_config_overlap(usetable, PIOMap

叫它吧。 我收到错误

ctypes.ArgumentError: argument 2: : expected LPc_char_Array_4096 instance instead of _ctypes.PyPointerType.

我理解这个错误,但是我该如何让ctype变成Array[4096]而不是PyPointerType呢?你知道吗


Tags: 项目httpsgithubcomconfigctypesintdll
1条回答
网友
1楼 · 发布于 2024-04-25 04:07:00

此语法创建数组实例:

>>> import ctypes
>>> (ctypes.c_char*4096)()
<__main__.c_char_Array_4096 object at 0x0000024D84E2D7C8>

由于它是一个char数组,您还可以使用:

>>> create_string_buffer(4096)
<__main__.c_char_Array_4096 object at 0x0000025AE48FE948>

函数的类型应为:

c_ec_config_overlap.argtypes = [ctypes.c_uint8, ctypes.c_void_p]

但为了更好地进行类型检查,您还可以使用:

c_ec_config_overlap.argtypes = [ctypes.c_uint8, ctypes.c_char_p]

相关问题 更多 >