如何延迟加载Windows私有程序集?是否可以?
-
14-11-2019 - |
题
我的项目是一个插件(Windows DLL),由无法在我的控件下执行的主机加载。我的dll想要加载一些额外的libs。我用私人装配来这样做;在怎么样可以在自己的目录中加载Win32 App插件的DLL,因为如何执行此操作。但是如果我在主DLL的链接线上添加生成的/delayload dependentlib.dll
以避免加载大会tIL它需要它(我必须出于各种原因),Windows不再搜索我的私人装配 - 似乎它忽略了我编译的清单。相反它在常规搜索路径中查找延迟加载的DLL。(我使用Sysinternals Procmon来检查。)
是一个已知的错误,或者是否有任何其他方法可以延迟装配组件?我宁愿不走加上LoadLibrary + GetProcAddress路线,在那里我必须知道我在依赖的lib中关心的所有符号。
解决方案
Your problem here is, when the first call to a delay loaded function is made, the applications default activation context is the current context.
What you need to do is create an activation context: CreateActCtx pointing at your own manifest (hinstance+resource id is possible I think).
Then, wrap all, or at least the very first, call to the dll with ActivateActCtx (and the corresponding deactivate function) to ensure the correct assemblies are searched.
In theory you could just embed the code to activate the appropriate context in the delayload helper function.
其他提示
This behaviour is by design, unfortunately. Essentially, when you specify /DELAYLOAD, you're just instructing the linker to insert the LoadLibrary and GetProcAddress calls for you. As a result, the behaviour when delay loading a DLL is the same as loading that DLL dynamically with LoadLibrary.
MSDN describes some of the consequences. On the plus side, you can override the default behaviour. I'd recommend writing your own delay load helper function.
FARPROC WINAPI __delayLoadHelper2(PCImgDelayDescr pidd, FARPROC * ppfnIATEntry)
{
//...
}
The linker will insert calls to this function whenever it needs to resolve an entry point in a delay loaded DLL. Your version could implement a custom search for your private assemblies. Here's additional information about the helper function on MSDN.