IE框架表单遍历

  1 // HtmlWeb.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <atlbase.h>
  6 #include <MsHTML.h>
  7 #include <oleacc.h>
  8 #pragma comment(lib,"oleacc.lib")
  9 int main()
 10 {
 11     BSTR sztitle;
 12     long lFramesLength = 0;
 13     HWND hwnd = (HWND)2426960;
 14     HRESULT hr = ::CoInitialize(NULL);
 15     UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
 16     LRESULT result;
 17     SendMessageTimeout(hwnd, nMsg, 0l, 0l, SMTO_ABORTIFHUNG, 1000, (PDWORD)&result);
 18     CComPtr <IHTMLDocument2> spDoc;
 19     hr = ::ObjectFromLresult(result, IID_IHTMLDocument2, 0, (LPVOID *)&spDoc);
 20     if (SUCCEEDED(hr))
 21     {
 22         CComPtr<IHTMLFramesCollection2> frames;
 23         CComPtr<IHTMLElementCollection> ElementCoole;
 24         spDoc->get_URL(&sztitle);
 25         wprintf_s(L"title:%s\r\n", sztitle);
 26         spDoc->get_cookie(&sztitle);
 27         wprintf_s(L"title:%s\r\n", sztitle);
 28         hr = spDoc->get_forms(&ElementCoole);
 29         if (SUCCEEDED(hr))
 30         {
 31             hr = ElementCoole->get_length(&lFramesLength);
 32             if (SUCCEEDED(hr))
 33             {
 34                 VARIANT varIndex, varResult;
 35                 VariantInit(&varIndex);
 36                 VariantInit(&varResult);
 37                 varResult.vt = VT_I4;
 38                 varResult.vt = VT_I4;
 39                 IDispatch *pDispatch;
 40                 for (long i =0;i<lFramesLength;i++)
 41                 {
 42                     varIndex.lVal = i;
 43                     if (ElementCoole->item(varIndex, varResult, &pDispatch) == S_OK)
 44                     {
 45                         CComQIPtr<IHTMLFormElement> spFormElement = pDispatch;
 46                         pDispatch->Release();
 47                         spFormElement->get_name(&sztitle);
 48                         wprintf_s(L"title:%s\r\n", sztitle);
 49                         IDispatch* spInputElement;
 50                         hr = spFormElement->get_length(&lFramesLength);
 51                         if (SUCCEEDED(hr))
 52                         {
 53                             for (long k=0;k<lFramesLength;k++)
 54                             {
 55                                 if (spFormElement->item(CComVariant(k), CComVariant(), &spInputElement) == S_OK)
 56                                 {
 57                                     CComQIPtr<IHTMLInputElement> spInputEle = spInputElement;
 58                                     spInputElement->Release();
 59                                     BSTR vName, vVal, vType;  //取得表单域的 名,值,类型 
 60                                     hr = spInputEle->get_name(&vName);
 61                                     if (FAILED(hr))
 62                                         continue;
 63                                     hr = spInputEle->get_type(&vType);
 64                                     if (FAILED(hr))
 65                                         continue;
 66                                     hr = spInputEle->get_value(&vVal);
 67                                     if (FAILED(hr))
 68                                         continue;
 69                                     wprintf_s(L"name:%s \t value:%s \t type:%s \r\n", vName,vVal,vType);
 70                                 }
 71                             }
 72                         }
 73                     }
 74                 }
 75             }
 76         }
 77 #if 0
 78         hr = spDoc->get_frames(&frames);
 79         if (SUCCEEDED(hr))
 80         {
 81             VARIANT varIndex, varResult;
 82             VariantInit(&varIndex);
 83             VariantInit(&varResult);
 84             varIndex.vt = VT_I4;
 85             varResult.vt = VT_DISPATCH;
 86             IDispatch *pDispatch;
 87             hr = frames->get_length(&lFramesLength);
 88             if (SUCCEEDED(hr))
 89             {
 90                 for (long i = 0; i < lFramesLength; i++)
 91                 {
 92                     varIndex.lVal = i;
 93                     hr = frames->item(&varIndex, &varResult);
 94                     if (SUCCEEDED(hr))
 95                     {
 96                         pDispatch = varResult.pdispVal;
 97                     }
 98                 }
 99             }
100 
101         }
102 #endif
103     }
104     ::CoUninitialize();
105     return 0;
106 }

posted @ 2018-05-14 18:09  轻风々  阅读(223)  评论(0编辑  收藏  举报