Rules for component Object models

Source: Internet
Author: User

Summary

The purpose of this article is to provide a quick reference for using and implementing Microsoft's Component Object Model (COM). Readers who want a better understanding of what COM is and what is hidden in its design and system should read the specification of the Component Object Model (MSDN Library, Technical specification).

Rule 1: IUnknown must be implemented

If an object does not implement at least one interface with a minimum of IUnknown, it is not a Microsoft Component Object Model (COM).

Interface Design Rules

Interfaces must inherit directly or indirectly from IUnknown.

The interface must have a unique identification (IID).

The interface is invariant. Once the IID is assigned and advertised, no element of the interface definition can be changed.

The member function of an interface should have a return value of the HRESULT type, so that the remote structure can report the situation of a remoting procedure call (RPC) error.

The string argument for an interface member function should be Unicode.

Implement IUnknown

Object of Identity. This requires that the QueryInterface call to the given object instance of any particular IUnknown interface return the same physical pointer variable. This led to the so-called two-interface QueryInterface (IID_IUnknown, ...) and results are compared to determine whether they are the same object (COM object identity).

The settings for the static interface. The setting of any interface that accesses an object via QueryInterface must be static rather than dynamic. That is, if QueryInterface obtains a given IID, it always invokes the same object (unless there is an unexpected case), and if QueryInterface cannot obtain a given IID, then the subsequent invocation of an object to the same IID must fail.

Object integrity. For the interface settings that can be handled, there must be reflexivity, symmetry, and transition. The given code is as follows:

IA * pA = (some function returning an IA*);
IB * pB = NULL;
HRESULT hr;
hr = pA->QueryInterface(IID_IB,&pB); // line 4
Symmetric: pA->QueryInterface(IID_IA, ...) must succeed (a>>a)
Reflexive: If, in line 4, pB was successfully obtained, then
pB->QueryInterface(IID_IA, ...)
must succeed (a>>b, then b>>a).
Transitive: If, in line 4, pB was successfully obtained, and we do
IC * pC = NULL;
hr = pB->QueryInterface(IID_IC, &pC); //Line 7
and pC is successfully obtained in line 7,then
pA->QueryInterface(IID_IC, ...)
must succeed (a>>b, and b>>c,then a>>c).

Minimum reference service size. We need to implement AddRef to maintain a service desk that is large enough to support 2 31–1 of all interfaces for a given object with excellent overall instruction. A 32-bit unsigned integer number satisfies the requirement.

Release does not mean failure. If a customer wants to know about a resource being freed, it must use the higher semantics of some object interfaces before calling release.

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.