What is interface in C++ COM programming?

  • 2020-04-02 02:46:42
  • OfStack

What is an interface?

When it comes to COM, we have to say interface; In the course of COM development, I have been dealing with various interfaces, so to speak. What's the interface? For COM, the interface is a memory structure containing an array of function Pointers, each array element containing the address of a function implemented by the component; So, for COM, an interface is just such a memory structure, and everything else is implementation details that COM doesn't care about.

In C++, you can use abstract base classes to implement COM interfaces. Since a COM component can support any number of interfaces, it can be implemented using multiple inheritance of abstract base classes for the component.

Benefits of interfaces

An interface provides a connection between two different objects. To a customer, a component is a set of interfaces. Customers can only interact with COM components through interfaces. On the whole, the customer knows little about a component. Even at some point, the customer doesn't even have to know all the interfaces that a component provides, as you do with Windows   When the Shell is being developed, it is often impossible to know all the interfaces of a component it provides. For an application, the interface is the most important. The component itself is nothing more than the implementation detail of the interface.

When you're actually developing, you don't have to worry about the implementation details of the component, you're working with the interface, working with the interface. Even if the component's developer replaces the component's implementation with the same interface, your program doesn't need to change. Interfaces, like a standard, let's comply with that standard. One of the previous projects was to replace the implementation layer of a component, and for the interface, no changes were required.

Simple implementation

A simple example to understand the interface:


/*
** FileName     : SimpleInterfaceDemo
** Author       : Jelly Young
** Date         : 2013/12/11
** Description  : More information, please go to //www.jb51.net
*/
#include <iostream>
#include <combaseapi.h>
using namespace std;
interface IExample1
{
     virtual void __stdcall Fx1() = 0;
     virtual void __stdcall Fx2() = 0;
};
interface IExample2
{
     virtual void __stdcall Fy1() = 0;
     virtual void __stdcall Fy2() = 0;
};
// Implementation
class CImplementation : public IExample1, public IExample2
{
public:
     // Implementation IExample1
     void __stdcall Fx1() { cout<<"CImplementation::Fx1"<<endl; }
     void __stdcall Fx2() { cout<<"CImplementation::Fx2"<<endl; }
     // Implementation IExample2
     void __stdcall Fy1() { cout<<"CImplementation::Fy1"<<endl; }
     void __stdcall Fy2() { cout<<"CImplementation::Fy2"<<endl; }
};
// Client
int main()
{
     cout<<"Create an instance of the component."<<endl;
     CImplementation *pCImplementation = new CImplementation;
     // Get the IExample1 pointer
     IExample1 *pIExample1 = pCImplementation;
     // Use the IExample1 interface
     pIExample1->Fx1();
     pIExample1->Fx2();
     // Get the IExample2 pointer
     IExample2 *pIExample2 = pCImplementation;
     // Use the IExample2 pointer
     // Use the IExample2 interface
     pIExample2->Fy1();
     pIExample2->Fy2();
     // Destroy the component
     if (pCImplementation != NULL)
     {
          delete pCImplementation;
          pCImplementation = NULL;
          pIExample1 = NULL;
          pIExample2 = NULL;
     }
}

In the above example, the client communicates with the component through two interfaces, pIExample1 and pIExample2. When declaring an interface, two purely abstract base classes IX and IY are used. The key to summarizing the above code is:

1.COM interface is implemented in C++ with pure abstract base class.
2. A COM component can provide multiple interfaces;
3. A C++ class can use multiple inheritance to implement a component that provides multiple interfaces.

Detail analysis

Where does interface come from? You will be very curious, is curious even the chin fell off? C++ also has the interface keyword, right? Yes, this keyword is defined in the combaseapi.h header file as follows:


#define __STRUCT__ struct
#define interface __STRUCT__

In plain English, it is a structure defined with the C++ keyword struct. What are the benefits of using a struct definition? First of all, we need to figure out the difference between a struct and a class. Those who have learned Java and C# know that because the interface is defined to be called by the client, there is no need for private and protected in the interface. If you use class, you must also use the public keyword to emphasize the public attribute of the interface, while struct defaults to the public attribute, which saves the trouble of adding the public keyword.

What's s s the s/s call? S. S. S. S. S. S. S. S. S. S. S. S. S. S. S.

1. Parameter transfer order, with successive stdcall, the parameters are pushed into the stack from right to left;
2. Who clears the call stack (the calling function or the called function), with successive successive calls indicating that the stack is modified by the called function.

Interfaces are implemented by pure virtual functions. Why? This is a long story, which I will summarize in the next post.

conclusion

Here on the interface of the basic knowledge of a literacy summary, and these simple knowledge points are often encountered in the future development, here to master these things, such as future development will feel very easy. I hope you can learn some knowledge from this blog post, and I also hope that you can make some pertinent Suggestions for my blog.


Related articles: