在.NET中inheritanceExcel 2010时出现exception

我试图从VSTO加载项inheritanceExcel 2010的主窗口。 这会导致程序closures时发生exception并导致Excel崩溃。

于是我创build了一个小例子,在我的机器上重现错误。 当调用WM_CLOSE时,CallWindowProc的调用显然会抛出一个ThreadAbortExceptionexception。

Public Class ThisAddIn Private Const GWL_WNDPROC As Integer = -4 Private Delegate Function WndProcDelegate( _ ByVal hWnd As IntPtr, _ ByVal msg As Int32, _ ByVal wParam As Int32, _ ByVal lParam As Int32) As Int32 Private Declare Function SetWindowLong _ Lib "user32.dll" Alias "SetWindowLongA" ( _ ByVal hWnd As IntPtr, _ ByVal nIndex As Int32, _ ByVal dwNewLong As IntPtr) As Int32 Private Declare Function SetWindowLong _ Lib "user32.dll" Alias "SetWindowLongA" ( _ ByVal hWnd As IntPtr, _ ByVal nIndex As Int32, _ ByVal dwNewLong As WndProcDelegate) As IntPtr Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _ (ByVal lpPrevWndFunc As IntPtr, ByVal hWnd As IntPtr, ByVal msg As Integer, _ ByVal wParam As Integer, ByVal lParam As Integer) As Integer <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.FunctionPtr)> _ Private mWndProc As WndProcDelegate Private mPrevWindowProc As IntPtr Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup mWndProc = New WndProcDelegate(AddressOf SubWndProc) mPrevWindowProc = SetWindowLong(New IntPtr(Application.Hwnd), GWL_WNDPROC, mWndProc) End Sub Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown SetWindowLong(New IntPtr(Application.Hwnd), GWL_WNDPROC, mPrevWindowProc) End Sub Private Function SubWndProc( _ ByVal hWnd As IntPtr, _ ByVal msg As Int32, _ ByVal wParam As Int32, _ ByVal lParam As Int32) As Int32 Try Return CallWindowProc(mPrevWindowProc, hWnd, msg, wParam, lParam) Catch ex As Exception Windows.Forms.MessageBox.Show(ex.ToString) End Try End Function End Class 

这是发生在我的机器上还是我做错了什么?

顺便说一句,如果我在WM_CLOSE或WM_DESTROY事件中恢复WndProc没有任何区别,Excel崩溃都是一样的。

Excel 2010,Windows XP,VS 2008

Interesting Posts