Search code examples
delphipackagedelphi-2007

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


I'm using SysUtils.LoadPackage function in order to load dynamic packages. Sometimes, it can happen that some packages are not synchronized and an error like this appears (Picture taken from internet):

enter image description here

In those cases, I would like to manage the exception avoiding the error dialog. Unfortunately, I noticed that the message is shown in the LoadPackage procedure. Is there a workaround or another function that doesn't raise error dialogs?


Solution

  • You can suppress this dialog which is raised by the system rather than Delphi.

    First of all you need to set the process error mode to suppress the Windows dialog that you have shown in your question. Do that at program startup by calling this function:

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

    The error mode defaults to showing dialogs for critical errors for reasons of backwards compatibility with ancient versions of 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.

    The code above does exactly that. And suppresses the dialog shown in the question.

    Then when you call LoadPackage you need to catch EPackageError exceptions that are raised and deal with them however you choose. These EPackageError exceptions are the Delphi runtime's way of telling you that the call to LoadPackage failed.

    Reading between the lines, I'd guess that you already handle these exceptions and the error mode setting is all that you need to do.