I am trying to call a C++ class DLL in python and it does not seem to work.
Here is my code :
// simple_lib.cpp : Defines the exported functions for the DLL.
#include "pch.h" // use pch.h in Visual Studio 2019
#include "simple_lib.h"
#include <iostream>
simplelib::simplelib()
{
}
simplelib::~simplelib()
{
}
int simplelib::add(int a, int b)
{
return a + b;
}
int simplelib::sub(int a, int b)
{
return a - b;
}
void simplelib::show() {
std::cout << "simple display" << std::endl;
}
// simple_lib.h - Contains declarations
#pragma once
#ifdef SIMPLE_EXPORTS
#define SIMPLE_API __declspec(dllexport)
#else
#define SIMPLE_API __declspec(dllimport)
#endif
extern "C" {
class simplelib {
public:
SIMPLE_API simplelib();
SIMPLE_API ~simplelib();
SIMPLE_API int add(int a, int b);
SIMPLE_API int sub(int a, int b);
SIMPLE_API void show();
};
}
The DLL is successfully generated but then when I try to use in Python the add function for instance I get the following error.
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> mylib = WinDLL("simple_lib.dll")
>>>print(mylib)
<WinDLL 'simple_lib.dll', handle 7ff835120000 at 0x219ece2b348>
>>> mylib.simplelib().add(3,4)
AttributeError: function 'simplelib' not found
Besides I have tried to use these functions without creating a class this way :
// simple_lib.cpp : Defines the exported functions for the DLL.
#include "pch.h" // use pch.h in Visual Studio 2019
#include "simple_lib.h"
#include <iostream>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
void show() {
std::cout << "simple display" << std::endl;
}
// simple_lib.h - Contains declarations
#ifdef SIMPLE_EXPORTS
#define SIMPLE_API __declspec(dllexport)
#else
#define SIMPLE_API __declspec(dllimport)
#endif
extern "C" SIMPLE_API int add(int a, int b);
extern "C" SIMPLE_API int sub(int a, int b);
extern "C" SIMPLE_API void show();
And here if I try to use the add function(), it works perfectly
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> mylib.add(3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mylib' is not defined
>>> mylib = WinDLL("simple_lib.dll")
>>> mylib.add(3,4)
7
My config : Windows 10 64Bits, Visual Studio 2019.
So can you please help me on why it is not working when I am using a C++ class ?
Thanks in advance