ImageVerifierCode 换一换
格式:DOCX , 页数:47 ,大小:24.59KB ,
资源ID:9947917      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9947917.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(COM聚合完整实例.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

COM聚合完整实例.docx

1、COM聚合完整实例/Aggre/CompA/Aggre/CompA/CompA.cpp/ CompA.cpp : Defines the entry point for the DLL application./#include stdafx.h#include #include #include objbase.h#include olectl.h#include CompA.h#include factory.h#include registry.hULONG g_LockNumber = 0;ULONG g_CompANumber = 0;HANDLE g_hModule;/ 16DCB

2、981-BEEB-11d2-B362-00104B08CC22extern C const GUID CLSID_CompA = 0x16dcb981, 0xbeeb, 0x11d2, 0xb3, 0x62, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 ;/ 16DCB982-BEEB-11d2-B362-00104B08CC22extern C const GUID IID_SomeInterface = 0x16dcb982, 0xbeeb, 0x11d2, 0xb3, 0x62, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 ;BOOL APIE

3、NTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) g_hModule = hModule; return TRUE;extern C HRESULT _stdcall DllGetClassObject(const CLSID& clsid, const IID& iid, void *ppv) if (clsid = CLSID_CompA) CAFactory *pFactory = new CAFactory; if (pFactory = NULL) return E_OUTOFMEM

4、ORY ; HRESULT result = pFactory-QueryInterface(iid, ppv); return result; else return CLASS_E_CLASSNOTAVAILABLE; extern C HRESULT _stdcall DllCanUnloadNow(void) if (g_CompANumber = 0) & (g_LockNumber = 0) return S_OK; else return S_FALSE;/ Server registration/extern C HRESULT _stdcall DllRegisterServ

5、er() char szModule1024; DWORD dwResult = :GetModuleFileName(HMODULE)g_hModule, szModule, 1024); if (dwResult = 0) return SELFREG_E_CLASS; return RegisterServer(CLSID_CompA, szModule, CompA.Object, CompA Component, NULL);/ Server unregistration/extern C HRESULT _stdcall DllUnregisterServer() return U

6、nregisterServer(CLSID_CompA, CompA.Object,NULL);/ Implemention of class CACA:CA (IUnknown *pUnknownOuter) m_Ref = 0; g_CompANumber + ; m_pUnknownOuter = pUnknownOuter;CA:CA()ULONG CA:NondelegatingAddRef() m_Ref +; return (ULONG) m_Ref;ULONG CA:NondelegationRelease () m_Ref -; if (m_Ref = 0 ) g_CompA

7、Number - ; delete this; return 0; return (ULONG) m_Ref;HRESULT CA:NondelegationQueryInterface(const IID& iid, void *ppv) if ( iid = IID_IUnknown ) *ppv = (INondelegatingUnknown *) this ; (IUnknown *)(*ppv)-AddRef() ; else if ( iid = IID_SomeInterface ) *ppv = (ISomeInterface *) this ; (ISomeInterfac

8、e *)(*ppv)-AddRef() ; else *ppv = NULL; return E_NOINTERFACE ; return S_OK;ULONG CA:AddRef () if ( m_pUnknownOuter != NULL ) return m_pUnknownOuter-AddRef(); else return NondelegatingAddRef();ULONG CA:Release () if ( m_pUnknownOuter != NULL ) return m_pUnknownOuter-Release (); else return Nondelegat

9、ionRelease();HRESULT CA:QueryInterface(const IID& iid, void *ppv) if ( m_pUnknownOuter != NULL ) return m_pUnknownOuter-QueryInterface(iid, ppv); else return NondelegationQueryInterface(iid, ppv);HRESULT CA:SomeFunction() printf(This is CA:SomeFunction!n); return S_OK;/Aggre/CompA/CompA.def; CompA.d

10、ef : Declares the module parameters for the DLL.LIBRARY CompADESCRIPTION CompA Component Windows Dynamic Link LibraryEXPORTS ; Explicit exports can go here DllGetClassObject 1 PRIVATE DllRegisterServer 2 PRIVATE DllUnregisterServer 3 PRIVATE DllCanUnloadNow 4 PRIVATE/Aggre/CompA/CompA.H#ifndef _Comp

11、A_H_#define _CompA_H_#ifndef _ISomeInterface_H_#include SomeIFace.h#endifclass INondelegatingUnknownpublic: virtual HRESULT _stdcall NondelegationQueryInterface(const IID& iid, void *ppv) = 0 ; virtual ULONG _stdcall NondelegatingAddRef() = 0; virtual ULONG _stdcall NondelegationRelease() = 0;class

12、CA : public ISomeInterface, public INondelegatingUnknownprotected: ULONG m_Ref;public: CA(IUnknown *pUnknownOuter); CA();public : / Delegating IUnknown virtual HRESULT _stdcall QueryInterface(const IID& iid, void *ppv) ; virtual ULONG _stdcall AddRef() ; virtual ULONG _stdcall Release() ; / Nondeleg

13、ating IUnknown virtual HRESULT _stdcall NondelegationQueryInterface(const IID& iid, void *ppv); virtual ULONG _stdcall NondelegatingAddRef(); virtual ULONG _stdcall NondelegationRelease(); virtual HRESULT _stdcall SomeFunction( ) ; private : IUnknown *m_pUnknownOuter; / pointer to outer IUnknown;#en

14、dif _CompA_H_/Aggre/CompA/Factory.cpp#include stdafx.h#include factory.h#include CompA.hextern ULONG g_LockNumber;extern ULONG g_CompANumber;CAFactory:CAFactory() m_Ref = 0;CAFactory:CAFactory()HRESULT CAFactory:QueryInterface(const IID& iid, void *ppv) if ( iid = IID_IUnknown ) *ppv = (IUnknown *)

15、this ; (IUnknown *)(*ppv)-AddRef() ; else if ( iid = IID_IClassFactory) *ppv = (IClassFactory *) this ; (IClassFactory *)(*ppv)-AddRef() ; else *ppv = NULL; return E_NOINTERFACE ; return S_OK;ULONG CAFactory:AddRef() m_Ref +; return (ULONG) m_Ref;ULONG CAFactory:Release() m_Ref -; if (m_Ref = 0 ) de

16、lete this; return 0; return (ULONG) m_Ref;HRESULT CAFactory:CreateInstance(IUnknown *pUnknownOuter, const IID& iid, void *ppv) HRESULT hr; / iid must be IID_IUnknown for aggregating if ( ( pUnknownOuter != NULL ) & ( iid != IID_IUnknown ) ) return CLASS_E_NOAGGREGATION; *ppv=NULL; hr=E_OUTOFMEMORY;

17、/Create the object passing function to notify on destruction. CA *pObj=new CA (pUnknownOuter); if (NULL=pObj) return hr; /Obtain the first interface pointer (which does an AddRef) hr = pObj-NondelegationQueryInterface(iid, ppv); if (hr != S_OK) /Kill the object if initial creation or FInit failed. g

18、_CompANumber -; / Reference count g_CompANumber be added in constructor delete pObj; return hr; HRESULT CAFactory:LockServer(BOOL bLock) if (bLock) g_LockNumber +; else g_LockNumber -; return NOERROR;/Aggre/CompA/Factory.h#ifndef _CompA_FACTORY_#define _CompA_FACTORY_#include Unknwn.hclass CAFactory

19、 : public IClassFactory protected: ULONG m_Ref; public: CAFactory (); CAFactory (); /IUnknown members HRESULT _stdcall QueryInterface(const IID& iid, void *ppv); ULONG _stdcall AddRef(); ULONG _stdcall Release(); /IClassFactory members HRESULT _stdcall CreateInstance(IUnknown *, const IID& iid, void

20、 *ppv); HRESULT _stdcall LockServer(BOOL);#endif / _CompA_FACTORY_/Aggre/CompA/Registry.cpp/ Registry.cpp/#include #include #include Registry.h/ Internal helper functions prototypes/ - These helper functions were borrowed and modifed from/ Dale Rogersons book Inside COM./ Set the given key and its v

21、alue.BOOL SetKeyAndValue(const char* pszPath, const char* szSubkey, const char* szValue) ;/ Convert a CLSID into a char string.void CLSIDtoString(const CLSID& clsid, char* szCLSID, int length) ;/ Delete szKeyChild and all of its descendents.LONG DeleteKey(HKEY hKeyParent, const char* szKeyString) ;/

22、 Constants/ Size of a CLSID as a stringconst int CLSID_STRING_SIZE = 39 ;/ Public function implementation/ Register the component in the registry./HRESULT RegisterServer(const CLSID& clsid, / Class ID const char *szFileName, / DLL module handle const char* szProgID, / IDs const char* szDescription,

23、/ Description String const char* szVerIndProgID) / optional / Convert the CLSID into a char. char szCLSIDCLSID_STRING_SIZE ; CLSIDtoString(clsid, szCLSID, sizeof(szCLSID) ; / Build the key CLSID. char szKey64 ; strcpy(szKey, CLSID) ; strcat(szKey, szCLSID) ; / Add the CLSID to the registry. SetKeyAn

24、dValue(szKey, NULL, szDescription) ; / Add the server filename subkey under the CLSID key. SetKeyAndValue(szKey, InprocServer32, szFileName) ; / Add the ProgID subkey under the CLSID key. if (szProgID != NULL) SetKeyAndValue(szKey, ProgID, szProgID) ; SetKeyAndValue(szProgID, CLSID, szCLSID) ; if (s

25、zVerIndProgID) / Add the version-independent ProgID subkey under CLSID key. SetKeyAndValue(szKey, VersionIndependentProgID, szVerIndProgID) ; / Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT. SetKeyAndValue(szVerIndProgID, NULL, szDescription) ; SetKeyAndValue(szVerIndProgID, CLSI

26、D, szCLSID) ; SetKeyAndValue(szVerIndProgID, CurVer, szProgID) ; / Add the versioned ProgID subkey under HKEY_CLASSES_ROOT. SetKeyAndValue(szProgID, NULL, szDescription) ; SetKeyAndValue(szProgID, CLSID, szCLSID) ; return S_OK ;/ Remove the component from the registry./HRESULT UnregisterServer(const

27、 CLSID& clsid, / Class ID const char* szProgID, / IDs const char* szVerIndProgID) / Programmatic / Convert the CLSID into a char. char szCLSIDCLSID_STRING_SIZE ; CLSIDtoString(clsid, szCLSID, sizeof(szCLSID) ; / Build the key CLSID. char szKey64 ; strcpy(szKey, CLSID) ; strcat(szKey, szCLSID) ; / De

28、lete the CLSID Key - CLSID. LONG lResult = DeleteKey(HKEY_CLASSES_ROOT, szKey) ; / Delete the version-independent ProgID Key. if (szVerIndProgID != NULL) lResult = DeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID) ; / Delete the ProgID key. if (szProgID != NULL) lResult = DeleteKey(HKEY_CLASSES_ROOT, szProgID) ; return S_OK ;/

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1