0

I've got a bit of a problem with my application using MSHTML. I have everything working except for the odd keystroke missing behavior when typing fast as mentioned in the subject line. I think it may have to do with the method I use to sink the events?

The details: my application is a separate program written in C++ and MFC in Visual Studio 2005. The program attaches to a currently running (independent) instance of Internet Explorer and gets the pointer to the IWebBrowser2 interface and passes it to an object of type CCmdTarget:


class CHandler : public CCmdTarget
{
  IWebBrowser2* m_pWebBrowser2;
  DWORD m_dwBrowserCookie;
  …
  DECLARE_DISPATCH_MAP()
};

This class keeps track of what's happening in the browser. I sink the browser events with the following command:


LPUNKNOWN pUnkSink = GetIDispatch(FALSE);
retval = AfxConnectionAdvise((LPUNKNOWN)m_pWebBrowser2, DIID_DWebBrowserEvents2, pUnkSink, FALSE, &m_dwBrowserCookie);

If I comment out the AfxConnectionAdvise, then no keystrokes are missed but no more events. If I leave it in I sink the events but miss the occasional keystroke if typing fast.

I know there are a number of ways of connecting to the events (AtlAdvise, connection points), but this was the only one I could get working.

Any suggestions would be great!

CinCout
  • 9,486
  • 12
  • 49
  • 67
Brian
  • 11
  • 4

2 Answers2

0

If you're just looking for keystrokes, can't you subclass the control?

Valentin Galea
  • 1,084
  • 8
  • 18
  • I don't think the OP wants keystrokes; I think they want webbrowser events and just want keystrokes to work without dropping characters. – i_am_jorf May 16 '09 at 18:12
  • That's correct, I'm not really interested in the keystrokes. But when typing fast some get dropped which is annoying as a user. I'm fairly certain it's because of the way I sink the events, because not sinking them fixes the problem. – Brian May 21 '09 at 15:12
0

Underneath the covers all the various connection methods (AtlAdvise, AfxConnectionAdvise, etc) all use IConnectionPointContainer and IConnectionPoint — they're just saving you typing boilerplate COM goo.

I suspect this has to do with how you're connecting to the running instance of IE. How are you getting the IWebBrowser2 pointer? Are you being loaded into the IE process or are you a separate process? If you're running on a different thread than the IWebBrowser2's original thread (the IE Tab UI thread), are you doing proper COM marshalling?

Mykola
  • 3,343
  • 6
  • 23
  • 39
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • I'm running in a separate thread. I use SHDocVw::IShellWindowsPtr to access the running instance of IE. To get the browser pointer I load it using: _variant_t va(index_to_window, VT_I4); spDisp = m_spSHWinds->Item(va); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); spBrowser->AddRef(); And save spBrowser for later processing, the m_pWebBrowser2 in the example above. I spin a thread off to help with processing at some point and use marshalling there, but not in the main program thread. It seems weird that it would work fine as long as I don't trap events? I'll look into that though. Thanks. – Brian May 21 '09 at 15:29