Internet Explorer 插件工具栏按钮错误
我目前正在 C# 中开发一个 IE 工具栏按钮,它应该能够获取当前选项卡的内容并使用它。但是,每当单击按钮时,都会调用 IObjectWithSite.SetSite 函数(我的代码将在最后发布)并返回以下错误:
无法转换 COM 对象类型 “System.__ComObject”到接口类型 SHDocVw.IWebBrowser2'。这个操作 由于 QueryInterface 调用而失败 在 COM 组件上 与 IID 接口 '
该函数在加载站点时可以正常工作,但只有在单击工具栏中的按钮时才会抛出此错误。如前所述,调用 SetSite 函数,然后调用 IOleCommandTarget.Exec 函数。
所以,我想我的问题是:如何转换传递到 SetSite 函数的对象,以便我可以访问 Internet Explorer 当前打开的选项卡上的文档?
我当前这两个函数的相关代码如下:
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;
}
提前致谢。
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. 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:
Unable to cast COM object of type
'System.__ComObject' to interface type
SHDocVw.IWebBrowser2'. This operation
failed because the QueryInterface call
on the COM componenet for the
interface with IID
'
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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询 IWebBrowser2 的记录方法工具栏按钮站点是从站点查询 IServiceProvider,然后查询 IID_IWebBrowserApp 的 QueryService
The documented way to query IWebBrowser2 from the toolbar button site is to query IServiceProvider from the site then QueryService for IID_IWebBrowserApp
Exec 函数在实际执行之前调用 SetSite。为了解决这个问题并且仍然让 Exec 函数能够访问有关浏览器的信息,SetSite 和 GetSite 函数应该存在于 Exec 函数访问的单独类中。
The Exec function calls SetSite before actually executing. 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.