如何在ctypes中使用typedef [英] How to use typedef in ctypes

查看:65
本文介绍了如何在ctypes中使用typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ctypes库将C库包装在Python中.

I am trying to wrap a C library in Python using ctypes library.

我用这种方式包装结构

typedef struct {
    int x;
    int y;
} Point;

file.py

import ctypes


class Point(ctypes.Structure):
    _fields_ = [("x": ctypes.c_int), ("y", ctypes.c_int)]

但是我有这样一条语句,无法找出如何包装它并获得 MyFucntion 类型.

But I have a statement like this and cannot find out how to wrap it and get the type MyFucntion.

typedef char *(* MyFunction)(char *);

推荐答案

检查那是一个函数指针.您可以像这样包装它:

That's a function pointer. You can wrap it like this:

FuncPtr = ctypes.CFUNCTYPE(ctypes.POINTER(ctypes.c_char), ctypes.POINTER(ctypes.c_char))

但这在很大程度上取决于您将如何使用它.

But it very much depends on how are you going to make use of it.

请注意, typedef ctypes 没有任何关系:例如, Point 的定义与没有 typedef (结构点{...}; ).

As a side note, typedef doesn't have anything to do with ctypes: for example Point definition would be the same without typedef (struct Point { ... };).

这篇关于如何在ctypes中使用typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆