zoukankan      html  css  js  c++  java
  • 试玩C++ 操作页面控件

    最近数字和金山吵的热火朝天的,群里有人说网友的投票可能有工具刷出来的,觉得应该很有意思,就想自己试一下,玩了半天终于可以操作页面进行投票了,但这个投票做了IP限制,所以工具也无用武之地啊!典型的需求没做好,反正也是自己玩,把过程记下来下给自己备忘一下:

     

     

       1:  
       2: #include "stdafx.h"
       3: #include <windows.h>
       4: #include <string>
       5: #include "gtest/gtest.h"
       6: #include "BrwHelperTest.h"
       7: //#include <wstring>
       8:  
       9:  
      10:  
      11: #include "EnumFormVal.h"
      12:  
      13: #include <atlbase.h>
      14:  
      15: CComModule _Module;        // 由于要使用 CComDispatchDriver ATL的智能指针,
      16:                         // 所以声明它是必须的
      17:  
      18: #include <mshtml.h>        // 所有 IHTMLxxxx 的接口声明
      19: #include <atlcom.h>
      20:  
      21: #ifdef _DEBUG
      22: #define new DEBUG_NEW
      23: #undef THIS_FILE
      24: static char THIS_FILE[] = __FILE__;
      25: #endif
      26:  
      27: /////////////////////////////////////////////////////////////////////////////
      28: // The one and only application object
      29:  
      30: using namespace std;
      31:  
      32: void EnumIE( void );                                //枚举浏览器函数
      33: void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 );    //枚举子框架函数
      34: void EnumForm ( IHTMLDocument2 * pIHTMLDocument2 );    //枚举表单函数
      35:  
      36: bool bTrue = false;
      37:  
      38: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
      39: {
      40:     
      41:     // ShellExecute(NULL,L"open", L"http://tech.qq.com/zt2010/360pkduba",L"",L"", SW_SHOW );
      42:  
      43:  
      44:     ::CoInitialize(NULL);    //初始化 COM 公寓
      45:  
      46:     int count = 0;
      47:     for(;;)
      48:     {
      49:         EnumIE();                //枚举浏览器
      50:         printf("tou piao : %d \n", count++);
      51:         printf("========================================================================\n");
      52:     }
      53:     
      54:  
      55:     ::CoUninitialize();        //释放 COM 公寓
      56:  
      57:     cout << _T("======完成======") << endl;
      58:  
      59:  
      60:     return 0;
      61: }
      62:  
      63: void EnumIE( void )
      64: {
      65:     cout << _T("开始扫描系统中正在运行的浏览器实例") << endl;
      66:  
      67:     CComPtr< IShellWindows > spShellWin;
      68:     HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
      69:     if ( FAILED ( hr ) )
      70:     {
      71:         cout << _T("获取 IShellWindows 接口错误") << endl;
      72:         return;
      73:     }
      74:  
      75:     long nCount = 0;        // 取得浏览器实例个数(Explorer 和 IExplorer)
      76:     spShellWin->get_Count( &nCount );
      77:     if( 0 == nCount )
      78:     {
      79:         cout << _T("没有在运行着的浏览器") << endl;
      80:         return;
      81:     }
      82:  
      83:     for(int i=0; i<nCount; i++)
      84:     {
      85:         CComPtr< IDispatch > spDispIE;
      86:         hr=spShellWin->Item(CComVariant( (long)i ), &spDispIE );
      87:         if ( FAILED ( hr ) )    continue;
      88:  
      89:         CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
      90:         if ( !spBrowser )        continue;
      91:  
      92:         CComPtr < IDispatch > spDispDoc;
      93:         hr = spBrowser->get_Document( &spDispDoc );
      94:         if ( FAILED ( hr ) )    continue;
      95:  
      96:         CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
      97:         if ( !spDocument2 )        continue;
      98:  
      99:         // 程序运行到此,已经找到了 IHTMLDocument2 的接口指针
     100:  
     101:         // 删除下行语句的注释,把浏览器的背景改变看看
     102:         // spDocument2->put_bgColor( CComVariant( "green" ) );
     103:         
     104:         EnumForm( spDocument2 );        //枚举所有的表单
     105:         if( bTrue )
     106:         {
     107:             return;
     108:         }
     109:     }
     110: }
     111:  
     112: void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 )
     113: {
     114:     if ( !pIHTMLDocument2 )    return;
     115:  
     116:     HRESULT hr;
     117:  
     118:     CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
     119:     pIHTMLDocument2->get_frames( &spFramesCollection2 );    //取得框架frame的集合
     120:  
     121:     long nFrameCount=0;                //取得子框架个数
     122:     hr = spFramesCollection2->get_length( &nFrameCount );
     123:     if ( FAILED ( hr ) || 0 == nFrameCount )    return;
     124:  
     125:     for(long i=0; i<nFrameCount; i++)
     126:     {
     127:         CComVariant vDispWin2;        //取得子框架的自动化接口
     128:         hr = spFramesCollection2->item( &CComVariant(i), &vDispWin2 );
     129:         if ( FAILED ( hr ) )    continue;
     130:  
     131:         CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
     132:         if( !spWin2 )    continue;    //取得子框架的 IHTMLWindow2 接口
     133:  
     134:         CComPtr < IHTMLDocument2 > spDoc2;
     135:         spWin2->get_document( &spDoc2 );    //取得字框架的 IHTMLDocument2 接口
     136:  
     137:         EnumForm( spDoc2 );            //递归枚举当前子框架 IHTMLDocument2 上的表单form
     138:  
     139:         if( bTrue )
     140:         {
     141:             return;
     142:         }
     143:     }
     144: }
     145:  
     146: void EnumForm( IHTMLDocument2 * pIHTMLDocument2 )
     147: {
     148:     if( !pIHTMLDocument2 )    return;
     149:  
     150:     EnumFrame( pIHTMLDocument2 );    //递归枚举当前 IHTMLDocument2 上的子框架fram
     151:  
     152:     HRESULT hr;
     153:     CComBSTR bstrTitle;
     154:     pIHTMLDocument2->get_title( &bstrTitle );    //取得文档标题
     155:  
     156:     USES_CONVERSION;
     157:     cout << _T("开始枚举“") << OLE2CT( bstrTitle ) << _T("”的表单") << endl;
     158:     CComQIPtr< IHTMLElementCollection > spElementCollection;
     159:     hr = pIHTMLDocument2->get_forms( &spElementCollection );    //取得表单集合
     160:     if ( FAILED( hr ) )
     161:     {
     162:         wcout << L"获取表单的集合 IHTMLElementCollection 错误" << endl;
     163:         return;
     164:     }
     165:  
     166:     long nFormCount=0;                //取得表单数目
     167:     hr = spElementCollection->get_length( &nFormCount );
     168:     if ( FAILED( hr ) )
     169:     {
     170:         wcout << L"获取表单数目错误" << endl;
     171:         return;
     172:     }
     173:     
     174:     for(long i=0; i<nFormCount; i++)
     175:     {
     176:         IDispatch *pDisp = NULL;    //取得第 i 项表单
     177:         hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
     178:         if ( FAILED( hr ) )        continue;
     179:  
     180:         CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
     181:         pDisp->Release();
     182:  
     183:         long nElemCount=0;            //取得表单中 域 的数目
     184:         hr = spFormElement->get_length( &nElemCount );
     185:         if ( FAILED( hr ) )        continue;
     186:  
     187:         int count = 0;
     188:         for(long j=0; j<nElemCount; j++)
     189:         {
     190:             CComDispatchDriver spInputElement;    //取得第 j 项表单域
     191:             hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
     192:             if ( FAILED( hr ) )    continue;
     193:  
     194:             CComVariant vName,vVal,vType;        //取得表单域的 名,值,类型
     195:             hr = spInputElement.GetPropertyByName( L"name", &vName );
     196:             if( FAILED( hr ) )    continue;
     197:             hr = spInputElement.GetPropertyByName( L"value", &vVal );
     198:             if( FAILED( hr ) )    continue;
     199:             hr = spInputElement.GetPropertyByName( L"type", &vType );
     200:             if( FAILED( hr ) )    continue;
     201:  
     202:             LPCTSTR lpName = vName.bstrVal?
     203:                     OLE2CT( vName.bstrVal ) : L"NULL";    //未知域名
     204:             LPCTSTR lpVal  = vVal.bstrVal?
     205:                     OLE2CT( vVal.bstrVal  ) :L"NULL";    //空值,未输入
     206:             LPCTSTR lpType = vType.bstrVal?
     207:                     OLE2CT( vType.bstrVal ) : L"NULL";    //未知类型
     208:  
     209:             wcout << L"[" << lpType << L"] ";
     210:             wcout << lpName << L" = " << lpVal << endl;
     211:             wstring typeradio = L"radio";
     212:             if ( typeradio.compare(lpType) == 0)
     213:             {
     214:                 count++;
     215:                 if(count == 2 ||
     216:                     count == 5 ||
     217:                     count == 7 ||
     218:                     count == 10)
     219:                 {
     220:                     spInputElement.PutPropertyByName(OLESTR("checked"), &CComVariant(true));
     221:                 }
     222:             }
     223:  
     224:             typeradio = L"submit";
     225:             if ( typeradio.compare(lpType) == 0)
     226:             {
     227:                 CComQIPtr< IHTMLElement > spSingleElement;
     228:                 hr = spInputElement->QueryInterface( IID_IHTMLElement , (void**)&spSingleElement); 
     229:                 if( FAILED( hr ) )
     230:                     continue; 
     231:                 hr = spSingleElement->click(); 
     232:                 bTrue = true;
     233:                 return;
     234:  
     235:             }
     236:         }
     237:     }
     238: }

     

     


    作者:GangWang
    出处:http://www.cnblogs.com/GnagWang/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

     
  • 相关阅读:
    Prometheus服务发现
    持久化查询
    PromQL进阶
    PromQL基础
    Prometheus概述
    监控系统概念
    zabbix5x解决中文字体问题
    allure 插件新手 demo
    关于时间复杂度~
    IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库
  • 原文地址:https://www.cnblogs.com/GnagWang/p/1752174.html
Copyright © 2011-2022 走看看