n_vcplus 0 Newbie Poster

Hello All,

I've been through this article and found it very helpful {http://msdn.microsoft.com/en-us/library/bb250489(VS.85).aspx}. I have a 'static' callback method in my BHO dll which is called whenever 'some' event on the computer happens. I need to call a javascript method on all open browser instances when that happens (or do nothing if no such javascript method exists on the page).

I need to access all BHO objects from that static method to call the javascript method on the page. My approach has been to store all IWebBrowser2 pointer variables from every BHO in a global vector (STL). In my static method i access the global vector, enumerate through all the IWebBrowser2 pointers. Then, while trying to access the IHTMLWindow object my IE throws a runtime error at the red line.

...
For every IWebBrowser2 object in the vector = m_spWebBrowser
HRESULT hr = S_OK;
CComPtr<IDispatch> spDispDoc;
hr = m_spWebBrowser->get_Document(&spDispDoc);
if (SUCCEEDED(hr)) {
	CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
	if (spHTMLDoc != NULL) {
		CComPtr<IHTMLWindow2> HTML2Wind;
		hr = spHTMLDoc->get_parentWindow(&HTML2Wind);
		if ( hr == S_OK ) {
			static const CComBSTR funcStr(L"method('param');");
			static const CComBSTR langStr(L"JavaScript");
			VARIANT vEmpty = {0};
			CComPtr<IHTMLWindow2> HTML2Wind;
			hr = spHTMLDoc->get_parentWindow(&HTML2Wind);
			if ( hr == S_OK ) {
				hr = HTML2Wind->execScript(funcStr, langStr, &vEmpty);
				if ( hr == S_OK ) {
				}
			}
		}
	}
}
...

This approach doesn't seem to work. Any comments on a better approach to achieve this or a potential fix?

I can post any specific part of code if explanation is not clear.

~Thanks