COM双接口

发布于 2024-09-01 13:18:23 字数 180 浏览 10 评论 0原文

COM 中的双接口是一种可以通过 DispInterface 或通过 VTable 方法访问的接口。

现在有人可以告诉我这两种方法之间到底有什么区别吗?

我认为 VTable 是一个虚拟表,在实现具有可以在子类中重写的虚拟函数的类层次结构时,它保存指向不同函数的指针。但是我不明白这与 COM 中的双接口有何关系?

A dual interface in COM is one that is able to be accessed via a DispInterface or via VTable methods.

Now can someone tell me what is exactly what the difference is between the two methods?

I thought that a VTable is a virtual table which holds the pointers to the different functions when implementing a class hierarchy which has virtual functions that can be overridden in child classes. However I do not see how this is related to a dual interface in COM?

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

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

发布评论

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

评论(3

无远思近则忧 2024-09-08 13:18:23

简而言之,COM是二进制规范,而不是语言规范。双接口和代码中的派生类之间确实没有关联。苹果和橙子。

VTable 是“早期绑定”的,因此速度更快。您知道在编译时调用的方法的类型。

使用 DispInterface 是“后期绑定”,因此速度较慢,但​​更灵活。它广泛用于脚本编写。方法和属性类型是在运行时确定的。

我希望这个简短的解释有所帮助。

In short, COM is binary specification, not a language specification. There really isn't a correlation between dual interfaces and deriving classes in code. Apples and oranges.

The VTable is "early bound" and this therefore faster. You know the type of method you are calling at compile time.

Using the DispInterface is "late bound" and is therefore slower, but more flexible. It's used extensively for scripting. The method and property types are determined at runtime.

I hope this brief explanation helps.

自此以后,行同陌路 2024-09-08 13:18:23

主要区别在于调用对象方法的方式。如果 DispInterface 调用通过 IDispatch::Invoke 方法(在脚本中使用或没有接口描述),请参阅 备注。此方法比第二种方法慢得多。第二种情况直接使用VTable进行方法调用(用于C++或.NET语言的调用)

The main difference is in the way of calling object methods. In the case of DispInterface call goes through IDispatch::Invoke method (used in scripts or in the absence of the interface description) see remarks. This method is much slower second option. In the second case used directly VTable for method calls (used for calls from C + + or. NET languages)

日久见人心 2024-09-08 13:18:23

我只想回答托尼的其他问题。

如果您想创建一个可以从 VBScript/JScript 或旧的“经典”ASP 访问的 COM,您必须实现 IDispatch。

在 Visual Basic 6 或 MS Office 的 VBA 中,可以使用这两种方法。如果您将引用添加到 COM,那么您将使用“早期绑定”(IUnknown 或 VTable)。如果您在 VB6 或 VBA 中通过 CreateObject ("ProgIdName") 使用 COM,那么您将使用“后期绑定”。

理解使 COM 可从 VB6/VBA 等访问非常重要。仅仅实现 IUnknown 接口是不够的。您必须使用 oleautomation 属性创建并注册类型库。为此,您可以在 COM 接口中仅使用 oleautomation 兼容的数据类型(请参阅 http://msdn.microsoft.com/en-us/library/aa367129%28VS.85%29.aspx)。为了理解类型库发挥了客户端封送 DLL 的作用,因此它可以帮助 VB6/VBA 等客户端软件将数据作为参数正确发送到 COM。你不应该忘记,即使你的COM将是一个InProc服务器,一个DLL,参数也不会直接转发到COM,而是需要被封送。在封送期间,将在运行 COM 的线程上创建数据副本。它使您的 COM DLL 线程从一侧安全,并且如果调用您的 COM 方法的线程在 COM 返回值之前结束,您的 COM 将不会崩溃。

也许我对封送处理的解释并不容易,但重要的是不要忘记创建和注册类型库,最好将其保存为 COM 内部的资源。

I want only answer to additional Tony's questions.

If you want create a COM which can be accessible from VBScript/JScript or from old "classic" ASP you have to implement IDispatch.

In Visual Basic 6 or in VBA of MS Office one can use both ways. If you add Reference to your COM, then you will be use "early bound" (IUnknown or VTable). If you use your COM in VB6 or VBA with CreateObject ("ProgIdName"), that you will be use "late bound".

It is very important to understand, that to make COM accessible from VB6/VBA ect. it's not enough just implement IUnknown interface. You have to create and register Type Library with oleautomation attribute. To be able to do so, you can use in the interface of your COM only oleautomation compatible data types (see http://msdn.microsoft.com/en-us/library/aa367129%28VS.85%29.aspx). For understanding the type library play a role of client marshaling DLL, so it helps a client software like VB6/VBA to send correctly data as a parameters to your COM. You should don't forget, that even your COM will be an InProc server, a DLL, parameters will be not forwards directly to COM, but need be marshaled. During marshaling a copy of data will be created on the thread where run your COM. It makes your COM DLL thread safe from one side and you COM will be not crash if the thread calling your COM method will be ended before COM returns the value.

Probably my explanation about marshaling is not easy, but it's just important don't forget to create and register the Type Library which is better to save as a resource inside of COM.

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