COM Introduction
WANG Hailong
1. myth
I learned from COM two years ago, and now I think of that experience, and some confessed.
I don't understand why people have to make some very clear things, but also to some of the real outstanding features, but not talking about? I wrote this article, I hope that developers interested in COM don't repeat my mistakes, and no longer be misleaded by some statements and information.
I just started to come into contact with COM, I was unhappy, and I learned from all respects as the following COM myth:
(1) COM is not related to the location. You don't have to know where the COM component is placed.
(2) COM is binary standard, language-independent. You can develop COM components in a variety of programming languages and call COM components.
and many more. Amazing. Just like magic.
My initiator of this mist, the mysterious COM is full, but there is no way.
Later, I had to stop learning these concepts directly from MSDN. I am running all COM examples, reading MSDN data. It is not surprised, and the conception of COM is ingenious, but these clever, it is very mentioned. And those who have been booked and Yang, there are many ingredients that make mystery.
(1) COM is not related to the location. You don't have to know where the COM component is placed.
Yes, you don't have to know where the COM component is placed. But you need to know the ID of a COM component (a unique numeric string), this component ID is stored in the Windows registry, which is recorded in this COM component. When you call this component, you need to take the COM component ID as a parameter to get this component. The Windows system finds the registry, finds the location of the component, starts, or returns the corresponding component.
Wait a minute, the problem is not so simple. The COM component needs to be created from the component factory. Therefore, the component ID you get is the ID of the component factory. You first create a component factory, then create the components you need through this component factory. (Note that you use the component factory through the "factory interface".)
What does the component plant creates the components you need? Through the interface ID. The interface ID is also a unique digital string, which corresponds to an interface definition. Your header file must contain the definition of this interface.
You pass the interface ID to the component factory, the component factory returns to you. You call the component's function through this interface. This is, "your header must contain this interface definition".
Let's see that we don't need location information, what information do we need? Component ID, interface ID, interface definition, (of course, there is a definition of "factory interface").
This complex call is an advantage? Is it still a shortcoming? It is obviously a disadvantage that it is purified into a mythical advantage. This method is to focus on the deployment information, as far as possible. Just register the component correctly, the client can run correctly. Is this worth it in order to deploy?
If it is just for the purpose of deployment, I think this cost is not worth it. We look at the use of various profiles in Java and .NET project, you know that there are a lot of more flexible and valid deployment methods.
COM thinking far exceeds the purpose of deployment, the next chapter will speak.
(2) COM is binary standard, language-independent. You can develop COM components in a variety of programming languages and call COM components. CoM is indeed a binary standard, the core is a vTable (virtual table), this "virtual table" concept and the concept of virtual function tables for the C class. So, use C to achieve COM, very nature.
As mentioned earlier, if you want to use the component, you must first know the interface definition of this component, your header file must contain the definition of this interface. If we use C to implement COM, the project will include a C header file, and the header file includes the definition of the interface. If the client also uses C , just copy this C header file to the user, the user includes the header file inside the C project, you can use this interface.
If you don't have C ? That is also good, don't I give you a C header file? The inside already contains the definition of the interface. You first study, how is the virtual table of this interface, then, you use your language, still write a header file, just comply with my virtual surface structure.
:-) just a joke.
I still have a compromise, have you used Corba? CORBA uses IDL to interface definition, you can use the tool to translate IDL into a variety of languages, C , Java, and more. I also use MIDL to write a set of interface definitions, you can also translate MIDL into a variety of languages, C , VB, Delphi, and more. what? Do you have such a tool? Then you write such a tool, it is not very difficult, just generate a virtual table structure.
Ok, we have MIDL as a public communication language, and now the problem is solved. Do you have any questions? Oh, you need a header file. What language do you use? C ? Ok, I sent you the C header file. There is also a friend, what language do you use? You use other languages, well, I will send you the MIDL header file. You find a conversion tool and convert MIDL into your language.
Oops, so many people come to the head file, too much trouble. I still have a method. I define these interfaces, and put them in the registry together. I am not giving you a component ID? You check this component to the registry, there will be something called "Type Library". Ok, now you can export your header file from the "Type Library". What, how do you use "type library"? Sorry, I forgot to tell you, you have to use it through the ITYPEINFO interface.
Do you have trouble? None trouble, you see, VC, VB, Delphi provides a guide that automatically exports the component interface definition. You don't have to do your hand in these things.
Now we have a complete solution. COM is binary standard, language-independent.
You still have questions, VB does not support pointers, how to call virtual table vtable? Why do you want to call COM with VB? Ok, I will work more, let my COM component support the idispathch interface, the IDispathch interface is called an automated interface. The IDispathch interface will check the table, you pass the function name to the IDispathch interface, the IDispathch interface automatically distributes the request to process. C users don't worry, I also reserve the previous virtual table vtable. Now, I named "Dual Interface" Dual Interface. Finally, I have a complete solution. COM is binary standard, language-independent.
2. "Group" - Powered by Queryinterface
"Group" is the concept of Lotus Lotus Company, I will borrow it here, express my admire between COM components.
The core idea of COM components is "binary interface" --VTable.
Let's see three ways to interface the IunkNow interface.
Addref and Release two methods, management component reference count, I have a reservation attitude (NEGATIVE OPINION :-).
The third method, QueryInterface is the essence of the entire COM component idea.
With QueryInterface, you can get another interface and another component service.
Why is I called the COM component as "group" because this is here. Each COM component supports a set of interfaces, a "group" interface, a set of functional services, not a single function.
Some people may say that this is too absolute, sometimes, people only need a simple and functional component. I want to say, the simplest COM component, but also to implement two interfaces - one is iUnkonw, the other is the interface you define. (I have a little raised here. :-)
QueryInterface is so good. I can't see something in EJB and CORBA. There may be similar situations, for example, one method of the component returns another component. But far from the degree of naturalness of COM components. The COM component is born to support a "group" interface.
Let's analyze the working principle of QueryInterface.
In order to illustrate the problem, I will introduce some Java and Design Pattern concept.
Java objects are capable of implementing multiple interfaces. External programs are when using Java objects, sometimes it is necessary to determine if the Java object supports an interface. For example, when an external program is to compare the size of the two Java objects, it is necessary to judge whether these Java objects support the Comparable interface. Such a statement IF (Obj InstanceOf Comparable). Let's see, how much this statement is like QueryInterface. QueryInterface is also used to query if components support an interface.
When we design, we don't want to provide only a "wide" interface, including all operations. Always try to assign different "roles" for different calories, ie, provide a set of "narrow" interfaces. For different requirements, the caller of different permissions, returns different interfaces. In Design Pattern, this is called Adapter Pattern.
The COM component is based on Adapter Pattern. First, you get an IUNKNOW interface, when you need a service, you will find a specific interface from the iUnknow interface.
Through the above analysis, we can see that Java objects and COM components are very similar. However, the implementation mechanism of both is large, and the Java object is implemented in accordance with the characteristics of the Java language, and all interface implementations are included in the same class. Another person is to say, Java objects can also contain some other objects to give some operations to these objects. What I said here is that this is the same class to declare, implement all interfaces. For example, Class MyObject Implements Comparable, Serializable, Interface1, ..., Interface n.
Only the statement such as Obj InstanceOf Comparable, Obj InstanceOf Interface1 can return true.
The COM component does not have such a restriction. The COM component returns only the binary VTABLE. The COM component itself does not implement these VTABLE, which can return VTABLE of other components. Different interfaces can be implemented in the same component or in different components. This is also the concept of "aggregation" and "containing" COM reuse.
Moreover, the survival status of the COM component is also very flexible, and can exist in the process space of the client program, referred to as the internal components, and may exist in the process space independent of the client program, called process interga components.
When a COM component (we call this component is a), the QueryInterface method returns other components (we call this component B), let's see, now, the status of the component A is very similar to the component factory of the B component, created through the component A component. B component. We can now understand why it does not directly create components, and uses a non-intuitive way to create components through another component-factory component. In this way, the concept created by the component is unified - the components are created through the factory components.
I don't know, this is a coincidence, or a designed thinking. In any case, the design concept of COM components gave me deep shock, it is really a clever work.
Note that due to QueryInterface's self-transliness requirements, the actual processing is complex. QueryInterface's antice: QueryInterface returns the interface of component B, then calls the QueryInterface of this component B, and can also find the interface of component A. It is also, if the QueryInterface returns the QueryInterface of the component B returns the interface of the component C, the QueryInterface of the component C can also find the interface of the component A.
Think of the benefits of QueryInterface, these complexity, or endure. :-)
This is not all COM, just the basic principle of COM. More exciting is a series of technologies based on COM, Structured, Ole Document, Automation, ActiveX, etc. Every technology is very good, the idea is ingenious, and it is amazed. The COM standard gives us so much troubles, it seems to be able to endure.
It is highly recommended to use these technologies based on COM, at least try to experience the thoughts, there will be a lot of inspiration. Starting from the beginning, define the development of its own basic COM components? :-) I do not know. Limited to the eye, I have never seen any excellent base COM components, at least ISTORAGE, IDSPATCH, etc. Microsoft's own definition interface is excellent.