views:

361

answers:

3

Here's my problem: I would like to create two separate instances of the same DLL.

The following doesn't work because Handle1 and Handle2 will get the same address

  Handle1 := LoadLibrary('mydll.dll');
  Handle2 := LoadLibrary('mydll.dll');

The following works, but I have to make a copy of the DLL and rename it to something else (which seems a bit silly)

  Handle1 := LoadLibrary('mydll.dll');
  Handle2 := LoadLibrary('mydll2.dll');

Is there a way to have only one DLL file, but load several instances of it?

+1  A: 

I don't think that's possible.

You'd have to write a .exe which loads the dll. Then you can span multiple processes (the .exe), and each will run its own instance of the dll. You'd have to use IPC (inter process communication) techniques to communicate with the .exes. Certainly doable, but not exactly a no-brainer.

Giel
A: 

Windows XP introduced side-by-side execution for Win32 DLL's (these guys know a lot about it).

With a lot of hoops you can now:

--jeroen

Jeroen Pluimers
The example of loading the same dll twice does it by using COM for one instance and LoadLibrary for the other. Although interesting, it may not be very practical in this case.
Lars Truijens
@Lars: It is the only way that I found which works. So: define 'practical' if you really need it :-)
Jeroen Pluimers
A: 

It won't work with LoadLibrary because Windows checks whether the dll has already been loaded and will return the same handle again and again.

I have got some code that was originally meant to load a dll from a resource bound to the executable but I guess it would also be possible to do the same for a memory area which was filled with the content of a file. I can't see any reason why it would not work twice, but I have not tested it.

You can find it here: http://svn.berlios.de/viewvc/dzchart/utilities/dzLib/trunk/src/u_dzResourceDllLoader.pas?view=markup

It is part of my library dzlib which is available under the MPL.

dummzeuch