Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IHTMLInputElement::put_value doesn't set editbox value?

65 views
Skip to first unread message

Jingcheng

unread,
May 4, 2010, 3:26:59 AM5/4/10
to
I want to set a the editbox in a html-form.
IHTMLInputElement::put_value returns S_OK, but the text of the
editbox is not set, instead it's just cleared. Can you please help me
take a look?

The following is my C++ code:

CString filename = _T("C:\\temp\\test.htm");

_variant_t varFlags(0L);
_variant_t varMissing(0L);
m_browser.Navigate(filename, &varFlags, &varMissing, &varMissing,
&varMissing);

LPUNKNOWN lpUnk = m_browser.GetControlUnknown();
IWebBrowser2Ptr ipWebCtrl(lpUnk);

VARIANT_BOOL bBusy = VARIANT_TRUE;
while (bBusy == VARIANT_TRUE)
ipWebCtrl->get_Busy(&bBusy);

IDispatchPtr ipDispatch;
HRESULT hr = ipWebCtrl->get_Document(&ipDispatch);

IHTMLDocument2Ptr ipHtmlDoc2(ipDispatch);
if (!ipHtmlDoc2)
return;

IHTMLElementCollectionPtr ipElements;
ipHtmlDoc2->get_all(&ipElements);

if (!ipElements)
return;

long count = 0;
ipElements->get_length(&count);

for(long i = 0; i < count; i++)
{
LPDISPATCH lpItem;
ipElements->item(CComVariant(i), CComVariant(i), &lpItem);

IHTMLInputElementPtr ipInput(lpItem);
if(!ipInput)
continue;

// Edit-Field?
BSTR bstrType;
ipInput->get_type(&bstrType);
if (CString(bstrType) == "text")
{ // Fill in Text
CString sTemp (_T("TestVal"));
BSTR bstrNewValue = sTemp.AllocSysString ();
ipInput->put_value (bstrNewValue);
SysFreeString(bstrNewValue);
}
}

The following is my test.htm:

<html>

<head>
</head>

<body>

<script language="JScript"><!--
function GoNext()
{
id = document.all.userID.value;
window.external.GoNext("$#pageName:#UserIDPage.htm$#userID:#" + id +
"$#");
}
//--></script>

<table border="0" width="60%" align="center">
<tr>
<td width="100%">

<form>
<p><input type="text" name="myEditBox" size="20"> Edit</p>
</form>
<form>
<p><input type="checkbox" name="myCheck1" value="ON">
Chk1&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="myCheck2" value="ON"
checked>Chk2&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="myCheck3" value="ON">Chk3</p>
</form>
<form">
<p><input type="radio" value="V1" checked name="myRadio"> Op1&nbsp;
<input type="radio" name="myRadio" value="V2">
Op2&nbsp;&nbsp; <input type="radio" name="myRadio" value="V3">Op3</
p>
</form>
<form>
<p><select size="1" name="myDropDown">
<option selected>Sel 1</option>
<option>Sel 2</option>
<option>Sel 3</option>
</select> Sel</p>
</form>

<form>
<p align="center"><input type="text" name="userID" size="20"></p>
</form>

<form>
<p align="center"><input type="button" onclick="GoNext();"
value="Next >>" name="NextBtn"></p>
</form>

</td>
</tr>
</table>

</body>

</html>

Igor Tandetnik

unread,
May 4, 2010, 7:50:11 AM5/4/10
to
Jingcheng wrote:
> I want to set a the editbox in a html-form.
> IHTMLInputElement::put_value returns S_OK, but the text of the
> editbox is not set, instead it's just cleared. Can you please help me
> take a look?
>
> VARIANT_BOOL bBusy = VARIANT_TRUE;
> while (bBusy == VARIANT_TRUE)
> ipWebCtrl->get_Busy(&bBusy);

I'm surprised you ever get out of this loop. In any case, I'm pretty sure the rest of your code runs too early, before the document was downloaded (check ReadyState property). For WebBrowser to successfully finish the download, its host must pump and dispatch window messages. It is best to start navigation then wait for DocumentComplete event.

> LPDISPATCH lpItem;
> ipElements->item(CComVariant(i), CComVariant(i), &lpItem);

You never Release, and thus leak, lpItem.

> // Edit-Field?
> BSTR bstrType;
> ipInput->get_type(&bstrType);

You never deallocate, and thus leak, bstrType
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925

Jingcheng

unread,
May 4, 2010, 12:55:07 PM5/4/10
to
Igor,

Thanks VERY much for pointing out the memory leak problem. It is my
first time to set values to web from from MFC (although I have
experience to get values from web form). So I just quickly grabbed
some code from internet and tried to see whether / how it can work ---
not on the stage to make it production mode code yet. Thanks very much
for reminder though.

I will try your suggestio and let you know whether I can figure it
out. Thanks again!

Jingcheng

Jingcheng

unread,
May 4, 2010, 5:21:17 PM5/4/10
to
It works great! Thanks Igor!

Jingcheng

0 new messages