简体   繁体   English

将Python实例转换为ctypes.c_void_p

[英]Converting Python instance to ctypes.c_void_p

I'm trying to convert a Python object into C void pointer so that it would be callable from C API's, however the following code doesn't seem to work from ctypes import * 我正在尝试将Python对象转换为C void指针,以便可以从C API调用它,但是以下代码似乎不适用于ctypes import *

class myclass:
    def __init__(self):
        self.val = 6
        return


foo = myclass()

data = c_void_p(foo)

Output: 输出:

Traceback (most recent call last):
  File "test.py", line 26, in <module>
    data = c_void_p(foo)
TypeError: cannot be converted to pointer

I've also tried pointer() and byref(), but none of them works. 我也试过了pointer()和byref(),但是它们都不起作用。 How can I achieve what I'm trying to do? 我如何才能实现自己的目标? Thanks in advance. 提前致谢。

Use data = ctypes.py_object(foo) . 使用data = ctypes.py_object(foo)

ctypes.py_object represents the PyObject * type in C. ctypes.py_object表示C中的PyObject *类型。

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

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