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