Passing DLL handle as an argument (in Windows)

Pekka Kytölä peksikytola at gmail.com
Fri Nov 18 04:31:42 EST 2011


On Nov 18, 10:27 am, Ulrich Eckhardt <ulrich.eckha... at dominolaser.com>
wrote:
> Am 18.11.2011 08:51, schrieb Pekka Kytölä:
>
> > Is it possible to pass my own dll's (already loaded) handle as an
> > argument to load/attach to the very same instance of dll? Thing is
> > that I've done plugin (dll) to a host app and the SDK's function
> > pointers are assigned once the dll is loaded in the host process.
>
> DLL handles are valid anywhere in a process, but...
>
> > I'd like to fire up python code with ShellExecuteEx from my plugin
> > dll and expose (wrap) these SDK funcs to that script. If I just load
> > the dll in python it will be different instance and the SDK function
> > pointers are all NULL. I tried passing the dll handle as lpParameter
> > but in vain.
>
> ...ShellExecuteEx will create a new process, where the DLL handle is not
> valid. A new process has a separate memory space, so the function
> pointers are different etc. This is so by design, it shouldn't matter to
> one process when a DLL it uses is loaded into another process, too.
>
> > Is this ShellExecute + dll handle passing even possible or do I need
> > to take a totally different route? What route that would be? Doesn't
> > have to be portable, just so it works in Windows environment.
>
> If all you want is to run Python code inside your application, you can
> link in a Python interpreter and run it from there. This is called
> embedding Python (docs.python.org/extending/embedding.html), as opposed
> to writing Python modules, don't confuse those two Python C APIs.
>
> Another way to get both into the same process is to convert your host
> application into a Python module, which you then import into Python.
> This would use the other Python C API
> (docs.python.org/extending/extending.html).
>
> It depends a bit on what you want to achieve, so you might want to
> elaborate on that. This is often better than asking for a way to achieve
> a solution that is impossible.
>
> Good luck!
>
> Uli

Thanks for reply, I'll try to elaborate a bit :)

I should have included this bit from:

http://docs.python.org/library/ctypes.html#loading-shared-libraries

"All these classes can be instantiated by calling them with at least
one argument, the pathname of the shared library. If you have an
existing handle to an already loaded shared library, it can be passed
as the handle named parameter, otherwise the underlying platforms
dlopen or LoadLibrary function is used to load the library into the
process, and to get a handle to it."

So, I've got this handle to my .dll and would want to get that passed
as an argument when firing up .py via ShellExecute and use one of the
dll load functions that take in handle. What I don't know if these
windows/python dll handles are interchangable at all. Think it would
be quite nice if they were. But if they aren't I need to forget about
it and just #include <Python.h>



More information about the Python-list mailing list