简体   繁体   English

Internet Explorer插件工具栏按钮错误

[英]Internet Explorer Addon Toolbar Button Error

I am currently developing an IE toolbar button in C# that is supposed to be able to get the contents of the current tab and work with it. 我目前正在用C#开发IE工具栏按钮,该按钮应该能够获取当前选项卡的内容并使用它。 However, whenever the button is clicked, the IObjectWithSite.SetSite function is called ( my code will be posted at the end) and returns the following error: 但是,每当单击按钮时,都会调用IObjectWithSite.SetSite函数(我的代码将在末尾发布)并返回以下错误:

Unable to cast COM object of type 'System.__ComObject' to interface type SHDocVw.IWebBrowser2'. 无法将类型为'System .__ ComObject'的COM对象转换为接口类型SHDocVw.IWebBrowser2'。 This operation failed because the QueryInterface call on the COM componenet for the interface with IID ' 该操作失败,因为在COM组件网络上对具有IID'的接口的QueryInterface调用

The function works properly when a site is loaded, but only throws this error when the button in the tool bar is clicked. 加载网站后,该功能可以正常工作,但是仅当单击工具栏中的按钮时,才会引发此错误。 As stated before, the SetSite function get called, and then the IOleCommandTarget.Exec function gets called. 如前所述,先调用SetSite函数,然后再调用IOleCommandTarget.Exec函数。

So, I guess my question is: what do I cast the object that is passed into the SetSite function so that I can access the document on the tab that Internet Explorer currently has open? 所以,我想我的问题是:我该如何转换传递给SetSite函数的对象,以便可以访问Internet Explorer当前打开的选项卡上的文档?

My current relevant code for those two functions is as follows: 我当前与这两个功能有关的代码如下:

int IObjectWithSite.SetSite(object site)
{
    String display = "";

    try { browser = (IWebBrowser2)site;}
    catch (Exception e) { display += e.Message + "\r\n"; }

    System.Windows.Forms.MessageBox.Show(display);

    return 0;
}


int IOleCommandTarget.Exec(IntPtr pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
    if (form == null)
    {
        form = new SomeForm();
        form.InputText = "";
    }
    form.ShowDialog();
    return 0;
}

Thanks in advance. 提前致谢。

从工具栏按钮站点查询IWebBrowser2的记录方法是从站点查询IServiceProvider,然后从查询服务查询IID_IWebBrowserApp。

The Exec function calls SetSite before actually executing. Exec函数在实际执行之前会调用SetSite。 In order to work around this and still have the Exec function be able to access information about the browser, the SetSite and GetSite functions should exist in a separate class that the Exec function accesses. 为了解决此问题,并使Exec函数仍然能够访问有关浏览器的信息,SetSite和GetSite函数应该存在于Exec函数访问的单独类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM