Implementing IDispatch
As long as you have a type library describing a corresponding dual interface, you can use an IDispatchImpl base class to provide an implementation of a dispinterface by simply specifying the necessary entry in the COM map (using the COM_INTERFACE_ENTRY2 or COM_INTERFACE_ENTRY_IID macro). For instance, this is a popular way to build the IDispatch interface. Both the ATL Simple Object Wizard and the Implement Interface Wizard add the proper entry to the map as they both presume that you want to implement IDispatch in this manner.
To assist you in implementing dispinterfaces without needing a type library providing the specification of a compatible dual interface, ATL provides the IDispEventImpl and IDispEventSimpleImpl classes.
Multiple Dual Interfaces
You could combine the benefits of multiple inheritances with the advantages of a dual interface (that is, the adaptability of both vtable and late binding, making the class available to scripting languages as well as C++).
It is not advisable to expose many dual interfaces on a single COM object, despite the fact that it is feasible to do so. There can only be one IDispatch interface exposed if there are numerous dual interfaces. The methods are available to make sure of this come with drawbacks like function loss or more sophisticated code. The developer who is adopting this strategy should carefully balance the benefits and drawbacks.
Dual Interfaces and Events
Even though it is technically possible to build an event interface as a dual, there are some compelling design arguments against doing so. The main cause is because the event's source will only fire it via the vtable or via Invoke, not both. It is obvious that the interface should have been a pure vtable interface if the event source fires the event as a direct call to a vtable function because the IDispatch methods will never be called. The vtable methods will never be used if the event source fires the event as a call to Invoke, making it obvious that the interface should have been a dispinterface.
It will be necessary for clients to implement a portion of an interface that will never be used if you specify your event interfaces as duals.
Note: Generally, this reasoning does not hold for dual interfaces. Duals are a rapid, practical, and well-supported method of implementing interfaces that are usable by various clients from the standpoint of implementation.
Frequently Asked Questions
What is a dual interface?
IDispatch interface and vtbl-based interface are both supported by dual interfaces. The strength and speed of a vtbl-based interface can therefore be used in non-scripting environments while also being used in scripting environments like VBScript. The discussion could then quickly turn into an analysis of dual interface problems, so be ready for this turn.
Can you have two dual interfaces in one class?
Yes. One class may include two dual interfaces, but only one of them may be the default. Due to the nature of dual interfaces, you cannot work with two dual interfaces simultaneously.
What’s the difference between COM and DCOM?
Once more, there is no right or wrong answer to this issue. Since DCOM extends COM, every DCOM object is also a COM object, and all COM objects are eligible to take part in DCOM transactions. MULTI QI (multiple QueryInterface()), security contexts, and other distributed environment improvements/optimizations were introduced by DCOM. DCOM made clear the significance of surrogate processes by showing that in-proc servers could not be run on distant machines. To do it, you require a surrogate procedure.) Introducing load balancing was DCOM.
Conclusion
In this article, we have extensively discussed the details of implementing the dual interface along with ATL Simple Object Wizard, ATL Simple Object Wizard, Implementing IDispatch, Multiple Dual Interfaces, and Dual Interfaces and Events.
We hope this blog has helped you enhance your knowledge about the implementation of dual interface, and You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!!
