1. IndependentCode
// ----------- Start ---------------------//
# Include <atlbase. h>
# Include <mshtml. h>
# Include <winuser. h>
# Include <comdef. h>
# Include <string. h>
Void enumie (void); // process the webpage
Ccommodule _ module; // use the smart pointer of ccomdispatchdriver ATL, which must be declared here
# Include <atlcom. h>
Void enumallelement (ihtmldocument2 * pihtmldocument2 );
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: 8082/CSP/"; // 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.
Enumallelement (spdocument2); // enumerate all elements
}
}
}
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
Enumallelement (spdoc2); // recursively enumerate all controls on the current sub-framework ihtmldocument2
}
}
Void enumallelement (ihtmldocument2 * pihtmldocument2) // enumerate all fields
{
If (! Pihtmldocument2) return;
Enumframe (pihtmldocument2); // recursively enumerate the sub-frame on ihtmldocument2
Hresult hr;
Ccomqiptr <ihtmlelementcollection> spallelement;
HR = pihtmldocument2-> get_all (& spallelement); // obtain all elements in all webpages
If (failed (HR) return;
Long nlength = 0;
Spallelement-> get_length (& nlength );
For (INT I = 0; I <nlength; I ++)
{
Ccomptr <idispatch> Pdisp;
HR = spallelement-> item (colevariant (long) I), colevariant (long) 0), & Pdisp); // obtain a single element
If (succeeded (HR ))
{
// Ccomqiptr <ihtmlelement, & iid_ihtmlelement> pelement (Pdisp );
Ccomqiptr <ihtmlelement, & iid_ihtmlelement> pelement;
Pdisp-> QueryInterface (& pelement );
BSTR btemp;
Pelement-> get_id (& btemp); // you can obtain other features based on specific elements.
Cstring strtemp = btemp;
If (! Strtemp. isempty () & strtemp = "callno") // according to callno (Performance Improvement text Control ID), the caller's number is used to obtain the value or perform other processing.
{
Ihtmlinputtextelement * input;
Pdisp-> QueryInterface (iid_ihtmlinputtextelement, (void **) & input );
Input-> get_value (& btemp );
If (btemp = NULL) strtemp = "null ";
Else strtemp = btemp;
Cstdiofile iofile;
Iofile. Open ("callerno.txt", cfile: modecreate | cfile: modewrite | cfile: modenotruncate );
Iofile. seektoend (); // first locate to the end of the file
Cstring strinsert = strtemp + "/N ";
Iofile. writestring (strinsert );
Iofile. Close ();
}
}
}
}
// ----------- End ---------------------//
2. Run the Code:
Void cdemodlg: onok ()
{
// Todo: add extra validation here
: Coinitialize (null); // initialize com
Enumie (); // enumeration Browser
: Couninitialize (); // release com
// Cdialog: onok ();
}
From: http://blog.csdn.net/fjssharpsword/article/details/6193564