具有多个接口的 ObjectForScripting 不起作用

发布于 2024-10-18 23:36:08 字数 1328 浏览 2 评论 0原文

我有一个 WinForm,它通过 ObjectForScripting 与 WebBrowserControl 交互。我的 WinForm 的基类不是 ComVisible,我不能或不会更改它。因为有一个 NonComVisibleBaseClass 我创建了一个 Interface 并将其设置为 ComVisible(true) 并设置 FormAttribute [ClassInterface(ClassInterfaceType.None)]。接口中的方法可以由 JavaScript 调用。它工作完美:

//Make the class visible for COM so we can set the ObjectForScripting
//Specify ClassInterfaceType.None to use the ComVisible Interface
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IMapControlInteractable
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IMapControlInteractable

但现在是我的问题。该接口包含多种功能。我想为单独的任务分组分离接口。所以我想要一个包含日志记录功能的接口和一个用于数据访问功能的接口等。

所以它会是这样的:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IDataAccess, ILogging
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IDataAccess
...
[ComVisible(true)]
public interface ILogging

但是当我这样做时,第二个接口(ILogging)的功能无法从Javascript访问。如果我切换接口的顺序,则无法访问 IDataAccess 函数。

因此,在 Javascript 中似乎只能访问第一个接口中的方法。

我该怎么做才能使每个接口的每个功能都可访问?再次强调,使 BaseClass ComVisible 并删除 ClassInterface 属性将起作用,但不是一个选项。

提前致谢!!

I have a WinForm which interacts with a WebBrowserControl through the ObjectForScripting. The baseclass of my WinForm is not ComVisible and I can not or will not change it. Because there is a NonComVisibleBaseClass I have created an Interface and set it ComVisible(true) and set the FormAttribute [ClassInterface(ClassInterfaceType.None)]. The methods in the Interface can be called by the JavaScript. And it works perfect:

//Make the class visible for COM so we can set the ObjectForScripting
//Specify ClassInterfaceType.None to use the ComVisible Interface
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IMapControlInteractable
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IMapControlInteractable

But now my problem. The Interface contains multiple functions. I want to separate Interfaces for separate task groupings. So I want an Interface which contains Logging functions and an Interface for DataAccess functions and so on.

So it would be something like:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IDataAccess, ILogging
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IDataAccess
...
[ComVisible(true)]
public interface ILogging

But when I do this the functions of the second Interface (ILogging) are not accessible from the Javascript. If I switch the order of the interfaces the IDataAccess functions are not accessible.

So it seems to be the case that only methods from the first Interface are accessible in Javascript.

What can I do to make every function of each Interface accessible? Once again, making the BaseClass ComVisible and deleting the ClassInterface attribute will work but is not an option.

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-10-25 23:36:08

在做类似的项目时,我们发现 JavaScript 只能访问生成 COM 包装器的默认接口,在您的情况下,它选择它找到的第一个 ComVisible 接口作为默认接口,因为您没有显式设置默认接口属性。问题是 JavaScript 没有 QueryInterface 类似物。

为了访问其他接口,我们需要为 JavaScript 创建我们自己的 QueryInterface 版本,方法是在默认接口中提供显式转换类型函数(不太优雅),或者拥有一个可以执行到正确的转换的单独对象ComVisible 接口类型。

希望有帮助!

When doing a similar project, we found that JavaScript was only able to access the default interface of the generate COM wrapper, in your case it is picking the first ComVisible interface it finds as the default one, since you are not explicitly setting the default interface attribute. The problem is that JavaScript has no QueryInterface analogue.

To get access to the other interfaces we need to create our own version of QueryInterface for JavaScript, either by providing an explicit cast-type function in the default interface (not so elegant) or having a separate object that can perform the conversion to the correct ComVisible interface type.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文