Přístup k C++ DLL z Pythonu

Pavel Hudeček edizon na seznam.cz
Pátek Červen 2 16:40:26 CEST 2023


Dobrý den všem,

když udělám DLL s C stylem, kde je extern "C" __declspec(dllexport) jde 
pak použít v Pythonu přes knihovnu ctypes.

Kdybych ale chtěl C++ styl, tedy export  __declspec(dllexport), kde je 
pak class s konstruktorem, co mám udělat?

Když mám testovací DLL, kde v main.h je:

//#define TESTCPP

#ifdef TESTCPP // nefunguje s ctypes
#ifdef POKUSDLLPY_EXPORTS
#define DLLe __declspec(dllexport)
#else
#define DLLe __declspec(dllimport)
#endif
#else // #ifdef TESTCPP  // funguje s ctypes
#ifdef POKUSDLLPY_EXPORTS
#define DLLe extern "C" __declspec(dllexport)
#else
#define DLLe extern "C" __declspec(dllimport)
#endif
#endif // #ifdef TESTCPP

#ifdef TESTCPP
// This class is exported from the dll
class DLLe pokusDllPy {
public:
     pokusDllPy(void);
     // TODO: add your methods here.
};
#endif // #ifdef TESTCPP

DLLe int r123(void);
DLLe int add(int a, int b);
DLLe int rABC(char* text);
DLLe int ver(char* text);

tak při vyplém TESTCPP funguje při použití přes ctypes a funguje při 
volání z EXE programu.
Když TESTCPP zapnu, z EXE funguje taky, ale přes ctypes ne.

Díky,
PH


Další informace o konferenci Hw-list