类厂也是组件,用于创建其他组件,通过类厂可以创建其它的组件,实际上类厂封装了创建组件的过程,使得创建组件变得标准化,ATL封装了组件的编写过程,
类厂封装了组件的创建。通过类厂机制,每添加一个组件a,就会在类厂组件中添加一个组件a的创建接口,所以以后创建组件的过程变成了:在DLL模块中获取指定组件
的类厂创建接口,利用类厂创建接口创建组件。
类厂的标准接口:IClassFactory组件
import "oaidl.idl"; import "ocidl.idl"; import "objidl.idl"; [ object, uuid(CFF0849D-61E2-4ED1-9DC9-0E43E2FBDE25) ] interface IMath :IUnknown { HRESULT Add(long nAdd1, long nAdd2, long* pnAdd) = 0; HRESULT Sub(long nSub1, long nSub2, long* pnSub) = 0; }; [ uuid(39B16755-783D-49B1-93E2-0FCA9F66CC2D) ] coclass ComObj { interface IMath; };
#pragma once //ATL库头文件 #include "atlbase.h" #include "atlcom.h" #include "comobj_h.h" extern const CLSID CLSID_Math; extern CComModule _Module; class CMath :public CComObjectRootEx<CComMultiThreadModel>, public IMath, public CComCoClass<CMath, &CLSID_ComObj> { public: CMath(); ~CMath(); //添加注册表资源宏 DECLARE_REGISTRY_RESOURCE(0) public: //用于接口查询等相关信息的宏 BEGIN_COM_MAP(CMath) COM_INTERFACE_ENTRY(IMath) END_COM_MAP() //IMath接口的函数 public: STDMETHOD(Add)(long nAdd1, long nAdd2, long* pnAdd); STDMETHOD(Sub)(long nSub1, long nSub2, long* pnSub); };
#include "stdafx.h" #include "Math.h" CComModule _Module; CMath::CMath() { } CMath::~CMath() { } STDMETHODIMP CMath::Add(long nAdd1, long nAdd2, long* pnAdd) { *pnAdd = nAdd1 + nAdd2; return S_OK; } STDMETHODIMP CMath::Sub(long nSub1, long nSub2, long* pnSub) { *pnSub = nSub1 - nSub2; return S_OK; }
IUnknown* MyCreateInstance2(CLSID clsid) { //从Module获取CLISD指定组件的类厂接口 IClassFactory* piClassFactory = NULL; _Module.GetClassObject(clsid, IID_IclassFactory, (LPVOID* &piClassFactory); //创建组件获取IUnknown接口 IUnknown *piUnknown = NULL; piClassFactory->CraeateInstance(IID_IUnknown, (LPVOID*)&piUnknown); return piUnknown; }