在 C++ 中禁用自动 DLL 加载

发布于 2024-08-06 08:12:59 字数 323 浏览 4 评论 0原文

我的场景如下:我的应用程序依赖于某个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紙鸢 2024-08-13 08:12:59

如果您想使用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 and dllimport, or LoadLibrary, GetProcAddress and pointers to functions.

帅气尐潴 2024-08-13 08:12:59

(我在链接期间使用它的库)

如果您想使用 LoadLibraryGetProcAddress 手动加载它,那么您不应该传递它的 *.lib文件到您的链接器。

(I use it's lib during linkage)

If you want to load it manually using LoadLibrary and GetProcAddress then you shouldn't pass its *.lib file to your linker.

城歌 2024-08-13 08:12:59

您可以通过不链接 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.

淤浪 2024-08-13 08:12:59

你可以hook延迟加载机制。将 __pfnDliNotifyHook2 设置为您提供的函数,然后在该挂钩中简单地终止您的应用程序。

You can hook the delayload mechanism. Set __pfnDliNotifyHook2 to a function you provide, and in that hook simply terminate your application.

赠佳期 2024-08-13 08:12:59

延迟加载功能在第一次函数调用之前不会加载 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.

深爱成瘾 2024-08-13 08:12:59

这是您需要的吗: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文