Load two instances of the same DLL in Delphi

岁酱吖の 提交于 2019-12-06 05:59:11

问题


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:


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.




回答2:


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.




回答3:


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:

  • load the same Win32 DLL from the same path into memory twice
  • load different versions of the same DLL into memory


来源:https://stackoverflow.com/questions/2675519/load-two-instances-of-the-same-dll-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!