Passing DLL handle as an argument (in Windows)

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Fri Nov 18 03:27:39 EST 2011


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



More information about the Python-list mailing list