ctypes:Multiple library access problem

M

Massi

Hi everyone,

in my script I need to execute multiple separated loading of the same
dll library, in order to handle the internal variables with different
threads.
Consider the followin piece of code:

lib1 = cdll.LoadLibrary("MyLib.dll"))
lib2 = cdll.LoadLibrary("MyLib.dll"))

lib1.var1 = 0
lib2.var1 = 1

Now, if I print the value of lib1.var1 I get 1, that is lib1 and lib2
point to the same memory space. Is there a way to create "different
instances" of the same library? Or, alternatively, does it exist any
workaround to avoid lib1 and lib2 share the same memory space?

Thanks in advance.
 
D

Dave Angel

Massi said:
Hi everyone,

in my script I need to execute multiple separated loading of the same
dll library, in order to handle the internal variables with different
threads.
Consider the followin piece of code:

lib1 = cdll.LoadLibrary("MyLib.dll"))
lib2 = cdll.LoadLibrary("MyLib.dll"))

lib1.var1 = 0
lib2.var1 = 1

Now, if I print the value of lib1.var1 I get 1, that is lib1 and lib2
point to the same memory space. Is there a way to create "different
instances" of the same library? Or, alternatively, does it exist any
workaround to avoid lib1 and lib2 share the same memory space?

Thanks in advance.
Windows will not load the same DLL twice into the same process in two
different places. When it detects that it's the same one, it simply
returns the same handle as the earlier one, without any loading or
initializing.

With some DLL's, you might get away with copying it to a different
filename, and then loading each as an independent item. But unless you
wrote it yourself, or it has been documented for that behavior, you're
taking a big risk.

On the other hand, if the DLL was written with threading in mind, then
it'll get notified for each new thread you create, and it can manage TLS
(thread local storage) rather than using extern vars. I have no idea
how to get at those from Python, however.

DaveA
 
S

Stefan Schukat

Massi said:
Windows will not load the same DLL twice into the same process in two
different places. When it detects that it's the same one, it simply
returns the same handle as the earlier one, without any loading or
initializing.

With some DLL's, you might get away with copying it to a different
filename, and then loading each as an independent item. But unless you
wrote it yourself, or it has been documented for that behavior, you're
taking a big risk.

On the other hand, if the DLL was written with threading in mind, then
it'll get notified for each new thread you create, and it can manage TLS
(thread local storage) rather than using extern vars. I have no idea
how to get at those from Python, however.

DaveA

The statement from Dave is as far true as the library is loaded without a path. Hence windows
searches the system path and always finds the same file. If you load a DLL with an absolute path
window will place the dll multiple times into the process.
e.g.

lib1 = cdll.LoadLibrary(r"c:\temp\MyLib.dll")
lib2 = cdll.LoadLibrary(r"d:\temp\MyLib.dll")

This has the same effect as renaming the library on the disk.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top