我是一個應用程序的子類。我的子類窗口過程是在一個DLL中。我在DLL中的子類代碼看起來有點像這樣(剝離下來,刪除了其他不相關的部分)。CallbackOnCollectedDelegate檢測到
class FooBar
{
private delegate int WndProcDelegateType(IntPtr hWnd, int uMsg,
int wParam, int lParam);
private const int GWL_WNDPROC = (-4);
private static IntPtr oldWndProc = IntPtr.Zero;
private static WndProcDelegateType newWndProc = new
WndProcDelegateType(MyWndProc);
internal static bool bHooked = false;
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex,
WndProcDelegateType dwNewLong);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex,
IntPtr dwNewLong);
[DllImport("user32")]
private static extern int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd,
int Msg, int wParam, int lParam);
private static int MyWndProc(IntPtr lhWnd, int Msg, int wParam, int lParam)
{
switch (Msg)
{
// the usual stuff
// finally
return CallWindowProc(oldWndProc, lhWnd, Msg, wParam, lParam);
}
internal static void Hook()
{
oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, newWndProc);
bHooked = oldWndProc != IntPtr.Zero;
}
internal static void Unhook()
{
if (bHooked) SetWindowLong(hWnd, GWL_WNDPROC, oldWndProc);
}
}
現在,即使我在委託的類級靜態實例變量持有的強引用WndProc中,我得到這個錯誤。
CallbackOnCollectedDelegate檢測
消息:回調被上型的 垃圾收集代表 '!PowerPointAddIn1 FooBar的+ WndProcDelegateType ::調用' 製成。 這可能會導致應用程序崩潰, 損壞和數據丟失。當通過 代表非託管代碼,他們必須保持 活着的託管應用程序,直到 它保證,他們將永遠不會調用 。
我在做什麼錯?
謝謝你的幫助,你的回答是正確的,但我仍然得到例外。錯誤的代碼在發佈這個問題之前,我已經做了修改,我在兩個地方發佈了代碼,併發布了以前的代碼,我沒有改變,但我仍然得到這個例外 – 2011-01-31 20:37:39