我有一個.NET 2.0程序,它具有對Interop.WIA.dll(.NET)的引用,它需要在系統中註冊庫wiaaut.dll。如何避免.net程序在dll未註冊時啓動崩潰?
如果我的程序運行在未註冊wiaaut.dll的操作系統(例如新的Windows XP安裝),則該程序在啓動時崩潰。
我已經在try/catch塊中包含了main中的所有代碼,但沒有引發異常。有什麼辦法可以解決這個問題嗎?
我有一個.NET 2.0程序,它具有對Interop.WIA.dll(.NET)的引用,它需要在系統中註冊庫wiaaut.dll。如何避免.net程序在dll未註冊時啓動崩潰?
如果我的程序運行在未註冊wiaaut.dll的操作系統(例如新的Windows XP安裝),則該程序在啓動時崩潰。
我已經在try/catch塊中包含了main中的所有代碼,但沒有引發異常。有什麼辦法可以解決這個問題嗎?
如果包含您的main方法的類的主要方法,或靜態字段,從缺少DLL引用類型,異常將被拋出時在執行主方法中的任何代碼之前進行JITting。
最好的解決方案是將所有引用問題的DLL移動到不同的類。通過這種方式,引用不需要JIT你的主要方法,並且你的try/catch可以工作。
喜歡的東西:
class Program
{
public static void Main()
{
try
{
MyClass.AccessMissingDll();
}
catch(...)
{
...
}
}
}
class MyClass
{
public static AccessMissingDll()
{
... access types in your missing DLL here
}
}
您可以嘗試捕獲並處理AppDomain.UnhandledException
或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
你可以在這樣的WPF
應用程序添加事件。
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