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?
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.
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:
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.