【问题标题】:ctypes, function returning pointer to a structurectypes,函数返回指向结构的指针
【发布时间】:2013-01-29 18:25:10
【问题描述】:

我的 C 代码返回一个指向该结构的指针,这就是我在 python 中定义它的方式

class CONTEXT(ctypes.Structure):
  _fields_ = [
                ("sockfd", ctypes.c_int), 
                ("eidSeq", ctypes.c_longlong)
             ]
# API
# connect
PY_connect=NativeDll.gf_connect
# connect input and output parameter declaration
PY_connect.argtype = [ 
                          ctypes.c_char_p, 
                          ctypes.c_char_p, 
                          ctypes.POINTER(ctypes.c_int)
                        ]
PY_connect.restype = [
                          ctypes.POINTER(CONTEXT)
                        ]

但我收到restype 的以下错误

TypeError: restype must be a type, a callable, or None

【问题讨论】:

  • 这里纯属猜测,但我建议 restype 应该只是一个普通类型,而不是您当前拥有的类型列表。

标签: python ctypes


【解决方案1】:

正如 DaveP 在 cmets 中已经正确猜到的那样,restype 一定不是类型列表。

PY_connect.restype = ctypes.POINTER(CONTEXT)

另请注意,参数类型由argtypes 属性设置,而不是argtype

【讨论】:

  • 我得到 type 必须有存储信息
  • @KonstantinosMonachopoulos 也许你输入的是pointer而不是POINTER
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多