滴水逆向联盟

标题: VC++实现浏览器自动填表 [打印本页]

作者: 大灰狼    时间: 2014-10-15 10:30
标题: VC++实现浏览器自动填表




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

复制代码







欢迎光临 滴水逆向联盟 (http://www.dtdebug.com/) Powered by Discuz! X3.2