Issuing commands to the HTML view
| 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.
- Use IHTMLDocument2::get_scripts() to get the IHTMLElementCollection of all scripts on the screen
- UseIHTMLElementCollection::item() to get an IDispatch pointer to the script function we want to run.
- Invoke the script function to get the data.
- 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.