Home > Software engineering >  Receive a pass a object by reference using ctypes
Receive a pass a object by reference using ctypes

Time:10-25

I have a library in C that has an object IedConnection that handles the connection to a remote equipment.

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

I understand how to pass and return ctype objects like c_int, but how would I declare the IedConnection con object in python using ctypes?

CodePudding user response:

IedConnection is a pointer to a structure (typedef struct sIedConnection * IedConnection). All pointers to structures should have the same representation, so you can just use a pointer to any structure, and need not define its contents, unless you want to dereference it or do pointer arithmetic. Hence this should work:

from ctypes import *

class SIedConnection(Structure):
    pass

IedConnection = POINTER(SIedConnection)

now you can use IedConnection as the return type and in the argument types as usual.

  • Related