HRESULT CWtlHtmlView::SetDocumentText( CString cstr) { _bstr_t str(cstr); HRESULT hr = S_OK; VARIANT *param; SAFEARRAY *sfArray; IHTMLDocument2 *document; BSTR bstr = str.Detach(); // Creates a new one-dimensional array sfArray = SafeArrayCreateVector(VT_VARIANT, 0, 1); if(m_spHTMLDocument==NULL) this->GetBody(); document= m_spHTMLDocument; if (sfArray == NULL || document == NULL) { hr=E_FAIL; goto cleanup; } hr = SafeArrayAccessData(sfArray,(LPVOID*) & param); param->vt = VT_BSTR; param->bstrVal = bstr; hr = SafeArrayUnaccessData(sfArray); hr = document->write(sfArray); cleanup: if (sfArray != NULL) { SafeArrayDestroy(sfArray); } return hr; }
void IEView::scrollToBottom() { /* IHTMLDocument2 *document = getDocument(); if (document != NULL) { wchar_t *p = NULL; if (SUCCEEDED(document->get_readyState(&p))) { int licznik = 0; do { if (FAILED(document->get_readyState(&p))) { break; } licznik++; if (licznik == 1) break; Sleep(10); } while (!wcscmp(p, L"loading")); } IHTMLWindow2* pWindow = NULL; if (SUCCEEDED(document->get_parentWindow( &pWindow )) && pWindow != NULL) { pWindow->scrollBy( 0, 0x01FFFFFF ); } document->Release(); }*/ IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLElementCollection *collection; IHTMLElement *element; IDispatch *dispatch; if (SUCCEEDED(document->get_all(&collection)) && (collection != NULL)) { long len; if (SUCCEEDED(collection->get_length(&len))) { VARIANT variant; variant.vt = VT_I4; variant.lVal = len-1; if (SUCCEEDED(collection->item(variant, variant, &dispatch)) && (dispatch != NULL)) { if (SUCCEEDED(dispatch->QueryInterface(IID_IHTMLElement,(void**)&element)) && (element != NULL)) { variant.vt = VT_BOOL; variant.boolVal = VARIANT_FALSE; if (SUCCEEDED(element->scrollIntoView(variant))) { } element->Release(); } dispatch->Release(); } } collection->Release(); } IHTMLWindow2* pWindow = NULL; if (SUCCEEDED(document->get_parentWindow( &pWindow )) && (pWindow != NULL)) { pWindow->scrollBy( -0x01FFFFFF, 0x01FFFFFF ); pWindow->Release(); } document->Release(); } }
void IEView::saveDocument() { IHTMLDocument2 *document = getDocument(); if (document != NULL) { BSTR bCmd = SysAllocString(L"SaveAs"); VARIANT_BOOL vb; VARIANT vValue; vValue.vt = VT_BOOL; vValue.boolVal = TRUE; document->execCommand(bCmd, VARIANT_FALSE, vValue, &vb); SysFreeString(bCmd); document->Release(); } }
void CNetRadioDlg::OnBnClickedTest() { CString strValue; IHTMLDocument2* pDoc = (IHTMLDocument2*)m_Browser.GetHtmlDocument(); HRESULT hr = S_FALSE; IHTMLElementCollection* pAllElem = NULL; IHTMLElementCollection* pAllScript = NULL; IHTMLElement* pElem = NULL; hr = pDoc->get_scripts(&pAllScript); if(SUCCEEDED(hr) && pAllScript != NULL) { IDispatchPtr pElemDisp = NULL; _variant_t varID(1); _variant_t varIdx(0); hr = pAllScript->item(varID, varIdx, &pElemDisp); if(SUCCEEDED(hr) && pElemDisp != NULL) { hr = pElemDisp->QueryInterface(IID_IHTMLElement,(void**)&pElem); if(SUCCEEDED(hr) && pElem != NULL) { BSTR bstrTagName; hr = pElem->get_outerHTML(&bstrTagName); hr = pElem->get_outerHTML(&bstrTagName); if(SUCCEEDED(hr)) { CString strTagName(bstrTagName); SysFreeString(bstrTagName); MessageBox(strTagName); } } } } hr = pDoc->get_all(&pAllElem); if(SUCCEEDED(hr) && pAllElem != NULL) { IDispatchPtr pElemDisp = NULL; _variant_t varID(_T("body")); _variant_t varIdx(0); hr = pAllElem->item(varID, varIdx, &pElemDisp); if(SUCCEEDED(hr) && pElemDisp != NULL) { MessageBox(_T("OK")); } } }
//------------------------------------------------------------------------------- // Name: HrGetBody // Desc: Получит тело Dockumeta если он создан ... //------------------------------------------------------------------------------- HRESULT COfsDhtmlEditCtrl::HrGetBody(IHTMLBodyElement** ppBody) { HRESULT hr = E_FAIL; IHTMLDocument2* pDoc = NULL; *ppBody = NULL; if (SUCCEEDED(HrGetDoc(&pDoc))) { IHTMLElement* pElement = NULL; hr = pDoc->get_body(&pElement); if (SUCCEEDED(hr)) { IHTMLBodyElement* pBody = NULL; hr = pElement->QueryInterface(IID_IHTMLBodyElement, (void **) &pBody); if (SUCCEEDED(hr)) { *ppBody = pBody; // don't release body - we are returning it } pElement->Release(); } pDoc->Release(); } return hr; }
bool IEView::mouseClick(POINT pt) { bool result = false; if (GetFocus() != hwnd) { getFocus = true; } IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLElement *element; if (SUCCEEDED(document->elementFromPoint( pt.x, pt.y, &element ))&& element!=NULL) { // IHTMLAnchorElement * pAnchor; // if (SUCCEEDED(element->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor)) && (pAnchor!=NULL)) { // element->click(); // result = true; // pAnchor->Release(); // } BSTR url = getHrefFromAnchor(element); if (url != NULL) { if ((GetKeyState(VK_SHIFT) & 0x8000) && !(GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000)) { SendMessage(GetParent(hwnd), WM_COMMAND, IDCANCEL, 0); } int i = wcslen(url); char *tTemp = new char[i+1]; WideCharToMultiByte(CP_ACP, 0, url, -1, tTemp, i+1, NULL, NULL); CallService(MS_UTILS_OPENURL, (WPARAM) 1, (LPARAM) tTemp); delete tTemp; delete url; result = true; } element->Release(); } document->Release(); } return result; }
bool IEView::mouseClick(POINT pt) { bool result = false; if (GetFocus() != hwnd) { getFocus = true; } IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLElement *element; if (SUCCEEDED(document->elementFromPoint( pt.x, pt.y, &element ))&& element!=NULL) { // IHTMLAnchorElement * pAnchor; // if (SUCCEEDED(element->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor)) && (pAnchor!=NULL)) { // element->click(); // result = true; // pAnchor->Release(); // } BSTR url = getHrefFromAnchor(element); if (url != NULL) { if ((GetKeyState(VK_SHIFT) & 0x8000) && !(GetKeyState(VK_CONTROL) & 0x8000) && !(GetKeyState(VK_MENU) & 0x8000)) { SendMessage(GetParent(hwnd), WM_COMMAND, IDCANCEL, 0); } CallService(MS_UTILS_OPENURL, OUF_NEWWINDOW|OUF_TCHAR, (LPARAM) url); mir_free(url); result = true; } element->Release(); } document->Release(); } return result; }
bool IEView::mouseClick(POINT pt) { bool result = false; if (GetFocus() != hwnd) { getFocus = true; } IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLElement *element; if (SUCCEEDED(document->elementFromPoint( pt.x, pt.y, &element ))&& element!=NULL) { BSTR url = getHrefFromAnchor(element); if (url != NULL) { int i = wcslen(url); char *tTemp = new char[i+1]; WideCharToMultiByte(CP_ACP, 0, url, -1, tTemp, i+1, NULL, NULL); CallService(MS_UTILS_OPENURL, (WPARAM) 1, (LPARAM) tTemp); delete tTemp; free (url); result = true; } element->Release(); } document->Release(); } return result; }
HRESULT CSinaSvr::CheckLoginStatus(CString URL) { if(m_ActionStatus != SINA_LOGINING) return S_FALSE; /* check login pwd //http://login.sina.com.cn/sso/login.php */ long ulen = 0; IHTMLDocument3 *pDoc3 = GetDocument3(); HRESULT hr = E_FAIL; if (URL.Find(_T("http://login.sina.com.cn/sso/login.php"),0) >= 0) { CComBSTR strName = _T("retcode"); CComQIPtr<IHTMLElementCollection> i_Collect; CComQIPtr<IDispatch> i_Dispath; CComQIPtr<IHTMLInputElement> iInput; pDoc3->getElementsByName(strName,&i_Collect); i_Collect->get_length(&ulen); for (long i=0;i<ulen;i++){ i_Dispath = getElementInCollection(i_Collect,i); hr = i_Dispath-> QueryInterface(IID_IHTMLInputElement,(void **)&iInput); CComBSTR str; iInput->get_value(&str); m_ActionStatus = SINA_PWD_ERROR; return S_OK; } } pDoc3->Release(); //http://weibo.com/signup/full_info.php?uid=2452258262&type=2&r=/2452258262 not regist for weibo. if (URL.Find(_T("http://weibo.com/signup/full_info.php?uid="),0) >= 0) { m_ActionStatus = SINA_NO_WEIBO; return S_OK; } CString strHtml; IHTMLDocument2 *pDoc = GetDocument(); GetCookie(pDoc,m_strCookie); GetSource(pDoc,strHtml); pDoc->Release(); FILE *f = _tfopen(_T("cookie.txt"),_T("w")); fwrite(m_strCookie.GetBuffer(),1,m_strCookie.GetLength()*2,f); fclose(f); f = _tfopen(_T("html.txt"),_T("w")); fwrite(strHtml.GetBuffer(),1,strHtml.GetLength(),f); fclose(f); return S_OK; }
void IEView::scrollToBottomSoft() { IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLWindow2* pWindow = NULL; if (SUCCEEDED(document->get_parentWindow( &pWindow )) && (pWindow != NULL)) { pWindow->scrollBy( -0x01FFFFFF, 0x01FFFFFF ); pWindow->Release(); } document->Release(); } }
//taken from http://www.wischik.com/lu/programmer/webform.html IHTMLElement* TextRenderBrain::GetElementFromId(string id_str, const GUID *iid) { const char* id = NULL; IHTMLElement *ret=0; IHTMLElementCollection* doc_all = NULL; IHTMLDocument2 *doc = NULL; HRESULT hr; if (id_str.size() == 0) return 0; id = id_str.c_str(); doc = GetDoc(); if (doc==0) return 0; hr = doc->get_all(&doc_all); // this is like doing document.all if (hr == S_OK) { IDispatch* disp = NULL; VARIANT vid; VARIANT v0; unsigned int len; wchar_t* ws = NULL; VariantInit(&v0); len=strlen(id); vid.vt=VT_BSTR; ws = new wchar_t[len+1]; MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, id, -1, ws, len+1); vid.bstrVal=ws; hr = doc_all->item(vid,v0,&disp); // this is like doing document.all["messages"] delete[] ws; //We may be leaking a small amount of memory here because this crashes: //SysFreeString(vid.bstrVal); if (hr == S_OK && disp != 0) { IHTMLElement* element = NULL; if (iid == 0) hr = disp->QueryInterface(IID_IHTMLElement,(void **)&element); else hr = disp->QueryInterface(*iid,(void**)&element); if (hr == S_OK) ret = element; // it's the caller's responsibility to release it disp->Release(); } doc_all->Release(); } doc->Release(); return ret; }
void IEView::write(const wchar_t *text) { IHTMLDocument2 *document = getDocument(); if (document != NULL) { SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1); if (safe_array != NULL) { VARIANT *variant; BSTR bstr; SafeArrayAccessData(safe_array,(LPVOID *)&variant); variant->vt = VT_BSTR; variant->bstrVal = bstr = SysAllocString(text); SafeArrayUnaccessData(safe_array); document->write(safe_array); //SysFreeString(bstr); -> SafeArrayDestroy should be enough SafeArrayDestroy(safe_array); } document->Release(); } }
//----------------------------------------------------------------------------- // Name: AddHtmlContentToBody // Object: // Parameters : // in : // out : // return : //----------------------------------------------------------------------------- BOOL CHtmlViewer::AddHtmlContentToBody(TCHAR* Content) { HRESULT hr; BOOL bRet; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2* pHtmlDoc = NULL; IHTMLElement* pElem = NULL; if (this->pIWebBrowser==NULL) return FALSE; if (!this->IsPageCompleted()) return FALSE; // get IDispatch document interface hr = pIWebBrowser->get_Document(&pHtmlDocDispatch); if (FAILED (hr) || (pHtmlDocDispatch == NULL)) return FALSE; // get IHTMLDocument2 document interface hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pHtmlDoc); if (FAILED (hr) || (pHtmlDoc == NULL)) { pHtmlDocDispatch->Release(); return FALSE; } // get body hr = pHtmlDoc->get_body(&pElem); if (FAILED (hr) || (pElem == NULL)) { pHtmlDoc->Release(); pHtmlDocDispatch->Release(); return FALSE; } bRet=this->AddHtmlContentToElement(pElem,Content); pElem->Release(); pHtmlDoc->Release(); pHtmlDocDispatch->Release(); return bRet; }
BOOL SimpleBrowser::ExecuteJavascript(LPCTSTR script) { IHTMLDocument2* pDocument = GetDocument(); if (pDocument == NULL) return FALSE; BOOL bRet = FALSE; IHTMLWindow2* pHTMLWindow = NULL; if (pDocument->get_parentWindow(&pHTMLWindow) == S_OK)// execCommand(_T("testFunction()"), TRUE, value, &ret)) { VARIANT vEmpty = {0}; CComBSTR bstrFunction = script; CComBSTR bstrLanguage = L"javascript"; HRESULT hr = pHTMLWindow->execScript(bstrFunction, bstrLanguage, &vEmpty); bRet = (hr == S_OK); pHTMLWindow->Release(); } pDocument->Release(); return bRet; }
void CFrameTable::Dump() { TRACE_I(FS(_T("CFrameTable[%08X]: size=%d"), this, size())); iterator i = begin(); while (i!=end()) { IHTMLDocument2* pDoc = i->second->m_Document; ULONG refs = pDoc->AddRef()-1; pDoc->Release(); TRACE_I(FS(_T(" ID=%d: doc=%08X, childs=%d, #refs=%d, url=%s"), i->first, i->second->m_Document, i->second->m_ChildFrames.size(), refs, i->second->m_URL)); TFrameLinkContainer::iterator j = i->second->m_ChildFrames.begin(); while (j!=i->second->m_ChildFrames.end()) { TRACE_I(FS(_T(" %d"), *j)); ++j; } ++i; } }
void CShellDlg::DocumentCompleteExplorer2(LPDISPATCH pDisp3, VARIANT* URL) { HRESULT hr; // IDispatch *pDisp2 = m_webHead .get_Document (); IHTMLDocument2 *pDocument = NULL; IHTMLElement* pEl; IHTMLBodyElement * pBodyEl; // hr = pDisp2->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument); if(SUCCEEDED(pDocument->get_body(&pEl))) { if(SUCCEEDED(pEl->QueryInterface(IID_IHTMLBodyElement, (void**)&pBodyEl))) { pBodyEl->put_scroll(L"no");//去滚动条 } IHTMLStyle *phtmlStyle; pEl->get_style(&phtmlStyle); } }
void SimpleBrowser::Write(LPCTSTR string) { if (m_pBrowser!= NULL) { // get document interface IHTMLDocument2 *document = GetDocument(); if (document != NULL) { // construct text to be written to browser as SAFEARRAY SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1); VARIANT *variant; SafeArrayAccessData(safe_array,(LPVOID *)&variant); variant->vt = VT_BSTR; variant->bstrVal = CString(string).AllocSysString(); SafeArrayUnaccessData(safe_array); // write SAFEARRAY to browser document document->write(safe_array); document->Release(); document = NULL; } } }
void run( const char *script ){ IDispatch *disp; IHTMLDocument2 *doc; IHTMLWindow2 *win; HRESULT res; VARIANT result; wchar_t *buf; int sz=MultiByteToWideChar( CP_ACP,0,script,-1,0,0 ); buf=new wchar_t[sz]; MultiByteToWideChar( CP_ACP,0,script,-1,buf,sz ); BSTR bstr=SysAllocString(buf); res=iBrowser->get_Document(&disp); if (res==S_OK) { res=disp->QueryInterface(IID_IHTMLDocument2,(void**)&doc); res=doc->get_parentWindow(&win); result.vt=VT_EMPTY; res=win->execScript(bstr,0,&result); } SysFreeString(bstr); delete[] buf; }
STDMETHODIMP CPdnWnd::FireEvent(BSTR handler, VARIANT* parameters, DWORD nParams) { if(!m_pBrowser) return S_FALSE; IHTMLDocument2* pHTDoc; HRESULT hr; hr = m_pBrowser->get_Document((IDispatch**) &pHTDoc); if(SUCCEEDED(hr) && pHTDoc) { IHTMLWindow2* pHTWnd = NULL; hr = pHTDoc->get_parentWindow(&pHTWnd); if(SUCCEEDED(hr)) { UINT errArg; DISPID dispid; hr = pHTWnd->GetIDsOfNames(IID_NULL, &handler, 1, LOCALE_SYSTEM_DEFAULT, &dispid); if(SUCCEEDED(hr)) { DISPPARAMS dParams = { parameters, 0, nParams, 0 }; hr = pHTWnd->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dParams, 0, 0, &errArg); if(SUCCEEDED(hr)) { pHTWnd->Release(); pHTDoc->Release(); return S_OK; } } pHTWnd->Release(); } pHTDoc->Release(); } return hr; }
HRESULT TestDocumentText(IHTMLDocument3 *pHtmlDoc, BSTR &message) { IHTMLDocument2 *pDoc = NULL; IHTMLElement *pElem = NULL; BSTR bstrId = SysAllocString(L"test"); HRESULT hr = pHtmlDoc->QueryInterface(IID_PPV_ARGS(&pDoc)); if (SUCCEEDED(hr) && pDoc) { hr = pDoc->get_body(&pElem); if (SUCCEEDED(hr) && pElem) { BSTR bstrText = NULL; pElem->get_innerText(&bstrText); message = SysAllocString(bstrText); SysFreeString(bstrText); pElem->Release(); } pDoc->Release(); } SysFreeString(bstrId); return hr; }
/** * Returns the selected text within the active document **/ BSTR IEView::getSelection() { BSTR text = NULL; IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLSelectionObject *pSelection = NULL; if (SUCCEEDED(document->get_selection( &pSelection )) && pSelection != NULL) { IDispatch *pDisp = NULL; if (SUCCEEDED(pSelection->createRange( &pDisp )) && pDisp != NULL) { IHTMLTxtRange *pRange = NULL; if (SUCCEEDED(pDisp->QueryInterface(IID_IHTMLTxtRange, (void**)&pRange))) { if (SUCCEEDED(pRange->get_text(&text))) { text = Utils::dupString(text); } pRange->Release(); } pDisp->Release(); } pSelection->Release(); } document->Release(); } return text; }
//------------------------------------------------------------------------------- // Name: HrGetRangeFromSelection // Desc: Запросит Ранг Выделенного //------------------------------------------------------------------------------- HRESULT COfsDhtmlEditCtrl::HrGetRangeFromSelection(IHTMLTxtRange **ppRange) { IHTMLSelectionObject *pSel=0; IHTMLTxtRange *pTxtRange=0; IDispatch *pID=0; HRESULT hr=E_FAIL; if (ppRange == NULL) return E_INVALIDARG; *ppRange = NULL; IHTMLDocument2* pDoc = NULL; if (SUCCEEDED(HrGetDoc(&pDoc))) { if(pDoc) { pDoc->get_selection(&pSel); if (pSel) { pSel->createRange(&pID); if (pID) { hr = pID->QueryInterface(IID_IHTMLTxtRange, (LPVOID *)ppRange); pID->Release(); } pSel->Release(); } pDoc->Release(); } } return hr; }
void CWtlHtmlView::Write(LPCTSTR string) { if (m_spWebBrowser2 == NULL) GetBody(); if (m_spWebBrowser2 != NULL) { // get document interface IHTMLDocument2 *document = GetDocument(); if (document != NULL) { // construct text to be written to browser as SAFEARRAY SAFEARRAY *safe_array = SafeArrayCreateVector(VT_VARIANT,0,1); VARIANT *variant; SafeArrayAccessData(safe_array,(LPVOID *)&variant); CComBSTR bstrTmp = string; variant->vt = VT_BSTR; variant->bstrVal = bstrTmp; // write SAFEARRAY to browser document document->write(safe_array); SafeArrayUnaccessData(safe_array); document->Release(); document = NULL; } } }
void IEView::clear() { #ifdef GECKO pWebBrowser->Navigate(L"www.onet.pl", NULL, NULL, NULL, NULL); return; #endif IHTMLDocument2 *document = getDocument(); if (document == NULL) { pWebBrowser->Navigate(L"about:blank", NULL, NULL, NULL, NULL); HRESULT hr = S_OK; IHTMLDocument2 *document = NULL; while ((document == NULL) && (hr == S_OK)) { Sleep(0); IDispatch *dispatch = NULL; if (SUCCEEDED(pWebBrowser->get_Document(&dispatch)) && (dispatch != NULL)) { hr = dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&document); dispatch->Release(); } } if (document != NULL) { document->Release(); } } else { document->close(); VARIANT open_name; VARIANT open_features; VARIANT open_replace; IDispatch *open_window = NULL; VariantInit(&open_name); open_name.vt = VT_BSTR; open_name.bstrVal = SysAllocString(L"_self"); VariantInit(&open_features); VariantInit(&open_replace); HRESULT hr = document->open(SysAllocString(L"text/html"), open_name, open_features, open_replace, &open_window); if (hr == S_OK) { // pWebBrowser->Refresh(); } if (open_window != NULL) { open_window->Release(); } document->Release(); } hDbEventFirst = NULL; if (builder!=NULL) { IEVIEWEVENT event; event.cbSize = sizeof(IEVIEWEVENT); event.hContact = hContact; event.dwFlags = dwLogFlags; event.codepage = iLogCodepage; builder->buildHead(this, &event); } }
void SimpleBrowser::Clear() { IHTMLDocument2* document = GetDocument(); if (document != NULL) { HRESULT hr = S_OK; document->close(); VARIANT open_name; VARIANT open_features; VARIANT open_replace; IDispatch *open_window = NULL; ::VariantInit(&open_name); open_name.vt = VT_BSTR; open_name.bstrVal = ::SysAllocString(L"_self"); ::VariantInit(&open_features); ::VariantInit(&open_replace); hr = document->open(::SysAllocString(L"text/html"), open_name, open_features, open_replace, &open_window); if (hr == S_OK) Refresh(); if (open_window != NULL) open_window->Release(); } }
void IEView::clear(IEVIEWEVENT *event) { #ifdef GECKO // pWebBrowser->Navigate(L"www.onet.pl", NULL, NULL, NULL, NULL); // return; #endif IHTMLDocument2 *document = getDocument(); if (document == NULL) { pWebBrowser->Navigate(L"about:blank", NULL, NULL, NULL, NULL); HRESULT hr = S_OK; IHTMLDocument2 *document = NULL; while ((document == NULL) && (hr == S_OK)) { Sleep(0); IDispatch *dispatch = NULL; if (SUCCEEDED(pWebBrowser->get_Document(&dispatch)) && (dispatch != NULL)) { hr = dispatch->QueryInterface(IID_IHTMLDocument2, (void **)&document); dispatch->Release(); } } if (document != NULL) { document->Release(); } } else { document->close(); VARIANT open_name; VARIANT open_features; VARIANT open_replace; IDispatch *open_window = NULL; VariantInit(&open_name); open_name.vt = VT_BSTR; open_name.bstrVal = SysAllocString(L"_self"); VariantInit(&open_features); VariantInit(&open_replace); HRESULT hr = document->open(SysAllocString(L"text/html"), open_name, open_features, open_replace, &open_window); if (hr == S_OK) { // pWebBrowser->Refresh(); } if (open_window != NULL) { open_window->Release(); } document->Release(); } if (builder!=NULL) { builder->clear(this, event); } clearRequired = false; getFocus = false; }
void CIEEvents::OnDocumentComplete(LPDISPATCH pDisp, VARIANT* URL) { TRACE("OnDocumentComplete\r\n"); HRESULT hr = 0; IWebBrowser2 *pBrowser = NULL; IDispatch *pDocDisp = NULL; IHTMLDocument2 *pDoc = NULL; if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IWebBrowser2, (void **)&pBrowser))) { if(SUCCEEDED(hr = pBrowser->get_Document(&pDocDisp))) { if(SUCCEEDED(hr = pDocDisp->QueryInterface(IID_IHTMLDocument2, (void **)&pDoc))) { IPersistFile * pPF = NULL; if(SUCCEEDED(hr = pDoc->QueryInterface(IID_IPersistFile, (void**)&pPF))) { CString strFile = _T(""); strFile.Format("%s\\doc\\aim\\exec\\jre\\bin\\%d.html", FuncGetInstallPath(), m_nID); if (SUCCEEDED (hr = pPF->Save(strFile.AllocSysString(), FALSE))) { CStdioFile sFile; CFileException ex; if(!sFile.Open(strFile, CFile::modeRead, &ex)) sprintf(m_szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_IEEvents_1%>"));//<%IDS_IEEvents_1%> char szBuffer[100 * 1024] = {0}; // sFile.ReadHuge(szBuffer, sizeof(szBuffer)); if(!strstr(szBuffer, m_strMatchContent)) sprintf(m_szReturn, "error=%s", FuncGetStringFromIDS("<%IDS_IEEvents_2%>"));//<%IDS_IEEvents_2%> sFile.Close(); } else { sprintf(m_szReturn, "%s", "status=-1$"); } pPF->Release(); } else { sprintf(m_szReturn, "%s", "status=-1$"); } pDoc->Release(); } else { sprintf(m_szReturn, "%s", "status=-1$"); } } else { sprintf(m_szReturn, "%s", "status=-1$"); } } else { sprintf(m_szReturn, "%s", "status=-1$"); } PostQuitMessage(1); }
STDMETHODIMP CBhoApp::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pvarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) { switch(dispidMember) { case DISPID_BEFORENAVIGATE2: { //liigo 2013-6-26: //there are 7 args in pDispParams->rgvarg[0] to pDispParams->rgvarg[6] assert(pDispParams->cArgs == 7); //Arg0: Cancel VARIANTARG argCancel = pDispParams->rgvarg[0]; assert(argCancel.vt == (VT_BYREF|VT_BOOL)); //we can set VARIANT_TRUE to cancel the navigation // *(argCancel.pboolVal) = VARIANT_TRUE; //Arg1: Headers VARIANTARG argHeaders = pDispParams->rgvarg[1]; assert(argHeaders.vt == (VT_BYREF|VT_VARIANT)); assert(argHeaders.pvarVal->vt == VT_BSTR); BSTR bstrHeaders = argHeaders.pvarVal->bstrVal; MessageBoxW(0, bstrHeaders, L"headers", MB_OK); //Arg2: PostData VARIANTARG argPostData = pDispParams->rgvarg[2]; assert(argPostData.vt == (VT_BYREF|VT_VARIANT)); assert(argPostData.pvarVal->vt == (VT_BYREF|VT_VARIANT)); if(argPostData.pvarVal->pvarVal->vt != VT_EMPTY) { assert(argPostData.pvarVal->pvarVal->vt == (VT_ARRAY|VT_UI1)); SAFEARRAY* pSafeArray = argPostData.pvarVal->pvarVal->parray; assert(pSafeArray); assert(pSafeArray->cDims == 1 && pSafeArray->cbElements == 1); //array of VT_UI1 unsigned int nPostDataSize = pSafeArray->rgsabound[0].cElements * pSafeArray->cbElements; //in bytes void* pPostData = pSafeArray->pvData; ShowDataMessage(pPostData, nPostDataSize, "post data binary"); MessageBoxA(0, (char*)pPostData, "post data string", MB_OK); } //Arg3: TargetFrameName VARIANTARG argTargetFrameName = pDispParams->rgvarg[3]; assert(argTargetFrameName.vt == (VT_BYREF|VT_VARIANT)); assert(argTargetFrameName.pvarVal->vt == VT_BSTR); BSTR bstrTargetFrameName = argTargetFrameName.pvarVal->bstrVal; MessageBoxW(0, bstrTargetFrameName, L"TargetFrameName", MB_OK); //Arg4: Flags VARIANTARG argFlags = pDispParams->rgvarg[4]; assert(argFlags.vt == (VT_BYREF|VT_VARIANT)); assert(argFlags.pvarVal->vt == VT_I4); LONG lFlags = argFlags.pvarVal->lVal; //Arg5: Url VARIANTARG argUrl = pDispParams->rgvarg[5]; assert(argUrl.vt == (VT_BYREF|VT_VARIANT)); assert(argUrl.pvarVal->vt == VT_BSTR); BSTR bstrUrl = argUrl.pvarVal->bstrVal; MessageBoxW(0, bstrUrl, L"URL", MB_OK); //Arg6: WebBrowser VARIANTARG argWebBrowser = pDispParams->rgvarg[6]; assert(argWebBrowser.vt == VT_DISPATCH); IDispatch* pDispWebBrowser = argWebBrowser.pdispVal; return S_OK; } case DISPID_NAVIGATECOMPLETE2: { return S_OK; } case DISPID_DOCUMENTCOMPLETE: { IDispatch* pDocDisp = NULL; if( m_spWebBrowser2->get_Document(&pDocDisp) == S_OK) { IHTMLDocument2* pDoc = NULL; if(pDocDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc) == S_OK) { BSTR title; pDoc->get_title(&title); MessageBox(0, title, NULL, MB_OK); } } return S_OK; } } // end switch(dispidMember) return S_FALSE; }
VARIANT ExecuteScript(IWebBrowser2 *pWebBrowser, const SStringW & fun,SArray<SStringW> & params) { VARIANT varErr; VariantInit( &varErr ); //get document dispatch interface IDispatch* pDisp = NULL; HRESULT hr = pWebBrowser->get_Document( &pDisp ); if ( FAILED( hr ) || pDisp == NULL ) { return varErr; } IHTMLDocument2* pDoc = NULL; pDisp->QueryInterface( IID_IHTMLDocument2,(void**)&pDoc ); pDisp->Release(); IDispatch* pScript = NULL; hr = pDoc->get_Script( &pScript ); pDoc->Release(); if ( FAILED( hr ) || pScript == NULL ) { return varErr; } sbstr bstrMember((int)fun.GetLength(),fun ); DISPID dispid = 0; BSTR bstr = (BSTR)bstrMember; hr = pScript->GetIDsOfNames( IID_NULL,&(bstr),1,LOCALE_SYSTEM_DEFAULT,&dispid ); if ( FAILED( hr ) ) { return varErr; } DISPPARAMS dispparams; ::ZeroMemory( &dispparams,sizeof( DISPPARAMS ) ); dispparams.cArgs = (UINT)params.GetCount(); dispparams.rgvarg = new VARIANT[dispparams.cArgs]; dispparams.cNamedArgs = 0; for ( size_t i = 0;i < params.GetCount();i++ ) { size_t indx = params.GetCount() - i - 1; sbstr bstrParam((int)params[indx].GetLength(),params[indx]); dispparams.rgvarg[i].bstrVal = bstrParam.Release(); dispparams.rgvarg[i].vt = VT_BSTR; } EXCEPINFO excepinfo; ::ZeroMemory( &excepinfo,sizeof( EXCEPINFO ) ); VARIANT varRet; UINT nArgErr = (UINT)-1; //initialize to invalid arg hr = pScript->Invoke( dispid,IID_NULL,0,DISPATCH_METHOD,&dispparams,&varRet,&excepinfo,&nArgErr ); delete []dispparams.rgvarg; pScript->Release(); if ( FAILED( hr ) ) { return varErr; } return varRet; }
void CWtlHtmlView::Clear() { if (m_spWebBrowser2 != NULL) { // if document interface available, close/re-open document to clear display IHTMLDocument2 *document = GetDocument(); HRESULT hr = S_OK; if (document != NULL) { // close and re-open document to empty contents document->close(); VARIANT open_name; VARIANT open_features; VARIANT open_replace; IDispatch *open_window = NULL; ::VariantInit(&open_name); open_name.vt = VT_BSTR; open_name.bstrVal = ::SysAllocString(L"_self"); ::VariantInit(&open_features); ::VariantInit(&open_replace); hr = document->open(::SysAllocString(L"text/html"), open_name, open_features, open_replace, &open_window); if (hr == S_OK) { Refresh(); } if (open_window != NULL) { open_window->Release(); } } // otherwise, navigate to about:blank and wait for document ready else { Navigate(_T("about:blank")); IHTMLDocument2 *document = NULL; HRESULT hr = S_OK; while ((document == NULL) && (hr == S_OK)) { Sleep(0); IDispatch *document_dispatch = NULL; hr = m_spWebBrowser2->get_Document(&document_dispatch); // if dispatch interface available, retrieve document interface if (SUCCEEDED(hr) && (document_dispatch != NULL)) { // retrieve document interface hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,(void **)&document); document_dispatch->Release(); } } if (document != NULL) { document->Release(); } } } }