Creating a Web Browser Object (Windows Embedded CE 6.0)
1/6/2010
After you create the browser window as described in Creating a Browser Window, you must create a Web browser object. To do this, create an instance of the IWebBrowser and use the IOleObject to set up OLE activation, and IOleClientSite to set up a client site.
The following code example shows a method that is defined in the browser class, CreateBrowserObject that retrieves the IWebBrowser2 interface from the OLE object.
//Create the Web browser object.
HRESULT CBrowser::CreateBrowserObject()
{
HRESULT hr;
IUnknown *pUnk = NULL;
IOleObject *pObject = NULL;
RECT rc; // for activate
if (!_pWB2)
{
// Create an instance of a web browser object (from Shdocvw.dll).
hr = CoCreateInstance(CLSID_WebBrowser, NULL,
CLSCTX_INPROC_SERVER |
CLSCTX_INPROC_HANDLER,
IID_IUnknown, (LPVOID *)(&pUnk));
if (FAILED(hr))
goto Cleanup;
// Retrieve an IOleObject interface to set up OLE activation.
hr = pUnk->QueryInterface(IID_IOleObject, (LPVOID *)(&pObject));
if (FAILED(hr))
goto Cleanup;
// Check if Shdocvw requires a client site to be created first.
DWORD dwFlags;
hr = pObject->GetMiscStatus(DVASPECT_CONTENT, &dwFlags);
if (FAILED(hr))
goto Cleanup;
if (dwFlags & OLEMISC_SETCLIENTSITEFIRST)
{
// Get an IOleClientSite instance from the browser and pass it to the browser.
IOleClientSite *pClientSite;
hr = QueryInterface(IID_IOleClientSite, (LPVOID *)(&pClientSite));
if (FAILED(hr))
goto Cleanup;
hr = pObject->SetClientSite(pClientSite);
pClientSite->Release();
if (FAILED(hr))
goto Cleanup;
}
// Activate the object.
// Store off the IOleObject reference.
_pObject = pObject;
_pObject->AddRef();
::GetClientRect(_hWnd, &rc);
hr = _pObject->DoVerb(OLEIVERB_UIACTIVATE, NULL, this, 0, _hWnd, &rc);
if (FAILED(hr))
goto Cleanup;
// Retrieve the IWebBrowser2 interface from the IOleObject.
hr = _pObject->QueryInterface(IID_IWebBrowser2, (LPVOID *)&_pWB2);
if (FAILED(hr))
goto Cleanup;
}
Cleanup:
if (pUnk)
pUnk->Release();
if (pObject)
pObject->Release();
return hr;
}
See Also
Concepts
Creating an Internet Browser
Setting Up Event Sinks