凤之焚的专栏

业与情的纠缠,死而复生的蜕变

从HWnd得到IWebbrowser2接口

IWebBrowser2* GetIEFromHWnd(HWND hIEWindow)
{ HWND hWnd ;
 if(hIEWindow==NULL){
   hWnd= FindWindow("IEFrame", NULL);
            if(hWnd==NULL)
   hWnd= FindWindow("CabinetWClass", NULL);
     if( hWnd == NULL){
      MessageBox (NULL,"No Running instance of Internet Explorer!","message", MB_OK);
     }
     // walk Shell DocObject View->Internet Explorer_Server
     HWND hWndChild = FindWindowEx(hWnd, 0, "Shell DocObject View", NULL);
     if(hWndChild !=0){
      hWndChild = FindWindowEx(hWndChild, 0, "Internet Explorer_Server", NULL);
  }
  hWnd=hWndChild;
 }
 else{
  hWnd=hIEWindow;
 }
 // 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
 HINSTANCE hInst = LoadLibrary( _T("OLEACC.DLL") );
 IWebBrowser2* pWebBrowser2=NULL;
 if ( hInst != NULL ){
  if ( hWnd != NULL ){
  
   LRESULT lRes;
   UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
   ::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

   LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
   if ( pfObjectFromLresult != NULL ){
    HRESULT hr;
    CComPtr<IHTMLDocument2>spDoc;
   
    hr=pfObjectFromLresult(lRes,IID_IHTMLDocument2,0,(void**)&spDoc);
    if ( SUCCEEDED(hr) ){
    
     CComPtr<IHTMLWindow2>spWnd2;
     CComPtr<IServiceProvider>spServiceProv;
     hr=spDoc->get_parentWindow ((IHTMLWindow2**)&spWnd2);
     if(SUCCEEDED(hr)){
     
     hr=spWnd2->QueryInterface (IID_IServiceProvider,(void**)&spServiceProv);
      if(SUCCEEDED(hr)){
       hr = spServiceProv->QueryService(SID_SWebBrowserApp,
                                         IID_IWebBrowser2,
                                         (void**)&pWebBrowser2);

      
      }
     }
    }

    }
   }
  
  ::FreeLibrary(hInst);
 }
 else{//如果没有安装MSAA
  MessageBox(NULL,_T("Please Install Microsoft Active Accessibility"),"Error",MB_OK);
 }
 return pWebBrowser2;
}

阅读更多
个人分类: IE编程相关
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

从HWnd得到IWebbrowser2接口

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭