我想在自己封装的dll中所有函数当某一个function有异常时,都会类似于有Application.OnException 的效果。主动通知我的dll的一个异常处理的function.
进行相关的异常处理。我不想在每个function里都加上
try
except
//异常处理的function
end
这样去做。这样太低效了。 dll是否也有一个可以指向自己定义异常处理函数的指过程过针呢。分不够可以再加。谢了。

解决方案 »

  1.   

    Creating a global exception handler is no problem as the code fragment in the example below shows.
    It is to my knowledge not possible to retrieve the line number and/or unit name in which the exception occured.
       
     // application-global exception handlertype
      TForm1 = class(TForm)
        procedure MyHandler(Sender: TObject; E : Exception);
        procedure FormCreate(Sender: TObject);
      end;procedure TForm1.MyHandler(Sender: TObject; E : Exception);
    begin
      // E.message is of type string
      toLogfile('Exception: ' + E.message);
      // either close the application or eat the exception
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnException := MyHandler;
    end; 
      

  2.   

    to jiangsheng(蒋晟.Net[MVP]) 
    我写的dll是由C#程式去调用,并不是由我自己写一个程式去调用dll,并且我不能要求C#的程式员去将C#的程式的异常句柄指到我dll里的处理function
    procedure Dll.MyHandler(Sender: TObject; E : Exception);
    begin
      // E.message is of type string
      toLogfile('Exception: ' + E.message);
      // either close the application or eat the exception
    end;我的dll只是C#程式中的某一个部分。
      

  3.   

    把父窗口的Application赋给dll的Application应该可以,没有试过。
      

  4.   

    没有把,dll又没有事件处理函数,都是一个一个独立的等待调用的函数互相可以没有任何关系
      

  5.   

    var ExceptProc: Pointer;DescriptionIn Delphi code, ExceptProc handles unhandled exceptions (exceptions that have not been caught in the except portion of a try..except statement.) If your application has a TApplication object, it automatically catches exceptions and generates an OnException event (to which you can respond using either the application's or a TApplicationEvents object's OnException event handler). Thus, because the TApplication object catches the exceptions, they never reach the ExceptProc handler. However, even if you have a TApplication object, it does not catch exceptions generated during initialization and finalization. These are handled by ExceptProc.You can hook into ExceptProc to change how unhandled exceptions are reported. To do so, save the contexts of ExceptProc before changing it to the address of your own exception handler. The first statement of your new exception handler must then reinstall the saved value of ExceptProc. In this way, the ExceptProc procedure becomes part of a chain of exception procedures, each of which is executed in reverse order of installation.The procedure assigned to ExceptProc must have a signature such as the following:
    procedure ExceptHandler(ExceptObject: TObject; ExceptAddr: Pointer);