How To Access Methods/Properties of Container From Script

This article has been archived. It is offered "as is" and will no longer be updated.
Summary
When hosting the WebBrowser control in a Visual C++ application, it may benecessary to access methods or properties of the container from script on aWeb page. This article describes how to do this by implementing theIDocHostUIHandler interface.
More information
By implementing the IDocHostUIHandler interface, you can control many ofthe User Interface features of the WebBrowser control in your hostingapplication. IDocHostUIHandler also allows you to extend the Dynamic HTML(DHTML) Object Model to access methods and properties of the container fromwithin script.

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

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

Finally, the WebBrowser control calls IDispatch::Invoke() with the DISPIDretrieved from GetIDsOfNames().

Here are the steps you must follow to extend the DHTML Object Model to beable to access the container's methods and properties from script:
  1. Implement IDocHostUIHandler.
  2. 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;}					
  3. 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.
  4. 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;}					
  5. Call a method or property of the container from script like this:
     <SCRIPT LANGUAGE="VBScript">    Sub SomeControl_OnClick       window.external.yourMethod    End Sub </SCRIPT>					
References
For more information about the technologies discussed in this article,please refer to the documentation regarding Advanced Hosting Interfaces andIDocHostUIHandler in the following MSDN web site:(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by ScottRoberts, Microsoft Corporation
Properties

Article ID: 188015 - Last Review: 01/12/2015 16:01:02 - Revision: 4.0

  • Microsoft Internet Explorer 4.01 Service Pack 1
  • kbnosurvey kbarchive kbFAQ kbhowto kbscript kbwebbrowser KB188015
Feedback