0

我有一个 C# BHO,它设置了一个钩子,以便我可以跟踪 IE 何时获得焦点:

public class BHO : IObjectWithSite, IOleCommandTarget
{
    delegate void WinEventDelegate(IntPtr hWinEventHook,
       uint eventType, IntPtr hwnd, int idObject,
       int idChild, uint dwEventThread, uint dwmsEventTime);

    const uint WINEVENT_OUTOFCONTEXT = 0;
    const uint EVENT_SYSTEM_FOREGROUND = 3;

    [DllImport("user32.dll")]
    static extern bool UnhookWinEvent(IntPtr hWinEventHook);
    [DllImport("user32.dll")]
    static extern IntPtr SetWinEventHook(uint eventMin,
        uint eventMax, IntPtr hmodWinEventProc,
        WinEventDelegate lpfnWinEventProc, uint idProcess,
        uint idThread, uint dwFlags);

   private static IntPtr m_hhook; 


       ............

        m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
           EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
           WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT);



       ............


   void WinEventProc(IntPtr hWinEventHook, uint eventType,
             IntPtr hwnd, int idObject, int idChild,
             uint dwEventThread, uint dwmsEventTime)
    {

    }

它工作了一段时间,然后 IE 崩溃并显示“尝试读取或写入受保护的内存。这通常表明其他内存已损坏”

请注意,即使 WinEventProc 中没有任何内容,它也会崩溃(如上所示)。如果我不设置钩子,它永远不会崩溃。

有任何想法吗?

4

1 回答 1

2

您的回调委托可能会被 GC 处理。而不是使用自动委托创建功能,而是创建一个显式的,将其传递给 SetWinEventHook,然后将其保留在实例成员中,直到您完成它。

代码可能类似于:

// In the class...
WinEventDelegate m_wineventprocDelegate;


// When you register the hook...
m_wineventprocDelegate = new winEventDelegate(WinEventProc);

// Pass in the delegate instead of WinEventProc
m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
       EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
       m_wineventprocDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);

让成员引用代表可以防止它被收集。

于 2012-08-29T18:12:12.383 回答