How to avoid .net program startup failure when DLL is not registered?
I have a .NET 2.0 program that has a link to Interop.WIA.dll (.NET) that requires the wiaaut.dll library to register with the system.
If my program is running on an OS where wiaaut.dll is not registered (like a fresh install of Windows XP), the program starts at startup.
I've included all the code mostly in a try / catch block, but no exception is thrown. Is there a way to handle this?
source to share
If your main method, or static fields in the class that contains your main method, reference types from a missing DLL, an exception will be thrown during JITting before any code in your main method is executed.
The best solution is to move all references to the DLL in question to a different class. This way no reference is needed to JIT your main method and your try / catch will work.
Something like:
class Program
{
public static void Main()
{
try
{
MyClass.AccessMissingDll();
}
catch(...)
{
...
}
}
}
class MyClass
{
public static AccessMissingDll()
{
... access types in your missing DLL here
}
}
source to share
You can try to catch and handle events AppDomain.UnhandledException
or Application.DispatcherUnhandledException
.
AppDomain.UnhandledException
http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
Application.DispatcherUnhandledException
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx
You can add an event to your app WPF
like this.
protected override void OnStartup(StartupEventArgs e)
{
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
base.OnStartup(e);
}
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// Generate Error message...
// Prevent default unhandled exception processing
e.Handled = true;
}
Dispatcher.UnhandledException
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.unhandledexception.aspx
source to share