CompB.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:4k
源码类别:

DNA

开发平台:

Visual C++

  1. // CompB.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include <comutil.h>
  5. #include <stdio.h>
  6. #include "objbase.h"
  7. #include "olectl.h"
  8. #include "CompB.h"
  9. #include "factory.h"
  10. #include "registry.h"
  11. ULONG    g_LockNumber = 0;
  12. ULONG    g_CompBNumber = 0;
  13. HANDLE  g_hModule;
  14. // {16DCB981-BEEB-11d2-B362-00104B08CC22}
  15. extern "C" const GUID CLSID_CompA = 
  16. { 0x16dcb981, 0xbeeb, 0x11d2, 
  17. { 0xb3, 0x62, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 } };
  18. // {16DCB982-BEEB-11d2-B362-00104B08CC22}
  19. extern "C" const GUID IID_SomeInterface = 
  20. { 0x16dcb982, 0xbeeb, 0x11d2, 
  21. { 0xb3, 0x62, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 } };
  22. // {39C07941-BFCC-11d2-A100-00A0C9A6F472}
  23. extern "C" const GUID CLSID_CompB = 
  24. { 0x39c07941, 0xbfcc, 0x11d2, 
  25. { 0xa1, 0x0, 0x0, 0xa0, 0xc9, 0xa6, 0xf4, 0x72 } };
  26. // {39C07942-BFCC-11d2-A100-00A0C9A6F472}
  27. extern "C" const GUID IID_OtherInterface = 
  28. { 0x39c07942, 0xbfcc, 0x11d2, 
  29. { 0xa1, 0x0, 0x0, 0xa0, 0xc9, 0xa6, 0xf4, 0x72 } };
  30. BOOL APIENTRY DllMain( HANDLE hModule, 
  31.                        DWORD  ul_reason_for_call, 
  32.                        LPVOID lpReserved
  33.  )
  34. {
  35.     g_hModule = hModule;
  36. return TRUE;
  37. }
  38. extern "C" HRESULT __stdcall DllGetClassObject(const CLSID& clsid, const IID& iid, void **ppv)
  39. {
  40. if (clsid == CLSID_CompB) {
  41. CBFactory *pFactory = new CBFactory;
  42. if (pFactory == NULL) {
  43. return E_OUTOFMEMORY ;
  44. }
  45. HRESULT result = pFactory->QueryInterface(iid, ppv);
  46. return result;
  47. } else {
  48. return CLASS_E_CLASSNOTAVAILABLE;
  49. }
  50. }
  51. extern "C" HRESULT __stdcall DllCanUnloadNow(void)
  52. {
  53. if ((g_CompBNumber == 0) && (g_LockNumber == 0))
  54. return S_OK;
  55. else
  56. return S_FALSE;
  57. }
  58. //
  59. // Server registration
  60. //
  61. extern "C" HRESULT __stdcall DllRegisterServer()
  62. {
  63. char szModule[1024];
  64. DWORD dwResult = ::GetModuleFileName((HMODULE)g_hModule, szModule, 1024);
  65. if (dwResult == 0)
  66. return SELFREG_E_CLASS;
  67. return RegisterServer(CLSID_CompB,
  68.                       szModule, 
  69.   "CompB.Object",
  70.   "CompB Component",
  71.   NULL);
  72. }
  73. //
  74. // Server unregistration
  75. //
  76. extern "C" HRESULT __stdcall DllUnregisterServer()
  77. {
  78. return UnregisterServer(CLSID_CompB,
  79.                         "CompB.Object",NULL);
  80. }
  81. // Implemention of class CB
  82. CB::CB ()
  83. {
  84. m_Ref = 0;
  85. g_CompBNumber ++ ;
  86. m_pUnknownInner = NULL;
  87. }
  88. CB::~CB ( )
  89. {
  90. m_Ref = 1;
  91. IUnknown *pUnknownOuter = this;
  92. pUnknownOuter->AddRef();
  93. if (m_pSomeInterface != NULL)
  94. m_pSomeInterface->Release();
  95. if (m_pUnknownInner != NULL) 
  96. m_pUnknownInner->Release();
  97. }
  98. HRESULT CB::Init()
  99. {
  100. IUnknown *pUnknownOuter = (IUnknown *)this;
  101. HRESULT result = ::CoCreateInstance(CLSID_CompA, pUnknownOuter, 
  102. CLSCTX_INPROC_SERVER, 
  103. IID_IUnknown, (void **)& m_pUnknownInner) ;
  104. if (FAILED(result))
  105. return E_FAIL;
  106. result = m_pUnknownInner->QueryInterface(IID_SomeInterface, (void **)&m_pSomeInterface);
  107. if (FAILED(result))
  108. {
  109. m_pUnknownInner->Release();
  110. return E_FAIL;
  111. }
  112. pUnknownOuter->Release();
  113. return S_OK;
  114. }
  115. ULONG CB::AddRef()
  116. {
  117. m_Ref ++;
  118. return  (ULONG) m_Ref;
  119. }
  120. ULONG CB::Release ()
  121. {
  122. m_Ref --;
  123. if (m_Ref == 0 )
  124. {
  125. g_CompBNumber -- ; 
  126. delete this;
  127. return 0;
  128. }
  129. return  (ULONG) m_Ref;
  130. }
  131. HRESULT CB::QueryInterface(const IID& iid, void **ppv)
  132. {
  133. if ( iid == IID_IUnknown )
  134. {
  135. *ppv = (IUnknown *) this ;
  136. ((IUnknown *)(*ppv))->AddRef() ;
  137. } else if ( iid == IID_OtherInterface ) 
  138. {
  139. *ppv = (IOtherInterface *) this ;
  140. ((IOtherInterface *)(*ppv))->AddRef() ;
  141. } else if ( iid == IID_SomeInterface ) 
  142. {
  143. return m_pUnknownInner->QueryInterface(iid, ppv) ;
  144. } else 
  145. {
  146. *ppv = NULL;
  147. return E_NOINTERFACE ;
  148. }
  149. return S_OK;
  150. }
  151. HRESULT CB::OtherFunction()
  152. {
  153. printf("This is CB::OtherFunction!n");
  154. return S_OK;
  155. }