请问如何通过IE的句柄来得到IWebBrowser2接口?我看了一篇C++的http://www.zxboy.com/article.asp?id=100但不知道相关的类在C#中是哪个

解决方案 »

  1.   

    IWebBrowser2[color=#0000ff]* 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;
    }
    如何改写啊,我找了很久在C#中的IServiceProvider只有两个参数,好像在net3.5有这个是三个的而且参数类型也一样