遍历ie元素

枚举IE实例及表单
本文介绍了一种方法,用于枚举系统中所有正在运行的Internet Explorer实例,并进一步枚举每个IE窗口中的表单元素及其值。此过程涉及使用COM接口与IE交互,包括改变背景颜色等操作。
  1. #include <atlbase.h> //atl
  2. #include <atlcom.h>
  3. #include <mshtml.h> //所有的IHTMLXXX的借口声明
  4. #include <iostream>
  5. #include <exdisp.h>
  6. using namespace std;
  7. CComModule  _Module; //由于要使用 CComDispatchDriver ATL的智能指针,所以声明它是必须的
  8. void EnumIE(); //枚举浏览器函数
  9. void EnumFrame(IHTMLDocument2* pDoc); //枚举子框架函数
  10. void EnumForm(IHTMLDocument2* pDoc);  //枚举表单函数
  11. int main()
  12. {
  13.     ::CoInitialize(NULL);
  14.     ::EnumIE();
  15.     ::CoUninitialize();
  16.     return 0;
  17. }
  18. void EnumIE()
  19. {
  20.     CComPtr<IShellWindows> spShellWin;
  21.     HRESULT hr = S_FALSE;
  22.     long nCount = 0;
  23.     long i = 0;
  24.     cout << "开始遍历系统中正在运行的IE实例" << endl;
  25.     hr = spShellWin.CoCreateInstance(CLSID_ShellWindows);
  26.     if (FAILED(hr))
  27.     {
  28.         cout << "获取IShellWindows接口错误!" << endl;
  29.         return;
  30.     }
  31.     spShellWin->get_Count(&nCount);
  32.     if (nCount <= 0)
  33.     {
  34.         cout << "没有正在运行的浏览器!" << endl;
  35.         return;
  36.     }
  37.     for (i = 0; i < nCount; i++)
  38.     {
  39.         CComPtr<IDispatch> spIe;
  40.         
  41.         hr = spShellWin->Item(CComVariant((long)i), &spIe);
  42.         if (FAILED(hr))
  43.         {
  44.             continue;
  45.         }
  46.         CComQIPtr<IWebBrowser2> spBrowser = spIe;
  47.         if (!spBrowser)
  48.         {
  49.             continue;
  50.         }
  51.         CComPtr<IDispatch> spDisp;
  52.         hr = spBrowser->get_Document(&spDisp);
  53.         
  54.         if (FAILED(hr))
  55.         {
  56.             continue;
  57.         }
  58.         CComQIPtr<IHTMLDocument2> spDoc = spDisp;
  59.         if (!spDoc)
  60.         {
  61.             continue;
  62.         }
  63.         spDoc->put_bgColor(CComVariant("blue"));
  64.         ::EnumForm(spDoc);
  65.     }
  66. }
  67. void EnumFrame(IHTMLDocument2* pDoc)
  68. {
  69.     HRESULT hr = S_FALSE;
  70.     long nCount = 0;
  71.     long i = 0;
  72.     if (!pDoc)
  73.     {
  74.         return;
  75.     }
  76.     CComPtr<IHTMLFramesCollection2> spCollFrm;
  77.     pDoc->get_frames(&spCollFrm);
  78.     hr = spCollFrm->get_length(&nCount);
  79.     if (FAILED(hr) || (nCount <= 0))
  80.     {
  81.         return;
  82.     }
  83.     for (i = 0; i < nCount; i++)
  84.     {
  85.         CComVariant vt;
  86.         hr = spCollFrm->item(&CComVariant(i), &vt);
  87.         if (FAILED(hr))
  88.         {
  89.             continue;
  90.         }
  91.         CComQIPtr<IHTMLWindow2> spWin2 = vt.pdispVal;
  92.         if (!spWin2)
  93.         {
  94.             continue;
  95.         }
  96.         CComPtr<IHTMLDocument2> spDoc2;
  97.         spWin2->get_document(&spDoc2);
  98.         ::EnumForm(spDoc2);
  99.     }
  100. }
  101. void EnumForm(IHTMLDocument2* pDoc)
  102. {
  103.     HRESULT hr = S_FALSE;
  104.     CComBSTR bstrTitle;
  105.     long nCount = 0;
  106.     long i = 0;
  107.     if (!pDoc)
  108.     {
  109.         return;
  110.     }
  111.     pDoc->get_title(&bstrTitle);
  112.     USES_CONVERSION;
  113.     cout << "开始枚举 " << OLE2CT(bstrTitle) << " 的表单" << endl;
  114.     CComQIPtr<IHTMLElementCollection> spCollElm;
  115.     hr = pDoc->get_forms(&spCollElm);
  116.     if (FAILED(hr))
  117.     {
  118.         cout << "获取表单集合错误!" << endl;
  119.         return;
  120.     }
  121.     hr = spCollElm->get_length(&nCount);
  122.     if (FAILED(hr))
  123.     {
  124.         cout << "获取表单输入错误!" << endl;
  125.         return;
  126.     }
  127.     for (i = 0; i < nCount; i++)
  128.     {
  129.         IDispatch* pDisp = NULL;
  130.         hr = spCollElm->item(CComVariant(i), CComVariant(), &pDisp);
  131.         if (FAILED(hr))
  132.         {
  133.             continue;
  134.         }
  135.         CComQIPtr<IHTMLFormElement> spEle = pDisp;
  136.         pDisp->Release();
  137.         long nElemCount = 0;
  138.         long j = 0;
  139.         hr = spEle->get_length(&nElemCount);
  140.         if (FAILED(hr))
  141.         {
  142.             continue;
  143.         }
  144.         for (j = 0; j < nElemCount; j++)
  145.         {
  146.             CComDispatchDriver spInputElem;
  147.             hr = spEle->item(CComVariant(j), CComVariant(), &spInputElem);
  148.             if (FAILED(hr))
  149.             {
  150.                 continue;
  151.             }
  152.             CComVariant vName, vVal, vType;
  153.             hr = spInputElem.GetPropertyByName(L"name", &vName);
  154.             if (FAILED(hr))
  155.             {
  156.                 continue;
  157.             }
  158.             hr = spInputElem.GetPropertyByName(L"value", &vVal);
  159.             if (FAILED(hr))
  160.             {
  161.                 continue;
  162.             }
  163.             hr = spInputElem.GetPropertyByName(L"type", &vType);
  164.             if (FAILED(hr))
  165.             {
  166.                 continue;
  167.             }
  168.             LPCTSTR lpName = vName.bstrVal ? OLE2CT(vName.bstrVal) : "NULL";
  169.             LPCTSTR lpVal = vVal.bstrVal ? OLE2CT(vVal.bstrVal) : "NULL";
  170.             LPCTSTR lpType = vVal.bstrVal ? OLE2CT(vType.bstrVal) : "NULL";
  171.             cout << "[" << lpType << "] " << lpName << " = " << lpVal << endl;
  172.         }
  173.     }
  174. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值