ATL接口映射宏详解(上)_第1页
ATL接口映射宏详解(上)_第2页
ATL接口映射宏详解(上)_第3页
ATL接口映射宏详解(上)_第4页
ATL接口映射宏详解(上)_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、ATL½Ó¿ÚÓ³ÉäºêÏê½â (ÉÏ) ÐòÑÔ£º Õ⼸Ìì¿´ÁË¿´ATLµÄ½Ó¿ÚÓ³Éäºê£¬&

2、#178;»Öª²»¾õ¿´µÃ±È½ÏÉîÈëÁË£¬Í»È»¾ÍÃÈ·¢ÁË°ÑËüд³öÀ´µÄÏë&

3、#183;¨¡£ATLÖж¨ÒåÁ˺ܶà½Ó¿ÚÓ³Éäºê£¬Óм¸¸ö»¹ÊDZȽÏÖØÒªµÄ£

4、2;ËäÈ»ºÃÏóûÓбØÒª°ÑËüËùÓеÄϸ½Ú¶¼ÅªµÃºÜÇå³þ£¬µ«ÉîÈ

5、5;ѧϰµÄ¹ý³ÌÖÐÒ²¿ÉÒÔ˳´øѧһѧÆäËûµÄATLÀ࣬¶ÔËüµÄ»úÖÆÒ&

6、#178;¿ÉÒÔ¸üÇå³þһЩ£¬Ó¦¸Ã»¹ÊÇ»áÓÐЩºÃ´¦µÄ°É¡£ÎÒ°´ÕÕÎÒÑ&

7、#167;Ï°µÄ¹ý³Ì°ÑËüд³öÀ´£¬Ò² ²»ÖªµÀ´ó¼ÒÄܲ»ÄÜ¿´¶®¡£ÏëÄ£·

8、ÂһϺîÀÏʦµÄÊÖ±ÊÁ¦Õù°ÑÆäÄÚ²¿Ï¸½Ú½âÊÍÇå³þ£¬µ«Ò²²»¸

9、Ò´óÑÔ²»²ÑµÄÃÀÆäÃûÔ»¡°ÉîÈëdz³ö¡±£¬ºÇºÇ£¬Ö»Ï£ÍûÄܶԴ

10、ó¼ÒÓÐËù°ïÖúÁË¡£ ÒԺ󽫷ֱð½éÉÜATLÖи÷¸öÐÎʽΪCOM_INTERFACE_ENTRY_XXµÄ½Ó

11、1;ÚÓ³Éäºê²¢½«°´ÕÕ´ÓÒ×µ½ÄѵÄ˳Ðò½²½â£¬Ã¿Ò»²¿·Ö¶¼½«½¨

12、3;¢ÔÚÇ°Ò»²¿·ÖµÄ»ù´¡ÉÏ¡£Ã¿Ò»²¿·Ö¶¼½«Í¨¹ý·ÖÎöʵ¼ÊµÄµ÷

13、1;ú¯Êý¶ÑÕ»À´½øÐзÖÎö,¶ÑÕ»µÄд·¨ÊÇ´ÓÏÂÏòÉÏ¡£ÎÄÖÐËùÉæ

14、88;°µÄ´úÂ붼ΪÂÔд£¬Ö»ÁгöÏà¹Ø²¿·Ö¡£ Ò»¡¢COM_INTERFACE_ENTRY(x) Ê×ÏÈÎÒÃÇ

15、80;ÓÒ»¸ö×îµäÐ͵ÄÓ¦ÓÿªÊ¼£º ¶¨ÒåÒ»¸ö×î¼òµ¥µÄATL DLL£º class ATL_NO_VTABLE CMyObject : public CCo

16、mObjectRootEx, public CComCoClass, public IDispatchImpl . BEGIN_COM_MAP(CMyObject) COM_INTERFACE_ENTRY(IMyObject) /Ò»¸öË«½Ó¿Ú COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP() . ; ±àдһ¶Î×î¼ò

17、81;¥µÄ²éѯ½Ó¿Ú´úÂ룺 IUnknown *pUnk; IMyObject *pMyObject; CoCreateInstance(CLSID_MyObject, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void *)&pUnk); pUnk->QueryInterface(IID_IMyObject, (void *)&pMyObject

18、); Ö´Ðпͻ§´úÂ룬Ê×ÏÈÎÒÃÇ¿´¿´×é¼þ¶ÔÏóÊÇÈçºÎ±»´´½¨µÄ¡

19、63; º¯Êýµ÷ÓöÑÕ»Ò»£º 4. 3.ATL:CComCreator< ATL:CComObject< CMyObject > >:CreateInstance(.) 2.ATL:CComCreator2< ATL:CComCreator< ATL:CComObject< CMyObject > >, ATL:CComCreator< ATL:CComAggObj

20、ect< CMyObject > > >:CreateInstance(.) 1.ATL:CComClassFactory:CreateInstance(.) 4.ATL:AtlModuleGetClassObject(.) 9.ATL:AtlInternalQueryInterface(.) 8.ATL:CComObjectRootBase:InternalQueryInterface(.) 7.ATL:CComClassFactory:_InternalQueryInterface(.) 6.ATL:CComObjectCached:QueryInterface(.

21、) 5.ATL:CComCreator >: CreateInstance(.) 4.ATL:AtlModuleGetClassObject(.) 3.ATL:CComModule:GetClassObject(.) 2.DllGetClassObject(.) 1.CoCreateInstance(.)(¿Í»§¶Ë) ½âÊÍÈçÏ£º 1: CoCreateInstance(CLSID_MyObject, NULL, CL

22、SCTX_INPROC_SERVER, IID_IUnknown, (void *)&pUnk); ÆäÄÚ²¿½«µ÷ÓÃOLE APIº¯ÊýCoGetClassObject(), ¶øCoGetClassObjectÔò»áͨ¹ý LoadLibrary(.)×°ÈëDLL£

23、;¬²¢µ÷ÓÃDLLÖеÄDllGetClassObject()º¯Êý¡£ 2: STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) return _Module.GetClassObject(rclsid, riid, ppv);ÆäÖÐÖµµÃ×

24、¢ÒâµÄÊÇ_Module±äÁ¿£¬ÔÚDLLÖж¨ÒåÁËÈ«¾Ö±äÁ¿£º CComModule _Module; ATLͨ¹ýÒ»×éºê£&#

25、186; BEGIN_OBJECT_MAP(ObjectMap) OBJECT_ENTRY(CLSID_MyObject, CMyObject) END_OBJECT_MAP() #define BEGIN_OBJECT_MAP(x) static _ATL_OBJMAP_ENTRY x = #define OBJECT_ENTRY(clsid, class) &clsid, class:UpdateRegistry, class:_ClassFactoryCreatorClass:CreateInstance, /¹Ø¼ü class:_Cre

26、atorClass:CreateInstance, NULL, 0, class:GetObjectDescription, class:GetCategoryMap, class:ObjectMain , #define END_OBJECT_MAP() NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL; Éú³ÉÒ»¸ö¾²Ì¬È«¾Ö_ATL_OBJMAP_ENTRYÐ

27、5;Êý×飺ObjectMap; È»ºóATLÓÖÔÚ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/ . _Module.Init(ObjectMap, hInstance, &LIBID_TEST2Lib); . Öгõʼ»¯_Module /&#

28、215;¢ÒâÔÚÓеÄÇé¿öÏÂÊÇÔÚInitInstance£¨£©Öгõʼ»¯_Module ÄÇô_Module³õʼ»¯¶¼×ö

29、;ÁËЩʲôÄØ£¬ÆäʵËûʲôҲû×ö£¬ÔÚCComModule:InitÖУ¬Ëüµ÷ÓÃAtlModuleInit(_ATL_MODULE* pM, _

30、ATL_OBJMAP_ENTRY* p, HINSTANCE h)£¬ÔÚÆäÖйؼüµÄÖ»ÓÐÒ»¾ä£ºpM->m_pObjMap = p;¿É¼û_Module½ö½öÊÇ°ÑÕâ¸ö

31、;È«¾Ö¶ÔÏóÓ³ÉäÊý×é ObjectMap¸ø´æÁËÆðÀ´¡£ÄÇôΪʲô¿ÉÒÔͨ¹ý_Module.GetCl

32、assObjectµÃµ½À೧ÄØ£¿Æäʵ¹Ø¼üÔÚÓÚÎÒÃǵÄ×é¼þCMyObject¼Ì³ÐµÄÓÖÒ»¸ö»

33、ùÀàCComCoClass! ÔÚCComCoClassÖÐȱʡ¶¨ÒåÁËÒ»¸öºêDECLARE_CLASSFACTORY()¶ø #define DECLARE_CLASSFACTORY() DECLARE_CLASSFACTORY_EX(CComClassFactory)#define DECLARE_CLASSFACTORY_EX

34、(cf) typedef CComCreator< ccomobjectcached< cf > > _ClassFactoryCreatorClass;CComCreator,CComObjectCachedÎÒÃÇÔÝÇÒ²»¹Ü£¬µ«Ò»¿´µ½CComClassFactory£¬¹ËÃû

35、;˼Ò壬ÎÒÃǾÍÖªµÀÎÒÃÇÒªµÄÀ೧ÖÕÓÚ³öÏÖÁË£¡Ã¿¸ö×é¼þÄÚ

36、;²¿Ô­À´¶¼ÓÐÒ»¸öÀ೧¶ÔÏó¡£ÈÆÁËÒ»´óȦ£¬ÎÒÃÇÏÖÔÚÒѾ­Öª

37、;µÀÁË_ModuleÖаüº¬ÁËÎÒÃÇËùÒªµÄÿ¸ö×é¼þµÄÀ೧¶ÔÏó£¬Õâ¶ÔÄ¿

38、9;°À´ËµÒѾ­×ã¹»ÁË£¬ÏÖÔÚ¼ÌÐø·ÓÉÏÂÈ¥£¡ 3: HRESULT CComModule:GetClassObject(REFCLSID rclsid,REFIID riid,LPVOID* ppv)return Atl

39、ModuleGetClassObject(this, rclsid, riid, ppv); CComModule:GetClassObjectµÄʵÏַdz£¼òµ¥£¬½ö½öÊǵ÷ÓÃATLµÄAPIº¯Êý¡£ 4: ATLINLINE ATLA

40、PI AtlModuleGetClassObject(_ATL_MODULE* pM, REFCLSID rclsid, REFIID riid, LPVOID* ppv) _ATL_OBJMAP_ENTRY* pEntry = pM->m_pObjMap;/´Ó_ModuleÖÐÈ¡³ö¶ÔÏóÓ³ÉäÊý×é while (pEntry->pclsid != NULL) if (p

41、Entry->pfnGetClassObject != NULL) && InlineIsEqualGUID(rclsid, *pEntry->pclsid) if (pEntry->pCF = NULL) hRes = pEntry->pfnGetClassObject(pEntry->pfnCreateInstance, IID_IUnknown, (LPVOID*)&pEntry->pCF); if (pEntry->pCF != NULL) hRes = pEntry->pCF->QueryInterface

42、(riid, ppv); break; pEntry = _NextObjectMapEntry(pM, pEntry); ÏÖÔÚºÃÏóÒѾ­Óе㿴²»¶®ÁË£¬¿´À´ÎÒÃǵÿ´¿

43、0;_ATL_OBJMAP_ENTRYµÄ½á¹¹ÁË struct _ATL_OBJMAP_ENTRY const CLSID* pclsid; HRESULT (WINAPI *pfnUpdateRegistry)(BOOL bRegister); _ATL_CREATORFUNC* pfnGetClassObject; _ATL_CREATORFUNC* pfnCreateInstance; IUnknown* pCF; DWORD dwRegister; _ATL_DESCRIPTIONFUNC* pfnG

44、etObjectDescription; _ATL_CATMAPFUNC* pfnGetCategoryMap; pclsidºÜÇå³þ¾Í´ú±í×ÅÎÒÃÇ×é¼þµÄCLSID£»pfnGetClassObjectÎÒÃÇÒ²ÒѾ­

45、;ÖªµÀÁËËü¾ÍÊÇCMyObject:_ClassFactoryCreatorClass:CreateInstance(ÎÒÃÇ×é¼þËù°üº¬µÄÀ೧¶ÔÏóµÄCreateInstanceº

46、¯Êý)£»pCFÎÒÃÇÒ²¿ÉÒԲ³öËüÊÇÖ¸ÏòÕâ¸öÀ೧µÄIUnknownÖ¸Õ룬´ú±íÕ

47、â¸öÀ೧¶ÔÏóÊÇ·ñ±»´´½¨¹ý£¬ÈôÀ೧¶ÔÏóÒѾ­´æÔÚ£¬¾Í²»Ó

48、ÃÔÙ´´½¨ÐµÄÀ೧¶ÔÏóÁË¡£ÏÖÔÚ¾ÍÊ£ÏÂpfnCreateInstanceÎÒÃÇ»¹²»Ã÷°×Ôõ&

49、#195;´»ØÊ¡£Æäʵ´ð°¸»¹ÊÇÔÚ CComCoClassÖУ¡ ÔÚCComCoClassÖÐȱʡ¶¨ÒåÁ˺êDECLARE_AGGREGATABLE(x),&

50、#213;â¸öºê±íʾÕâ¸ö×é¼þ¼È¿ÉÒÔÊǾۼ¯µÄÒ²¿ÉÒÔÊǷǾۼ¯µÄ£¬&

51、#185;ØÓÚ¾Û¼¯µÄ¸ÅÄîÎÒÃÇÔÝÇÒ²»Àí£¬ÏÈ¿´ËüµÄ¶¨Ò壺 #define DECLARE_AGGREGATABLE(x) public: typedef

52、CComCreator2< ccomcreator< CComObject< x > >, CComCreator< ccomaggobject< x > > > _CreatorClass; ÎÒÃÇ¿´µ½ÁËÒ»¸öÊìϤµÄ×Ö·û´®_CreatorClass, &#

53、212;­À´Õ⻹ÓÐÒ»¸ö×é¼þ°üº¬µÄ¶ÔÏó¡£µ«»¹ÓÐÒ»¸öÎÊÌâÎÒÃÇû&#

54、211;иãÇå³þ£¬¾ÍÊÇΪʲô_ClassFactoryCreatorºÍ_CreatorClassºóÃ涼Ҫ¸ú×ÅÒ»¸öCreateInstance? ¿´À´&

55、#206;ÒÃDZØÐëÏÈÀ´¿´¿´CComCreatorÊǸöʲô¶«Î÷ÁË¡£ template < class T1 >class CComCreator public: static HRESULT WINAPI CreateInstance(void* p

56、v, REFIID riid, LPVOID* ppv) . ; Ô­À´ËüÀïÃæÖ»ÓÐÒ»¸öCreateInstanceº¯Êý£¬ÎÒÃÇÏÖÔÚÖÕÓÚ´óÌåÃ÷&

57、#176;×_ClassFactoryCreatorClass:CreateInstance ±íʾʲôÒâ˼ÁË£¬Ëü¾Í´ú±íCComClassFactory:CreateInstance(.)°É£¬²î²»¶à¾

58、5;ÊÇÕâÑùÁË¡£ÄÇÎÒÃÇÔÙÀ´¿´¿´CComCreator2ÓÐʲô²»Í¬£º template < class T1, class T2 >class CComCreator2 public: stati

59、c HRESULT WINAPI CreateInstance(void* pv, REFIID riid, LPVOID* ppv) return (pv = NULL) ? T1:CreateInstance(NULL, riid, ppv) : T2:CreateInstance(pv, riid, ppv); ; Õâ¸öÀàÓëCComCreatorºÜÀàËÆ,¶¼Ö»ÓÐÒ

60、»¸öCreateInstance³ÉÔ±º¯Êý£¬´Ó_CreatorClass ÖÐÎÒÃÇ¿ÉÒÔÖªµÀËüʵ¼ÊÉÏ°üº¬Á½¸ö&#

61、192;àCComObject,CComAggObjectµÄCreateInstanceº¯Êý(ͨ¹ýCComCreator),ÆäÖÐCComObjectÓÃÓڷǾۼ¯¶ÔÏó£¬CComAggObjectÓÃÓÚ¾Û

62、;¼¯¶ÔÏó¸ù¾ÝÇé¿öËü½¨Á¢ÏàÓ¦µÄ¶ÔÏó¡£(ATLÖÐʵ¼ÊÉú³ÉµÄ×é¼þ¶&

63、#212;Ïó²»ÊÇCMyObject,¶øÊÇ CComObject,CComAggObject»òCComPolyObject¶ÔÏó,Õâ¸ö¸ÅÄîºÜÖØÒª,µ«ÏÖÔÚÔÝÇÒ

64、8;»Ì¸) ÏÖÔÚÎÒÃǶÔAtlModuleGetClassObject(.)»ù±¾ÒѾ­ÖªµÀÊÇÔõô»ØÊÂÁË£¬Ëü¾ÍÊÇ

65、¸ù¾Ý´æÔÚ¶ÔÏóÓ³ÉäÊý×éÖеĴ´½¨À೧µÄº¯ÊýµÄµØÖ·À´´´½¨

66、À೧¡£pfnGetClassObjectÒÔ¼° pfnCreateInstanceÎÒÃÇ»ù±¾É϶¼ÒѾ­ÖªµÀÊÇÔõô»ØÊÂÁË£¬&

67、#181;«»¹ÓÐÒ»¸öÎÊÌâΪʲôҪ°ÑpEntry->pfnCreateInstance×÷ΪpEntry->pfnGetClassObject(.)ÖеÄÒ»¸ö²ÎÊý&#

68、180;«µÝ£¿´ð°¸ÔÚÏÂÃæÄØ£¬ÈÃÎÒÃǼÌÐø·ÓÉÏÂÈ¥£¡ 5: CComCreator:CreateInstance(void* pv, REFIID riid, LPVOID*

69、ppv) T1* p = NULL; ATLTRY(p = new T1(pv)/´´½¨À೧¶ÔÏó if (p != NULL) p->SetVoid(pv); p->InternalFinalConstructAddRef(); hRes = p->FinalConstruct(); p->InternalFinalConstructRelease(); if (hRes = S_OK) hRes = p->QueryInterface

70、(riid, ppv); if (hRes != S_OK) delete p; ×¢ÒâÕâÀïµÄT1ÊÇCComObjectCached< ATL:CComClassFactory >,ÕâÊÇÎÒÃǸøCComCreator µÄÄ£°å²ÎÊý¡

71、£ÎÒÃÇÓÖÒ»´Î¿´µ½ÁËÎÒÃÇÊìϤµÄ²Ù×÷·û'new'!Ö±µ½ÏÖÔÚÎÒÃÇÖÕ

72、1;Ú´´½¨ÁË×é¼þµÄÀ೧¡£µ«»¹Ã»Í꣬¼ÌÐøÍùÏÂ×ߣ¬¿´¿´SetVoid(pv)Àï¸

73、;ÉÁËЩʲô£¿ void CComClassFactory:SetVoid(void* pv) m_pfnCreateInstance = (_ATL_CREATORFUNC*)pv; ´ó¼Ò»¹¼ÇµÃÎÒÃÇÔø¾­°ÑCMyObject:_CreatorClass:Cre

74、ateInstance×÷Ϊ²ÎÊý´«¸ø pEntry->pfnGetClassObject(.)°É£¬µ±Ê±ÎÒÃDz»Ã÷°×ÊÇÔõô»ØÊ£¬Ï

75、;ÖÔÚÒѾ­»íÈ»¿ªÀÊ£¡Ô­À´ÊÇÀ೧ÐèÒªËüÀ´´´½¨×é¼þ¶ÔÏó£¡Ë

76、;äÈ»ÎÒÃÇÖ»ÊÇ´Ó×ÖÃæÒâ˼²Â³öÕâÒ»µã£¬µ«Êµ¼ÊÉÏÒ²ÕýÈçÎÒÃ

77、;ÇËùÔ¤ÁϵÄÄÇÑù£¬ÔÚCComClassFactory:CreateInstance(.)ÖУ¬ÎÒÃÇ¿´µ½ÁËm_pfnCreateInstance(pUnkOuter, riid, ppvObj);ÏÖÔÚÒ

78、7;Çж¼ÒѾ­Ã÷°×ÁË£¬ ATLΪÎÒÃÇ´´½¨À೧¶ø×÷µÄ²ã²ã°ü×°ÎÒÃǶ

79、¼ÒѾ­´ò¿ª£¬Ê£ÏµĴ´½¨×é¼þµÄ¹ý³ÌÒѾ­ÊÇÎÒÃǺÜÊìϤµÄ¹

80、ý³ÌÁË£¡ µ«ÊÇÏÖÔÚ»¹Ã»ÓÐÍ꣬ÎÒÃÇ»¹ÐèҪΪÀ೧¶ÔÏó²éѯÒ

81、;»¸öIUnknownÖ¸Õ룬Õâ¸öÖ¸Õë¾Í´æÔÚÎÒÃÇÔÚÇ°ÃæËù¿´µ½µÄpEntry->pCFÖС£ 6: STD

82、METHOD(QueryInterface)(REFIID iid, void * ppvObject)return _InternalQueryInterface(iid, ppvObject);ÏÖÔÚµ÷ÓõÄÊÇCComObjectCached:QueryInterface,ÖÁÓÚÕâ¸öÀàÓкÎÌØ&

83、#177;ðÖ®´¦,ÎÒÃÇÏÖÔÚºÃÏó»¹²»ÐèÒªÖªµÀ£¬ÎÒÒ²ºÜÀÛµÄ˵£¬ºÇºÇ

84、¡£ 7: HRESULT _InternalQueryInterface(REFIID iid, void* ppvObject) return InternalQueryInterface(this, _GetEntries(), iid, ppvObject); ËùÓеÄÀàµÄ_InternalQueryInterface(.)¶¼ÊÇÔÚBEGIN_COM_MAPÖж&#

85、168;ÒåµÄ¡£ CComObjectCachedûÓÐBEGIN_COM_MAPºê£¬ËùÒÔÏÖÔÚµ÷ÓõÄÊÇCComClassFactoryµÄ¡£×¢Òâ°ÑthisÖ

86、¸ÕëºÍ½Ó¿ÚÓ³ÉäÊý×é_GetEntries()´«¸øÁËInternalQueryInterface(), ÕâÊÇInternalQueryInterface(.)ʵÏÖ²éѯµÄÒÀ&#

87、190;Ý¡£ÔÚBEGIN_COM_MAP(x)Öж¨ÒåÁËÒ»¸ö¾²Ì¬µÄ½Ó¿ÚÓ³ÉäÊý×飺 _ATL_INTMAP_ENTRY _entries£»Ã¿Ò&

88、#187;¸ö½Ó¿ÚÓ³Éäºêʵ¼ÊÉ϶¼ÊÇÏòÕâ¸öÊý×éÖÐÔö¼ÓÁËÒ»Ïî¡£Ò»¸&

89、#246;½Ó¿ÚÓ³Éäºê°üÀ¨Èý¸ö²¿·Ö£º½Ó¿ÚµÄIIDºÅ¡¢Æ«ÒÆÖµ£¨´ó²¿·Ö

90、2;±ºòÏ£©¡¢ÐèÒªÖ´Ðеĺ¯Êý£¬¶ÔÒ»°ã½Ó¿ÚÀ´Ëµ²»ÓÃÖ´ÐÐÆä

91、3;ûº¯Êý¡£_GetEntries()¾ÍÊÇ·µ»ØÕâ¸öÊý×é¡£»¹ÓÐһЩϸ½ÚÎÊÌâÒÔºóÔÙ

92、03;µ¡£ 8: static HRESULT WINAPI InternalQueryInterface(void* pThis,const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void* ppvObject) . HRESULT hRes = AtlInternalQueryInterface(pThis, pEntries, iid, ppvObject);. ÏÖÔÚµ÷ÓõÄÊÇCCom

93、ObjectRootBase:InternalQueryInterface(.) 9:ÏÖÔÚÎÒÃÇÖÕÓÚµ½ÁËQueryInterfaceµÄ±Ç×æÁË¡£AtlInternalQueryInterface(.)ÊÇÕû¸ö²éѯ

94、;¹ý³ÌµÄÖյ㣬Ëü±éÀú½Ó¿ÚÓ³Éä±í£¬²¢¸ù¾ÝÿһÏî×ö³öÏàÓ¦

95、;µÄ¶¯×÷¡£ATLÖеÄÏûÏ¢Ó³ÉäºêÓкܶàÖÖ£¬ÏàÓ¦µÄ¶¯×÷Ò²ºÜ¶à£&#

96、172;µ«ÏÖÔÚÎÒÃDz»¹ÜÄÇЩ£¬ÏÖÔÚÎÒÃÇÒª×öµÄ¾ÍÊDz鵽һ¸öIUnknown½Ó

97、¿Ú£¬ÕâºÜÈÝÒ×£¬ÎÒÃÇÉõÖÁ²»ÐèÒª±éÀú½Ó¿ÚÓ³Éä±í¡£ ATLINLINE ATLAPI AtlInternalQueryIn

98、terface(void* pThis, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void* ppvObject) ATLASSERT(pEntries->pFunc = _ATL_SIMPLEMAPENTRY); if (ppvObject = NULL) return E_POINTER; *ppvObject = NULL; if (InlineIsEqualUnknown(iid) / use first interface IUnknown* pUnk = (IUnknown*)(int)pThis+pEntries->

99、;dw); pUnk->AddRef(); *ppvObject = pUnk; return S_OK; . /»¹ÓÐÒ»´ó¶ÑÄØ£¬µ«ÏÖÔÚÓò»ÉÏ£¬¾Í½ÚÊ¡µã¿Õ¼ä

100、°É ÕâÀïÓÐÒ»¸ö¹æ¶¨£¬½Ó¿ÚÓ³Éä±íµÄµÚÒ»¸ö½Ó¿Ú±ØÐëÊÇ_ATL_SIMPLEENTRYÐ&

101、#205;µÄ¡£ÖÁÓÚΪʲôÓÐÕâ¸öÒªÇó£¬ÒÔ¼°pThis+pEntries->dwÊÇʲôÒâ˼£¬ÎÒÃ

102、9;ÒÔºóÔÙ˵°É£¬ÄÇÒ²ÊÇÒ»¶ÑÎÊÌâ¡£×ÜÖ®£¬ÎÒÃÇÏÖÔÚÈçÔ¸ÒÔ³

103、5;ÇáËɵĻñµÃÁËÎÒÃÇËùÐèÒªµÄÀ೧¶ÔÏóÒÔ¼°IUnknownÖ¸Õë¡£ 4:ÎÒ²îÒ»

104、;µãÒÔΪÎÒÃÇ¿ÉÒÔʤµÃ·µ»Øµ½µÚÒ»²½ÁË£¬µ«ÔÚATL:AtlModuleGetClassObject ´¦È´ÓÖÍ&

105、#163;ÁËÏÂÀ´£¬¿´¿´ËüµÄÔ´Â룬ԭÀ´»¹ÒªÔÙͨ¹ýÎÒÃǸջñµÃµÄIUnknow

106、nÖ¸Õë²éѯ IClassFactoryÖ¸Õë¡£ÓÖÊÇһͨÏàͬµÄµ÷Ó㬴ӵÚ6²½µ½µÚ9²½Ò&

107、#187;Ä£Ò»Ñù£¬ÎÒÃǽ«½øÐÐÏàͬµÄµ÷Óᣵ«×¢ÒâÔÚµÚ9²½ÖУ¬ÎÒÃ

108、ÇÕâ»Ø²éµÄ²»ÔÙÊÇIUnknownÖ¸ÕëÁË£¬ËùÒÔÎÒÃÇÐèÒª¿´¿´ÎҸղŻ¹Ã

109、7;ÁгöµÄ´úÂ룬µ«ÕâÁôµ½ÏÂÒ»´Îº¯Êý¶ÑÕ»ÔÙ¿´°É 1:ÖÕÓÚÖÕÓÚÎ&

110、#210;ÃÇÒѾ­Íê³ÉÁË´´½¨À೧¶ÔÏóµÄÈ«²¿²Ù×÷£¬ÏÖÔÚÎÒÃÇÒª×öµ&

111、#196;¾ÍÊÇÎÒÃÇÊìϤµÄµ÷ÓÃÀ೧¶ÔÏóµÄCreateInstance(.)º¯Êý´´½¨×é¼þµÄ¹ý³Ì&#

112、193;Ë¡£ÕýÈçÎÒÃÇËù¼ûµ½µÄ£¬ÏÖÔÚOLE¿ªÊ¼µ÷ÓÃCComClassFactory:CreateInstance()ÁË£¬ÎÒÃÇ»¹&#

113、195;»Íü¼Ç£¬ÔÚÀ೧¶ÔÏóÖб£ÁôÁË´´½¨×é¼þÓõÄCreateInstance()º¯Êý, Õâ¸ö¹&#

114、253;³ÌÒѾ­ºÜÃ÷ÀÊÁË¡£ 2.²»ÓÃÔÙÖظ´ÁË°É£¬¿´µÚ4²½¡£ 3.²»ÓÃÔÙÖØ&

115、#184;´ÁË°É£¬¿´µÚ4²½¡£ 4.Èç¹û¼ÌÐø·ÓÉÏÂÈ¥µÄ»°£¬ÎÒÃǵĶÑÕ»»

116、85;¿ÉÒԺܳ¤£¬µ«ÕâÖ»ÊÇÖظ´µÄ¿ÝÔêµÄÀͶ¯¡£ÎҾͲ»¼ÌÐø×ßÏ

117、94;È¥ÁË£¬ÎÒÒ²ºÜÀÛµÄ˵£¬°¦¡£ º¯Êýµ÷ÓöÑÕ»¶þ£º 0:. 5.ATL:AtlInternalQueryInterface(.) 4.ATL:CComObject

118、RootBase:InternalQueryInterface(.) 3.CMyObject:_InternalQueryInterface(.) 2.ATL:CComObject< CMyObject >:QueryInterface(.) 1.pUnk->QueryInterface(IID_IMyObject, (void *)&pMyObject);£¨¿Í»§¶Ë£© ½âÊÍÈçÏ

119、4;£º 1.ÎÒÃÇͨ¹ý¸Õ¸Õ»ñµÃµÄ×é¼þ¶ÔÏóµÄIUnknown½Ó¿ÚÖ¸ÕëÀ´²éѯIMyObjectÖ¸&#

120、213;룬Õâ²ÅÊÇÎÒÃÇÕæÕýÐèÒªµÄÖ¸Õë¡£ 2.»¹¼ÇµÃÎÒÃÇ˵¹ýATLÕæÕý´&#

121、180;½¨µÄ×é¼þ²¢²»ÊÇCMyObject,¶øÊÇCComObject,CComAggObject »òCComPolyObject,ÕâÀïÎÒÃÇ´´½¨µÄÊÇCComObject.ËùÒ&

122、#212;ÀíËùµ±È»ÎÒÃÇÒªµ÷Óà CComObject:QueryInterface(.),¶øȷʵCComObjectҲʵÏÖÁËÕâ¸öº¯Êý¡£ ST

123、DMETHOD(QueryInterface)(REFIID iid, void * ppvObject) return _InternalQueryInterface(iid, ppvObject); ËüÖ»ÊǼòµ¥µØµ÷ÓÃ_InternalQueryInterface(.),ÎÒÃÇҲ˵¹ý£¬

124、14;»ÓÐÀàÀïÃæÉêÃ÷ÁËBEGIN_COM_MAPºê²Å»áÓÐ_InternalQueryInterface(.),ËùÒÔÏÖÔÚÖ´ÐÐתµ½ÁËËü

125、;µÄ¸¸ÀàCMyObjectÖÐÈ¥£¬ËùÒÔ½«µ÷ÓÃCMyObject:_InterfaceQueryInterface(.) 3.ÒÔºóµÄµ÷ÓÃÎÒÃÇÒѾ­º

126、0;ÊìϤÁË£¬»¹ÓÃÎÒÔÙ˵һ±éÂð£¬ºÇºÇ 4.Õâ¸öµ÷ÓÃÎÒÃÇÒ²ºÜÊìÏ&

127、#164;ÁË£¬²»Óöà˵ÁË°É 5.ÏÖÔÚÎÒÃǽ«Òª²éѯµÄÊÇÒ»¸ö·ÇIUnknown½Ó¿Ú£&#

128、172;ËùÒÔÎÒÃÇÀ´¿´¿´ÎÒÃÇÒÔǰûÁгöµÄ´úÂë ATLINLINE ATLAPI AtlInternalQueryInterface(void* pThis, const _ATL_INTMAP_ENTRY* pEntries,

129、REFIID iid, void* ppvObject) /È·±£½Ó¿ÚÓ³ÉäµÄµÚÒ»ÏîÊǸö¼òµ¥½Ó¿Ú /ÈôÊDzéѯIUnknown½Ó¿&#

130、218;£¬Ö´ÐÐÏàÓ¦µÄ²Ù×÷ /ÒÔϽ«±éÀú½Ó¿ÚÓ³Éä±í£¬ÊÔͼÕÒµ½ÏàÓ

131、¦µÄ½Ó¿Ú while (pEntries->pFunc != NULL) BOOL bBlind = (pEntries->piid = NULL); if (bBlind | InlineIsEqualGUID(*(pEntries->piid), iid) /_ATL_SIMPLEMAPENTRY¾Í±íÃ÷ÊǸö¼òµ¥½Ó¿

132、Ú if (pEntries->pFunc = _ATL_SIMPLEMAPENTRY) /offset ATLASSERT(!bBlind); IUnknown* pUnk = (IUnknown*)(int)pThis+pEntries->dw); pUnk->AddRef(); *ppvObject = pUnk; return S_OK; else /Èç¹û²»ÊÇÒ»¸ö¼òµ¥½

133、11;¿Ú£¬ÔòÐèÒªÖ´ÐÐÏàÓ¦µÄº¯Êý HRESULT hRes=pEntries->pFunc(pThis,iid,ppvObject,pEntries->dw); if (hRes = S_OK | (!bBlind && FAILED(hRes) return hRes; pEntries+; return

134、E_NOINTERFACE; º¯ÊýµÄÂß¼­ºÜÇå³þ£¬Ö»ÓÐÁ½µã¿ÉÄܲ»Ì«Àí½â£¬Ò»¸öÊÇ (IUnknow

135、n*)(int)pThis+pEntries->dw)ÊÇʲôÒâ˼£¬ÁíÒ»¸öÊÇpEntries->pFuncµ½µ× Òª¸ÉЩʲôÊ¡£Ç°Ò»

136、;¸öÎÊÌ⽫ÔÚ½²ÊöCOM_INTERFACE_ENTRY2Öн²Êö£¬ºóÒ»¸öÎÊÌ⽫ÔÚÒÔºó½²Êö²»

137、05;¬ÀàÐ͵ĽӿÚʱ·Ö±ð½âÊÍ¡£·¹×ÜÊÇÒªÒ»¿ÚÒ»¿Ú³ÔµÄÂºÇ

138、86;Ç¡£ ÏÖÔÚÎÒÃÇÖ»Ðè¹ØÐÄÒ»ÏÂÎÒÃǵÄIMyObjectÊÇÔõô±»²éÕҵġ£¿´Ò

139、;»ÏÂËüµÄºêÎÒÃÇ°ÑCOM_INTERFACE_ENTRY(IMyObject)½â¿ªÒÔºóÐÎʽΪ£º &_ATL_IIDOF(IMyObject), /µÃµ½IMyObjectµÄIIDÖ

140、;µ offsetofclass(IMyObject, CMyObject), /¶¨ÒåÆ«ÒÆÁ¿ _ATL_SIMPLEMAPENTRY,/±íÃ÷ÊǸö¼òµ¥½Ó¿Ú ͬÑù¶ÔÓÚoffsetofclass(IMyObject, CM

141、yObject)ÎÒÃÇÒ²½«Áôµ½ÏÂÒ»´ÎÔÙ½²¡£¸ù¾ÝÕâ¸ö½á¹¹£¬ÎÒÃǺÜÈÝÒ×

142、0;ÍÄÜ»ñµÃIMyObject½Ó¿ÚÖ¸Õë¡£ 0:OK£¬it is over.ÒÀ´ÎÍËÕ»·µ»Ø¡£ ÆäʵÕâ´Î²éѯ&#

143、183;¢ÉúµÄ¹ý³ÌÔڸղŵĵ÷ÓÃÐòÁÐÖÐÒ²·¢ÉúÁË£¬µ±²éѯIClassFactory½Ó¿Ú&

144、#202;±¾ÍÓÐÀàËƵĹý³Ì£¬µ«»¹ÊÇ°ÑËüµ¥¶ÀÌáÁ˳öÀ´£¬Ö»ÎªÁË¿´&

145、#191;´µäÐ͵ÄÇéÐΣ¬ºÇºÇ¡£ ¶þ¡¢COM_INTERFACE_ENTRY2(x, x2) ²ÎATLÀý³Ì£ºCOMMAP ATLÖÐÊÇÒÔ¶àÖؼ&

146、#204;³ÐµÄ·½Ê½À´ÊµÏÖ×é¼þµÄ£¬µ«Ôڼ̳ÐÊ÷ÖÐÈç¹ûÓжà¸ö·ÖÖ§Ê&

147、#181;ÏÖÁËͬһ¸ö½Ó¿Ú£¬µ±²éѯÕâ¸ö½Ó¿Úʱ¾ÍÐèÒªÖªµÀ°ÑÄĸö·&

148、#214;Ö§·µ»Ø¸øËü¡£Õâ¸öºê¾ÍÊǸÉÕâ¸ö¹¤×÷µÄͨ³£Õâ¸öºêÊÇÓÃÓ&

149、#218;IDispatch½Ó¿Ú¡£ÎÒÃÇÏÈÀ´¿´¿´ËüµÄµäÐÍÓ÷¨£º class COuter : public IDispatchImpl< IOuter1, &IID_IOuter1, &LIBID_COMMAPLib>,/

150、IOuter1ÊÇÒ»¸öË«½Ó¿Úpublic IDispatchImpl< IOuter2, &IID_IOuter2, &LIBID_COMMAPLib>,/IOuter2Ò²ÊÇÒ»¸öË«½Ó¿Úpublic . public: COuter() . BEGIN_COM_MAP(COuter) CO

151、M_INTERFACE_ENTRY2(IDispatch, IOuter2) ,/½«±©Â¶IOuter2Ëù¼Ì³ÐµÄ·Ïß , COM_INTERFACE_ENTRY(IOuter1) COM_INTERFACE_ENTRY(IOuter2) . END_COM_MAP; IDispatchImpl<.>Õâ¸öÀàÖ

152、8;ʵÏÖÁËIDispatch½Ó¿Ú£¬ËùÒÔÏÖÔÚ×é¼þÖÐÓÐÁ½¸öIDispatch µÄʵÏÖ¡£ÄDzéѯIDi

153、spatch½Ó¿Úʱ£¬·µ»ØÄĸöʵÏÖÄØ£¿ ÎÒÃÇÔÙÀ´¿´¿´COM_INTERFACE_ENTRY2(x, x2)µÄ¶¨Òå#define BEGIN_

154、COM_MAP(x) public: typedef x _ComMapClass; . #define COM_INTERFACE_ENTRY2(x, x2) &_ATL_IIDOF(x), /µÃµ½½Ó¿ÚµÄIIDÖµ (DWORD)(x*)(x2*)(_ComMapClass*)8)-8, _ATL_SIMPLEMAPENTRY, /±íÃ÷ÊÇÒ»¸ö

155、88;òµ¥½Ó¿Ú ÏÖÔÚÎÊÌâ¾ÍÔÚÓÚ(DWORD)(x*)(x2*)(_ComMapClass*)8)-8ÊǸöʲôÒâ˼£¿ ÎÒÃÇÏÈÀ´&#

156、191;¼²ìÒ»ÏÂÏÂÃæÒ»¶Î´úÂ룺 class A1 public: virtual void Test() ; class A2 : public A1 public: virtual void Test() ; class A3 : public A1 public: virtual void Test() ; class A : public A2, public

157、A3 ; DWORD dw; dw = (DWORD)(A *)8); /dw = 0x08 dw = (DWORD)(A3 *)(A *)8); /dw = 0x0c dw = (DWORD)(A1 *)(A3 *)(A *)8); /dw = 0x0c dw = (DWORD)(A1 *)(A3 *)(A *)8) - 8;/dw = 4 Õâ¸ö¼Ì³ÐͼÊǸöµäÐ͵ÄÁ&

158、#226;Ðνṹ£¬ÔÚÀàAÖб£´æÓÐÁ½¸öÐ麯Êý±íÖ¸Õ룬·Ö±ð´ú±í×ÅË

159、üµÄÁ½¸ö·ÖÖ§¡£µ±ÎªÀàAÉêÃ÷Ò»¸ö¶ÔÏó²¢ÊµÀý»¯Ê±£¬ÏµÍ³»áÎ

160、;ªÆä·ÖÅäÄÚ´æ¡£ÔÚÕâ¿éÄÚ´æµÄ×¶Ë±£Áô×ÅËüµÄÁ½¸öÐ麯Ê

161、;ý±íÖ¸Õë¡£·ÖÎö³ÌÐòÔËÐеĽá¹û£¬¿ÉÒÔ¿´³ö£¬×îºóµÄ½á¹û4

162、0;ú±íÁËÖ¸Ïò½Ó¿ÚA3µÄÐ麯Êý±íÖ¸ÕëÓëÀàA¶ÔÏóµÄÄÚ´æ¿é¶¥¶ËÖ®&

163、#188;äµÄÆ«ÒÆÁ¿¡£ ÏÂÃæÎÒÃÇÔÙ¿´Ò»¸ö¸üΪ¸´ÔÓµãµÄ¼Ì³Ð¹Øϵ£º

164、 class B1 public: virtual void Test() ; class B2 public: virtual void Test() ; class B3 public: public: virtual void Test() ; class B4 : public B1, public B2 public: virtual void Test() ; class B5 : public B2, public B3 public: virtual void Test() ; class B : public B4, public B5 ; DWORD dw; dw = (D

165、WORD)(B *)8); /dw = 0x08 dw = (DWORD)(B5 *)(B *)8); /dw = 0x10 dw = (DWORD)(B2 *)(B5 *)(B *)8); /dw = 0x10 dw = (DWORD)(B2 *)(B5 *)(B *)8) - 8;/dw = 8 ÀàB½«±£ÁôËĸöÐ麯Êý±íÖ¸Õë

166、3;¬ÒòΪËü¹²ÓÐËĸö·ÖÖ§¡£ÎÒÃǵÄÄ¿µÄÊÇÏë»ñµÃB:B5:B2Õâ¸ö·ÖÖ§Ö

167、08;µÄB2½Ó¿Ú£¬×îºóµÄ½á¹û8ÕýÊÇÎÒÃÇËùÐèÒªµÄ£¬Ëü±íʾÔÚÀàBÄÚ´

168、;æ¿éµÄÆ«ÒÆÁ¿¡£´ÓÉÏÃæÁ½¸öÀý×ÓÖУ¬ÎÒÃÇÒѾ­Ã÷°×ÁË(DWORD)(x*)(x2*)(_ComMapClass

169、*)8)-8µÄ×÷ÓÃͨ¹ýÕâ¸öÖµÎÒÃÇÄÜ»ñµÃÎÒÃÇËùÐèÒªµÄ½Ó¿Ú¡£ ÏÂÃæÎ

170、;ÒÃÇÕë¶ÔÎÒÃǵÄʵ¼ÊÇé¿öCOM_INTERFACE_ENTRY2(IDispatch, IOuter2)À´·ÖÎöÒ»ÏÂIDispatchImpl< class T£¬. >Ä£°åÀà´ÓÀàTÖÐÅÉÉ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论