First, introduce
The Password input box in the Web page is different from the general, it has no handle and so on, but by obtaining IE's ihtmlinputtextelement interface, you can get the contents of the input box (including text and password input box) in the Web page.
Source code in the VC Knowledge Base home Operation effect chart is as follows:
Second, the specific code
VARIANT ID, index;
Ccomptr<idispatch> Spdispatch;
Ccomqiptr<ihtmldocument2, &IID_IHTMLDocument2> pDoc2;
Ccomptr<ihtmlelement> pelement;
Ccomptr<ihtmlelementcollection> Pelementcol;
Ccomptr<ihtmlformelement> pformelement;
Ccomptr<ihtmlinputtextelement> pinputelement;
First get the IWebBrowser2 interface
CoInitialize (NULL); You have to initialize this sentence.
Shdocvw::iwebbrowser2ptr Spbrowser (SPDISP);
if (m_spshwinds = NULL)
{
if (M_spshwinds.createinstance (__uuidof (shdocvw::shellwindows))!= S_OK)
{
MessageBox ("Failed");
CoUninitialize ();
}
}
if (m_spshwinds)
{
int n = m_spshwinds->getcount ();
for (int i = 0; i < n; i++)
{
_variant_t v = (long) i;
Idispatchptr spdisp = M_spshwinds->item (v);
Shdocvw::iwebbrowser2ptr Spbrowser (SPDISP); Generate a smart pointer to an IE window
if (Spbrowser)
{
Get IHTMLDocument2 interface
if (SUCCEEDED (Spbrowser->get_document (&spdispatch))
PDOC2 = Spdispatch;
if (pdoc2!=null)
{
AfxMessageBox ("Already acquired IHTMLDocument2");
if (SUCCEEDED (Pdoc2->get_forms (&pelementcol))
{
AfxMessageBox ("Already acquired IHTMLElementCollection");
Long p =;
if (SUCCEEDED (Pelementcol->get_length (&p))
if (p!=0)
{
For (Long i=0;i<= (p-1); i++)
{
V_VT (&id) = VT_I4;
V_I4 (&id) = i;
V_VT (&index) = VT_I4;
V_I4 (&index) = 0;
if (SUCCEEDED (Pelementcol->item (Id,index, &spdispatch))
if (succeeded Spdispatch->queryinterface (Iid_ihtmlformelement, (void**) &pformelement))
{
AfxMessageBox ("Already acquired ihtmlformelement");
Long q=0;
if (SUCCEEDED (Pformelement->get_length (&Q))
For (Long j=0;j<= (q-1); j + +)
{
V_VT (&id) = VT_I4;
V_I4 (&id) = j;
V_VT (&index) = VT_I4;
V_I4 (&index) = 0;
if (SUCCEEDED (Pformelement->item (Id,index, &spdispatch))
if (succeeded Spdispatch->queryinterface (Iid_ihtmlinputtextelement, (void**) &pinputelement))
{
AfxMessageBox ("Already acquired ihtmlinputtextelement");
CComBSTR value;
CComBSTR type;
Pinputelement->get_type (&type); Get the type of input box (password box or text box)
CString strtype (type);
Strtype. Makeupper ();
if (strtype. Find ("text")!=-1//Get the value of the text box
{
Pinputelement->get_value (&value);
CString str (value);
if (!str. IsEmpty ())
M_ctrlie.insertitem (0, _bstr_t (value) +_bstr_t ("" may be user name or other content to be submitted ""));
}
else if (strtype. Find ("PASSWORD")!=-1)//Get the value of the Password box
{
Pinputelement->get_value (&value);
CString str (value);
if (!str. IsEmpty ())
M_ctrlie.insertitem (0, _bstr_t (value) + _bstr_t ("" "should be Password"));
}
}
}
}
}
}
}
}
}
}
}
Note: Because I am also lazy, the framework of this article is called "How to control the behavior of IE" ... Thank the original author here, but the main code of this article is I wrote, (in fact, it is too easy to write a framework, but I have to work AH:( Excuse me! It is best not to provide technical support to the author! Thanks for reading!
This article supporting source code