利用 SHDocVw::IShellWindowsPtr m_spSHWinds 遍历所有IE浏览器,得到里面的所有元素

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);//设置文本框的值(密码)
                                  }

      }

                                  }
          }
}
      }
}
        }
      }
     
          }
      }
      }
}

 

你可能感兴趣的:(浏览器,IE,null)