COM principles and applications----COM objects and Interfaces

Source: Internet
Author: User
Tags definition

1, the understanding of COM objects

COM objects are similar to the concepts of classes in C + + language, each instance of a class represents a COM object, it also includes attributes (i.e., state) and methods (i.e., operations), and States reflect the existence of objects, the method being interfaces.

2, COM object identification-clsid

The GUID is a 128-bit random number with very low repetition probability. Its value comes from two parts: the space value (the network card address or the random number) and the time value.

The GUID value can be obtained using the tools provided by VC + +: GUIDGen.exe and UUIDGen.exe. Or use the COM library's API function Cocreatguid ().

3, COM objects and C + + objects are compared

The COM object encapsulates the data completely inside the object. The encapsulation of C + + objects is semantically encapsulated, and data encapsulation is implemented through different data types.

The reusability of COM objects is achieved through containment and aggregation. The reusability of C + + objects is implemented through the inheritance of classes.

The polymorphism of COM object is embodied by its interface, and the polymorphism of C + + object is embodied by its virtual function.

4, the function and significance of COM interface

The core content of the COM specification is the definition of interfaces, although COM itself is not complex, but around the COM interface has a lot of content worthy of careful discussion, including the identity of the interface, interface function of the call habits, parameter processing, interface and object relations, and interface and C + + relations, COM interface with many features.

COM defines a complete set of interface specifications, which can not only make up for the insufficiency of API as component interface, but also play the advantages of component objects and realize the polymorphism of component objects.

5. Interface Definition and identification

Technically, an interface is a data structure that contains a set of functions through which client code can invoke the functionality of a Component object.

The client invokes an interface member function with a pointer to the interface function structure. The interface pointer actually points to another pointer pvtable.

The table of interface functions is called the virtual function table (the virtual functions tables, abbreviated vtable), and the pointer to vtable is pvtable. For an interface, its virtual function table vtable is determined.

6, the interface design problems

In an interface member function, a string variable must be a Unicode character pointer, which is required by the COM specification.

COM API functions Use the usual _stdcall invocation habits of most languages.

To define COM interface in C language, it is necessary to have the structure body struct define its interface structure, the interface member function must have a this pointer.

The COM interface is defined in C + + language, because the implementation mechanism of C + + language class shows that the vtable in COM interface structure is identical with class's vtable (class's virtual function table), so it is the most convenient method to describe COM interface with class. At this point, the interface member function hides the this pointer.

The description of the class type interface is much simpler than the description of the struct type interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.