Principles and Applications of com _ Reading Notes

Source: Internet
Author: User
Tags ole

Recommended reference books:
Inside COM (COM technology insider)
Inside Ole
Principles and Applications of COM
Essential COM (the essence of COM)

====================

OMG and Microsoft put forward the following concepts: CORBA and COM, which are used for Unix while com for Windows;
Not only defines the interaction standards between components, but also provides the environment required for component program running.
The component can be DLL-in-process component, or EXE program-Out-Of-process component.
COM objects are different from objects in general object-oriented languages (such as C ++). COM objects are built on binary executable code levels, c ++ and other language objects are built on the source code level.
For students and computer software researchers, COM is not only a technology, but also a software architecture implementation.
In the first version of OLE, Ole 1 does not use Com for communication between component programs and client programs, but uses a Dynamic Data Exchange (DDE, Dynamic Data Exchange) (Disadvantage: low efficiency and poor stability)
One COM component contains multiple COM objects, and one COM object contains multiple com interfaces. However, most COM components only contain one COM object.
The implementation part of the COM standard is the com Library

====================

The com library is generally not implemented at the application layer, but at the operating system level. Therefore, an operating system only has one com library.
According to the COM specification, the customer program creates objects through the com library. Com library creates components through the information provided by the system registry
The component program saves the COM Object Information and interface information it implements to the Registry. This step is called the registration of components. If a component has this self-registration capability, we call this component self-registered

====================

COM component registration information:
The hkey_classes_root node used by com. The main subkey under it is the CLSID subkey.
If it is an in-process component, the CLSID sub-Key of the component contains the inprocserver32 sub-key. The default value of this Sub-key is the full path File Name of the component program;
If it is a non-process component, the CLSID sub-key contains the localserver32 sub-key. The default value of this Sub-key is the full path File Name of the component program.
In Windows, apart from using CLSID to uniquely identify a COM object, you can also use a string to name the component object and use the name string to find the object, this name is called porgid (Program identifier)

====================

The clsidfromprogid and progidfromclsid functions are used to convert CLSID and progid.
Component category: If all automation objects support the idispatch interface, you can classify them into a category of "automation objects"
Windows provides regsvr32.exe, a registration tool used in the registration process.
As long as the components in the process provide the corresponding entry functions dllregisterserver and dllunregisterserver, regsvr32 can complete registration or cancellation.

====================

You can use the following statement to complete the registration of In-process components:
Regsvr32 c: \ mycomobject \ mycomobj. dll
Regsvr32/U c: \ mycomobject \ mycomobj. dll

For components outside the EXE process, it is an executable program, and it cannot provide entry functions for other programs to call. Therefore, the COM specification stipulates that, external components that support self-registration must support two command line parameters/regserver and/unregserver to complete registration or logout (command line parameters are case-insensitive and/can be replaced)

====================

3.3 category Factory
The component program must provide a standard entry function dllgetobjectclass to provide the component information of this component program.
Specifically, a class factory is called an "Object factory" because the class factory is the production base of COM objects, and the com library creates COM objects through the class factory.
Com stipulates that each COM object class should have a corresponding class factory object. If a component program implements multiple COM objects, there should be multiple class factories.
Because the class factory itself is also a COM object and it is used to create other COM objects, who will create the class factory object itself? The answer is dllgetclassobject. The dllgetclassobject function is not a com library function, but a derived function implemented by the component program.
After receiving the instruction for object creation, the com library calls the dllgetclassobject function of the component in the process. This function creates a class factory object and returns the interface pointer of the class factory object, once the com library or customer has the interface pointer of the class factory, they can create the corresponding COM object through the createinstance member function of the class factory interface iclassfactory.

====================

Interaction between COM library and class factory

There are three API functions in the com library that can be used to create objects, cogetclassobject cocreateinstance cocreateinstanceex. Normally, the customer program calls one of them to create the object and returns the display interface pointer of the object.
Cogetclassobject calls the dllgetclassobject function. The dllgetclassobject creates a class factory and returns the class factory object interface pointer.
Cocreateinstance also calls cogetclassobject internally. You only need to specify the CLSID of the object class and the output interface pointer and Interface ID to obtain the interface pointer of the object, the customer program can not deal with the class factory; but this method cannot create objects on the remote machine.
Cocreateinstanceex can obtain remote objects
From the above three functions used to create objects, we generally choose according to the following principles:
1. If you create a remote object or want to obtain multiple interface pointers of the object at a time, use cococreateinstanceex
2. If we want to obtain the class factory object or call some member functions of the class factory, we will use cogetclassobject.
3. In other cases, using cocreateinstance is the most common method.
Generally, the client program or com Library only uses the interface pointer of the class factory object when creating the component object. After the creation, the class factory object is discarded, if you want to create a component object in the future, you can obtain the class factory object again. Therefore, the class factory object is not saved for a long time, but used during the creation process.

 

 

Because the lock count is responsible for the entire component program, if one component program implements multiple class factories, all class factories can share one lock counter without having to use multiple counters separately; in addition, to access this counter in other functions, it is usually set as a global variable.
After the lock operation is introduced, a condition is added to determine whether a component program can be written into memory. It is required to determine whether a component object exists in the component program, determine whether the lock counter of the component program is 0

Com Library
If an application uses the com feature, before calling a function, you must first call the initialization function of the COM Library: hresult coinitialize (imalloc * pmalloc)
The version of the COM library is constantly updated. If the application depends on a specific COM library, it needs to call the cobuildversion function for version verification.
Any process or program module that calls the coinitialize function to return s_ OK must have a corresponding release operation to terminate the resources maintained by COM: void couninitialize (void)

 

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.