BOOL SimpleBrowser::GetElementText(LPCTSTR elementName, LPTSTR bf, UINT bfLen) { IHTMLDocument3* pDocument = GetDocument3(); if (pDocument == NULL) return FALSE; BOOL bRet = FALSE; IHTMLElement* pElement = NULL; CComBSTR elemName = elementName; if (pDocument->getElementById(elemName, &pElement) == S_OK && pElement != NULL) { CComBSTR innerText; if (pElement->get_innerText(&innerText) == S_OK) { if (innerText.Length() > 0) { _tcsncpy(bf, innerText, bfLen); bf[bfLen - 1] = 0; } else bf[0] = 0; bRet = TRUE; } pElement->Release(); } pDocument->Release(); return bRet; }
//----------------------------------------------------------------------------- // Name: AddHtmlContentToElement // Object: // Parameters : // in : // out : // return : //----------------------------------------------------------------------------- BOOL CHtmlViewer::AddHtmlContentToElement(TCHAR* Id,TCHAR* Content) { if (this->pIWebBrowser==NULL) return FALSE; if (!this->IsPageCompleted()) return FALSE; HRESULT hr; BOOL bRet; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument3* pHtmlDoc = NULL; IHTMLElement* pElem = NULL; BSTR bstrElementId; #if ((!defined(UNICODE))&& (!defined(_UNICODE))) WCHAR* psz; #endif // get IDispatch document interface hr = pIWebBrowser->get_Document(&pHtmlDocDispatch); if (FAILED (hr) || (pHtmlDocDispatch == NULL)) return FALSE; // get IHTMLDocument3 document interface hr = pHtmlDocDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHtmlDoc); if (FAILED (hr) || (pHtmlDoc == NULL)) { pHtmlDocDispatch->Release(); return FALSE; } // get pointer to element from it's Id #if (defined(UNICODE)||defined(_UNICODE)) bstrElementId=SysAllocString(Id); #else CAnsiUnicodeConvert::AnsiToUnicode(Id,&psz); bstrElementId=SysAllocString(psz); free(psz); #endif hr = pHtmlDoc->getElementById(bstrElementId,&pElem); SysFreeString(bstrElementId); if (FAILED (hr) || (pElem == NULL)) { pHtmlDoc->Release(); pHtmlDocDispatch->Release(); return FALSE; } bRet=this->AddHtmlContentToElement(pElem,Content); pElem->Release(); pHtmlDoc->Release(); pHtmlDocDispatch->Release(); return bRet; }