This is a simple example of what I am trying to get before solving an actual problem. Code C:
typedef struct { uint32_t seconds; uint32_t nanoseconds; } geoTime; int myTest(geoTime *myTime){ printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds); myTime->seconds = myTime->nanoseconds; geoTime T = {314, 159}; printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds); return 314; }
Python Code:
import ctypes import time import math lib_astro = ctypes.CDLL("libastroC.so") class geoTime(ctypes.Structure): _fields_ = [("seconds", ctypes.c_uint), ("nanoseconds", ctypes.c_uint)] now = time.time() print "Python Now: ", now now_geoTime = geoTime() now_geoTime.seconds = ctypes.c_uint(int((math.floor(now)))) now_geoTime.nanoseconds = ctypes.c_uint(int(math.floor(math.modf(now)[0] * 1000000000))) print "Python geoTime now:", now_geoTime.seconds, now_geoTime.nanoseconds lib_astro.myTest.argtypes = [ctypes.POINTER(geoTime)] lib_astro.myTest.restype = geoTime print "************* ENTERING C ********************" test = lib_astro.myTest(ctypes.byref(now_geoTime)) print "************* EXITING C **********************" print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds print "test: ",test
Output:
Python Now: 1336401085.43 Python geoTime now: 1336401085 432585000 ************* ENTERING C ******************** Time: 1336401085 432585000 MyTime: 432585000 432585000 retValue: 314 159 ************* EXITING C ********************** Modified now_geoTime: 432585000 432585000 test: 314
The above code works exactly as I expected, my pointer goes in and changes, and I return my integer. The problem occurs when I try to create a geoTime structure in C and return it back to Python.
Added / changed code in C:
geoTime patTest(geoTime *myTime){ printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds); myTime->seconds = myTime->nanoseconds; geoTime T = {314, 159}; printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds); return T;
}
Changed Python code:
lib_astro.patTest.argtypes = [ctypes.POINTER(geoTime)] lib_astro.patTest.restype = geoTime print "************* ENTERING C ********************" test = lib_astro.patTest(ctypes.byref(now_geoTime)) print "************* EXITING C **********************" print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds print "Type of test: ",test print "Information in test: ", test.seconds, test.nanoseconds
As soon as I change my code this way, the C code becomes meaningless in myTime instead of the information from Python, and the return value is put in now_geoTime instead of the test. Any ideas on what could go wrong? It seems that the python code is not doing something as I expect, because the C code is working correctly with the values ββthat are being passed.
The conclusion from the last example:
Python Now: 1336404920.77 Python geoTime now: 1336404920 773674011 ************* ENTERING C ******************** Time: 90500 -17037640 MyTime: -17037640 -17037640 retValue: 314 159 ************* EXITING C ********************** Modified now_geoTime: 314 159 Type of test: <__main__.geoTime object at 0x82bedf4> Information in test: 137096800 134497384
Any ideas would be much appreciated, Iβve been trying to get this to work for a long time. Thanks in advance!