COM's truth
COM is a software architecture that allows applications and systems to be constructed with components provided by different software vendors. It is a binary and network standard that allows any software to communicate with each other regardless of hardware, operating system OS, and programming languages for development. COM is not a programming language but a set of specifications, it defines how components can communicate with each other.
Each COM component is written to meet the binary standards specified by COM. This standard is:
N components require attention to their own (ie, component object [3]) creation and destruction
N components require their functionality in standard ways (ie, through interface [4])
The location of the n component requires transparent to the user
The advantage of using components is to have the ability to load or unload the application system. In order to achieve this, the component must meet two needs:
The n component must dynamically link [5]. This allows them to change at runtime. If the application consists of a component composed of a static link when changing each component, the application is equivalent to a single application.
n The components must be hidden when implementing. Each component has a unique identification of its own ID as an identifier. These IDs appear in the System Registry HKEY_CLASSES_ROOT directory.
Dynamic links are a crucial requirement for components, and implementing detail hide is a necessary condition for dynamic links.
COM work mode
So far, it is clear that COM is not just a written form specification. It also contains system-level code, that is, its own implementation. The CoM rule appears in the COM run library.
The component object library, that is, the COM runtime library is a system component, which provides COM that can be called within the process, processes or through the network.
COM core, briefly, is a specification that the component object and the client use binary standards interact. The COM is implemented in the operating system to run the time library. COM runtime library includes:
N API function
N service
n Standard mechanisms that allow applications to control memory allocation within processes
N ...
The component object library is implemented by compobj.dll in Windows 3.1 and the OLE32.DLL in Windows NT and Windows 9x.
interface
n outline
n component objects are highly packaged. The internal implementation of the component object is completely hidden to the user. It is not possible to know how the component object is using the data structure and how these data is processed. Each component has an interface that is a way to access component object functions. The interface consists of a declaration of a set of virtual functions. It makes the expected behavior and response. There is no implementation of these functions. The function is implemented by a Coclass class. Generate a component object when component class is instantiated.
The n interface is achieved with a memory structure called a virtual function table (VTABLE). Whenever a component object is created, the component object also creates a corresponding virtual function table in memory. The virtual function table consists of a series of pointers that store the address of the member function implemented by the component object.
n The client creates an interface pointer, the interface pointer points to a virtual pointer, the virtual pointer points to the virtual function table. Using interface pointers and virtual poins, the client can access member functions implemented by component objects.
Vtable is a function of a function pointer array memory structure. Each array element contains a function address implemented by the component. For COM, the interface is the memory structure, other things, both COM does not care about the details.
n characteristics
The N interface is a collection of related functions (/ methods) in the same name (which is a system unique ID value, called IID). This indicates that the interface must be the only worldwide.
n As mentioned earlier, generate component objects when the component class is instantiated. The interface is a collection of functions implemented by the component object. The interface cannot be instantiated because it is not implemented.
Communication between N components is based on an interface. The interface is a contract with a strict type between components and its customers. Two objects that implement the same interface are considered to be polymorphic, where the polymorphism does not include the meaning of the base class pointer to the derivative object, which means that the same interface can be implemented by multiple components objects. The N component objects can implement multiple interfaces. For example, bank transaction components (BankTransaction) support two interfaces, IDEBIT and ICredit, ie borrowed loans;, for example, SQLIMPLEMentation supports two interfaces, one is maintenance data processing, the other is maintenance data definition. Data processing interfaces provide an increase, update, and delete data methods, however, data definition interface provides methods for creating databases, tables, and views.
The N interface has no version and is constant. If the function must change to accommodate an interface, a completely new interface with unique identifiers will be created. Component objects implements these two interfaces, thus solving the version problem. Clients made for earlier versions of interfaces still access the original interface.
n attention
n The original old interface can be changed, but the function in the interface cannot be changed, that is, the function name of the function in the interface, the parameter form cannot be changed, as this change needs to make the client call the code to modify and re-re-re- Cenhed. However, the function of the function in the interface can be modified. This is the essence of the invariance of the interface and the COM specification.
COM identifier
Because the world is developing and using components, it is necessary to identify the unique identifier to each component.
Development Software Foundation (OSF) studies an algorithm that produces a unique identifier called the world's unique identifier (UUID). In COM, UUID is called a global unique identifier (GUID). GUID is a 128-bit or 16-byte number of interfaces, component classes, and type libraries. GUID uniquely identifies components.
The algorithm for generating the GUID is based on the following aspects:
n Current date and time.
n Network Adapter Card Address.
n.
n Automatic incrementing counter.
The 128-bit interface identifier used in COM may create a separate interface of approximately 340,823,669209000000000000000000000000000000000000000000,000 interfaces to the future 107828975245600000 per second.
Net card addresses are different from each other, and the machines without a network card, the address remains unique.
The GUID may be generated by executing uuidgen.exe or guidgen.exe. Guidgen is a tool to load Microsoft Visual Studio, which is usually in C: / Program Files / Microsoft Visual Studio / Common / Tools / Guidgen.exe.
GUID is divided into three categories, see below:
N CLSID is a general form of a Uniquely identifies class or component, conventionally, a general form of CLSID is CLSID_
Expressed in myProj_i.c
Const CLSID CLSID_MYCOM = {0xFeb7BDEF, 0xFB6F, 0x446B, {0xBe, 0x31, 0xDF, 0x0A, 0x3A, 0XD3, 0X91, 0XBA}}
Expressed in myProj.IDL
[
UUID (Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA),
Helpstring ("Mycom Class")
]
CoClass mycom
{
[default] interface iMycom;
}
Expressed in mycom.RGS
HKCR {
MyProj.mycom.1 = s 'mycom class'
{
CLSID = S '{Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA}'
}
MyProj.mycom = s 'mycom class'
{
CLSID = S '{Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA}'
Curver = s 'myproj.mycom.1'
}
Noremove CLSID
{
Forceremove {Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA} = s 'Mycom Class'
{
PROGID = s 'myproj.mycom.1'
VersionIndependentProgid = s 'myproj.mycom'
Forceremove 'programable'
InprocServer32 = S '% module%'
{
Val threadingmodel = s 'apartment'
}
'TypeLib' = s '{FE651184-11DE-4D01-BD69-B07DDFA12D0C}'
}
}
}
n Progid is apparent that the CLSID above is difficult to remember and it is difficult to use. PROGID is a user-friendly name assigned to an object. PROGID cannot be single. Each ProgID is mapped to the CLSID. Name habits can be
You can see in mycom.RGS, there are two sentences
...
PROGID = s 'myproj.mycom.1'
VersionIndependentProgid = s 'myproj.mycom'
...
The progidFromClsid and ClsidFromProgID can be converted to CLSID via respectively, and CLSID is converted to ProgID.
As in the client program in this example
HRESULT HR = Coinitialize (NULL);
CLSID CLSID;
HR = CLSIDFROGID (Olestr ("MyProj.mycom"), & clsid;
IF (Failed (HR))
{
AfxMessageBox ("com failed");
Return;
}
The call function CLSIDFROMPROGID stores the CLSID of the PROGID as the first parameter into the second parameter.
N IID is a GUID that uniquely identifies the interface. According to conventions, the general form of IID is IID_
Expressed in myProj_i.c
Const IID IID_IMYCOM = {0x65460F9C, 0x3BAb, 0x4055, {0x88, 0x5A, 0x8E, 0xD5, 0x9F, 0x5F, 0XA9, 0XB0}
Expressed in myProj.IDL
[
Object,
UUID (65460F9C-3BAB-4055-885A-8ED59F5FA9B0),
Dual,
Helpstring ("Imycom Interface"),
Pointer_DEFAULT (UNIQUE)
]
Interface Imycom: idispatch
{
[ID (1), Helpstring ("Method Myf1")] HRESULT Myf1 (); [ID (2), Helpstring ("Method Myf2")] HRESULT MYF2 ([In] BSTR STR, [OUT, RETVAL] INT * VAL );
[ID (3), Helpstring ("Method Myf3")] HRESULT MYF3 ([In] BSTR STR, [OUT, RETVAL] BSTR * RETSTR);
[ID (4), Helpstring ("Method Myf4")] HRESULT MYF4 ([IN] INT X, [OUT, RETVAL] INT * VAL);
}
n Typelibid is the type library that identifies the system. According to the practice, Typelibid's general form is libid_
Expressed in myProj_i.c
Const IID libid_myprojlib = {0xFe651184, 0x11de, 0x4d01, {0xBD, 0x69, 0xB0, 0X7D, 0xDF, 0XA1, 0X2D, 0x0c}}
Expressed in myProj.IDL
[
UUID (FE651184-11DE-4D01-BD69-B07DDFA12D0C),
Version (1.0),
Helpstring ("MyProj 1.0 Type Library")
]
Library myprojlib
{
Importlib ("stdole32.tlb");
Importlib ("stdole2.tlb");
}
This can also be seen in mycom.RGS.
'TypeLib' = s '{FE651184-11DE-4D01-BD69-B07DDFA12D0C}'
Author Blog:
http://blog.9cbs.net/callzjy/