How to use typedef in ctypes

Viewed 754

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

I wrap structs this way

file.c

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

file.py

import ctypes


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

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

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

1 Answers

Check [Python 3.Docs]: ctypes - A foreign function library for Python.

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.

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 { ... };).

Related