Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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
Python 从c+传递结构+;巨蟒_Python_C++_Ctypes - Fatal编程技术网

Python 从c+传递结构+;巨蟒

Python 从c+传递结构+;巨蟒,python,c++,ctypes,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 *

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;
   }
}
这就是错误:

运行脚本:“C:\Users\Administrator\Desktop\test.py”

查询电话:567

回溯(最近一次呼叫最后一次):

文件“C:\Users\Administrator\Desktop\test.py”,第41行,在

打印(结果高度)

AttributeError:“int”对象没有属性“height”


不确定100%,但是在猜测中,我会说你的问题在于GATALIOFF的C++代码返回一个框指针(类型<代码> Box *<代码>),这实际上是一个参照内存中的位置的整数。然后,您试图获取此整数指针的height属性,这将导致错误

AttributeError:'int'对象没有属性“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结构框*GetAllInfo()
{
方框*Box1=新方框;
框1->高度=5;
框1->长度=6;
框1->宽度=7;

谢谢你的帮助……)

我有这个密码

工作正常

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=result[0]
print(Box1.height)

<>你正在混合C和C++。你更新的代码是因为<代码> cType。结构< /代码>为你解决这个问题。你还需要释放你分配的内存。你如何做这要看你的程序需要用什么代码< box >代码>。一个可能是在代码< GETALIOFION<代码>中使用一个I/OUT参数。(实现留给读者作为练习)


struct-Box{
int length;//框的长度
int宽度;//长方体的宽度
int height;//长方体的高度
};
外部“C”测试框*GetAllInfo(){
方框*Box1=新方框;
框1->高度=5;
框1->长度=6;
框1->宽度=7;

cout这是相当难以辨认的。请看哪一个解释了如何格式化您的代码这不是一个好主意,因为它会造成内存泄漏,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);
}
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=result[0]
print(Box1.height)
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;
}