Load two instances of the same DLL in Delphi

2019-04-10 16:01发布

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?

3条回答
冷血范
2楼-- · 2019-04-10 16:37

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.

查看更多
再贱就再见
3楼-- · 2019-04-10 16:39

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:

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-10 16:40

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.

查看更多
登录 后发表回答