C++ code
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 code
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)
This is the error:
Running script: "C:\Users\Administrator\Desktop\test.py"
Info is : 567
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\test.py", line 41, in
print (result.height)
AttributeError: 'int' object has no attribute 'height'