关闭

从HWnd得到IWebbrowser2接口

标签: nullmicrosoftinternetservershellhtml
5544人阅读 评论(10) 收藏 举报
分类:

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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:141520次
    • 积分:2013
    • 等级:
    • 排名:第19129名
    • 原创:46篇
    • 转载:25篇
    • 译文:0篇
    • 评论:67条
    最新评论
    开发相关