views:

55

answers:

3

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?

+1  A: 

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.

DevSolo
@DevSolo: What exactly do you mean with 'binary specification'?
Tony
@Tony: It means that COM only addresses how components communicate at a binary level -- it says nothing about how the components are developed or what language the code is written in or whether there is a "class hierarchy". See The Component Object Model: A Technical Overview @ http://msdn.microsoft.com/en-us/library/ms809980.aspx
Tuzo
+1 for early vs. late binding.
Tuzo
+3  A: 

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)

Victor
So the invoke method on IDispatch is used to invoke methods from a script such as VBScript or JScript?
Tony
@Tony Correct - if you wish the COM object to be usable from a script you'll need a dual interface.
Stephen Nutt
Yes, when you're in the script calls a method, it first calls IDispatch:: GetIDsOfNames and then IDispatch:: Invoke
Victor
A: 

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.

Oleg