views:

93

answers:

1

I found this example http://www.mvps.org/user32/webhost.cab that host an Internet Explorer WebBrowser object, and it uses this code to access the object

void webhostwnd::CreateEmbeddedWebControl(void)
{
  OleCreate(CLSID_WebBrowser,IID_IOleObject,OLERENDER_DRAW,0,&site,&storage,(void**)&mpWebObject);

  mpWebObject->SetHostNames(L"Web Host",L"Web View");

  // I have no idea why this is necessary. remark it out and everything works perfectly.
  OleSetContainedObject(mpWebObject,TRUE);

  RECT rect;
  GetClientRect(hwnd,&rect);

  mpWebObject->DoVerb(OLEIVERB_SHOW,NULL,&site,-1,hwnd,&rect);

  IWebBrowser2* iBrowser;
  mpWebObject->QueryInterface(IID_IWebBrowser2,(void**)&iBrowser);

  VARIANT vURL;
  vURL.vt = VT_BSTR;
  vURL.bstrVal = SysAllocString(L"http://google.com");
  VARIANT ve1, ve2, ve3, ve4;
  ve1.vt = VT_EMPTY;
  ve2.vt = VT_EMPTY;
  ve3.vt = VT_EMPTY;
  ve4.vt = VT_EMPTY;

  iBrowser->put_Left(0);
  iBrowser->put_Top(0);
  iBrowser->put_Width(rect.right);
  iBrowser->put_Height(rect.bottom);

  iBrowser->Navigate2(&vURL, &ve1, &ve2, &ve3, &ve4);

  VariantClear(&vURL);

  iBrowser->Release();
}

I don't have much experience with cpp, I want to know how to access that same ie object (to use Navigate2 for example) from a button or something like that. How could I achieve this?

A: 

mpWebObject is a member of the class webhostwnd. You can use the code,

IWebBrowser2* iBrowser;
  mpWebObject->QueryInterface(IID_IWebBrowser2,(void**)&iBrowser);

anywhere in the class to access the browser interface( once the mpWebObject is created).

If you are not hell bent on using the same code, here is a better example that could serve your purpose.

Jujjuru
thank you very much
extintor
Hi jujjuru, do you know how to access m_Browser from outside the CMyBrowserView class in that example you gave me? regards
extintor
You can add a function to CMyBrowserView that returns a reference to the m_Browser. Use api, GetActiveView( ) to access the view object. Refer to msdn for more explanation on how to use GetActiveView( ).
Jujjuru
Awesome, thank you very much!
extintor