C++/(COM)IWeb Browser2와 IHTML Document2, IHTML Window2 구하기





1. IWebBrowser2 #

  1. inline IWebBrowser2* GetIWebPointer()
  2. {
  3.     HRESULT hr ;
  4.     IOleContainer *pIContainer = NULL ;
  5.     IWebBrowser2 *pIWeb = NULL ;
  6.     IServiceProvider *pISP = NULL ;
  7.     // Get IOleClientSite interface pointer.
  8.     LPOLECLIENTSITE pIClientSite = NULL;
  9.     GetClientSite(&pIClientSite);
  10.     // Get IOleContainer interface poineter.
  11.     hr = pIClientSite->GetContainer(&pIContainer) ;
  12.     if (hr != S_OK) {
  13.         pIClientSite->Release() ;
  14.         return NULL ;
  15.     }
  16.     // Get IServiceProvider interface pointer.
  17.     hr = pIClientSite->QueryInterface(IID_IServiceProvider,(void **)&pISP) ;
  18.     if (hr != S_OK) {
  19.         pIContainer->Release() ;
  20.         pIClientSite->Release() ;
  21.         return NULL ;
  22.     }
  23.     // Get IWebBrowser2 interface pointer.
  24.     hr = pISP->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&pIWeb) ;
  25.     if (hr != S_OK) {
  26.         pIContainer->Release() ;
  27.         pIClientSite->Release() ;
  28.         pISP->Release() ;
  29.         return NULL ;
  30.     }
  31.     // release interface.
  32.     pIContainer->Release() ;
  33.     pIClientSite->Release() ;
  34.     pISP->Release() ;
  35.     return pIWeb ;
  36. }

2. IHTMLDocument2 #

  1. inline IHTMLDocument2 *GetDocument(IWebBrowser2* pWebBrower)
  2. {
  3.     IHTMLDocument2 *document = NULL;
  4.     if (pWebBrower != NULL) {
  5.         // get browser document's dispatch interface
  6.         IDispatch *document_dispatch = NULL;
  7.         HRESULT hr = pWebBrower->get_Document(&document_dispatch);
  8.         if (SUCCEEDED(hr) && (document_dispatch != NULL)) {
  9.             // get the actual document interface
  10.             hr = document_dispatch->QueryInterface(IID_IHTMLDocument2,
  11.                 (void **)&document);
  12.             // release dispatch interface
  13.             document_dispatch->Release();
  14.         }
  15.     }
  16.     return document;
  17. }

3. IHTMLWindow2 #

  1.     HRESULT hr;
  2.     CComVariant vtResult(0);
  3.     CComPtr<IDispatch>      spDisp = NULL;
  4.     // 도큐먼트 인터페이스 구하기
  5.     IWebBrowser2* pWebBrower = GetIWebPointer();
  6.     CComPtr<IHTMLDocument2> spDocument = GetDocument(pWebBrower);
  7.     CComPtr<IHTMLWindow2>   spWindow = NULL;
  8.     hr = spDocument->get_parentWindow(&spWindow);
  9.     spDocument = NULL;
  10. #if 0 // delete me (예제)
  11.     if (SUCCEEDED(hr) && spWindow != NULL)
  12.     {
  13.         spWindow->execScript(varResult.bstrVal, 
  14.             CComBSTR(_T("JScript")), &vtResult);
  15.         spWindow = NULL;
  16.     }
  17. #endif

이 글에는 0 개의 댓글이 있습니다.