python - 使用 ctypes 通过引用接收传递对象

标签 python c ctypes

我有一个 C 语言库,其中有一个对象 IedConnection,用于处理与远程设备的连接。

IedConnection con = IedConnection_create();
IedConnection_connect(con, &error, hostname, tcpPort);
IedConnection_destroy(con);

我了解如何传递和返回 ctype 对象,例如 c_int,但是如何使用 在 python 中声明 IedConnection con 对象>ctypes

最佳答案

IedConnection是一个指向结构的指针(typedef struct sIedConnection * IedConnection)。所有指向结构的指针都应该具有相同的表示形式,因此您可以只使用指向任何结构的指针,并且不需要定义其内容,除非您想取消引用它或进行指针算术。因此这应该有效:

from ctypes import *

class SIedConnection(Structure):
    pass

IedConnection = POINTER(SIedConnection)

现在您可以使用 IedConnection 作为 return type并在 argument types像往常一样。

关于python - 使用 ctypes 通过引用接收传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69700568/

相关文章:

c - 如何告诉openmp不要同步数组

objective-c - Objective C 中的 "->"是什么?

python - 使用 ctypes 和线程时出现段错误

python - 如何减少递归函数定义中所需的参数(用于计算a*b)

python - 解析 XSD 文件以获取名称和描述

C++ : memory management

python - 如何在 python 字符串中表示十六进制值?

python - Ctypes 抛出 "WindowsError: [Error 193] %1 is not a valid Win32 application",但这不是 32/64 位问题

python - 当 values_list 有多个元素时从两个列表创建字典

python - jupyter:如何停止执行错误?