First question on extending Python...
larry.bates at websafe.com
Sat Jun 10 01:30:19 CEST 2006
Redefined Horizons wrote:
> I still new to Python, and I've only dabbled in C, but I've got my
> first project I need to tackle that involves both languages. I was
> hoping to get some advice on how to proceed.
> There is a third-party application that I need to work with. It is
> closed-source, but it exposes a C API. I want to wrap this C API so
> that it is available from Python. I have no ability to modify the C
> API of the third part application.
> Do I the C functions that I wrap in an extensions module for Python
> need to be in a certian format? If so, I will have to write an
> intermediate DLL in C that wraps the third-party application and
> exports the functions in a form that Python can use.
> Or can an extension module for Python wrap any C function? If this is
> the case I think I can skip the intermediate C DLL.
> Are there any advantages to using the intermediate DLL written in C in
> this particular case where I will not have ability to manipulate the C
> API of the third party application directly? (For example, if the
> third-party application developers are willing to call a "call-back"
> function that must be written in C, but not Python. Could the same
> extension module export both C functions and serve as a Python
> Scott Huey
John Machin has answered most of your questions in a separate post. All you
need is to take a look at ctypes module for Python. You can call virtually
any C based API that is stored in a .DLL using ctypes. I've called .DLL
APIs for Castelle's Faxpress, Expervision's OCR toolkit and Softrak's ADS.DLL
with no problems. The trick is creating some functions/classes that help
with the C structures that need to be passed back and forth. For that you
will probably also need to take a look at the Python struct module, but ctypes
has some built-in helper functions also.
ctypes can be located here: http://starship.python.net/crew/theller/ctypes/
More information about the Python-list