[python-win32] py2dll - or an example of a dll writen in python?

Thomas Heller theller at python.net
Fri Sep 2 20:41:35 CEST 2005


Darren Blaser <darrenb at extendedsystems.com> writes:

> Hi,
>
> I have a dll that I'd like to write a simple replacement for in python. 
> Is this possible? Is there an example somewhere of a dll written in python?
>
> The example could be for something as simple as a single dll export 
> implementing the following GetApiVersion call...
>
> typedef enum _Status
> {
>    STATUS_SUCCESS       = 0x00000000,
>    STATUS_FAILED        = 0x00000001
> }Status;
>
> typedef struct _ApiVersionInfo
> {
>    DWORD dwMajor;
>    DWORD dwMinor;
>    DWORD dwDetailed
> }ApiVersionInfo;
>
> Status GetApiVersion(ApiVersionInfo *pstVersion);
>
> My googling turned up this old post from 5 years ago, but nothing more 
> recent.
> http://mail.python.org/pipermail/python-list/2001-November/074600.html
>
> Thank you very much for any feedback.

In principle this is not complicated.
Write the dll as usual, and call Python from the exported functions.
Just make sure that you initialize Python somewhere, and be sure to
handle the GIL correctly.

It gets more complicated when the exe-process may also be using Python,
or if other dlls used in the same process are also using Python (maybe
because inproc COM objects are implemented in Python).  Then you must be
prepared to handle the case that Python already *is* initialized.

The next step would be if you want to use py2exe or a similar tool to
distribute your dll to machines that don't have Python installed (if you
need that).

But, of course there is prior art:

pywin32, for example, has a ISAPI extension (which is a dll):
http://cvs.sourceforge.net/viewcvs.py/pywin32/pywin32/isapi/src/pyISAPI.cpp?rev=1.2&view=auto

And pywin32's pythoncom, which is the inproc COM server dll.

Thomas





More information about the Python-win32 mailing list