将结构从c ++传递给python

时间:2015-08-20 16:56:23

标签: python c++ ctypes

C ++代码

typedef struct Box
{
   public:
      int length;   // Length of a box
      int breadth;  // Breadth of a box
      int height;   // Height of a box
};

extern "C"
{
    //struct Box __declspec(dllexport) GetAllInfo();
    TESTAPI struct Box * GetAllInfo();
}

extern "C"
{
    TESTAPI struct Box * GetAllInfo()
    {
       Box *Box1 = new Box;
       Box1->height = 5;
       Box1->length = 6;
       Box1->breadth = 7;

       cout << "Info is : " << Box1->height<<Box1->length<<Box1->breadth <<endl;

       return Box1;
   }
}

Python代码

import ctypes
from ctypes import Structure, c_int, c_double, windll
from ctypes import *
from ctypes import wintypes
from collections import namedtuple

#astdll = windll.CDll
class Box(object):

    def __init__(self,height,length,breadth):

        self.height = height
        self.length = length
        self.breadth = breadth

#class Box(Structure):

# _fields_ = [

# ("height", ctypes.c_int),
# ("length", ctypes.c_int),
# ("breadth", ctypes.c_int)

# ]

Box = namedtuple("Box", "height length breadth")

lib = cdll.LoadLibrary("F:\\QT SCADA\\forPythonDLL\\Neha_Test\\Debug\\Neha_Test.dll")

#lib.GetAllInfo.restype = POINTER(Box)

s = Box

global result

result = lib.GetAllInfo()

#s = result
s.height = 20

print (result.height)
print (s.height)

这是错误:

  

运行脚本:&#34; C:\ Users \ Administrator \ Desktop \ test.py&#34;

     

信息是:567

     

追踪(最近一次呼叫最后一次):

     

文件&#34; C:\ Users \ Administrator \ Desktop \ test.py&#34;,第41行,

     

print(result.height)

     

AttributeError:&#39; int&#39;对象没有属性&#39; height&#39;

3 个答案:

答案 0 :(得分:0)

对此不是100%肯定,但我猜你的问题在于GetAllInfo的C ++代码返回一个Box指针(类型Box*),这实际上只是一个整数,指向内存中的位置。您然后尝试获取此整数指针的height属性,这会导致错误AttributeError: 'int' object has no attribute 'height'

尝试返回一个Box对象(如下所示),而不是Box指针,看看是否能修复它。

TESTAPI struct Box * GetAllInfo()
{
   Box *Box1 = new Box;
   Box1->height = 5;
   Box1->length = 6;
   Box1->breadth = 7;

   cout << "Info is : " << Box1->height<<Box1->length<<Box1->breadth <<endl;

   return Box1;
}

变为

TESTAPI struct Box GetAllInfo()
{
   Box *Box1 = new Box;
   Box1->height = 5;
   Box1->length = 6;
   Box1->breadth = 7;

   cout << "Info is : " << Box1->height<<Box1->length<<Box1->breadth <<endl;

   return (*Box1);
}

答案 1 :(得分:0)

感谢所有帮助....:)

我收到了这段代码

工作正常

import ctypes
from ctypes import Structure, c_int, c_double, windll
from ctypes import *
from ctypes import wintypes
from collections import namedtuple


lib = cdll.LoadLibrary(DLL_PATH)

class Box(Structure):
_fields_ = [
    ("length", c_int),
    ("breadth", c_int),
    ("height", c_int)]

 lib.GetAllInfo.restype = POINTER(Box)
result = lib.GetAllInfo()
print ("result type: ", type(result))
print ("-"*30)
print ("result: ",result)
print ("-"*30)
print (result.contents)

BOX1 =导致[0]    打印(Box1.height)

答案 2 :(得分:0)

您正在混合使用C和C ++。您的更新代码有效,因为ctypes.Structure已为您解决此问题。您还需要释放分配的内存。如何执行此操作取决于您的程序需要使用Box。一种可能性是在GetAllInfo中使用输入/输出参数(作为读者练习留下的实现)。

struct Box {
    int length;   // Length of a box
    int breadth;  // Breadth of a box
    int height;   // Height of a box
};

extern "C" TESTAPI Box* GetAllInfo() {
    Box *Box1 = new Box;
    Box1->height = 5;
    Box1->length = 6;
    Box1->breadth = 7;

    cout << "Info is : " << Box1->height << Box1->length << Box1->breadth << endl;
    return Box1;
}