Below is the structure that I had defined in the C dll:
//structure.h
#include<stdio.h>
typedef struct Point
{
double x, y;
} Point;
And the .c file is as follows:
//structure.c
#include<stdio.h>
#include "structure.h"
#include <math.h>
/* Function to calculate hypotenuse of a triangle */
__declspec(dllexport) double distance(Point *p1, Point *p2)
{
return hypot(p1->x - p2->x, p1->y - p2->y);
}
Using ctypes in python, I had written a script as below which can have access to the C DLL:
//structure.py
import ctypes as C
from ctypes import *
class Point(C.Structure):
_fields_ = [('x', C.c_double),
('y', C.c_double)]
mydll=C.cdll.LoadLibrary('structure.dll')
distance=mydll.distance
distance.argtypes=(C.POINTER(Point),C.POINTER(Point))
distance.restype=C.c_double
p1=Point(1,2)
p2=Point(4,5)
print(mydll.distance(p1,p2))
As this is just a very small structure having very less variables, it is easy to write the python script along with the C file.
But in case of a project having 1000's of structure and also infinite number of variables in them, is there a way by which this duplicate effort of writing the structures in ctypes as well can be reduced?
Or is there some tool available in Python that can directly convert C header file to Python structure?And also is there is some python module or tool which can convert the C structure directly to ctypes structure?
P.S.- I want all of this to be implemented in Windows environment.