Detection of failures of called DLLs called from .NET.

I have some native DLLs (Delphi) that I call from a .NET application. If any type of failure occurs in the DLL, the entire .NET application crashes. How can I prevent this and ensure that the .NET application does not crash and does not recognize the error. I do not need a specific error-feedback from the native DLL (which is impossible, since it crashed). But I want to write something to my .NET log files, for example "xyz.dll crashed".

Is this possible for P / calling a dll (Delphi or C ++) from .NET?

+4
source share
2 answers

This will happen if your DLL has thrown exceptions from the exported functions. A DLL should not do this, so you need to catch all the exceptions at the border of your DLL and convert them to error code return values.

If you do not have control over the DLL, you will need to write a wrapper layer. Create another native DLL that calls the source DLL and catches all the exceptions that it throws. You could do this with another Delphi DLL, I suspect, or, alternatively, with a C or C ++ DLL that used SEH to catch exceptions.

It would be possible for a script to generate code for such a DLL wrapper. You would like to do this if there were many functions that you needed to wrap.

+3
source

All entry points to the Delphi DLL should be placed in try ... except block and return a value (int) to the .NET side, indicating whether the call was completed with or without errors.

+3
source

Source: https://habr.com/ru/post/1484413/


All Articles