1

I am developing an Optical character recognition software using WPF. I have third party hardware that does this ocr thing. I also have the device driver provided from the manufacture. I have referenced the device driver's dll in my project and I call one of its method to convert image to text. For example, I can put my passport on the device and I get array of information from the passport. The function works as expected but sometimes the application closes without any exception thrown. This is due to the device driver dll as seen from the window event logs. There is an unhandled exception in the dll that closes the application suddenly.

I want my WPF application to run no matter the whatever be the exception thrown by the dll file.

I have globally handled error in the app.cs file

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    var dialogContent = "Exception Message:" + "\n" + e.Exception.Message +
                        "\n" + "Stack Trace:" + "\n" + e.Exception.StackTrace;
    MessageBox.Show(dialogContent);

    e.Handled = true;
}

I have also added tried to add these attributes on my method.

[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
public void ConvertImagetoText()
{
    try
    {
         //do something from the dll file.
    }
    catch
    {
         throw;
    }
}
demo
  • 6,038
  • 19
  • 75
  • 149
Anuj Tamrakar
  • 81
  • 2
  • 9
  • Does wpf application close without any interaction or during the recognition? – Rekshino Mar 21 '19 at 09:44
  • during the recognition mostly when i do not put the passport properly on the device – Anuj Tamrakar Mar 21 '19 at 09:45
  • If the DLL is leaking a native exception there's nothing you can do. The DLL is broken. The best option if you absolutely can't get the developer to fix it is to wrap the DLL yourself - catch the exception on the native side and write a sane interface for the managed consumer. – J... Mar 21 '19 at 09:48
  • @Rekshino `try/catch` won't catch native exceptions leaked from an unmanaged module. – J... Mar 21 '19 at 09:48
  • Is it a way to run the call in a separate thread? – Rekshino Mar 21 '19 at 09:49
  • @J... can you give an example to handle the native exception. – Anuj Tamrakar Mar 21 '19 at 09:59
  • Read [this question](https://stackoverflow.com/questions/3312003/is-it-possible-to-catch-an-access-violation-exception-in-net) and its answers. However, you have no chance if the SEH or the CSE native exception will be raised on a thread spawned in that buggy library or on the Finalizer thread. The .NET runtime will just crash, so your app too. Nothing you can do here. – dymanoid Mar 21 '19 at 10:08
  • By *"on the native side"*, I mean you would need to write your own wrapper in something like C++, then consume that wrapper in C#. How to handle exceptions in C++ is well documented elsewhere. As @dymanoid noted, though, if the exception is leaking from a background thread then there is probably nothing you can realistically do other than to press the DLL's developer to fix their product. – J... Mar 21 '19 at 11:01
  • this didnot do anything either. :( – Anuj Tamrakar Mar 21 '19 at 11:25

0 Answers0