These days, I learned ATL, I remembered the way "in-depth MFC": Simulation, tossing for half a day, got something, is willing to share with comrades who are struggling to struggle in ATL. When I was written, I didn't write ATL's COPY. I didn't see its source code. Many names only remembered, and some names I think I am more easier to understand, so the name may be a bit different, don't blame me! Since I am afraid that it is in the big maze, I am not polite "cut off", "cut off", for example, I have not considered the aggregation at all simulation. In addition, I don't quote what other category libraries (so as not to find the class library manual), and even have no API. I have added some annotations. Don't say it, look! I am willing to be a bit for you.
//---------Tlmy.h
#include
// Common type definition type; typef unsigned long dword; typedef unsigned long dword; typedef int buol;
// Return to the value #define s_ok 0 # define e_fail 1;
#define interface class
#ifdef _debug_on # define assert (b) {if (! b) __ASM INT 3H} #else #define assert (b) #ENDIF
// Calculate the offset of the interface #define _tl_packing 8 # define offsetofclass ((dword) (static_cast
// identifier type Definition Typedef unsigned long; typedef guid iid; typedef guid clsid; #define defineiid (x, n) const samein _ ## x = n
// Debug output #ifdef _debug_on # define traceln (x) cout << "| debug: << x << endl # define trace (x) cout <<< | debug: << x # define tracea (x) Cout << x # define tracelna (x) cout << x << Endl # else # define traceln (x) #define trace (x) #define tracea (x) #define tracelna (x) # ENDIF / / Define Interface Method #define method (x) / public: / virtual hResult x
/ / Define IUNKNOWNDEFINEID (IUNKNOWN, 0X010001); Interface IUNKNOWN {Method (Queryinterface) (IID IID, Void ** PPV) = 0; method (address) = 0; method (RELEASE) = 0;} ;
// define IClassFactoryDEFINEIID (IClassFactory, 0x347a24); INTERFACE IClassFactory: public IUnknown {METHOD (CreateInstance) (IUnknown * pUnkOuter, IID riid, void ** ppvObject) = 0; METHOD (LockServer) (BOOL bLook) = 0;}; / / define class mapping entry structtypedPedef HRESULT (* CREATEFUNC) (VOID * PV, IID RIID, VOID ** PPV);
TypedEf struct; createfunc pfncreateObject; cretefunc pfncreateinstance; iUnknown * pcf; // class factory iUnknown} _object_map_entry;
#define defineclsid (x, n) const clsid _ ## x = n
#define begin_object_map (x) / static _object_map_entry x [] = {
#define object_entry (CLSID, CLASS) / {Clsid, Class :: _ ClassFactory :: CreateInstance, / Class :: _ CreatanceObject :: CreateInstance, Null},
# df {{clsid_ ## Class, CLSID _ ## Class, Class :: _ ClassFactory :: CreateInstance, / Class :: _ CreatanceObject :: CreateInstance, Null},
#define end_object_map () / {0, NULL, NULL, NULL} /};
// define Interface map entry structtypedef struct {IID iid; DWORD dw;} _ INTERFACES_ENTRY; // define interface mapping macro # define BEGIN_INTERFACES_MAP (CN) / public: / typedef CN _ComClass; / const static _INTERFACES_ENTRY * _GetInterfaceMap () / {/ static _Interfaces_entry _entry [] = / {/ {IID_IUNKNOWN, 0},
#define interface_entry (in) / {IID _ ## in, offsetofclass (in, _comclass)}
#define end_interfaces_map () / {0,0} /}; / return & _entry [1]; /} / method (queryinterface) (IID IID, VOID ** PPV) = 0; / Method (ADDREF) (Void) = 0 ; / Method (RELEASE) (VOID) = 0;
// class definition template
METHOD (LockServer) (BOOL bLock) {return S_OK;} void SetVoid (void * pv) {m_pfnCreateInstance = (CreateFunc) pv;} CreateFunc m_pfnCreateInstance; BEGIN_INTERFACES_MAP (CComClassFactory) INTERFACE_ENTRY (IClassFactory) END_INTERFACES_MAP ()};
Template
}
#define define_classfactory () / typef ccomcreator
#define define_object (x) / typef ccomcreator
Template
class CComModule {public: void Init (_OBJECT_MAP_ENTRY * p) {m_pObjectMap = p;} void Trim () {ASSERT (m_pObjectMap); _OBJECT_MAP_ENTRY * pEntry = m_pObjectMap; while (pEntry-> clsid = NULL!) {if (pEntry-> PCF! = null) PENTRY-> PCF-> Release (); Pentry ;
} _OBJECT_MAP_ENTRY * m_pObjectMap; METHOD (GetClassObjcet) (CLSID clsid, IID riid, void ** ppv) {ASSERT (m_pObjectMap); _OBJECT_MAP_ENTRY * pEntry = m_pObjectMap; while (! PEntry-> clsid = NULL) {if (pEntry-> clsid == CLSID) {if (PENTRY-> PCF == NULL) {HRESULT HRES = PENTRY-> PFNCREATEOBJECT (PENTRY-> PFNCREATEINSTANCE, RIID, (Void **) & Pentry-> PCF); * PPV = (void *) Pentry -> PCF; Return S_OK;} else {* ppv = (void *) PENTRY-> PCF; Return S_OK;}} PENTRY ;} Return E_FAIL;}}
//Test.cpp
#include "stdafx.h" #include "atlmy.h" defineiid (iMy, 0x34242); Interface Imy: Public iunknown {Method (f1) (void) = 0;
DEFINECLSID (MyClass, 0x25346); class MyClass: public CComObjectRoot
Begin_Object_map (Objectmap) Object_ENTRYEX (MyClass) End_Object_map ()
CCOMMODULE _MODULE;
int main (int argc, char * argv []) {_Module.Init (ObjectMap); IClassFactory * pCF = NULL; IMy * pMy = NULL; _Module.GetClassObjcet (CLSID_MyClass, IID_IClassFactory, (void **) & pCF); pCF- > CREATEINSTANCE (NULL, IID_IMY, (VOID **) & PMY);
IMy * pMy1 = NULL; IClassFactory * pCF1 = NULL; _Module.GetClassObjcet (CLSID_MyClass, IID_IClassFactory, (void **) & pCF1); pCF1-> CreateInstance (NULL, IID_IMy, (void **) & pMy1); pMy1-> F1 ( ); PMY1-> release (); PMY-> F1 (); PMY-> Release ();
_Module.trim ();
Return 0;}