win32: exposing python scripts as .dll's

Thomas Heller theller at python.net
Thu Mar 20 14:33:44 EST 2003


Syver Enstad <syver-en+usenet at online.no> writes:

> Now and then I need to interface with something by writing a .dll
> (example: simple mapi provider, controlpanel applet, isapi
> extension). It would have been nice to be able to write these .dll's
> in python instead of C++. 
> 
> What would be cool would be a tool that generated a dynamic link
> library from a python module. As far as I can see, such a tool would
> have to generate exported functions, import the python module and
> dispatch function calls to their python versions. It would be kind of
> like ctypes only the other way round. Is there any prior art in this
> area?
> 
Not that I know of (except that I've heard it has been done with
Gordon's installer).

ctypes is growing COM support, also for inproc servers: in this
case it is 'easy' (well, not really) because there are only 2 exported
functions with known names DllGetClassObject and DllCanUnloadNow.

For the case you describe, here's a sketch how it *could* work (without
a C compiler):

1. Create a dll containing say 20 or so exported functions.
2. If loaded, the dll imports a Python module, the name should
be stored in a string resource, for example.

3. Create a Python script which copies the template dll to another file
and is able to set the string resource to a certain value (py2exe
contains an extension which can do this, also it can be done with
win32all functions)

4. The script should also be able to parse the PE format, find the names
table, and patch the names of the functions to the required ones.

(If a C compiler is available, it should be much easier: create C source
code from the script and compile.)

So far I didn't have a need for this.

Thomas




More information about the Python-list mailing list