关闭

自动登录yahoo邮箱

标签: yahoonullvalidationloginbrowseruser
349人阅读 评论(0) 收藏 举报

感觉这个很有参考价值。

原文链接:http://blog.csdn.net/suisuibianbian/article/details/550260


在yahooDlg.h中定义变量

 IWebBrowser2 * pBrowser;
 BOOL bReady;       //判断yahoo邮箱页面是否完全打开
 CString m_strURL;
 CString m_strUserName;
 CString m_strPassWord;

在yahooDlg.cpp中

void CYahooDlg::OnOK() 
{
 // TODO: Add extra validation here
 m_strURL = "http://cn.mail.yahoo.com/";  //URL地址
 m_strUserName = "name";      //邮箱用户名
 m_strPassWord = "password";     //用户密码

 VARIANT id, index;

 BSTR bsStatus;
 bReady=0;
 BSTR bsPW = m_strPassWord.AllocSysString();
 BSTR bsUser=m_strUserName.AllocSysString();
 
 CString mStr;
 HRESULT hr;

 hr = CoInitialize(NULL);
 if(!SUCCEEDED(hr))
 {
  return;
 }

 hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, 
  IID_IWebBrowser2, (LPVOID *)&pBrowser); //Create an Instance of web browser
 if(hr!=S_OK)
 {
  char a[1024] = {0};
  DWORD b = GetLastError();
  sprintf(a,"CoCreateInstance fail a=%d hr = 0x%x",b,hr);
  return;
 }
 
 VARIANT_BOOL pBool=true;

 pBrowser->put_Visible( pBool ) ; //Commentout this line if you dont want the browser to be displayed
 COleVariant vaURL(m_strURL) ;
 COleVariant null;

 pBrowser->Navigate2(vaURL,null,null,null,null) ; //Open the URL page

 while(!bReady)  //This while loop maks sure that the page is fully loaded before we go to the next page
 {
  //如果用户手动关闭IE窗口,退出循环
  SHANDLE_PTR hHwnd;
  pBrowser->get_HWND(&hHwnd);
  if (NULL == hHwnd)
  {
   bReady=1;
   return;
  }

  //等待网页完全打开,退出循环
  pBrowser->get_StatusText(&bsStatus);
  mStr=bsStatus;
  if(mStr=="完毕" || mStr=="完成" || mStr=="Done" )
  {
   bReady=1;
  }

  Sleep(200);
 }
 
 IDispatch* pDisp;
 hr=pBrowser->get_Document(&pDisp);//Get the underlying document object of the browser
 
 if (pDisp == NULL )
 {
  CoUninitialize();
  return;
 }
 
 IHTMLDocument2* pHTMLDocument2;
 
 hr = pDisp->QueryInterface( IID_IHTMLDocument2,
  (void**)&pHTMLDocument2 );//Ask for an HTMLDocument2 interface

 if (hr != S_OK)
 {
  CoUninitialize();
  return ;
 }

 IHTMLElementCollection* pColl = NULL;//Enumerate the HTML elements
 
 hr = pHTMLDocument2->get_all( &pColl );
 if (hr != S_OK || pColl == NULL)
 {
  pHTMLDocument2->Release();
  CoUninitialize();
  return;
 }

 LONG celem;
 hr = pColl->get_length( &celem );//Find the count of the elements
 
 if ( hr != S_OK )
 {
  pHTMLDocument2->Release();
  pColl->Release();
  CoUninitialize();
  return;
 }

 for ( int i=0; i< celem; i++ )//Loop through each elment
 {
  IDispatch* pDisp2;
  
  V_VT(&id) = VT_I4;
  V_I4(&id) = i;
  V_VT(&index) = VT_I4;
  V_I4(&index) = 0;
  hr = pColl->item( id,index, &pDisp2 );//Get an element
  
  if ( hr == S_OK )
  {
   IHTMLElement* pElem;

   //Ask for an HTMLElemnt interface
   hr = pDisp2->QueryInterface(IID_IHTMLElement,(void **)&pElem);
   if ( hr == S_OK )
   {
    BSTR bstr;
    
    IHTMLInputTextElement* pUser;//We need to check for input elemnt on login screen
    hr = pDisp2->QueryInterface(IID_IHTMLInputTextElement,(void **)&pUser );
    if ( hr == S_OK )
    {
     pUser->get_name(&bstr);
     mStr=bstr;
     if(mStr=="login")
     {//Is this a User Id frield
      pUser->put_value(bsUser);//Paste the User Id
     }
     else if(mStr=="passwd")
     {//Or, is this a password field
      pUser->put_value(bsPW);// Paste your password into the field                 
     }
     pUser->Release();
     
    }
    else
    {
     IHTMLInputButtonElement* pButton;
     hr = pDisp2->QueryInterface(
      IID_IHTMLInputButtonElement, 
      (void **)&pButton);
     if ( hr == S_OK )
     {
      //pButton->get_type(&bstr);//This will send the all the information in correct format         
      pButton->get_value(&bstr);
      mStr=bstr;
      if (mStr=="登 录")
      {
       pElem->click();

       i=celem;
      }
      pButton->Release();
     }
    }
    pElem->Release();
   }
   pDisp2->Release();
  }
 }

 pColl->Release();

 pHTMLDocument2->Release();//For the next page open a fresh document

 pDisp->Release();

 CoUninitialize();
 return;
 //CDialog::OnOK();
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6214次
    • 积分:86
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:7篇
    • 译文:0篇
    • 评论:3条
    文章存档
    最新评论