ATL simulation

zhaozj2021-02-16  54

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 ) -_ atl_packing) -_ atl_packing)

// 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 class CComObjectRoot {public: METHOD (FinalAddRef) (void) {m_dwRef ; TRACE ( "AddRef m_dwRef ="); TRACELNA (m_dwRef); return S_OK;} METHOD (FinalRelease) (void ) {M_dwref -; assert (m_dwref> = 0)); traceLNA (M_DWREF); if (m_dwref <= 0) {Delete this; Traceln ("Delete ...") } Returnid (finalqueryinterface) (Void * pthis, IID, void ** ppv) {assert (pthis && iid); const _interfaces_entry * pentry = t :: _ getInterfaceMap (); assert (pentry); while ! (PENTRY-> DW == 0 && PENTRY-> IID == 0) {IF (IID == IID_IUNKNOWN) {* ppv = (iUnknown *) ((t *) pthis); FinalAddref (); traceln (" QueryInterface iUnknown "); return s_ok;} if (iID == pentry-> iid) {* ppv = (void *) ((DWORD) PTHIS) PENTRY-> DW); FinalAddRef (); Traceln (" Queryinterface OK) "); Return s_ok;} PENTRY ;} traceln (" queryinterface failed "; Return E_FAIL;} public: ccomobject () {m_dwref = 0;} void setvoid (void * P v) {} DWORD M_DWREF;}; Template CLASS CCOMOBJECT: Public T {Method (Queryinterface) (IID, Void ** PPV) {Return FinalQueryinterface (this, IID, PPV);} Method (AddRef) (ADDREF) void) {return FinalAddRef ();} METHOD (Release) (void) {return FinalRelease ();}}; class CComClassFactory: public CComObjectRoot , public IClassFactory {METHOD (CreateInstance) (IUnknown * pUnkOuter, IID iid, void ** PPV) {RETURN M_PFNCREATEINSTANCE (PUNKOUTER, IID, PPV);

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 class ccomcreator {public: static hResult createInstance (Void * PV, IID RIID, VOID ** PPV) {Assert (RIID); T * p = new T; if (P-> Queryinterface (RIID, PPV) ! = S_OK) {Delete P; Return E_FAIL;} P-> setvoid (PV); Return S_OK;

}

#define define_classfactory () / typef ccomcreator > _CLASSFACTORY

#define define_object (x) / typef ccomcreator > _creatorObject;

Template CCIMCOCLASS {public: define_classfactory () define_Object (t)};

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 , public CComCoClass , public IMy {METHOD (F1) (void) {cout << "Hello" << endl; return S_OK;} BEGIN_INTERFACES_MAP (MyClass ) Interface_entry (iMy) end_interfaces_map ()};

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;}

转载请注明原文地址:https://www.9cbs.com/read-27053.html

New Post(0)