1. IndependentCode
// ----------- Start ---------------------//
# Include <atlbase. h>
# Include <mshtml. h>
# Include <winuser. h>
# Include <comdef. h>
# Include <string. h>
Void enumie (void); // process the webpage
Void enumframe (ihtmldocument2 * pihtmldocument2); // Processing Framework
Void enumform (ihtmldocument2 * pihtmldocument2); // process the form
Ccommodule _ module; // use the smart pointer of ccomdispatchdriver ATL, which must be declared here
# Include <atlcom. h>
Void enumfield (ccomdispatchdriver spinputelement, cstring comtype, cstring comval, cstring comname); // process form fields
Void enumie (void)
{
Ccomptr <ishellwindows> spshellwin;
Hresult hR = spshellwin. cocreateinstance (clsid_shellwindows );
If (failed (HR ))
{
Return;
}
long ncount = 0; // obtain the number of browser instances (Explorer and iexplorer)
spshellwin-> get_count (& ncount );
If (0 = ncount)
{< br> return;
}
for (INT I = 0; I {< br> ccomptr spdispie;
hR = spshellwin-> item (ccomvariant (long) I), & spdispie);
If (failed (HR) continue;
ccomqiptr spbrowser = spdispie;
If (! Spbrowser) continue;
ccomptr spdispdoc;
hR = spbrowser-> get_document (& spdispdoc);
If (failed (HR )) continue;
ccomqiptr spdocument2 = spdispdoc;
If (! Spdocument2) continue;
// Modify by jncao 2007-09-17
//************************************** **************************************** *
Cstring cieurl_filter; // set the URL (this URL must be valid for the website );
Cieurl_filter = "http: // 127.0.0.1/smtccs_manage/"; // sets the filtered URL
//************************************** **************************************** *
Ccombstr ieurl;
Spbrowser-> get_locationurl (& ieurl );
Cstring cieurl_get; // complete http url obtained from the machine;
Cieurl_get = ieurl;
Cieurl_get = cieurl_get.left (cieurl_filter.getlength (); // capture the first n digits
If (strcmp (cieurl_get, cieurl_filter) = 0)
{
//ProgramAt this point, the ihtmldocument2 interface pointer has been found.
Enumform (spdocument2); // enumerate all forms
}
}
}
Void enumframe (ihtmldocument2 * pihtmldocument2)
{
If (! Pihtmldocument2) return;
Hresult hr;
Ccomptr <ihtmlframescollection2> spframescollection2;
Pihtmldocument2-> get_frames (& spframescollection2); // retrieves a set of Frame
Long nframecount = 0; // obtain the number of child frameworks
HR = spframescollection2-> get_length (& nframecount );
If (failed (HR) | 0 = nframecount) return;
For (long I = 0; I <nframecount; I ++)
{
Ccomvariant vdispwin2; // obtain the automatic interface of the sub-framework
HR = spframescollection2-> item (& ccomvariant (I), & vdispwin2 );
If (failed (HR) continue;
Ccomqiptr <ihtmlwindow2> spwin2 = vdispwin2.pdispval;
If (! Spwin2) continue; // obtain the ihtmlwindow2 interface of the sub-framework
Ccomptr <ihtmldocument2> spdoc2;
Spwin2-> get_document (& spdoc2); // obtain the ihtmldocument2 interface of the sub-framework
Enumform (spdoc2); // recursively enumerate the Form on the current child frame ihtmldocument2
}
}
Void enumform (ihtmldocument2 * pihtmldocument2)
{
If (! Pihtmldocument2) return;
Enumframe (pihtmldocument2); // recursively enumerate the sub-frame on ihtmldocument2
Hresult hr;
Uses_conversion;
Ccomqiptr <ihtmlelementcollection> spelementcollection;
HR = pihtmldocument2-> get_forms (& spelementcollection); // retrieves a form set
If (failed (HR ))
{
Return;
}
Long nformcount = 0; // obtain the number of forms
HR = spelementcollection-> get_length (& nformcount );
If (failed (HR ))
{
Return;
}
For (long I = 0; I <nformcount; I ++)
{
Idispatch * Pdisp = NULL; // obtain the I-th form
HR = spelementcollection-> item (ccomvariant (I), ccomvariant (), & Pdisp );
If (failed (HR) continue;
Ccomqiptr <ihtmlformelement> spformelement = Pdisp;
Pdisp-> release ();
Long nelemcount = 0; // obtain the number of fields in the form.
HR = spformelement-> get_length (& nelemcount );
If (failed (HR) continue;
For (long J = 0; j <nelemcount; j ++)
{
Ccomdispatchdriver spinputelement; // obtain the J-entry form field
HR = spformelement-> item (ccomvariant (J), ccomvariant (), & spinputelement );
If (failed (HR) continue;
Ccomvariant vname, vval, vtype; // obtain the name, value, and type of the form field.
HR = spinputelement. getpropertybyname (L "name", & vname );
If (failed (HR) continue;
HR = spinputelement. getpropertybyname (L "value", & vval );
If (failed (HR) continue;
HR = spinputelement. getpropertybyname (L "type", & vtype );
If (failed (HR) continue;
Lpctstr lpname = vname. bstrval? Ole2ct (vname. bstrval): _ T ("null"); // unknown domain name
Lpctstr lpval = vval. bstrval? Ole2ct (vval. bstrval): _ T ("null"); // null value, not input
Lpctstr lptype = vtype. bstrval? Ole2ct (vtype. bstrval): _ T ("null"); // unknown type
Enumfield (spinputelement, lptype, lpval, lpname); // pass and process the type, value, and name of the form field
} // Form field loop ends
} // Form loop ends
}
Void enumfield (ccomdispatchdriver spinputelement, cstring comtype, cstring comval, cstring comname)
{// Process form fields
If (comtype. find ("text")> = 0) & strcmp (comval, "null") = 0 & strcmp (comname, "username") = 0)
{
Cstrings TMP = "123456 ";
Ccomvariant vsetstatus (TMP );
Spinputelement. putpropertybyname (L "value", & vsetstatus );
}
If (comtype. find ("password")> = 0) & strcmp (comval, "null") = 0 & strcmp (comname, "password") = 0)
{
Cstrings TMP = "123456 ";
Ccomvariant vsetstatus (TMP );
Spinputelement. putpropertybyname (L "value", & vsetstatus );
}
If (comtype. Find ("Submit")> = 0 ))
{
Ihtmlelement * phelement;
Spinputelement-> QueryInterface (iid_ihtmlelement, (void **) & phelement );
Phelement-> click ();
}
}
// -------------------- End --------------------------------------//
2. Execute:
Void cdemodlg: onok ()
{
// Todo: add extra validation here
: Coinitialize (null); // initialize com
Enumie (); // enumeration Browser
: Couninitialize (); // release com
// Cdialog: onok ();
}