Python类型传入指针并获取结构B

2024-05-12 23:13:37 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个简单的例子,我试图在解决一个实际有用的问题之前开始工作。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代码:

^{pr2}$

输出:

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而不是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

任何想法都将不胜感激,我已经试着让这个工作有一段时间了。提前谢谢!在


Tags: 代码testtimenowsecondsprintprintfmytime
1条回答
网友
1楼 · 发布于 2024-05-12 23:13:37

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

相关问题 更多 >