| 发表于:2008-01-16 12:14:236楼 得分:0 |
贴一段代码或许对你有用 void onlogin() { _variant_t v; hresult hr; variant id, index; ccomptr <idispatch> spdispatch; ccomqiptr <ihtmldocument2, &iid_ihtmldocument2> pdoc2; ccomqiptr <ihtmlelement, &iid_ihtmlelement> pelement; ccomqiptr <ihtmlelementcollection,&iid_ihtmlelementcollection> pelementcol; ccomptr <ihtmlinputtextelement> pinputtextelement; if (m_spshwinds == null) { hr = m_spshwinds.createinstance(__uuidof(shdocvw::shellwindows)); if (failed(hr)){ messagebox( "failed "); couninitialize(); } } if (m_spshwinds) { long n=0; m_spshwinds-> get_count(&n); //得到浏览器的个数 //遍历每一个浏览器 for (long i = 0; i < n; i++) { ccomptr <idispatch> spdisp; v = (long)i; spdisp =m_spshwinds-> item(&v); shdocvw::iwebbrowser2ptr spbrowser(spdisp); //生成一个ie窗口的智能指针 if (spbrowser) { //获取ihtmldocument2接口 if (succeeded(spbrowser-> get_document( &spdispatch))) pdoc2 = spdispatch; if(pdoc2!=null) { //获取所有元素的集合 if(succeeded(pdoc2-> get_all(&pelementcol))){ long p=0; if(succeeded(pelementcol-> get_length(&p))) { if(p!=0){ //遍历所有元素 for(long i=0;i <p;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_ihtmlelement,(void**)&pelement))) { //应该就这附近的几行错误吧 if(succeeded(pelement-> queryinterface(iid_ihtmlinputtextelement,(void**)&pinputtextelement))) { ccombstr strname( "csdn "); //用户名 ccombstr strpwd( "123456 "); //密码 ccombstr type; //输入框类型 pinputtextelement-> get_type(&type);//获取输入框类型 cstring strtype(type); strtype.makeupper(); if (lstrcmpi(strtype, _t( "text ")) == 0) { pinputtextelement-> put_value(strname);//设置文本框的值(用户名) } if (lstrcmpi(strtype, _t( "password ")) == 0) { pinputtextelement-> put_value(strpwd);//设置文本框的值(密码) } } } } } } } } } } } } } the following code will click submit button of html page in ie, modify it to click other button. /////////////////////////////////////////////////////////////////////////////////////// //click submit button of ie window //if it works, it is written by masterz,otherwise i don't //know who writes it^_^ /////////////////////////////////////////////////////////////////////////////////////// void cgetiesrcdlg::navigatetourl() { // import the following files in your stdafx.h // #import <mshtml.tlb> // internet explorer 5 // #import <shdocvw.dll> // refer to "connect to internet explorer instances, from your own process. " in www.codeguru.com shdocvw::ishellwindowsptr m_spshwinds; coinitialize(null); if(m_spshwinds.createinstance(__uuidof(shdocvw::shellwindows)) == s_ok) { idispatchptr spdisp; long ncount = m_spshwinds-> getcount(); for (long i = 0; i < ncount; i++) { _variant_t va(i, vt_i4); spdisp = m_spshwinds-> item(va); shdocvw::iwebbrowser2ptr spbrowser(spdisp); if (spbrowser != null) { idispatchptr spdisp; if(spbrowser-> get_document(&spdisp) == s_ok && spdisp!= 0 ) { mshtml::ihtmldocument2ptr sphtmldocument(spdisp); mshtml::ihtmlelementptr sphtmlelement; if(sphtmldocument==null) continue; sphtmldocument-> get_body(&sphtmlelement); if(sphtmldocument==null) continue; hresult hr; mshtml::ihtmlelementcollection* pcoll=null; hr=sphtmldocument-> get_all(&pcoll); if(pcoll!=null&&succeeded(hr)) { mshtml::ihtmlelement* pelem=null; _variant_t index; index.vt=vt_i4; index.intval=0; _variant_t name("submit"); idispatchptr disp; disp=pcoll-> item(name,index); if(disp==null) hr=e_fail; else { hr=disp-> queryinterface(&pelem); } if (succeeded(hr)&& pelem != null) { // bstr bstrhtml; pelem-> get_outerhtml(&bstrhtml); cstring str(bstrhtml); afxmessagebox(str); pelem-> click(); pelem-> release(); } pcoll-> release(); } } } } } else { afxmessagebox("shell windows interface is not avilable"); } couninitialize(); } | | |
|