Ptyon Ctypes passing in a pointer and returning a structure

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!

+2
source share
1 answer

I switched to python / 64 bit 64 bit libraries and the problem disappeared. When I feel a little more inspirational, I can try to isolate it before the compiler, os or python, but for now I will work with it.

+1
source

Source: https://habr.com/ru/post/1483890/


All Articles