Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythoncTypes传入指针并返回结构_Python_C_Pointers_Struct_Ctypes - Fatal编程技术网

PythoncTypes传入指针并返回结构

PythoncTypes传入指针并返回结构,python,c,pointers,struct,ctypes,Python,C,Pointers,Struct,Ctypes,这是一个简单的例子,在解决一个实际有用的问题之前,我一直在努力工作。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->nano

这是一个简单的例子,在解决一个实际有用的问题之前,我一直在努力工作。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
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
输出:

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代码:

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
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而不是test中。对可能出现的问题有什么想法吗?看起来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
Python Now:1336404920.77 Python地理时间:1336404920 773674011 *************输入C******************** 时间:90500-17037640 MyTime:-17037640-17037640 retValue:314 159 *************退出C********************** 现修改地理时间:314159 试验类型: 测试信息:137096800 134497384
任何想法都将不胜感激,我一直在努力让这项工作已经有一段时间了。提前谢谢

我切换到64位python/64位DLL,问题就解决了。当我觉得有点灵感时,我可能会尝试将它分离到编译器、操作系统或python,但现在我将使用它来运行。

我对您的代码进行了一次尝试,与您的代码完全一样。我得到了您想要的正确输出。所以我只能说我相信你的代码是正确的;这可能是编译器或平台问题。好的,谢谢。经过一番挖掘,我发现出于某种原因,从我编译的库返回结构根本不起作用。现在,我将假设对于我需要返回结构的几个方法,我将在C端包装它,以便从堆中malloc一些内存,并返回一个带有结构副本的指针。我可能会尝试查看用于返回结构的编译器选项,看看其中是否有我注意到的内容。如果其他人有什么想法,我愿意接受