Reputation: 735
I'm having a C function that returns a pointer to an struct:
struct iperf_test *
iperf_new_test()
{
struct iperf_test *test;
test = (struct iperf_test *) malloc(sizeof(struct iperf_test));
...
return test;
}
This function is called from Python in the following way:
self.lib = cdll.LoadLibrary("lib.so")
self._test = self.lib.iperf_new_test()
The struct has some values such as:
struct iperf_test
{
int server_port;
int bind_port;
};
The examples I see on the internet shows that I need to use a function that receives a pointer to alter the values, for example in python:
self.lib.iperf_set_test_server_port(self._test, int(port))
And in C:
void
iperf_set_test_server_port(struct iperf_test *ipt, int srv_port)
{
ipt->server_port = srv_port;
}
Is there a way to change the value bind_port directly without using a C function?
Upvotes: 1
Views: 1621
Reputation: 155458
Yes. This is why ctypes
supports defining your own structs, and defining prototypes for functions.
You'd need to make a Python level definition of your structure, e.g.:
from ctypes import Structure, c_int, POINTER
class iperf_test(Structure):
_fields_ = [("server_port", c_int),
("bind_port", c_int)]
Then, before calling your C function, you set its restype
correctly:
# Load library as normal
self.lib = cdll.LoadLibrary("lib.so")
# New, so Python knows how to interpret result
self.lib.iperf_new_test.restype = POINTER(iperf_test)
# Call, and Python returns pointer to Python definition of struct
self._test = self.lib.iperf_new_test()
Now you can use it by dereferencing (done with [0]
since Python lacks a *
pointer dereference operator) and setting attributes on the dereferenced struct directly:
self._test[0].bind_port = new_bind_port
Upvotes: 3