Com Principle and Application -- COM Object and Interface

Source: Internet
Author: User

1. Understanding of COM objects

The COM object is similar to the class concept in the C ++ language. each instance of the class represents a COM object. It also includes attributes (I .e. states) and methods (I .e. operations ), the status indicates the existence of the object, and the method is interface.

2. Mark-CLSID of the COM Object

GUID is a 128-bit random number with extremely low repetition probability. Its value comes from two parts: the Space Value (NIC address or random number) and the time value.

To obtain the guid value, use the tools provided by VC ++: guidgen.exe and uuidgen.exe. Or use the com library's API function cocreatguid ().

3. Comparison between COM Object and C ++ object

COM object completely encapsulates data in the object. The encapsulation of C ++ objects is semantic encapsulation. Different data types are used to encapsulate data.

The reusability of COM objects is achieved through inclusion and aggregation. The reusability of C ++ objects is achieved through class inheritance.

COM Object polymorphism is reflected through its interface, and C ++ object polymorphism is reflected through its virtual function.

4. Functions and significance of the COM interface

The core content of the COM specification is the interface definition. Although com itself is not complex, it is worth exploring about many contents around the COM interface, including interface identification, calling habits of interface functions, parameter processing, relations between interfaces and objects, relations between interfaces and C/C ++, and multiple features of COM interfaces.

COM defines a complete set of interface specifications, which can not only make up for the shortcomings of Apis as component interfaces, but also give full play to the advantages of component objects and realize the polymorphism of component objects.

5. Interface Definition and Identification

Technically, an interface contains the data structure of a group of functions. Through this data structure, the customer code can call the functions of component objects.

The client uses a pointer to the interface function structure to call the interface member function. In fact, the interface pointer points to another pointer pvtable.

An interface function table is called a virtual function table (vtable). The pointer to a vtable is pvtable. For an interface, its virtual function table vtable is OK.

6. Interface Design Problems

In an interface member function, a string variable must use a Unicode Character pointer, which is required by the COM standard.

Com api functions use the _ stdcall call habits commonly used in most languages.

To define a COM interface in C language, you must have a struct to define its interface structure. The interface member function must have a this pointer.

Use the C ++ language to define the COM interface, because the implementation mechanism of the C ++ language class shows that the vtable in the COM interface structure and the vtable of the class (virtual function table of the Class) therefore, using class to describe the COM interface is the most convenient method. In this case, the interface member function hides the this pointer.

The description of class interfaces is much simpler than that of struct interfaces.

7. Contact between COM interface and Object

The interface class is just a description and does not provide a specific implementation process. If a COM Object wants to implement an interface, the COM object must associate itself with the interface class in some way, and then expose the interface class pointer to the client program, therefore, the client program can call the interface function of the object.

Using a class interface to bind the interface pointer (this) with object data, the support for the COM interface is intuitive, simple, and easy to understand. In fact, you can also use other methods to implement the interface, as long as the this pointer (that is, the interface pointer) in the interface member function can establish a definite connection with the object data, you can access object data in interface member functions. For example, the VC ++ MFC Library and the ATL (Active Template Library) template library adopt different mechanisms to provide support for the COM interface.

8. Interface Description Language (IDL)

Based on the DCE specification of OSF to describe the Interface Description Language (IDL) of the Remote Call interface, the COM specification is extended to form the description language of the COM interface. The Interface Description Language provides an interface description method that does not depend on any language. Therefore, it can be a common language between component programs and customer programs.

The IDL Interface Description Language used by the COM standard can be used not only to define com interfaces, but also to define some common data types and user-defined data structures. For interface member functions, we can define the type, input and output features of each parameter, and even the description of an array of variable lengths. VC ++ provides the midl tool to compile the IDL Interface Description file into a C/C ++ compatible interface description header file (. h ).

9. Interface Memory Model

COM objects often have their own attribute data, which reflect the object state and are used to differentiate different objects. For customers with multiple objects, data attributes cannot be shared.

10. interface features

Binary features

Interface immutability

Inheritance (scalability): similar to the inheritance of classes in C ++, interfaces can also be inherited and developed in different ways. Class inheritance not only describes inheritance, but also implements inheritance. That is, a derived class can inherit the function implementation of the base class, while interface inheritance only describes inheritance, that is, the derived interface only inherits the member function description of the base interface, but does not inherit the implementation of the base interface. Class inheritance supports multiple inheritance, but interface inheritance only supports single inheritance. According to the COM specification, all interfaces must be derived from iunknown, which can be derived directly or indirectly. However, most of them are directly derived. In the OLE system, the last letter of the interface is "2" or "ex", which is an inherited interface.

Polymorphism: the COM object has polymorphism, which is reflected through the COM interface.

11. The iunknown interface provides two very important features: Lifetime Control (using reference count) and interface query.

12. iunknown interface reference count setting level

When the reference counter is implemented at the component level, the resolution of the counter is too rough (select global variables). At the object level, it is implemented exactly (using member variables of the C ++ class ), at the interface level, the counting resolution is too small (using class member variables ).

13. Use reference counting rules

Use or pass the interface pointer scalar for classification based on different occasions, and give the corresponding rules:

(1) Use interface pointer variables in function parameters.

Input parameter: Because the input parameter is controlled by the calling function, the interface pointer must be valid during the execution of the called function, and the reference count does not need to be changed.

Output parameter: An output parameter is a parameter that is assigned a value during the execution of the called function. The called function does not use the value passed in during function initialization, the output parameter is equivalent to a return value of the function. In C/C ++, the output parameter is a pointer variable (reference variables are not used in COM ). Because the output parameter is equivalent to generating a new interface pointer variable in the called function, you should call addref for the output parameter to increase the interface reference count by 1 before being returned by the called function. This rule also applies when the return value of a function is an interface pointer variable.

Input-output parameter: Before the parameter is modified, call release for the previously passed interface pointer to reduce the reference count by 1. After the parameter is modified, call addref for the new interface pointer variable to mark the reference to the new interface pointer. If the parameter is not modified during function execution, no change is required.

(2) partial interface pointer variable: Because the interface pointer is always valid in a partial function block, a partial interface pointer variable is assigned a value and called an interface member function, the reference count does not need to be changed.

(3) global interface pointer variable: before passing the global interface pointer variable as an input parameter to a function, you should call addref to ensure that the interface pointer variable can be used in the function call, because it is a global variable, other functions may call the release function. Call the release function after the function is returned.

(4) class member variables in C ++ are interface pointer variables, because for the scope of the class, member variables are equivalent to global variables, so they apply to rules (3 ).

(5) When any of the above situations is inappropriate, use the following general rules:

During sequential execution, if you want to assign values to an interface pointer variable, you can call addref for the interface pointer variable after the assignment. If the interface pointer variable before the assignment is not completed, before assigning values, you must call release to end its use.

If you want to stop using an interface pointer variable and stop using it later, call the release function.

14. Interface Query

Use the QueryInterface function to Query Interfaces. The returned values include s_ OK, e_nointerface, and e_unexpected.

15. COM object interface principles

(1) for different interface pointers of the same object, the iunknown interface obtained by the query must be identical. That is, the iunknown interface pointer of each object is unique.

(2) interface symmetry. That is, an interface should always be successfully queried.

(3) Self-inverse interfaces.

(4) interface transmission.

(5) Time independence of API query.

16. Implementation of multi-interface COM objects

There are two implementation methods in the C ++ language: one is to use multiple inheritance, use the supported interfaces as its base class, and then implement interface member functions in the object class; second, use embedded interface class members.

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.