How to get a IHTMLElement pointer to the <object> tag hosting an activex control

牧云@^-^@ 提交于 2019-12-01 05:34:35

问题


I have an ActiveX control generated by the FireBreath framework (http://firebreath.org). I need to get a reference to the <object> tag in the page that hosts the plugin from C++.

If I were using NPAPI, I would use the NPNVPluginElementNPObject constant with NPN_GetValue.

so to make sure I am being clear, say I have the following in the page:

<object id="testPlugin" type="application/x-someplugin" width="100%" height="100%"></object>

I want to get a reference to the plugin like I would if I used document.getElementById("testPlugin"), except from within the C++ code of the activex control that is inserted for that mimetype.

Please note that passing the id in as a <param> is not a good option for me, but if there is a way to get the ID from inside the activex control that may work.

edit: I am considering using getElementsByTagName and trying to find it through the DOM, but it would be difficult to tell the difference between two instances of the same plugin.


回答1:


Thanks to FireBreath contributor jtojanen from Finland, we finally have a solution.

The first thing is that the COM object must be registered as "Apartment", not "Single" (in the registry). Otherwise, this will not work; seems to be a bug in COM.

Then anywhere after SetClientSite is called, you can do the following:

CComQIPtr<IOleControlSite> site(m_spClientSite);
CComPtr<IDispatch> dispatch;
site->GetExtendedControl(&dispatch);
CComQIPtr<IHTMLElement2> htmlElement = dispatch;

Hope this saves someone some time; it's taken me almost 2 years to find someone who could answer this for me.

The object in htmlElement will be the <object> tag that wraps your plugin; so if you queryInterface for any of your interfaces, it should succeed, but it may not actually literally be your object, it will likely be a wrapper to your object.




回答2:


In C#:

    public int SetSite(object site)
    {
        if (site != null)
        {
            var oleControl = (IOleControlSite)site;
            object oHtmlElement;
            oleControl.GetExtendedControl(out oHtmlElement);
            var htmlElement = (IHTMLElement2)oHtmlElement;
            ...
        }
    }


来源:https://stackoverflow.com/questions/4103315/how-to-get-a-ihtmlelement-pointer-to-the-object-tag-hosting-an-activex-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!