Objective
In COM programming--The understanding component also concludes that COM is a specification for how to build a dynamic, interactive component that provides some of the standards that customers and components should follow in order to be able to interoperate. When implementing and using QueryInterface, it is necessary to follow some rules, only if these rules are complied with, to be a correct COM component; Only by understanding these rules can you truly understand COM development.
Implementation rules for QueryInterface
To implement QueryInterface, you need to follow the following five rules:
1.QueryInterface always returns the same IUnknown pointer
An instance of a component has only one IUnknown interface. Because when querying the IUnknown interface of a component instance, whatever interface is passed, the resulting will be the same pointer value. To determine whether two interfaces point to the same component, you can query the IUnknown interface through the two interfaces, and then compare the return values.
This rule is very important, if the QueryInterface implementation does not follow this rule, then will not be able to determine whether two interfaces point to the same component;
2. If the customer has ever acquired an interface, it will always get the interface
This rule defines the invariance of its QueryInterface for an instance of a component; You can imagine that if the interface set of a component instance is not fixed, the client will not be able to programmatically determine what kind of functionality a component has, and the customer will lose patience with your COM component. There's no point in using any of your COM components.
3. Customers can get the interface they already have
If the customer has an IX interface, it can then query the IX interface pointer again, and it must be successful. It may sound a bit strange to inquire about yourself by yourself, but this is a must.
4. Customers can return from any interface to the start interface
If the customer has an IX interface pointer and successfully uses it to query a iy interface, it will be able to use this iy interface to query an IX interface, which is useful for actual project development.
5. If you can get a specific interface from an interface, you will be able to get this interface from any interface
If a particular interface can be obtained from a component, the customer will be able to obtain this interface through any interface supported by this component. For example, if you can get an interface iy through an interface IX, you can get iz through iy, then you can get Iz by IX. This rule allows QueryInterface to be available.
The inner focus of all the rules is that no matter how many interfaces the component implements, the component implements only one QueryInterface, so in all vtbl of the interfaces, the corresponding QueryInterface is the QueryInterface address of the component implementation, All interface pointers invoke the same QueryInterface that are invoked when QueryInterface are queried, so this satisfies the above rules. When you read the above rules, inevitably there will be some indifferent feeling, feel is the text, very boring, I started the time is also the case, because of this, in the actual development, eat a lot of pain, so, today, here again these rules again to tidy up again, I hope you do not in the actual project in the fall and then come back to find reasons, why not to prevent it?
To add a new interface
Previous posts have also concluded that COM interfaces are not going to change. When a component publishes an interface and is used by a customer, this interface will never change, and will remain unchanged forever. What does that mean, exactly? Because each interface has a unique corresponding interface identifier IID. In general, we do not change the interface, but we can create a new interface and assign it a new IID. When QueryInterface receives a query to the old IID, it returns the old interface, and when it receives a query to the new IID, it returns the new interface. For QueryInterface, a IID is an interface.
Therefore, the corresponding interface with a IID will never change. The new interface can inherit the old interface, and it can be completely different from the old one. Since the old interface remains unchanged, the operation of existing customers will not be affected. New customers can decide for themselves whether to use the old interface or the new interface because it is free to decide which interface to query.
New interface naming
Although everyone's naming rules, each company naming rules are different, but the name of the COM interface is generally consistent, for example: the original interface named IX, the new interface named IX2, rather than ixex and so on. I've been through so many projects, I've written, and I've called a lot of COM components, and I'm basically following this rule, which is to add a number to the back of the old name.
Summarize
This article sums up the theory so that people who don't like the theory will be a little disappointed. However, the reason is that, there is no theory as the basis of practice, are promiscuous. Do anything, must have a certain theoretical basis, so, I passed two posts, to QueryInterface detailed summary. Hope to have a certain help to everyone, finally, also hope that you put forward your ideas and I share. I firmly believe that communication is a very important way to learn.