Issuing commands to the HTML view

InfoDabble > Tech Notes > Exoware > Issuing commands to the HTML view
Jump to: navigation, search
Exoware ..... Tech Note
Issuing Commands to the HTML View Eric Hartwell, September 1998

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.

  1. IDispatchPtr spDisp(spDoc->GetScript());
  2. if (spDisp)
  3. {
  4. USES_CONVERSION;
  5. OLECHAR FAR* szMember = T2OLE(pszFunctionName);
  6. DISPID dispid;
  7. HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
  8. LOCALE_SYSTEM_DEFAULT, &dispid);
  9. if (SUCCEEDED(hr))
  10. {
  11. COleVariant vtResult;
  12. static BYTE parms[] = VTS_BSTR;
  13. try
  14. {
  15. COleDispatchDriver dispDriver(spDisp);
  16. dispDriver.InvokeHelper(dispid, DISPATCH_METHOD, VT_VARIANT,
  17. (void*)&vtResult, parms, pszArguments);
  18. _variant_t vResult(vtResult);
  19. strResult = (const char *)(_bstr_t)vResult;
  20. }
  21. catch (...) {} // Put some real code in here
  22. }
  23. }

Resources:

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