如何访问浏览器帮助程序对象方法

时间:2011-07-27 05:46:53

标签: visual-c++ c++-cli

  

可能重复:
  Call BHO from Javascript function

如何从java脚本访问BHO的方法

1 个答案:

答案 0 :(得分:0)

很久以前我找到了同样问题的解决方案,但这只是BHO的一次性实验,所以今天很难记住该做什么。但我可以把这段代码作为进一步搜索的起点。

void CEventSink::Event_NavigateComplete(IDispatch *pDisp, LPOLESTR url)
{
    HRESULT hr;
    IDispatch *pTmpDisp;

    IWebBrowser2 *pFrame;
    hr = pDisp->QueryInterface(IID_IWebBrowser2, (void **)&pFrame);
    if (FAILED(hr) || !pFrame)
        return;

    hr = pFrame->get_Document(&pTmpDisp);
    if (FAILED(hr) || !pTmpDisp)
        return;

    IHTMLDocument2 *pDocument;
    hr = pTmpDisp->QueryInterface(IID_IHTMLDocument2, (void **)&pDocument);
    if (FAILED(hr) || !pDocument)
        return;

    IHTMLWindow2 *pWindow;
    pDocument->get_parentWindow(&pWindow);
    if (FAILED(hr) || !pWindow)
        return;

    IDispatchEx *pDispEx;
    hr = pWindow->QueryInterface(IID_IDispatchEx, (void **)&pDispEx);
    if (FAILED(hr) || !pDispEx)
        return;

    DISPID dispId;
    BSTR name = SysAllocString(_T("someMethod"));
    pDispEx->GetDispID(name, fdexNameEnsure, &dispId);
    SysFreeString(name);
    if (FAILED(hr) || dispId == DISPID_UNKNOWN)
        return;

    hr = this->QueryInterface(IID_IDispatch, (void **)&pTmpDisp);
    if (FAILED(hr) || !pTmpDisp)
        return;

    VARIANT v;
    VariantInit(&v);
    v.vt = VT_DISPATCH;
    v.pdispVal = pTmpDisp;

    DISPID ppid = DISPID_PROPERTYPUT;
    DISPPARAMS p;
    p.rgvarg = &v;
    p.cArgs = 1;
    p.rgdispidNamedArgs = &ppid;
    p.cNamedArgs = 1;

    hr = pDispEx->InvokeEx(dispId, LOCALE_USER_DEFAULT, 
                           DISPATCH_PROPERTYPUT, &p, NULL, NULL, NULL); 
}