First question on extending Python...

  • Thread starter Redefined Horizons
  • Start date
R

Redefined Horizons

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
module?)

Thanks,

Scott Huey
 
J

John Machin

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?
No.


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.

No way. You need only *one* layer of glue.
Or can an extension module for Python wrap any C function?

I've never heard of a C function that couldn't be wrapped. If there were
such a function, having more C code in a separate DLL wouldn't make it
wrappable.
If this is
the case I think I can skip the intermediate C DLL.

I think so too.
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.

No advantages that I can see. Your C call-back function could then call
a Python function if you really wanted to -- the 3rd-party devs don't
need to know :)
Could the same
extension module export both C functions and serve as a Python
module?)

Yes.

Cheers,
John
 
L

Larry Bates

Redefined said:
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
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
 
L

Larry Bates

Redefined said:
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
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
 
C

Cameron Laird

Redefined Horizons wrote: .
.
.

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.
 
Joined
Sep 27, 2006
Messages
3
Reaction score
0
ctypes to mex dll

Hi All,

How do I interface with a MEX dll that was created in matlab? if I use ctypes then it says

Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
ctypes.cdll.load(file1)
File "F:\Python24\Lib\site-packages\ctypes\_loader.py", line 112, in load
return self._load(libname, mode)
File "F:\Python24\Lib\site-packages\ctypes\_loader.py", line 124, in _load
return self._dlltype(libname, mode)
File "F:\Python24\Lib\site-packages\ctypes\__init__.py", line 300, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Errno 126] The specified module could not be found

Should I try use the COM route that somebody suggested? If so, how do I do that?

Thanks
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top