Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C++ DLLs from Python

Tags:

ctypes

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?

like image 969
Emil D Avatar asked Feb 12 '10 17:02

Emil D


People also ask

Can Python read DLL?

New in Python version 2.5 is the ctypes, a foreign function library. It provides C-compatible data types and allows calling functions in DLLs or shared libraries.

What is _fields_ in Python?

The only field is _fields_ , which is a list of tuples. Each tuple is (<variable-name>, <ctypes. TYPE>) . ctypes has types like c_char ( char ), and c_char_p ( *char ). ctypes also includes POINTER() which creates a pointer type from any type passed to it.


2 Answers

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.

like image 81
jkp Avatar answered Sep 21 '22 23:09

jkp


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

like image 32
Chris H Avatar answered Sep 18 '22 23:09

Chris H