9

I'd like to know if it is possible to use ctypes to access C++ DLLs from Python and run functions (or class methods) from them.If not, is there another way to do this?

Emil D
  • 1,864
  • 4
  • 23
  • 40
  • 1
    Possible duplicate of [Extending Python with C/C++](http://stackoverflow.com/questions/1076300/extending-python-with-c-c) – Peter Wood Aug 17 '16 at 14:06

2 Answers2

10

This question has been answered many times (this one is an exact duplicate!)

In summary, no you can't do it with Ctypes (yet) but there are a number of other code generators you could use to do the job. SWIG and Boost.Python are two of the most popular.

Community
  • 1
  • 1
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 1
    I don't consider that a proper answer. When one has a compiled DLL, it is not to *extend* Python. It is just to load this DLL in memory and call its imported methods. SWIG and Boost.Python is not about that – Sergey Nudnov Apr 27 '22 at 15:08
1

the boost python library makes it REALLY easy to wrap a C++ class and expose it as python module. It took me around 10 minutes the first time I tried.

http://www.boost.org/doc/libs/1_42_0/libs/python/doc/tutorial/doc/html/index.html

Chris H
  • 6,433
  • 5
  • 33
  • 51
  • 1
    I wasn't so lucky: C1083 Cannot open include file: 'pyconfig.h': No such file or directory EquityCalculatorMontecarlo C:\Users\dickr\git\vcpkg\installed\x64-windows\include\boost\python\detail\wrap_python.hpp 57 – Nickpick Nov 30 '19 at 07:04