C#COM自动化的双接口和仅分发接口之间的区别 [英] Difference between Dual interface and Dispatch only interface for C# COM automation

查看:112
本文介绍了C#COM自动化的双接口和仅分发接口之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在针对C ++ COM服务器实现c#COM客户端。当我将COM接口标记为 Dual时,COM客户端可以正常工作,但是当我删除 Dual属性时,它会引发InvalidCastException。

I am implementing a c# COM client against a C++ COM server. The COM client functions correctly when I mark the COM interface as "Dual" but it throws an InvalidCastException when I remove the "Dual" attribute.

因此,对我来说很容易解决将其标记为双重。但是从在线阅读来看,它似乎不是推荐用于COM服务器的方法。任何人都可以向我解释(用外行术语)将接口标记为双重接口的重要性,为什么不推荐使用它?

Therefore the easy fix for me is to mark it as Dual. But from reading online, it looks like it is not the recommended approach to use for COM servers. Can anyone explain to me the significance (in layman terms) to marking an interface as dual and why it would not be recommended?

我仅出于测试目的需要它,而我正在使用C#客户端(不要指望我会使用VB)

I only need it for testing purposes, and I'm using C# client (don't anticipate that I ever will use VB)

或者,有人可以为我指出一个为非双接口创建C#COM客户端的好方法(MSDN上的示例都是双接口)。

Alternatively, can anyone point me to a good walkthrough on creating a C# COM client for non-Dual interfaces (the examples on MSDN all are dual interfaces) Thanks!

推荐答案

创建供公共消费的COM对象时,对于如何使您的接口可供公共消费,您有不同的选择。

When you create a COM object for public consumption, you have different options as to how you can make your interface(s) available for public consumption.

如果您只想使COM对象可用于支持指针和vtables之类的低级编程语言(如C ++,C,C#等),则只需创建一个

If you only want to make your COM object available to low-level programming languages (like C++, C, C#, etc.) which support things like pointers and vtables, you can just create a "custom" interface which inherits from IUnknown.

这种方法的问题在于,您的COM对象将无法用于脚本客户端(VBScript,JScript,WScript等)。 ),因为这些客户端不了解指针和vtable。相反,您可以将COM对象接口实现为IDispatch的派生。 IDispatch提供了一些方法,这些方法允许脚本客户端在运行时而不是在编译时动态发现其方法和属性。但是,此过程比使用IUnknown效率低,因为客户端必须在调用所需COM对象的方法之前进行额外的方法调用。

The problem with this approach is that your COM object will not be available to scripting clients (VBScript, JScript, WScript, etc.), as these clients don't understand pointers and vtables. Instead, you would implement your COM object interface as a derivation of IDispatch. IDispatch provides some methods that allow a scripting client to dynamically discover its methods and properties at runtime instead of compile time. However, this process is less efficient than using IUnknown, as the client has to make extra method calls before calling the desired COM object's method.

因此,如果您只是针对低级编程语言,您可以使用继承自IUnknown的自定义接口来实现COM接口

So, if you're only targeting low-level programming languages, you could implement your COM interfaces using "custom" interfaces that inherit from IUnknown

如果您仅针对脚本客户端,则可以实现仅使用IDispatch的接口。

If you're only targeting scripting clients, you could implement your interfaces using only IDispatch.

如果您希望COM对象可用于脚本语言,并且还希望低级语言能够在非常有效的方式,您应该同时实现这两种方式,这被称为双重。微软的示例可能都是双重的,因为他们希望它们的示例既可以使用脚本语言也可以使用低级语言(低级语言可以使用IDispatch,但是需要更多的工作)。

If you want your COM object to be available to scripting languages, and you also want low-level languages to be able to call your methods in a very efficient manner, you should implement both, and this is called "Dual". Probably, Microsoft's examples are all "Dual" because they want their examples to work with both scripting and low-level languages (low-level languages can use IDispatch, but it takes more work).

有关更多信息,我建议您观看 .Net PluralSight互操作性课程(您可以注册免费试用订阅)。本课程的 COM部分向您展示如何创建非Double(简单IUnknown接口)以及 Dual接口。

For more info, I would suggest you watch the .Net Interoperability Course at PluralSight (you can sign up for a free trial subscription). The "COM" part of this course shows you how to create a non-Dual (simple IUnknown interface) as well as "Dual" interfaces.

这篇关于C#COM自动化的双接口和仅分发接口之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆