error in Delphi loadlibrary()

北城余情 提交于 2019-12-01 21:17:00

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!