Recover From Access Violation Exception

2019-09-16 01:51发布

问题:

I am using EZTwain (an unmanaged dll) to process some PDF's. I am aware it can only read PDF's that it has created. Unfortunately in the real world it is encountering PDFs which have been created by other libraries. This causes it to fail and throw an AccessViolationException.

This in itself is not a huge problem as my C# app continues on its way. Unfortunately when it next comes to this file and tries to reprocess it, it is still locked. Presumably EZTwain is still loaded...in what state i don't know - but has been left with the file lock.

Is there anyway i can force the DLL to unload and so release the lock on the file (P/Invoke calls or otherwise)?

If i can achieve this i can then skip use of EZTwain the second time around.

Cheers

[UPDATE]

I have tried calling LoadLibrary, catching the access violation exception and then FreeLibrary but my file is still locked.

I've checked with Process Explorer: the DLL is definitely being unloaded but the file remains locked...how come?

(Looks like i will have to go with the app domain or separate Exe suggestion)