DispID在各个接口上必须是唯一的吗? [英] DispID must be unique across interfaces?

查看:118
本文介绍了DispID在各个接口上必须是唯一的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在旧的VB6应用程序中使用COM。

I use COM with an old VB6 application.

我将代码更改为在接口中使用DispID,因为它似乎比使用 [ClassInterface(ClassInterfaceType.AutoDual)]

I changed my code to use DispID in interfaces as it seems to work better than using [ClassInterface(ClassInterfaceType.AutoDual)].

但是,即使一个类使用两个接口,也可以从DispID(1)开始在每个接口中开始计数吗?

But is it allowed to begin in each interface counting from DispID(1), even when a class uses two interfaces?

这种方式稳定吗?还是我错过了一些误会?

Does it work this way stable? Or do I missunderstood something?

[ComVisible(true)]
[Guid("9E1125A6-...")]
public interface IMyInterface1
{
    [DispId(1)]
    string Name1 { get; }
}

[ComVisible(true)]
[Guid("123425A6-...")]
public interface IMyInterface2
{
    [DispId(1)]
    string Name2 { get; }
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class MyClass : IMyInterface1, IMyInterface2
{
    public string Name1 { get { return "Name1"; } }
    public string Name2 { get { return "Name2"; } }
}


推荐答案


即使类使用两个接口,是否也可以从DispID(1)开始在每个接口中计数?

Is it allowed to begin in each interface counting from DispID(1), even when a class uses two interfaces?

DISPID仅在界面内必须唯一。最好使用两个接口,每个接口都有自己的(不同的)DISPID 1属性,即使两个接口都是由同一个COM对象实现的。

DISPIDs have to be unique within the interface only. You are good to go with two interfaces each one having their own (different) DISPID 1 properties, even if both interfaces are implemented by the same COM object.

因为VB6是但是,您需要牢记的是,VB6不会喜欢在同一COM对象上实现的2个以上的调度接口,并且可能仅看到第一个/主接口。也就是说,问题不在于DISPID冲突(这根本不是问题),而是VB6无法与暴露2个双接口的对象一起正常工作的事实。在多个双接口

Since VB6 is mentioned, however, you need to keep in mind that VB6 won't like 2+ dispatch interfaces implemented on the same COM object, and might be "seeing" only the first/main one. That is, the problem is not DISPID collision (which is not a problem at all), but the fact that VB6 is unable to work correctly with objects exposing 2+ dual interfaces. The reason why this is happening in described on MSDN in Multiple Dual Interfaces:


由于仅公开了一个IDispatch接口,因此只能通过IDispatch接口访问对象的客户端将

Because only one IDispatch interface is exposed, clients that can only access your objects through the IDispatch interface will not be able to access the methods or properties in any other interface.

不幸的是,VB6就是这种情况。与更高级的环境不同,它以无法访问任何其他接口中的方法或属性的方式来查询接口。分配不同的DISPID并不能解决问题。

And sadly, it is the case of VB6. Unlike more advanced environments, it is querying interfaces in the way that "methods or properties in any other interface" are inaccessible. Assigning different DISPIDs is not going to help there though.

这篇关于DispID在各个接口上必须是唯一的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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