我是一个应用程序的子类。我的子类窗口过程是在一个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