Eric Hartwell's InfoDabble

 
Welcome to Eric Hartwell's InfoDabble
About | Site Map
Home Tech Notes Apollo 17: Blue Marble Apollo 17 Flight Journal   Calendars About me  
 Exoware .....  

Tech Note 

Issuing Commands to the HTML View

Eric Hartwell, September 1998

While it's possible to get the IDispatch pointer of the COM object and control it directly (see Setting HTML View Text Directly From a String), in some cases that may be overkill. This tech note describes a simpler approach.

It's fairly straightforward for an application to call a script function within an HTML page (see Q185127). So, to call a method of your COM object, make a script wrapper function and then call the wrapper.

  1. Use IHTMLDocument2::get_scripts() to get the IHTMLElementCollection of all scripts on the screen
  2. UseIHTMLElementCollection::item() to get an IDispatch pointer to the script function we want to run.
  3. Invoke the script function to get the data.
  4. Release all pointers and exit.

The sample program calls a script function by name and expects to get a string in return.


    IDispatchPtr spDisp(spDoc->GetScript());
    if (spDisp)
    {
        USES_CONVERSION;
        OLECHAR FAR* szMember = T2OLE(pszFunctionName);
        DISPID dispid;
        HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
                                LOCALE_SYSTEM_DEFAULT, &dispid);
        if (SUCCEEDED(hr))
        {
            COleVariant vtResult;
            static BYTE parms[] = VTS_BSTR;
            try
            {
                COleDispatchDriver dispDriver(spDisp);
                dispDriver.InvokeHelper(dispid, DISPATCH_METHOD, VT_VARIANT,
                                        (void*)&vtResult, parms, pszArguments);
                _variant_t vResult(vtResult);
                strResult = (const char *)(_bstr_t)vResult;
            }
            catch (...) {}                  // Put some real code in here
        }
    }

Resources:

MSDN, Microsoft Knowledge Base, Site Builder Workshop, Platform SDK, etc.

Creative Commons License

Unless otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License

 

Site Map | About Me