Problem in listening to Internet Explorer events
hi all,
i am trying to connect to internet explorer 6.0 in C++ using IE connection point DWebBrowserEvents2 . But the advise method of the IConnetionPoint interface fails giving an errorCONNECT_E_CANNOTCONNECT error code, also the cookie populated is zero. it has been observed that the IE queries the EventSink (using its IUnknown supplied in the Advise method) for the following interfaces.
IMarshal
IStdMarshalInfo
IExternalConnection
IUnknown
and some other GUID's
Following is the piece of code for the same :-
IClassFactory *pIFact=NULL;
IEnumConnectionPoints *pEnum=NULL;
IConnectionPoint *pAryCP;
ULONG ulConnReq=1,ulFetched=0;
static IID pIID;hr=CoGetClassObject(CLSID_InternetExplorer,CLSCTX_LOCAL_SERVER,NULL,IID_IClassFactory,(
void **)&pIFact);if(SUCCEEDED(hr)){hr=pIFact->CreateInstance(NULL,IID_IConnectionPointContainer,(LPVOID *)&m_pICPC);if(SUCCEEDED(hr)){
hr=m_pICPC->EnumConnectionPoints(&pEnum);
pEnum->Reset();
while(pEnum!=NULL){
hr=pEnum->Next(ulConnReq,&pAryCP,&ulFetched);
if(SUCCEEDED(hr)){
pAryCP->GetConnectionInterface(&pIID);
bool bStatus=IsEqualGUID(pIID,DIID_DWebBrowserEvents2);if(bStatus){
hr=pAryCP->Advise(m_pEventSink,&dwCookie);
if
(SUCCEEDED(hr) && dwCookie!=0)MessageBox(NULL,
"Event Sink Adviced","Success",0);elseMessageBox(NULL,"Advise Fails","FAILURe",0);
pEnum->Release();
pAryCP->Release();
break;}
}
pAryCP->Release();
}
kinly suggest what needs to be done to resolve this error.
Thanks in advance
Regards
Sumit Mehrotra
This class will catch events. WebBrowser will invoke WebBrowserEventsHandler::Invoke on events.
Use IDispEventSimpleImpl::DispEventAdvise(IUnknown*)/DispEventUnadvise(IUnknown*) to advise on events.
class
WebBrowserEventsHandler : public IDispEventSimpleImpl<0, WebBrowserEventsHandler, &DIID_DWebBrowserEvents2>{
virtual
HRESULT STDMETHODCALLTYPE Invoke( /* [in] */ DISPID dispIdMember,/* [in] */ REFIID riid,/* [in] */ LCID lcid,/* [in] */ WORD wFlags,/* [out][in] */ DISPPARAMS *pDispParams,/* [out] */ VARIANT *pVarResult,/* [out] */ EXCEPINFO *pExcepInfo,/* [out] */ UINT *puArgErr);//empty sink mapBEGIN_SINK_MAP(WebBrowserEventsHandler)
END_SINK_MAP()
};
Then you should use:
CComPtr<CAxHostWindow> BrowserHost_;
CComQIPtr<IWebBrowser2> WebBrowser_;
WebBrowserEventsHandler WebBrowserEvHandler_;
HRESULT hr = S_OK;
hr = CAxHostWindow::CreateInstance(&BrowserHost_);
if( FAILED(hr) ){
return hr;}
BSTR wbCLSID;
StringFromCLSID(CLSID_WebBrowser, &wbCLSID);
_bstr_t browserCLSID(wbCLSID);
CoTaskMemFree(wbCLSID);
//create WebBrowserif( FAILED(hr = BrowserHost_->CreateControl( browserCLSID, <your parent window>, NULL)) ) {
return hr;}
BrowserHost_->m_bAllowContextMenu = 0;
if( FAILED(hr = BrowserHost_->m_spUnknown->QueryInterface( IID_IWebBrowser2, (void**)&WebBrowser_ )) ){
return hr;}
WebBrowserEvHandler_.Advise( WebBrowser_ );
_variant_t url( <your url> ), vt;
hr = WebBrowser_->Navigate2( &url, &vt, &vt, &vt, &vt );
I wish it will help you
Thanks,
but i was able to resolve the error in COM itself
actually earlier i implemented the EventSink as a different class, when i implemented it in the component itself it worked.
Now i am able to advise the event sink to the IE, but a new problem has croppped up.
As and when the event sink is advised my the IE gets corrupted. Thereafter to open the IE i have to right click the IE icon and click run as <user name>. simple clicking the IE icon does creates an IE instanse but the window is not visible.
Also if i request for a web page an error message saying "c:\documents and settings\sumit.mehrotra\desktop is not accessible"
To resolve this i have to rename/remove my credentials folder i.e. "c:\documents and settings\<sumit.mehrotra" using the administrator login and then restart the computer.
kindly suggest the needful to resolve this error.