What is com?

Source: Internet
Author: User
Tags case statement

What is a COM component?
 
What kind of software products do users need? This is a multi-choice question, but it is highly efficient and robust. How can a software developer meet users' needs? Ensure that the backward compatibility with previous versions is not damaged during application upgrade. System services must be extended without a specific operating system. Object-Oriented Programming is obviously a revolutionary change. Using an object-oriented design method, we can easily abstract the problematic things to be solved into various types, and encapsulate and hide internal actions to provide only some interfaces. But this does not completely solve our problem. Yesterday I saw in programmer magazine that today is the post-oo era. What WILL oo be like in the future? It should be for components.


Raytheon has just read the book "COM technology insider" and compiled a FAQ for your reference when learning this book.
This is the first part, including the content of the first three chapters.

Faq1: What is a COM component? Chapter 1 〗
Faq2: The component is not ......? Chapter 1 〗
Faq3: What is an interface? Chapter 2 〗
Faq4: What is the function of the interface? Chapter 2 〗
Faq5: What is iunknown? Chapter 3 〗
Faq6: What is the function of QueryInterface? Chapter 3 〗
Faq7: What are the Implementation Rules of the QueryInterface function? Chapter 3 〗
Faq8: What does QueryInterface look like? Chapter 3 〗
Faq9: What is the IID parameter of the QueryInterface function? Chapter 3 〗
Faq10: when do I need to create a new COM component version? Chapter 3 〗

Question:
What is a COM component?
Answer:
COM components are executable code published in the format of Win32 dynamic link library (DLL) or executable file (exe.
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
Question:
The component is not ......?
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

Question:
What is an interface?
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.

Question:
What is the role of an interface?
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.

Question:
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.

Question:
What is iunknown?
Answer:
Iunknown is an interface.
All com interfaces inherit iunknown.
Iunknown is defined in the unknwn header file of Win32 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;
}

Question:
What is the function of QueryInterface?
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 the case statement because the QueryInterface function returns an hresult structure instead of

Number.
QueryInterface is also a version mechanism for unblocked processing components. This mechanism allows the new and old versions of components to interoperate.

Question:
What are the Implementation Rules of QueryInterface functions?
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.
Question:
What is the IID parameter of the QueryInterface function?
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.

Question:
When do I need to create a new COM component version?
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 advocates source code reuse. 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-reusing 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. The programming language is responsible for implementing 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.
 

 

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.