wb2 GetIE(HWND w) { SHDocVw::IShellWindowsPtr shp; shp.CreateInstance(__uuidof(SHDocVw::ShellWindows)); int count = shp->GetCount(); IDispatchPtr spDisp; // ---- get the browsers for (long i = 0; i < count; i++) { _variant_t va(i, VT_I4); spDisp = shp->Item(va); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); if (spBrowser != NULL) { HWND hw; try { hw = (HWND)spBrowser->GetHWND(); } catch ( ... ) { hw = NULL; } if (hw == w) return spBrowser; } } return NULL; }
bool CMyInternetExplorer::FindUsingTitle (const CString & sTitleToSearch) { if (m_pWebBrowser != NULL) { m_pWebBrowser->Release (); m_pWebBrowser = NULL; } HRESULT hr; SHDocVw::IShellWindowsPtr spSHWinds; hr = spSHWinds.CreateInstance (__uuidof(SHDocVw::ShellWindows)); if (FAILED (hr)) return false; ASSERT (spSHWinds != NULL); long nCount = spSHWinds->GetCount (); IDispatchPtr spDisp; for (long i = 0; i < nCount; i++) { _variant_t va (i, VT_I4); spDisp = spSHWinds->Item (va); IWebBrowser2 * pWebBrowser = NULL; hr = spDisp.QueryInterface (IID_IWebBrowser2, & pWebBrowser); if (pWebBrowser != NULL) { HRESULT hr; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2 * pHtmlDoc = NULL; // Retrieve the document object. hr = pWebBrowser->get_Document (&pHtmlDocDispatch); if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL)) { // Query for IPersistStreamInit. hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc); if (SUCCEEDED (hr) && (pHtmlDoc != NULL)) { CString sTitle; HWND hWnd = NULL; pWebBrowser->get_HWND ((long*)(&hWnd)); if (::IsWindow (hWnd)) { int nLen = ::GetWindowTextLength (hWnd); ::GetWindowText (hWnd, sTitle.GetBufferSetLength (nLen), nLen + 1); sTitle.ReleaseBuffer (); } // If I cannot get the window title (should never happen though) // So, lets just use the title of the document if (sTitle.IsEmpty ()) { BSTR bstrTitle; hr = pHtmlDoc->get_title (&bstrTitle); if (!FAILED (hr)) { sTitle = bstrTitle; SysFreeString (bstrTitle); } } if (StringHelper::WildcardCompareNoCase (sTitleToSearch, sTitle)) { m_pWebBrowser = pWebBrowser; pHtmlDoc->Release (); pHtmlDocDispatch->Release (); // Exit the method safely! return true; } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } pWebBrowser->Release (); } } return false; }