首页 > delphi > 在delphi中调用DLL时出现异常?

在delphi中调用DLL时出现异常? (Exception when calling DLL in delphi?)

问题

我有一个从DLL调用名为[main()]的函数的过程,这是调用者过程:

procedure call_dll(path:string);
var
  lib: HMODULE;
  mainfn: procedure(); stdcall;
begin
if FileExists(path) then
 begin
   try
     lib := LoadLibrary(PAnsiChar(path));
     Win32Check(lib <> 0);
     try
       @mainfn := GetProcAddress(lib, 'main');
       Win32Check(Assigned(mainfn));
       mainfn();
     finally
       FreeLibrary(lib);
     end;
   except
   end;
 end;
end;

直到所有东西都工作正常,我的意思是在写完DLL的正确路径之后一切正常工作但没有问题但是如果我在路径参数中写了一个错误的路径(其他文件路径)它会向我显示一个弹出错误,这是不是一个Win32 DLL,但我不想用这种类型的错误打扰用户,所以我需要一个函数检查DLL,如果不是那么它会自动再次询问另一个文件而不显示弹出错误?

解决方法

正是您的代码引发了异常。您的代码通过引发异常来明确选择处理错误。您的代码在此处引发异常:

Win32Check(lib <> 0);

如果您不想引发异常,请不要使用Win32Check。而是lib通过您认为合适的方式检查值并处理任何错误。

您的其他用途也存在同样的问题Win32Check

当然,您正在使用catch all exception handler处理器吞下所有异常。捕获所有异常处理程序通常是个坏主意。我不明白为什么你把它包括在内。我想你应该把它删除。

因此,如果您在调试器外部运行时看到对话框,则表示系统正在生成对话框。您应该通过调用SetErrorMode启动传递来禁用系统的错误消息对话框SEM_FAILCRITICALERRORS

var
  Mode: DWORD;
....
Mode := SetErrorMode(SEM_FAILCRITICALERRORS);
SetErrorMode(Mode or SEM_FAILCRITICALERRORS);

这里有点笨重的双重调用:http//blogs.msdn.com/b/oldnewthing/archive/2004/07/27/198410.aspx

问题

I have a procedure to call a function named [main()] from a DLL , this is the Caller procedure :

procedure call_dll(path:string);
var
  lib: HMODULE;
  mainfn: procedure(); stdcall;
begin
if FileExists(path) then
 begin
   try
     lib := LoadLibrary(PAnsiChar(path));
     Win32Check(lib <> 0);
     try
       @mainfn := GetProcAddress(lib, 'main');
       Win32Check(Assigned(mainfn));
       mainfn();
     finally
       FreeLibrary(lib);
     end;
   except
   end;
 end;
end;

Until yet every thing is working fine , I mean after writing the correct path of the DLL everything work without a problem but if I write a wrong path (other file path) in the path parameter it show me a popup error that this is is not a Win32 DLL but I don't want to bother the user with this type of errors , so I need a function to check the DLL and if it's not then it will automatically ask for another file again without showing the popup error ?

解决方法

It is your code that is raising the exception. Your code makes an explicit choice to handle errors by raising exceptions. The exception is raised by your code here:

Win32Check(lib <> 0);

If you don't want to raise an exception, don't use Win32Check. Instead check the value of lib and handle any errors by whatever means you see fit.

The same issue is present for your other use of Win32Check.

Of course you are swallowing all exceptions with your catch all exception handler. A catch all exception handler is usually a bad idea. I don't understand why you have included that. I think you should remove it.

So if you are seeing dialogs when running outside the debugger it follows that the system is producing the dialogs. You should be disabling the system's error message dialogs by calling SetErrorMode on startup passing SEM_FAILCRITICALERRORS.

var
  Mode: DWORD;
....
Mode := SetErrorMode(SEM_FAILCRITICALERRORS);
SetErrorMode(Mode or SEM_FAILCRITICALERRORS);

The somewhat clunky double call is explained here: http://blogs.msdn.com/b/oldnewthing/archive/2004/07/27/198410.aspx

相似信息