Can I change the name of a dll to load at runtime?

江枫思渺然 提交于 2021-01-28 04:10:41

问题


Say I have a dll named middle.dll that is importing functions from runme.dll

I now have a replacement runmeDBG.dll that has all the same functions exported as runme.dll

Is there a way to use middle.dll but have it link to the functions exported from runmeDBG.dll instead of runme.dll?

I can change the code for middle.dll but do not know the name of the dll that will contain the functions to use when I build middle.dll.

(And all of this in the context of C/C++ and the Mingw toolset)


回答1:


I'm assuming middle.dll is not yours, and it links statically to runme.dll. No, there's no way to change dll name in case of static linkage; you can, however, rename the runmeDBG.dll to runme.dll and place it somewhere where dll loader will find it. Or use a manifest to point the executable straight to the specific path. It's not a requirement that runme.dll sits in at specific path, is it?




回答2:


If you access all the functions in the DLL via use of GetProcAddress then yes, you could switch out which module you use at run time. Just load the alternative module (see LoadLibrary) and use its handle as the argument to GetProcAddress.

You wouldn't want to use this for too may imports however, it would get very tedious!




回答3:


Just rename the replacement and put it in the same directory as your executable.




回答4:


This question is old but I found it first in Google as I met a very similar issue and then I found a better solution elsewhere. Here's the solution I found that is explained in another thread: Delay Loading DLLs.

How do I rename a DLL but still allow the EXE to find it?

This technique can be useful if, for example, a DLL was designed to be linked statically (with classes in the interface) but you need to actually load it dynamically without changing its interface. You simply have to tell Windows to delay load the DLL and when it actually loads the DLL, Windows calls your preload hook and then you just return an Handle to a different DLL (which maybe have a different name) that have the same interface.



来源:https://stackoverflow.com/questions/5474816/can-i-change-the-name-of-a-dll-to-load-at-runtime

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