简体   繁体   English

通过COM为C ++客户端公开C#

[英]Exposing C# via COM for C++ Client

we're considering exposing some C# types to C++ clients via COM. 我们正在考虑通过COM向C ++客户端公开一些C#类型。 What problems can we expect to hit over the life of the project? 我们可以期待在项目的整个生命周期中遇到什么问题? Eg how will versioning be managed? 例如,如何管理版本控制?

On versioning, it would seem from reading this that we should decorate our types to be exposed with [ClassInterface(ClassInterfaceType.None)] and use an explicit interface. 在版本控制中,从阅读本文看 ,我们应该使用[ClassInterface(ClassInterfaceType.None)]来装饰我们的类型,并使用显式接口。 That way I assume we fully control the interface that will be exposed to COM clients. 这样我认为我们完全控制将暴露给COM客户端的接口。

Thanks in advance. 提前致谢。

Since you are using a C++ client you should definitely use explicit interfaces for early binding. 由于您使用的是C ++客户端,因此您应该使用显式接口进行早期绑定。 Dispatch interfaces are useful when using scripting clients such as VBS but they are rarely useful for C++ clients. 在使用诸如VBS之类的脚本客户端时,调度接口很有用,但它们对C ++客户端很少有用。

The only way to version an interface is to create a new interface (possibly inheriting from the original interface). 版本化接口的唯一方法是创建一个新接口(可能继承自原始接口)。 When using explicit interfaces you have full control over this process. 使用显式接口时,您可以完全控制此过程。

This means you should create an interface for every class that you intend to expose via COM. 这意味着您应该为要通过COM公开的每个类创建一个接口。 Don't forget to mark every interface and class with the ComVisible and Guid attributes. 不要忘记使用ComVisible和Guid属性标记每个接口和类。 Also all your classes must have a default constructor. 此外,您的所有类都必须具有默认构造函数。

You'll have to read about the GUID attribute ( including this ) to maintain binary compatibility and only rebuild the clients when necessary. 您必须阅读GUID属性( 包括此属性)以维护二进制兼容性,并且仅在必要时重建客户端。

Also you might be interested in the ComVisible attribute that helps reduce registry pollution . 您也可能对有助于减少注册表污染的ComVisible属性感兴趣。

To get full control over COM interfaces, define them in MIDL. 要完全控制COM接口,请在MIDL中定义它们。 Build a type library with those interfaces in a C++ project, then import type library to C# and implement interfaces. 在C ++项目中使用这些接口构建类型库,然后将类型库导入C#并实现接口。

This approach is useful with complex interfaces where marshaling is not trivial. 这种方法对于复杂的接口很有用,其中编组不是微不足道的。

Versions should be done COM-style, changing GUIDs and adding new or inheriting interfaces. 版本应该是COM样式,更改GUID并添加新的或继承的接口。

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

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