Chapter VII
1. The unique function of the class factory component is to create other components. More precisely, a particular class will create a component that is only a specific CLSID. Customers can control the process of creating components of the class by the interface supported by the class factory. 2, creating class factory: COM library function: HRESULT _stdcall CoGetClassObject (const CLSID & clsid, DWORD dwClsContext, CONSERVERINFO * pServerInfo, const IID & iid, void ** ppv); CoGetClassObject returns to point to the desired component class factory and It is not a pointer to the component itself. Customers can create the required components with pointers returned by COGETClassObject. This pointer is usually an iClassFactory pointer. 3. The standard interface for creating components supported by the class plant is IclassFactory. interface IClassFactroy: IUnknown {HRESULT _stdcall CreateInstance (IUnknown * pUnknownOUter, const IID & iid, void ** ppv); HRESULT _stdcall LockServer (BOOL bLock);}; 4, CoCreateInstance CoGetClassObject by actually achieved. Such as: HRESULT CoCreateINstance (const CLSID & clsid, IUnkonwn * pUnknownDuter, DWORD dwClsContext, const IID & iid, void ** ppv) {** ppv = NULL; IClassFactory * pIFactory = NULL; HRESULT hr = CoGetClassObject (clsid, dwClsContext, NULL, IID_IClassFactory (Void **); if (succeededed (hr)) {hr = pifactory-> CreateInstance (PunkNownouter, IID, PPV); Pifactory-> Realease ();} return hr;} 5, in both cases Using CogetherClassObject without using COCREATEINSTANCE: (1) If you want to create an interface different from IclassFactory, you must use COGETCLASSOBJECT. (2) To create multiple instances of the same component, then use CogetClassObjet will be available more High efficiency. 6. Some features of the class factory: (1) An instance of the class factory will only create components corresponding to a CLSID. (2) The corresponding type of factory corresponding to a particular CLSID will be implemented by the developer of the assembly. In most cases, the class factory component is included in the same DLL as the components it creates. 7. CogetherClassObject requires a specific function DllgetClassObject in the DLL to create a component of the component. Stdapi DllgetClasObject (Const CLSID & CLSID, Const IID & IID, VOID & IID, VOID * PPV); 8, component creation process: (1) Customer calls CogetClassObject to start the component's creation process. (2) The COM library implements the CogetClassObject function.
(3) DLL, which implements the DllgetClassObject function called by COGETCLASSOBJECT. 9, Example: STDAPI DllGetClassObject (const CLSID & clsid, const IID & iid, void ** ppv) {if (! Clsid = CLSID_Component1) {return CLASS_E_CLASSNOTAVAILABLE;} CFactory * pFactroy = new CFactory; if (pFactroy == NULL) {return E_OUTOFMEMORY ;} HRESULT hr = pFactroy-> QueryInterface (iid, ppv); pFactroy-> Release (); return hr;} HRESULT _stdcall CFactory :: CreateInstance (IUnknown * pUnknownOuter, const IID & iid, void ** ppv) {if (pUnknownOuter ! = Null) {RETURN CLASS_E_NOAGGREGATION;} Ca * PA = New Ca; if (PA == NULL) {Return E_OUTOFMEMORY;} HRESULT HR = PA-> Queryinterface (IID, PPV); PA-> Release (); Return HR The implementation of the DllgetClasObject shown above has actually completed three things. First it will check the customer's class mill component that it can be created. Second it will use the New operator to complete the creation of the component. Finally, it will query the client requested by the class factory component. The implementation of CreateInstance is the same as DllgetClassObject. Both functions will create a component and query an interface to it, which is only CREATEINSTANCE to create CA, and DllgetClassObject creates CFActory. 10. DllgetClasObject makes we can implement multiple components in the same DLL. Because the CLSID of the components to be created can be passed to DllgetClassObject. For each CLSID, DllgetClassObject can easily create a different class. 11. The correspondence between the instance of the class plant and the CLSID will always be one-on-one. 12. Locksever provides customers with a way to save the server in memory until the method is used. Customers simply call LockServer (True) to lock the server and then call LockServer (false) after use. /
Chapter 8 Components Reuse: Accession and Aggregation
1. In C , the transformation of the class is to be implemented with inclusion and inheritance. In COM, inclusion and aggregation can be used to modify the components. 2, inclusion and aggregation are actually a technology that uses another component using one component. For this component, it is referred to as an external component and an internal component, respectively. In the case of inclusive, the outer component will contain internal components; and in the case of polymerization, the internal components are intended to polymerize internal components. 3. The inclusion is done at the interface level. The external component contains a pointer to the internal components interface. At this time, the external component is just a customer of the internal components, which will use the internal components to implement its own interface. External components can also re-implement an interface supported by internal components by calling forward to forward to internal components. And the external components can also be modified to the interface before and after the internal component code. 4, polymerization is a special case of inclusive. When an external component aggregates an interface of an internal component, the external component will directly return the interface pointer of the internal component to the customer. Therefore, the external components will not need to re-implement and forward all functions in the interface, but the external components will not be modified in the interfaces in the interface. 5. Successful aggregation: Make external components and internal components as the same component. 6. The interface pointer to the interior component to point to the internal component will be a three-step process. First, you need to guarantee that the component will not try to release yourself again. Second, you need to call AddRef for external components, which is due to the release call of the internal components, will result in the Release call to the external component. Finally, the external components can be released. 7, avoid blind aggregation: (1) When implementing external components, do not include an interface that may be conflicted in functional back to the interface that may be in the internal components. (2) Implement external components and customers or external components and internal components as match pairs. 8, those interfaces that are not possible in the same interface have conflicted in the same assembly are called a meta-interface, or a class interface. The meta interface is used to operate the component itself instead of the assembly implementation. ///
Chapter 9 Programming Work
1. A smart pointer is actually a class that overloads operators ->. The intelligent interface pointer includes a pointer to another object. When the user calls the-> operator on the intelligent pointer, the smart pointer forwards this call to the object referred to in the pointer it contains. The pointer contained in the intelligent interface pointer will point to an interface. 2, pay attention to using the smart pointer: avoid calling the Release function on the enclosed interface; the member functions in the smart pointer are accessed by "." Rather than "->". 3. To package the interface, you can use the C packaging class. A packaging class is a customer of one or more COM interfaces, which provides abstraction for these interfaces. 4. Unlike the intelligent pointer, regardless of whether the packaging class wants to change the behavior of the interface, it must reach all members in the interface it packs. The packaging class can add new code in front of the calling interface member function. Packaging classes can combine several separate interfaces into a logical unit.