如何避免为 SysUtils.LoadPackage 函数引发 "the procedure entry point could not be located in the dynamic link library" 消息

How to avoid "the procedure entry point could not be located in the dynamic link library" message raised for SysUtils.LoadPackage function

我正在使用 SysUtils.LoadPackage 函数来加载动态包。 有时候会出现一些包没有同步,出现这样的错误(图片来自网络):

在那些情况下,我想管理异常以避免出现错误对话框。 不幸的是,我注意到消息显示在 LoadPackage 过程中。 是否有不会引发错误对话框的解决方法或其他功能?

您可以抑制由系统而不是 Delphi 引发的此对话框。

首先,您需要设置进程错误模式以抑制您在问题中显示的 Windows 对话框。通过调用此函数在程序启动时执行此操作:

procedure SetProcessErrorMode;
var
  Mode: DWORD;
begin
  Mode := SetErrorMode(SEM_FAILCRITICALERRORS);
  SetErrorMode(Mode or SEM_FAILCRITICALERRORS);
end;

出于与旧版本 Windows 向后兼容的原因,错误模式默认显示严重错误的对话框。 Microsoft say:

Best practice is that all applications call the process-wide SetErrorMode function with a parameter of SEM_FAILCRITICALERRORS at startup. This is to prevent error mode dialogs from hanging the application.

上面的代码正是这样做的。并抑制问题中显示的对话框。

然后当您调用 LoadPackage 时,您需要捕获引发的 EPackageError 异常并根据您的选择进行处理。这些 EPackageError 异常是 Delphi 运行时告诉您对 LoadPackage 的调用失败的方式。

从字里行间看,我猜您已经处理了这些异常,您只需设置错误模式即可。