Microsoft KB Archive/188015

= How To Access Methods/Properties of Container From Script =

Article ID: 188015

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q188015



SUMMARY
When hosting the WebBrowser control in a Visual C++ application, it may be necessary to access methods or properties of the container from script on a Web page. This article describes how to do this by implementing the IDocHostUIHandler interface.



MORE INFORMATION
By implementing the IDocHostUIHandler interface, you can control many of the User Interface features of the WebBrowser control in your hosting application. IDocHostUIHandler also allows you to extend the Dynamic HTML (DHTML) Object Model to access methods and properties of the container from within script.

The GetExternal method of IDocHostUIHandler provides this functionality. When script on a Web page calls "window.external.yourMethod," the WebBrowser control calls your GetExternal method to retrieve a pointer to the IDispatch of your hosting application. It is through this pointer that the WebBrowser control is able to access your methods and properties.

Once the WebBrowser control has a pointer to the IDispatch of the container, it then calls IDispatch::GetIDsOfNames to get the DISPID of the method or property called from script, yourMethod in this case.

Finally, the WebBrowser control calls IDispatch::Invoke with the DISPID retrieved from GetIDsOfNames.

Here are the steps you must follow to extend the DHTML Object Model to be able to access the container's methods and properties from script:  Implement IDocHostUIHandler.  Implement the IDocHostUIHandler::GetExternal method. Set the IDispatch parameter to that of your container like so: STDMETHOD(GetExternal)(IDispatch** ppDispatch) {  // Assumes you inherit from IDispatch *ppDispatch = (IDispatch*)this; (*ppDispatch)->AddRef;

return S_OK; }                    Return the dispatch ID (DISPID) of your method or property from GetIDsOfNames. If you added your method or property using a wizard, this will be done for you.  Implement the DISPID of your method or property in your implementation of IDispatch::Invoke like so: STDMETHODIMP CAtlBrCon::Invoke(DISPID dispidMember, REFIID riid,                              LCID lcid, WORD wFlags,                               DISPPARAMS* pDispParams,                               VARIANT* pvarResult,                               EXCEPINFO* pExcepInfo, UINT* puArgErr) {  switch (dispidMember) {     case DISPID_MYMETHOD_OR_PROPERTY: // Do something here

default: return E_INVALIDARG; }

return S_OK; }                     Call a method or property of the container from script like this:  Sub SomeControl_OnClick window.external.yourMethod End Sub  </ol>

<div class="references_section">