简体   繁体   English

如何使用ctypes将void *来回传递给python中的共享库?

[英]How do you pass a void * back and forth to a shared library in python using ctypes?

I believe the basic process is: 我相信基本过程是:

from ctypes import *

LEAP = ctypes.CDLL('leap.dylib')

leap_controller = LEAP.leap_controller
leap_controller.res_type = c_void_p

leap_controller_dispose = LEAP.leap_controller_dispose
leap_controller_dispose.argtype = [c_void_p]

However, when I invoke this code it mangles the void * pointer. 但是,当我调用此代码时,它将破坏void *指针。 Debugging from the associated C code, I get: 从关联的C代码进行调试,我得到:

doug:test doug$ python test.py
Controller init!
returning: 0x7fcf9a0022c0
Created controller

Controller dispose!
argument: 0xffffffff9a0022c0
Segmentation fault: 11

Obviously the free() call fails because the pointer isn't right. 显然,由于指针不正确,所以free()调用失败。

I've tried a couple of variations on this, for example, defining a structure, like: 我已经尝试了几种变体,例如,定义了一个结构,例如:

class LEAP_CONTROLLER(Structure):
  _fields_ = [
      ("data", c_void_p)
  ]

leap_controller = LEAP.leap_controller
leap_controller.res_type = POINTER(LEAP_CONTROLLER)

leap_controller_dispose = LEAP.leap_controller_dispose
leap_controller_dispose.argtype = [POINTER(LEAP_CONTROLLER)]

...but the result always seems to be the same. ...但是结果似乎总是一样。

Python reads the pointer, but only keeps 32 bits worth of data, and returns a broken pointer with the remaining bits all 1. Python会读取该指针,但仅保留32位的数据,并返回一个残破的指针,其余所有位均为1。

Both of the binaries are 64 bit: 这两个二进制文件都是64位的:

Non-fat file: /usr/local/bin/python is architecture: x86_64
Non-fat file: ../../dll/libcleap.dylib is architecture: x86_64

I saw this ancient issue, here: http://bugs.python.org/issue1703286 我在这里看到了这个古老的问题: http : //bugs.python.org/issue1703286

...but it's marked as resolved. ...但已标记为已解决。

A few other random mailing this threads seem to mention ctypes 'truncating 64-bit pointers to 32-bit', but people all seem to have magically resolved their issues without doing anything ('it works now') or gotten no answer to their questions. 其他一些随机发送该线程的邮件似乎提到了ctypes“将64位指针截断为32位”,但是人们似乎都神奇地解决了他们的问题而没有做任何事情(“现在可以正常工作”)或无法解决问题。 。

Anyone know what I'm doing wrong? 有人知道我在做什么错吗?

Answer as per the comments; 根据评论回答; this was a typo in my code. 这是我代码中的错字。

 Per the docs, it's argtypes and restype, not argtype and res_type. –  eryksun Oct 4 at 2:31 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM