在使用 DLL 之前先确定它是否存在
使用 Visual C++ 2008 Express 版。我正在将我的应用程序与目标系统上可能存在或不存在的 DLL 的导入库 (.lib
) 链接。在您提问之前:我无法随我的应用程序分发 DLL。
如果 DLL 不存在,一旦我从 DLL 调用函数(但不会更早!),我就会收到类似以下的消息
此应用程序无法启动,因为找不到 SomeLibrary.dll。重新安装应用程序可能会解决此问题。
我想要发生的是,应用程序检测到 DLL 不存在,并且简单地禁用依赖于它的功能。我可以调用 LoadLibrary
并查看它是否成功,但我不确定这是否足够。也许导入库在幕后做了更多的工作?
简单的 LoadLibrary
调用就足够了吗?如果没有,我还需要做什么?这还可以吗?
更新:当然,我可以使用LoadLibrary
,然后为我想要使用的每个函数使用GetProcAddress
。但这很麻烦,我希望避免这种情况,只需使用提供的导入库即可。
Using Visual C++ 2008 Express Edition. I'm linking my application with an import library (.lib
) for a DLL that might or might not be present on the target system. Before you ask: I cannot distribute the DLL with my application.
If the DLL is not present, as soon as I call a function from the DLL (but not sooner!), I get a message like
This application has failed to start because SomeLibrary.dll was not found. Re-installing the application may fix this problem.
What I want to happen instead, is that the application detects that the DLL is not there, and simply disables functionality that depends on it. I could make a call to LoadLibrary
and see whether it succeeded, but I'm not sure whether this is sufficient. Maybe the import library does more work behind the scenes?
Is a simple LoadLibrary
call sufficient? If not, what else do I need to do? Can this even be done?
Update: Of course I can use LoadLibrary
, and then GetProcAddress
for each of the functions I want to use. But that's a hassle, and I was hoping to avoid that and simply use the provided import library instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是 DelayLoad 链接器选项的用途,但我不知道 Express 版本是否支持它。
This is what the DelayLoad linker option is for, but I don't know whether the Express edition supports it.
不,
LoadLibrary()
正是您想要的。使用它的唯一后果是,当您成功加载 DLL 时,将函数指针设置到 DLL 中会很麻烦,但该过程在网络上的其他地方已有详细介绍。No,
LoadLibrary()
is exactly what you want. The only consequence to using it is the hassle of setting up the function pointers into the DLL when you successfully load the DLL, but the process for that is well-covered elsewhere on the net.如果您去查看此处 (MSDN)将会看到,当
LoadLibrary
加载失败时,该函数返回 NULL 值,您甚至可以使用GetLastError
函数检查特定错误(应该是未找到文件)。If you go check here (MSDN) you will see that when
LoadLibrary
fails loading, the function returns a NULL value, and you can even check the specific error (that should be a file not found) usingGetLastError
function.