I’ve been battling this for a few hours now, and it may just be that I don’t understand reference assemblies as well as I should.
I have a .NET CF 1.0 application running on Windows CE 4.2/5.0.
There is a managed assembly that is part of the SDK for the device that I added as a reference to my project.
Most of the time, everything works great. I can call the methods and successfully modify the state of the device (e.g. set the keyboard state or dim the backlight).
On some devices, when my application hits the code that references the DLL, it just blows up on itself
My problem is that I can’t recover from this. The code is fully contained in a try/catch catching a generic Exception:
try
{
if (Terminal.API.UnitAPI.KbdGetKeyInputState() ==
(int)Terminal.API.KbdState.AlphaOn)
Terminal.API.UnitAPI.KbdSetKeyInputState
(Terminal.API.KbdState.AlphaDown);
}
catch (Exception e)
{
log("Error loading DLL: " + e.Message);
}
If anyone has any ideas on how to recover correctly from this, I would appreciate it. Thanks.
If your assembly A references another assembly B, then B is not actually loaded until the first method is JITed that references something in assembly B. If assembly B is not available, a runtime crash will occur while JIT-compiling your method, not when executing the call. This exception cannot be reliably caught, because it is not actually an exception.
If this referenced assembly may not be available on some platforms, you should instead load it via reflection. This mechanism will throw an exception that you can catch.