HWND MkWebBrowser::Open(const MkStr& pageUrl, int posX, int posY, int width, int height) { if (pageUrl.Empty()) return NULL; HRESULT hr; IWebBrowser2* pWebBrowser = NULL; VARIANT vtHeader2 = {0}; VARIANT vtTarget2 = {0}; VARIANT vtEmpty2 = {0}; vtHeader2.vt = VT_BSTR; vtHeader2.bstrVal = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\n"); vtTarget2.vt = VT_BSTR; vtTarget2.bstrVal = SysAllocString(L"_top"); VariantInit(&vtEmpty2); CoInitialize(NULL); CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&pWebBrowser); pWebBrowser->put_Width((long)width); pWebBrowser->put_Height((long)height); pWebBrowser->put_Left((long)posX); pWebBrowser->put_Top((long)posY); pWebBrowser->put_MenuBar(VARIANT_FALSE); pWebBrowser->put_ToolBar(VARIANT_FALSE); pWebBrowser->put_AddressBar(VARIANT_FALSE); pWebBrowser->put_StatusBar(VARIANT_FALSE); BSTR cBuf = SysAllocString(pageUrl.GetPtr()); hr = pWebBrowser->Navigate(cBuf, &vtEmpty2, &vtTarget2, &vtEmpty2, &vtHeader2); SysFreeString(cBuf); SysFreeString(vtHeader2.bstrVal); SysFreeString(vtTarget2.bstrVal); HWND exh = NULL; if (SUCCEEDED(hr)) { pWebBrowser->put_Visible(VARIANT_TRUE); pWebBrowser->get_HWND((long*)&exh); } else { pWebBrowser->Quit(); } pWebBrowser->Release(); CoUninitialize(); return exh; }
DWORD Application_InternetExplorer::closeAllInternetExplorers(IClassFactory* internet_explorer_factory) { DWORD iReturnVal; iReturnVal = 0; // Create another fake IE instance so that we can close the process IWebBrowser2* pInternetExplorer; HRESULT hr = internet_explorer_factory->CreateInstance(NULL, IID_IWebBrowser2, (void**)&pInternetExplorer); if( hr == S_OK ) { OutputDebugStringA("IE CloseAll created fake IE instance.\n"); HWND hwndIE; DWORD dProcessId; pInternetExplorer->get_HWND((SHANDLE_PTR*)&hwndIE); GetWindowThreadProcessId(hwndIE, &dProcessId); // Close the IE process - try 1 EnumWindows(Application_InternetExplorer::EnumWindowsProc, (LPARAM)dProcessId); // Close the IE process - try 2 HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, dProcessId); DWORD tempProcessId = GetProcessId(hProc); if(tempProcessId == dProcessId) { if(!TerminateProcess(hProc, 0)) { iReturnVal = GetLastError(); OutputDebugStringA("IE CloseAll unable to terminate 1.\n"); } } if( hProc != NULL) { CloseHandle( hProc ); } } if(pInternetExplorer!=NULL) { pInternetExplorer->Release(); } //then all processes that match the exe DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) { OutputDebugStringA("IE CloseAll couldn't enum processes.\n"); iReturnVal = -1; } // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); for ( i = 0; i < cProcesses; i++ ) { if( aProcesses[i] != 0 ) { std::wstring processName = L"c:\\Program Files\\Internet Explorer\\iexplore.exe"; size_t iPos = processName.find_last_of(L"\\"); processName.erase(0, iPos +1); if(compareName(aProcesses[i], processName)==0) { OutputDebugStringA("IE CloseAll IE process left over. Closing....\n"); EnumWindows(Application_InternetExplorer::EnumWindowsCloseAppProc, (LPARAM) aProcesses[i]); HANDLE hPro = OpenProcess(PROCESS_TERMINATE,TRUE, aProcesses[i]); if(!TerminateProcess(hPro, 0)) { iReturnVal = GetLastError(); OutputDebugStringA("IE CloseAll unable to terminate 2.\n"); } if( hPro != NULL ) { CloseHandle (hPro); } } } } return iReturnVal; }
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; }