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

Windows编程

开发平台:

Visual C++

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