First question on extending Python...

Cameron Laird claird at lairds.us
Sun Jun 11 23:45:21 EDT 2006


In article <448A048B.7000007 at websafe.com>,
Larry Bates  <larry.bates at websafe.com> wrote:
>Redefined Horizons wrote:
			.
			.
			.
>> 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
>> module?)
>> 
>> Thanks,
>> 
>> 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/
>
>-Larry Bates

All true.

I think it's worth mentioning that some DLLs are accessible through
COM; when this is possible, I find it often more convenient than
ctypes.

I have no idea whether COM applies in the situation at hand.



More information about the Python-list mailing list