BHO reading HTML input value
-
I am trying to read an html page and retrieve the value of three hidden input fields sure as: VC++6.0/ATL/BHO -- Below is the code I am working with to read in the value (1500) but it is not working. I think i am missing something. HRESULT hr; TCHAR sPort[25]; IDispatch* pElemDisp = NULL; IHTMLInputHiddenElement* pElem = NULL; IHTMLElementCollection *pElemcoll; // Get the WebBrowser's document object CComPtr pDisp; HRESULT hr = m_spWebBrowser2->get_Document(&pDisp); if (FAILED(hr)) return false; CComQIPtr spHTML; spHTML = pDisp; _variant_t tagName("input"); _variant_t tagNamePort("port"); BSTR* pValuePort = NULL; spHTML->get_forms(&pElemcoll); hr = pElemColl->tags( tagName, &pElemDisp ); if ( SUCCEEDED(hr) ) { hr = pElemDisp->QueryInterface( IID_IHTMLInputHiddenElement, (void**)&pElem ); if ( SUCCEEDED(hr) ) { hr = pElem->get_value(pValuePort); if ( SUCCEEDED(hr) ) { //Set port _stprintf(sPort, _T("%s"), (LPCTSTR)pValuePort); m_sPort = (int)sPort; } pElem->Release(); } pElemDisp->Release(); } //at this point m_sPort is NULL not 1500 :-( I think I may not be handling the Element Collection object correctly. Any pointers as to where i am going wrong? Thank you for any help you can give..
-
I am trying to read an html page and retrieve the value of three hidden input fields sure as: VC++6.0/ATL/BHO -- Below is the code I am working with to read in the value (1500) but it is not working. I think i am missing something. HRESULT hr; TCHAR sPort[25]; IDispatch* pElemDisp = NULL; IHTMLInputHiddenElement* pElem = NULL; IHTMLElementCollection *pElemcoll; // Get the WebBrowser's document object CComPtr pDisp; HRESULT hr = m_spWebBrowser2->get_Document(&pDisp); if (FAILED(hr)) return false; CComQIPtr spHTML; spHTML = pDisp; _variant_t tagName("input"); _variant_t tagNamePort("port"); BSTR* pValuePort = NULL; spHTML->get_forms(&pElemcoll); hr = pElemColl->tags( tagName, &pElemDisp ); if ( SUCCEEDED(hr) ) { hr = pElemDisp->QueryInterface( IID_IHTMLInputHiddenElement, (void**)&pElem ); if ( SUCCEEDED(hr) ) { hr = pElem->get_value(pValuePort); if ( SUCCEEDED(hr) ) { //Set port _stprintf(sPort, _T("%s"), (LPCTSTR)pValuePort); m_sPort = (int)sPort; } pElem->Release(); } pElemDisp->Release(); } //at this point m_sPort is NULL not 1500 :-( I think I may not be handling the Element Collection object correctly. Any pointers as to where i am going wrong? Thank you for any help you can give..
Have you stepped through with the debugger? Which line in there is failing? If you just look at
m_sPort
after it's done, you can't tell which call fails. Once you figure that part out, you need to fix your string handling. You don't pass aBSTR*
toget_value()
, you pass the address of aBSTR
so thatget_value()
can store the return value in it:BSTR bsValuePort = NULL;
hr = pElem->get_value ( &bsValuePort );
// ... use bsValuePort ...// Don't forget to free the string:
SysFreeString ( bsValuePort );Then, casting a
BSTR
to aLPCTSTR
will do you no good in an ANSI build. Nor will casting achar
array to anint
. Assumingm_sPort
is aCString
, You can just write:m_sPort = (LPCWSTR) bsValuePort;
and let the
CString
constructor do the conversion. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ You cannot stop me with paramecium alone!