这是一些简单的例子,我试图得到解决一个实际有用的问题之前工作。 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代码:
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
输出:
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
上面的代码工作正是我所期望的,我的指针进入了和被修改,我让我的整数回来。 当我尝试创建在C geoTime结构和返回回Python中的问题发生。
用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;
}
修改Python代码:
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
有一次,我改变这样的C代码得到废话成指明MyTime对应,而不是从Python的信息和返回值我的代码被放置到now_geoTime,而不是测试。 什么任何想法可能是想错了? 它看起来像Python代码没有做什么我期望,因为C代码似乎与获得传递的值正常工作的方式。
从上个示例的输出:
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
任何想法将不胜感激,我一直试图让这个到现在相当长一段时间的工作。 提前致谢!