Recent

Author Topic: Intercepting exceptions in a dll  (Read 1042 times)

DenReq

  • New member
  • *
  • Posts: 8
Intercepting exceptions in a dll
« on: May 11, 2022, 03:10:31 pm »
Hello,

I hesitated to put this post in the "beginners" section.

I am trying to set up a Win32 DLL to intercept all the various exceptions and errors that can occur, including in threads.

Currently I'm failing completely, and I think my inexperience with FPC is most likely the cause. Something that seems pretty basic to me like setting up an ErrorProc procedure, which is supposed to convert a call to RunError into an exception, doesn't work at all: the set up function is obviously not called and the program execution abruptly terminates. This seems to me to be a sign that I'm missing something as big as a house.

The minimal code is here. To me the call to TestExceptionRunError from the host application should produce an exception intercepted by the try except block, not terminate immediately. Am I making a mistake?

Code: Pascal  [Select][+][-]
  1. Library FPCDLL;
  2.  
  3. uses SysUtils;
  4.  
  5. procedure TestExceptionRunError; stdcall; export;
  6.  
  7.    begin
  8.    try
  9.       RunError(123);
  10.    except
  11.    end;
  12.    end;
  13.  
  14. Procedure RunErrorToExcept (ErrNo : Longint; Address : CodePointer; Frame : Pointer);
  15.  
  16.    begin
  17.    raise Exception.Create('Run error '+IntToStr(ErrNo));
  18.    end;
  19.  
  20. exports
  21.    TestExceptionRunError;
  22.  
  23. begin
  24. ErrorProc:=@RunErrorToExcept;
  25. end.
  26.  

I call FPC compiler using the following FPC.cfg file for options.
Code: Pascal  [Select][+][-]
  1.  -Twin32
  2.  -ve
  3.  -S2
  4.  -Sc
  5.  -Se
  6.  -Sg
  7.  -Sm
  8.  -Sd
  9.  -FeErrPrg.Log
  10.  -Ci-
  11.  -FuC:\FPC\3.2.2\units\i386-win32\fcl-base
  12.  -gl
  13.  -Rintel
  14.  -MDelphi
  15.  

Could someone possibly give me an example of this kind of thing? I've seen that exception handling in a DLL is a boring topic, but I must not be the first one to break my teeth on this ?

Thanks in advance.
« Last Edit: May 11, 2022, 04:03:21 pm by DenReq »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Intercepting exceptions in a dll
« Reply #1 on: May 12, 2022, 10:13:11 am »
Some points:

  • you don't need to set ErrorProc, because the default handler provided by the SysUtils unit already converts all run errors
  • you don't need to declare your TestExceptionRunError as export as you have it in the exports clause
  • your assumptions about the RunError function are wrong: it does not use the exception handling hooks, but always terminates; normal run errors (e.g. triggered by I/O errors) go through the HandleError*-family of functions (and not RunErrror) which are handled by the exception handling hooks and are not exported from the System unit (so to test this you need to trigger a run error some other way)

DenReq

  • New member
  • *
  • Posts: 8
Re: Intercepting exceptions in a dll
« Reply #2 on: May 16, 2022, 09:18:01 am »
Hello PascalDragon,

Thank you for the reply and the informations.

Regards

 

TinyPortal © 2005-2018