簡體   English   中英

從c ++傳遞結構到python

[英]passing structure from c++ to python

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)

這是錯誤:

運行腳本:“ C:\\ Users \\ Administrator \\ Desktop \\ test.py”

信息是:567

追溯(最近一次通話):

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

打印(結果高度)

AttributeError:“ int”對象沒有屬性“ height”

對此並不是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);
}

感謝所有的幫助.... :)

我得到了這個代碼

工作正常

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 ++。 更新的代碼行得通,因為ctypes.Structure為您解決了此問題。 您還需要釋放分配的內存。 如何執行此操作取決於您的程序需要使用Box做什么。 一種可能性是在GetAllInfo使用in / out參數(該實現留給讀者練習)。


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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM