component.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #include <iostream.h>
  3. #include "Componentcomponent.h" // Generated by MIDL
  4. #include "registry.h" // Add This!!!
  5. // {10000002-0000-0000-0000-000000000001}
  6. const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
  7. long g_cComponents = 0;
  8. long g_cServerLocks = 0;
  9. class CInsideCOM : public ISum
  10. {
  11. public:
  12. // IUnknown
  13. ULONG __stdcall AddRef();
  14. ULONG __stdcall Release();
  15. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  16. // ISum
  17. HRESULT __stdcall Sum(int x, int y, int* retval);
  18. CInsideCOM() : m_cRef(1) { g_cComponents++; }
  19. ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
  20. private:
  21. ULONG m_cRef;
  22. };
  23. ULONG CInsideCOM::AddRef()
  24. {
  25. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  26. return ++m_cRef;
  27. }
  28. ULONG CInsideCOM::Release()
  29. {
  30. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  31. if(--m_cRef != 0)
  32. return m_cRef;
  33. delete this;
  34. return 0;
  35. }
  36. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  37. {
  38. if(riid == IID_IUnknown)
  39. {
  40. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  41. *ppv = (IUnknown*)this;
  42. }
  43. else if(riid == IID_ISum)
  44. {
  45. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  46. *ppv = (ISum*)this;
  47. }
  48. else 
  49. {
  50. *ppv = NULL;
  51. return E_NOINTERFACE;
  52. }
  53. AddRef();
  54. return S_OK;
  55. }
  56. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  57. {
  58. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  59. *retval = x + y;
  60. return S_OK;
  61. }
  62. class CFactory : public IClassFactory
  63. {
  64. public:
  65. // IUnknown
  66. ULONG __stdcall AddRef();
  67. ULONG __stdcall Release();
  68. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  69. // IClassFactory
  70. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  71. HRESULT __stdcall LockServer(BOOL bLock);
  72. CFactory() : m_cRef(1) { }
  73. ~CFactory() { }
  74. private:
  75. ULONG m_cRef;
  76. };
  77. ULONG CFactory::AddRef()
  78. {
  79. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  80. return ++m_cRef;
  81. }
  82. ULONG CFactory::Release()
  83. {
  84. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  85. if(--m_cRef != 0)
  86. return m_cRef;
  87. delete this;
  88. return 0;
  89. }
  90. HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
  91. {
  92. if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  93. {
  94. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  95. *ppv = (IClassFactory *)this;
  96. }
  97. else
  98. {
  99. *ppv = NULL;
  100. return E_NOINTERFACE;
  101. }
  102. AddRef();
  103. return S_OK;
  104. }
  105. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv)
  106. {
  107. if(pUnknownOuter != NULL)
  108. return CLASS_E_NOAGGREGATION;
  109. CInsideCOM *pInsideCOM = new CInsideCOM;
  110. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  111. if(pInsideCOM == NULL)
  112. return E_OUTOFMEMORY;
  113. // QueryInterface probably for IID_IUNKNOWN
  114. HRESULT hr = pInsideCOM->QueryInterface(iid, ppv);
  115. pInsideCOM->Release();
  116. return hr;
  117. }
  118. HRESULT CFactory::LockServer(BOOL bLock)
  119. {
  120. if(bLock)
  121. g_cServerLocks++;
  122. else
  123. g_cServerLocks--;
  124. return S_OK;
  125. }
  126. HRESULT __stdcall DllCanUnloadNow()
  127. {
  128. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  129. if(g_cServerLocks == 0 && g_cComponents == 0)
  130. return S_OK;
  131. else
  132. return S_FALSE;
  133. }
  134. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
  135. {
  136. cout << "Component: DllGetClassObject" << endl;
  137. if(clsid != CLSID_InsideCOM)
  138. return CLASS_E_CLASSNOTAVAILABLE;
  139. CFactory* pFactory = new CFactory;
  140. if(pFactory == NULL)
  141. return E_OUTOFMEMORY;
  142. // QueryInterface probably for IClassFactory
  143. HRESULT hr = pFactory->QueryInterface(iid, ppv);
  144. pFactory->Release();
  145. return hr;
  146. }
  147. HRESULT __stdcall DllRegisterServer()
  148. {
  149. return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  150. }
  151. HRESULT __stdcall DllUnregisterServer()
  152. {
  153. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  154. }