COM technology insider note (2)

Source: Internet
Author: User

Chapter 7 factories

1. The only function of the category factory component is to create other components. More precisely, a specific class factory will create components that are only corresponding to a specific clsid. The customer can control the process of creating components in the class factory through the interfaces supported by the class factory.
2. Create a class factory: Functions in the com Library:
Hresult _ stdcall cogetclassobject (const CLSID & CLSID, DWORD dwclscontext, conserverinfo * pserverinfo, const IID & IID, void ** bp );
Cogetclassobject returns a pointer to the class factory of the required component rather than to the component itself. You can use the pointer returned by cogetclassobject to create the required component. This pointer is usually an iclassfactory pointer.
3. The standard interface supported by the class factory for creating components is iclassfactory.
Interface iclassfactroy: iunknown
{
Hresult _ stdcall createinstance (iunknown * punknownouter, const IID & IID, void ** GMM );
Hresult _ stdcall lockserver (bool block );
};
4. cocreateinstance is actually implemented through cogetclassobject. For example:
Hresult cocreateinstance (const CLSID & CLSID,
Iunkonwn * punknownduter,
DWORD dwclscontext,
Const IID & IID,
Void ** GMM)
{
** GMM = NULL;
Iclassfactory * pifacloud = NULL;
Hresult hR = cogetclassobject (CLSID, dwclscontext, null, iid_iclassfactory,
(Void **) & pifacloud );
If (succeeded (HR ))
{
HR = pifacloud-> createinstance (punknownouter, IID, and PVs );
Pifacloud-> reallocate ();
}
Return hr;
}
5. Use cogetclassobject instead of cocreateinstance in two cases:
(1) If you want to use a creation interface different from iclassfactory to create a component, you must use cogetclassobject.
(2) To create multiple instances of the same component, you can use cogetclassobjet to achieve higher efficiency.
6. Some features of the category Factory:
(1) A factory instance can only create components corresponding to a CLSID.
(2) The factory class corresponding to a specific CLSID will be implemented by developers who implement components. In most cases, the class factory component is included in the same dll as the component it creates.
7. cogetclassobject requires a specific function dllgetclassobject In the DLL to create a class factory for the component.
Stdapi dllgetclasobject (const CLSID & CLSID, const IID & IID, void ** GMM );
8. component creation process:
(1) The customer calls cogetclassobject to start the component creation process.
(2) The com library implements the cogetclassobject function.
(3) DLL, which implements the dllgetclassobject function called by cogetclassobject.
9. instance:
Stdapi dllgetclassobject (const CLSID & CLSID, const IID & IID, void ** GMM)
{
If (CLSID! = Clsid_component1)
{
Return class_e_classnotavailable;
}
Cfactory * pfactroy = new cfactory;
If (pfactroy = NULL)
{
Return e_outofmemory;
}
Hresult hR = pfactroy-> QueryInterface (IID, GMM );
 
Pfactroy-> release ();
 
Return hr;
}
Hresult _ stdcall cfactory: createinstance (iunknown * punknownouter, const IID & IID, void ** bp)
{
If (punknownouter! = NULL)
{
Return class_e_noaggregation;
}
Ca * pA = new CA;
If (Pa = NULL)
{
Return e_outofmemory;
}
Hresult hR = pa-> QueryInterface (IID, GMM );
Pa-> release ();
Return hr;
}
The preceding Implementation of dllgetclasobject actually completes three things. First, it will check that the class factory component requested by the customer is created by it. The new operator is used to create components. Finally, it will query the interface requested by the customer from the class factory component.
The createinstance implementation method is the same as that of dllgetclassobject. Both functions create a component and query an interface. The difference is that createinstance creates a ca, and dllgetclassobject creates a cfactory.
10. dllgetclasobject allows us to implement multiple components in the same DLL. Because the CLSID of the component to be created can be passed to dllgetclassobject. For each CLSID, dllgetclassobject can easily create a different class factory.
11. The correspondence between the class factory instance and CLSID will always be one-to-one.
12. locksever provides customers with a method to store servers in the memory until they are used up. The customer only needs to call lockserver (true) to lock the server and call lockserver (false) to unlock it after use.

//////////////////////////////////////// /////////////////////////////////////////

Chapter 4 reuse of components: Inclusion and aggregation

1. In C ++, class transformation is implemented with inclusiveness and inheritance. In COM, you can use inclusion and aggregation to transform components.
2. Inclusiveness and aggregation are actually a technology that enables one component to use another component. This component is called an external component and an internal component respectively. In the case of inclusion, external components will contain internal components, while in the case of aggregation, they are called external components to aggregate internal components.
3. compatibility is completed at the interface level. An external component contains a pointer to an internal component interface. At this time, the external component is only a customer of the internal component. It uses the interface of the internal component to implement its own interface. An external component can call the method forwarded to an internal component to re-implement an interface supported by the internal component. In addition, external components can add some code before and after the internal component code to transform the interface.
4. Aggregation is a special case of inclusiveness. When an external component aggregates an interface of an internal component, the external component directly returns the interface pointer of the internal component to the customer. Therefore, external components do not need to re-implement and forward all functions in the interface, but external components cannot perform any transformation on functions in the interface.
5. Successful aggregation: make the external component and internal component look like the same component.
6. Releasing the interface pointer pointing to the internal component in an external component is a three-step process. First, make sure that the component does not try to release itself again. Second, you need to call addref for the external component. This is because the release call to the internal component will lead to the release call to the external component. Finally, the external component can be released.
7. Avoid Blind aggregation: (1) when implementing external components, do not include interfaces that may be in the same way as internal components. In this case, interfaces that may conflict with internal components can be returned in a functional manner. (2) external components and customers or external components and internal components are implemented as matching pairs.
8. interfaces that cannot conflict with existing interfaces in components are called meta interfaces or class interfaces. The meta interface is used to abstract components rather than component implementations.

//////////////////////////////////////// //////////////////////////////////////// ///

Chapter 2 simplification of programming

1. A smart pointer is actually a class that reloads the operator>. The Smart interface pointer class contains a pointer to another object. When you call the-> operator on a smart pointer, the smart pointer forwards the call to the object indicated by the pointer contained in it. The pointer in the Smart interface pointer will point to an interface.
2. When using smart pointers, note that you should avoid calling the release function on an inclusive interface. The member functions in the smart pointer are accessed through "." instead of "->.
3. To encapsulate interfaces, you can use C ++ packaging class. A packaging class is a customer of one or more com interfaces. It provides abstraction for these interfaces.
4. Unlike smart pointers, a packaging class must implement all the members of the encapsulated interface no matter whether or not it wants to change the interface behavior. The packaging class can add new code before and after calling the interface member function. The packaging class can combine several separate interfaces into a logical unit.

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.