通過視窗控制代碼,擷取IE控制項裡的內容(擷取IHTMLDocument2和HTML)

來源:互聯網
上載者:User
  1. char str[128];
  2. GetClassName(hwnd,str,sizeof(str));//擷取類名
  3. if(strcmp(str,"Internet Explorer_Server")==0)
  4. {
  5.     CoInitialize( NULL );
  6.     HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
  7.     CComPtr<IHTMLDocument2> spDoc;
  8.     LRESULT lRes;
  9.      
  10.     UINT nMsg = RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
  11.     SendMessageTimeout( hwnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
  12.         
  13.     LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );//匯出ObjectFromLresult函數
  14.     if ( pfObjectFromLresult != NULL )
  15.     {
  16.         HRESULT hr;
  17.         hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );//擷取IHTMLDocument2
  18.         if ( SUCCEEDED(hr) )
  19.         {
  20.             HGLOBAL hMemory;
  21.             hMemory = GlobalAlloc(GMEM_MOVEABLE, 0);//分配一個全域記憶體塊
  22.             
  23.             if (spDoc != NULL)
  24.             {
  25.                 CComPtr<IStream> spStream;
  26.                 CComQIPtr<IPersistStreamInit> spPersistStream = spDoc;
  27.                 if (SUCCEEDED(CreateStreamOnHGlobal(hMemory, TRUE, &spStream)))
  28.                 {
  29.                     spPersistStream->Save(spStream, FALSE);
  30.                     LPCTSTR pstr = (LPCTSTR) GlobalLock(hMemory);
  31.                     if (pstr != NULL)
  32.                     {
  33.                         
  34.                            
  35.                    
  36.                         CString refString (pstr);
  37.                             
  38.                         AfxMessageBox(refString);
  39.       
  40.                     }
  41.                     GlobalUnlock(hMemory);
  42.                 }
  43.             }
  44.         }
  45.     }
  46.     ::FreeLibrary( hInst );
  47.     CoUninitialize();
  48. }

 #include <mshtml.h>
#include <afxhtml.h>
#include <atlbase.h>
#include <oleacc.h>
#include <MsHtmcid.h>

標頭檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.