Python Ctypes传递指针并返回结构 [英] Python Ctypes Passing in Pointer and Getting Struct Back

查看:294
本文介绍了Python Ctypes传递指针并返回结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在尝试解决实际有用问题之前要尝试进行的工作的简单示例. C代码:

This is a simple example of something I'm trying to get working before tackling an actual useful problem. The C code:

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时,就会发生问题.

The above code works exactly as I would expect, my pointer goes in and is modified and I get my integer back. The problem happens when I try to create a geoTime structure in C and return that back to Python.

在C语言中添加/修改的代码:

Added/modified 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;

}

修改后的Python代码:

Modified 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

一旦我更改了代码,就像C代码将废话变成了myTime而不是来自Python的信息,并且返回值被放入了now_geoTime而不是test中.关于可能出了什么问题的任何想法?看起来python代码没有按照我期望的方式执行操作,因为C代码似乎可以正确处理传入的值.

Once I change my code like that the C code gets nonsense into myTime instead of the information from Python and the return value gets placed into now_geoTime instead of test. Any ideas on what might be going wrong? It looks like the python code isn't doing something the way I expect because the C code seems to be working correctly with the values that get passed in.

最后一个示例的输出:

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 greatly appreciated, I've been trying to get this to work for quite a while now. Thanks in advance!

推荐答案

我切换到64位python/64位dll,问题消失了.当我感觉受到更多启发时,我可能会尝试将其隔离到编译器,os或python,但现在我将使用它运行.

I switched to 64 bit python/64 bit dlls and the problem went away. When I'm feeling a little more inspired I might try to isolate it down to the compiler, the os or python but for now I'm going to run with it.

这篇关于Python Ctypes传递指针并返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆