error in Delphi loadlibrary()

前端 未结 1 1455
小鲜肉
小鲜肉 2021-01-21 02:08

i have given a chance to my software user to select dll from openfile dialog.(so my user can download dlls form my website and use it with the main project ). everything is work

相关标签:
1条回答
  • 2021-01-21 02:45

    There's no exception to catch because an exception is not raised when LoadLibrary fails; it just returns '0'.

    You should check if 'dllHandle' is 0 or not, if it is, show the error information to the user by using GetLastError as documented. Alternatively you can use the Win32Check function in the RTL which will raise an exception with the appropriate error message:

    (edit: Documentation of 'LoadLibrary' states that: To enable or disable error messages displayed by the loader during DLL loads, use the SetErrorMode function. So if you don't want the OS to show an additional dialog you'd set the error mode before calling LoadLibrary.)

    var
      dllHandle: HMODULE;
      ErrorMode: UINT;
    begin
      if OpenDialog1.Execute then begin
        ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS); // disable OS error messages
        try
          dllHandle := LoadLibrary(PChar(OpenDialog1.FileName));
        finally
          SetErrorMode(ErrorMode);
        end;
        if Win32Check(Bool(dllHandle)) then begin  // exception raised if false
          // use the libary
    
          end;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题