Com features
Practicality
Object-orientedProgramDifferences between design and component-oriented programming:
Object-Oriented Programming = polymorphism + (some) late binding + (some) Encapsulation
+ Inheritance
Component-Oriented Programming = polymorphism + (completely) late binding + (completely, mandatory) Encapsulation + interface inheritance
+ Binary reusability
Simple Plasticity
Malleable
Plastic
The book "COM technology insider" has compiled a FAQ for your reference when learning this book.
Faq1: What is a COM component? Chapter 1 〗
Answer:
The COM component is executable that is published in the Win32 dynamic link library (DLL) or executable file (exe ).Code.
COM components are written in accordance with the COM specification
The COM component is a few small binary executable files.
COM components can provide services to applications, operating systems, and other components.
Custom COM components can be connected to other components at runtime to form an application.
COM components can be dynamically inserted or detached from applications
COM components must be dynamically linked
The internal implementation details of COM components must be hidden (encapsulated ).
The COM component must hide the language it implements.
COM components must be published in binary format
The COM component must be upgraded without interfering with existing users.
The COM component can be transparently assigned a location on the network.
COM components declare their existence in a standard way
Faq2: The component is not ......? Chapter 1 〗
Answer:
COM component is not a computer language
The COM component is not a DLL, but the DLL is used to provide dynamic links to the component.
The COM component is not an API function set.
COM component is not a class
Faq3: What is an interface? Chapter 2 〗
Answer:
An interface is a connection between two different objects.
A computer program is connected by a group of functions, which define interfaces of different parts of the program.
The DLL interface is the functions it outputs.
The C ++ class interface is the member function set of the class.
Com interfaces are a group of functions implemented by components that are provided to customers.
In COM, an interface is a memory structure containing an array of function pointers. An array element is a function address implemented by a component.
Faq4: What is the function of the interface? Chapter 2 〗
Answer:
Interfaces are required to connect components to form an application.
In COM, the interface is everything. To the customer, the component is the interface set, and the customer can only deal with the component through the interface.
This interface can protect the system from external changes. This is the embodiment of encapsulation.
The interface enables users to process different components in the same way. This is a manifestation of polymorphism.
Faq5: What is iunknown?
Chapter 3 〗
Answer:
Iunknown is an interface.
All com interfaces inherit iunknown.
Iunknown is defined in Win32
The unknwn header file in the SDK.
/// Iunknown Definition
Interface
Iunknown
{
Virtual hresult _ stdcall QueryInterface (const
IID & IID, void ** GMM) = 0;
Virtual ulong _ stdcall
Addref () = 0;
Virtual ulong _ stdcall
Release () = 0;
}
Faq6: What is the function of QueryInterface? Chapter 3 〗
Answer:
QueryInterface is a member function of iunknown. You can use this function to query whether a component supports a specific interface.
The QueryInterface function returns a pointer to the interface supported by the component.
If the QueryInterface function does not find the interface supported by the component, the return pointer is null.
The QueryInterface function can use if... Then... Else statement, array, hash, and tree.
The QueryInterface function cannot use case statements because the QueryInterface function returns an hresult structure instead of a number.
QueryInterface is also a version mechanism for unblocked processing components. This mechanism allows the new and old versions of components to interoperate.
Faq7: What are the Implementation Rules of the QueryInterface function? Chapter 3 〗
Answer:
The iunknown pointer returned by QueryInterface is always the same.
If the customer obtains an interface, it can always obtain this interface.
The customer can obtain the existing interfaces again.
The customer can return to the starting interface.
If you can obtain a specific interface from an interface, you can obtain this interface from any interface.
Faq8: How is the interface implemented?
Answer:
Com interfaces are implemented using pure abstract base classes in C ++.
A com component can support multiple interfaces.
A c ++ class can use multiple inheritance to implement a component that supports multiple interfaces.
Components support any number of interfaces.
The interface should be immutable. During component upgrade, you should add a new interface instead of modifying the original interface.
The implementation interface should be carefully designed to support various implementations.
Faq9: What is the IID parameter of the QueryInterface function? Chapter 3 〗
Answer:
It is a structure and an interface identifier structure.
The IID identifies the interface required by the customer.
Each interface has a unique interface identifier. Therefore, an interface corresponding to the IID will never change.
The api iid determines the version of the COM component.
Different interfaces have different IDs, including interfaces of different versions.
Faq10: when do I need to create a new COM component version? Chapter 3 〗
Answer:
When a new ID is specified for an existing interface, the condition below should be at least one.
When the number of functions in the API changes.
The order of functions in the interface is changed.
The parameter of a function in the interface is changed.
The order of parameters of a function in the API changes.
The parameter type of a function in the interface is changed.
The Return Value of the function in the interface is changed.
The Return Value Type of the function in the interface is changed.
The meaning of function parameters in the interface has changed
The meaning of the function in the interface has changed
In short, COM is a way to share binary code across applications and languages. Unlike C ++, it advocatesSource codeReuse. ATL is a good example. Code-Level Reuse is good, but can only be used in C ++. It also brings about the possibility of name conflicts, not to mention the constant copying and reuse of code, resulting in Project expansion and bloated.
Windows uses DLLs to share code at the binary level. This is also the key to running Windows programs-Reuse
Kernel32.dll,
User32.dll. However, DLLs is written for C interfaces and can only be used by C or a language that understands C call specifications. ByProgramming LanguageTo implement the shared code, rather than the DLLs itself. In this case, the use of DLLs is restricted.
MFC introduces another MFC extension DLLs binary sharing mechanism. However, its use is still limited-it can only be used in the MFC program.
Com solves these problems by defining binary standards. That is, com explicitly states that the binary modules (DLLs and exes) must be compiled to match the specified structure. This standard also specifies exactly how to organize COM objects in the memory. The binary standards defined by COM must also be independent of any programming language (such as naming modification in C ++ ). Once these conditions are met, these modules can be easily accessed from any programming language. The binary code generated by the compiler is compatible with the standard. This makes it easier for later users to use the binary code.
In memory, this standard form of COM objects is occasionally used in C ++ virtual functions, so this is why many com codes use C ++. But remember, the language used to write the module is irrelevant because the result binary code is available in all languages.
In addition, COM is not unique to Win32. Theoretically, it can be transplanted to Unix or other operating systems. However, it seems that I have never heard of COM outside of windows.