win32: exposing python scripts as .dll's

David Abrahams dave at boost-consulting.com
Thu Mar 20 21:28:46 CET 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?

If you can stand C++, I think doing something like that with
Boost.Python would be relatively trivial.  An (untested) sketch:

// Retrieve the python module
object pymodule(
    handle<>(borrowed(PyImport_AddModule("module_name"))) );

// Now expose some of its functions:
int __declspec(dllexport) f1(char const* s, int x)
{
    static object f = pymodule.attr("f1");
    return extract<int>(f(s,x));
}

std::string __declspec(dllexport) f2(double x)
{
    static object f = pymodule.attr("f2");
    return extract<std::string>(f(x));
}
...

HTH,    
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Python-list mailing list