如果没有DLL,如何防止“由于找不到xxx.dll而无法继续执行代码"错误 [英] How to prevent 'The code execution cannot proceed because xxx.dll was not found' error if DLL is not present

查看:70
本文介绍了如果没有DLL,如何防止“由于找不到xxx.dll而无法继续执行代码"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Delphi 10中开发了一个DLL,该DLL在某些旧版Delphi 6应用程序中使用.DLL中的新功能仅适用于少数几个客户端,因此不需要推广到我们的所有客户端.如果我们尝试在没有DLL的情况下部署Delphi 6应用程序,则会出现错误由于找不到xxx.dll,因此无法继续执行代码".应用开始运行时,我们会收到错误消息.如果DLL不存在,是否有办法防止此错误?在我们的Delphi 6代码中,我们已经使用FileExists(xxx.dll)来查看是否应该使DLL中的功能可用,因此,如果没有dll,我们就不会冒应用程序崩溃的风险.

We have developed a DLL in Delphi 10 that we use in some legacy Delphi 6 applications. The new functionality in the DLL is only for a few clients and is therefore not required to roll out to all our clients. If we try to deploy the Delphi 6 application without the DLL we get the error "The code execution cannot proceed because xxx.dll was not found.". We get the error the moment the app starts to run. Is there a way to prevent this error if the DLL does not exist? In our Delphi 6 code we already use FileExists(xxx.dll) to see if we should make the functionality in the DLL available therefore we don't have the risk that the application will crash if the dll is not present.

我们也很想知道Delphi 6应用程序在哪里/何时检查DLL是否存在,因为它发生在Application.Initialize(DPR文件中的第一行代码)之前.

We would also be keen to learn where/when the Delphi 6 app checks if the DLL exists because it happens before the Application.Initialize, which is the first line of code in the DPR file.

推荐答案

这种类型的故障是由

This type of failure is caused by statically (aka : implicitly) linking the DLL. There is no option if you have chosen to use static linking - the DLL must be present on the system trying to run the application.

有两种方法可以选择允许DLL存在.

There are two ways to allow the DLL to be optionally present.

一种方法是重写受影响的代码部分,以使用动态为您的DLL链接(又称​​ explicit ),而不是静态链接.对于Delphi 6,很遗憾,这是您的 only选项.

One is to rewrite the affected sections of code to use dynamic linking (aka : explicit ) for your DLLs instead of static linking. For Delphi 6, this is unfortunately your only option.

另一个选择(如果仅针对Windows的选项,,如果您使用的是Delphi 2010或更高版本,则是),方法是通过使用 delayed 指令:

The other, an option if you are targeting Windows only and if you are compiling with Delphi 2010 or greater, is to use delayed loading by decorating your import declarations with the delayed directive:

 function GetSomething: Integer; external 'somelibrary.dll' delayed;

这实际上只是动态加载之上的语法糖,但它确实为将静态链接的代码迁移到动态链接的模型提供了更简单的路径,而无需进行大量的重写.

This is really just syntactic sugar resting on top of what is otherwise dynamic loading, but it does provide an easier path to migrate statically linked code to a dynamic linked model without needing an extensive rewrite.

来自Embarcadero :

在运行该应用程序的目标操作系统上不存在导入的例程的情况下,延迟指令很有用.静态导入的例程要求操作系统在启动应用程序时查找并加载该库.如果在已加载的库中找不到该例程,或者该库不存在,则操作系统将停止执行该应用程序.使用Delayed指令使您能够在运行时检查操作系统是否支持所需的API.只有这样,您才能调用导入的例程.

The delayed directive is useful in the case where the imported routines do not exist on the target operating system on which the application is run. Statically imported routines require that the operating system find and load the library when the application is started. If the routine is not found in the loaded library, or the library does not exist, the Operating System halts the execution of the application. Using the delayed directive enables you to check, at run time, whether the Operating System supports the required APIs; only then you can call the imported routines.

注意:尝试调用无法解决的延迟例程会导致运行时错误(如果加载了SysUtils单元,则会导致异常).

Note: Trying to call a delayed routine that cannot be resolved results in a run-time error (or an exception, if the SysUtils unit is loaded).

无论是延迟加载还是动态加载,您都需要抓住无法解决DLL的问题,并优雅地拒绝用户访问DLL提供的任何功能.

In either case, delayed or dynamic loading, you would need to catch the failure to resolve the DLL and gracefully deny users access to whatever functions the DLL provides.

这篇关于如果没有DLL,如何防止“由于找不到xxx.dll而无法继续执行代码"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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