我有一个 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 中没有任何内容,它也会崩溃(如上所示)。如果我不设置钩子,它永远不会崩溃。
有任何想法吗?