启动应用程序后立即加载动态链接的DLL [英] Dynamically linked DLL is loaded immediately after starting the application

查看:157
本文介绍了启动应用程序后立即加载动态链接的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 libhunspell.dll (HunSpell)动态链接到我的应用程序.它可以工作,但是有一个愚蠢的问题,我不知道为什么会发生.

I've dynamically linked libhunspell.dll (HunSpell) to my application. It works, but there is a dumb problem which I don't know why it happens.

甚至在我使用LoadLibrary("path\\to\\libhunspell.dll");加载和使用它之前,它就在应用程序启动时尝试自行加载该库.如果将 libhunspell.dll 放入我的主要可执行文件所在的路径中,则可以加载它,否则在启动应用程序后立即报告错误-此应用程序无法启动,因为找不到LIBHUNSPELL.DLL.重新安装该应用程序可能会解决此问题.,并且该应用程序无法启动.

Even before I use LoadLibrary("path\\to\\libhunspell.dll"); to load it and use it, on the start of the application it attempts to load the library by itself. If I place the libhunspell.dll into the path where my main executable resides, it can load it, otherwise it reports an error, immediately after starting the application - This application has failed to start because LIBHUNSPELL.DLL was not found. Re-installing the application may fix this problem. and the application doesn't start.

我会理解LoadLibrary是否会使用无效路径,但这会在可执行文件运行后立即发生,甚至在WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)中的第一条语句执行之前(我试图放置一个断点,它甚至没有达到它,所以这发生在之前.)

I would understand if the LoadLibrary would use invalid path but this happens as soon as the executable runs, even before the first statement in WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) executes (I've tried to place a breakpoint and it doesn't even reach it, so this happens before).

因此,结果,我必须将 libhunspell.dll 放置在与我的应用程序可执行文件相同的文件夹中,而不是我想要的路径中.

So, as a result, I must place libhunspell.dll in the same folder as my application executable, and not in the path I want.

尽管我不需要寻找什么,这可能很容易解决.

This is probably easy to fix although I don't what to look for.

所以问题是-如何避免立即加载它并让它等到我使用LoadLibrary通话?

So the question is - how do I avoid it loading it immediately and have it wait until I use LoadLibrary call?

如果可以帮助的话,这是我的链接方式:

Here is how I linked if it can help:

1)在Visual Studio 2015中编译了libhunspell.dll(我使用/MT选项进行静态链接,因此它没有VC ++可再发行组件作为依赖项).

1) compiled libhunspell.dll in Visual Studio 2015 (I used /MT option to link it statically so it doesn't have VC++ Redistributable as a dependency).

2)使用implib.exe -a -c -f libhunspell.lib libhunspell.dll

3)使用#pragma comment(lib, "libhunspell.lib")将其链接到正在使用它的源.cpp单元(它是RAD Studio 2010,因此与较新版本不同,需要.lib).

3) linked that to the source .cpp unit which is using it using #pragma comment(lib, "libhunspell.lib") (it is RAD Studio 2010 so the .lib is required unlike newer versions).

4)稍后在同一.cpp中使用LoadLibrary加载并使用了该库.

4) later in the same .cpp used LoadLibrary to load this library and used it.

推荐答案

通过链接导入存根(libhunspell.lib),操作系统将为您加载DLL,因为它现在是 static 依赖项

By linking in the import stubs (libhunspell.lib) the OS will load the DLL for you as it is now a static dependency.

一种方法是将库指定为延迟加载依赖项:/DELAYLOAD:libhunspell.lib通过链接器选项.然后,您可以在DLL上调用LoadLibrary.

One approach would be specify the library as a delayload dependency: /DELAYLOAD:libhunspell.lib via the linker options. You can then call LoadLibrary on the DLL.

唯一的选择是停止在链接器步骤中包含.lib,使其真正成为动态依赖项.

The only other option is to stop including the .lib in the linker step, making it truly a dynamic dependency.

这篇关于启动应用程序后立即加载动态链接的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆