This case require you to enable exception tracer for host application. You should do this in the same way as you do it for typical application without any DLLs. For example, if you have VCL Forms application as the host - then you need to enable EurekaLog for host application and set project type to "VCL Forms Application". This will add EurekaLog code and data into final .exe file. It would also set hook for Forms.TApplication.HandleException method, which will allow to automatically handle exceptions without writing any code.
Now, the host has exception tracer. The host must be configured with different settings depending on your API design:
Each DLL must also has EurekaLog enabled and project type must be set to "Lightweight DLL". Such settings will inject debug information into DLL, but will not include exception tracer code. Rather DLL will ask host application for information. Please note that majority of EurekaLog settings will be ignored for DLL, since there will be no EurekaLog's code in your DLL.
Note: it's not strictly necessary to enable EurekaLog for DLLs in this example. You can just supply debug information and keep EurekaLog off. For example, DLLs can:
Host application loads multiple DLLs with "Lightweight DLL" profile
Let's see this on practice. Create a new VCL application and place buttons to invoke functions from DLL.
...
Code is pretty simple: we load DLL on form's creating and unload it when form is gone. There are 3 buttons on the form to invoke different testing routines. First button raises exception in application itself. The 2nd and 3rd buttons raise exceptions in DLL.
Don't forget to enable EurekaLog for host and set project type to "VCL Forms Application". That's all.
Now, create a new DLL project:
library Project2; Windows, EBase;
This simple DLL exports 2 functions to test exceptions. First function raises exception and lets it escape DLL, so it will be catched by caller. In our test example caller would be the host application. Such approach is not recommended - as it's already explained above: you should never let exceptions escape DLL. This is done only as example (illustration). It will work correctly for our case, because DLL and exe are both compiled by exactly the same compiler. This may not work properly for a generic case. See this article for more info.
Second function illustrate more correct approach: we catch exceptions in the function and handle exceptions. For this example we will do very simple handling. More proper approach was discussed above: you should use some kind of error indication (such as boolean flag, HRESULT, etc.) to indicate failure condition to the caller.
Now, enable EurekaLog for this DLL and set project type to "Lightweight DLL".
Note: EAppDLL unit will be added automatically when you set profile to "Lightweight DLL". This unit contains default callbacks into host to ask for exception's info and handling. You may use your own custom callbacks instead.
Compile both host and DLL project, run host application.
Hit buttons 1-3.
Typical exception in host application Call stack shows only items for exe
Exception escaped DLL Call stack shows mixed exe/DLL lines Notice line numbers for routine in DLL
Exception did not escape DLL, it was handled by DLL Call stack shows mixed exe/DLL lines Notice line numbers for routine in DLL
Please note: while results for Test1 and Test2 looks very similar, but it is important to understand that only second example (Test2) is safe. Test1 is not safe, because exception object will cross module boundary.
See also:
|