Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2011
Views 4,623
Bookmarked 9 times

C/C++ – Python interop

By | 13 Dec 2011 | Technical Blog
Python interop in C/C++.
A Technical Blog article. View original blog here.[^]

Making Python work with C/C++ can be achieved in a couple of ways, either writing a DLL with a Python API, or using the ctypes library module.

When working with Python, it is useful to have virtual environments so that we have a clean and isolated Python environment. Although it is not necessary for this example, I’ll run through the steps necessary:

Then:

c:\> cd c:\python27\scripts
c:\python27\scripts> easy_install virtualenv
c:\python27\scripts> easy_install pip

and then:

C:\Python27\Scripts>virtualenv --distribute --prompt=(myenv) %USERPROFILE%\myenv

New python executable in C:\Users\user\myenv\Scripts\python.exe
Installing distribute...........................................................
................................................................................
....................................................done.
Installing pip.................done.

Then enter your virtualenv

c:\> C:\Users\user\myenv\Scripts\activate.bat
(myenv) c:\>

where you can now use pip install to download and install python modules into the virtualenv as necessary.

If you want to use the Python Tools for Visual Studio, ensure that you change %USERPROFILE% to $(UserProfile) in the (Python) project settings so that you run Python from the correct virutalenv.

ctypes example

The library documentation on ctypes can be found here.

If we start from scratch in Visual Studio, we can create a solution that contains two projects: a simple Python program to demonstrate ctypes, and a simple DLL from the VS wizards.

Quoting verbatim from the Python docs: ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries. So by using the LoadLibrary method on cdll we can load our DLL into Python, and then start accessing the methods.

The following Python except demonstrates this in practice:

#dll_path = '....'
print "Loading dll {0}".format(dll_path)

#  load it
mydll = cdll.LoadLibrary(dll_path)

#  function has to be extern "C" and have an undecorated name:
result = mydll.fnSimpleDll()
print "Function returned:  {0}".format(result)

#  first way to call C++ decorated method - by ordinal
cppFunction = mydll[1]
cppresult = cppFunction()
print "C++ decorated function by ordinal returned:  {0}".format(cppresult)

#  second way - by decorated name
newCppFunction = getattr(mydll, "?cppFunction@@YAHXZ")
newCppResult = newCppFunction()
print "C++ decorated function by ordinal returned:  {0}".format(newCppResult)

print "Finished"

Using depends, we can examine our DLL and see the ordinals and decorated names (or use a .def file if we really want to). Prefixing functions in C++ with extern "C" makes them directly available in Python as if they were a standard Python module as the function names are undecorated.

For a library or project where you use code generation, you can easily generate a Python stub to your DLLs or .so files that is available cross platform. Other obvious uses are to use Python as a harness for your C/C++ DLLs and run Python based tests against them: this can be particularly useful when Python has some benefit or feature that C++ does not have built-in or available in your environment, that you might want to take advantage of during testing, such as Regular Expressions, REST calls, and so on.

Attached is a Zip file containing the example shown here.

  • PythonCTypes.zip (requires VS2010, Python 2.7, PTVS, and a virtualenv as described above).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Barry Lapthorn



United Kingdom United Kingdom

Member

Jack of all trades.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNice PinmemberShakti Misra6:45 16 Dec '11  
AnswerRe: Nice PinprotectorBarry Lapthorn11:41 16 Dec '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 Dec 2011
Article Copyright 2011 by Barry Lapthorn
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid