在 C++ 中禁用自动 DLL 加载
我的场景如下:我的应用程序依赖于某个DLL(我在链接期间使用它的lib)。但是,当我的应用程序执行时,我想使用 LoadLibrary 显式加载该 DLL。但是,默认情况下,当代码到达需要该 DLL 的范围时,环境会自动查找它,然后加载它。我想禁用此行为,并且对于我所关心的,如果应用程序达到了想要执行属于该 DLL 的代码的程度,我更希望它会崩溃而不是自动加载它(因此 DLL 将仅被加载因为我明确调用了 LoadLibrary)。
同时,我使用延迟加载功能(因此只有当 DLL 实际需要加载时才会发生加载触发)。但是,我希望如果 DLL 尚未加载,应用程序就会崩溃。
也许这里有人熟悉实现这一目标的方法?
My scenario is as follows: my application depends on a certain DLL (I use it's lib during linkage). However, when my application executes, I want to explicitly load that DLL using LoadLibrary. However, by default, when the code reaches a scope where that DLL is needed, the environment automatically look it up, and then loads it. I want to disable this behavior, and for all I care, if the application reached a point where it wants to execute code that belongs to that DLL, I prefer that it will crash instead of loading it automatically (So the DLL will be loaded only because I explicitly called LoadLibrary).
In the meanwhile, I'm using the delay-load ability (so the load trigger will occur only when the DLL actually needs to be loaded). However, I would prefer that the application will just crash if the DLL wasn't loaded yet.
Perhaps anyonehere is familiar with a way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想使用
LoadLibrary
,则不要将应用程序与导入库链接。 PE 格式不支持未解析的外部,因此您可以使用标头和 dllimport,或者使用 LoadLibrary、GetProcAddress 和函数指针。If you want to use
LoadLibrary
, then don't link application with the import library. PE format doesn't support unresolved externals, so you either use headers anddllimport
, orLoadLibrary
,GetProcAddress
and pointers to functions.如果您想使用
LoadLibrary
和GetProcAddress
手动加载它,那么您不应该传递它的*.lib
文件到您的链接器。If you want to load it manually using
LoadLibrary
andGetProcAddress
then you shouldn't pass its*.lib
file to your linker.您可以通过不链接 DLL 的导入库(.lib 文件)来防止自动加载。然后,您可以在需要时使用 LoadLibrary 手动加载 DLL。
我发布了一篇关于做这类事情的博客文章 这里< /a>.
You can prevent automatic loading by not linking against the DLL's import library (the .lib file). You can then use LoadLibrary to manually load the DLL whenever you need it.
I posted a blog entry about doing this sort of thing here.
你可以hook延迟加载机制。将 __pfnDliNotifyHook2 设置为您提供的函数,然后在该挂钩中简单地终止您的应用程序。
You can hook the delayload mechanism. Set
__pfnDliNotifyHook2
to a function you provide, and in that hook simply terminate your application.延迟加载功能在第一次函数调用之前不会加载 dll,而不是作用域。如果您有调用该 dll 的全局初始值设定项,那么这可能就是您认为它基于作用域的原因。
我公司采用的是使用前调用LoadLibrary的技术,没有问题。我建议进一步深入研究您的问题。
The delayload functionality won't load a dll until its first function call, not scope. If you have global initializers that call into that dll, then that maybe be why you think its scope based.
My company uses the technique of calling LoadLibrary before use without problems. I suggest digging further into your problem.
这是您需要的吗: http://msdn.microsoft .com/en-us/library/151kt790(VS.80).aspx?
我的意思是,您可以提供自己的函数来加载 DLL,并从那里崩溃您的应用程序。所提供的链接中有详细说明。
Is this what you need: http://msdn.microsoft.com/en-us/library/151kt790(VS.80).aspx?
I mean, you can provide you own function to load DLL, and crash your aplication from there. It is detailed in the link provided.