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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #include <iostream.h>
  3. #include "Componentcomponent.h" // Generated by MIDL
  4. #include "registry.h" // Add This!!!
  5. HINSTANCE g_hInstance;
  6. long g_cComponents = 0;
  7. long g_cServerLocks = 0;
  8. HANDLE g_hEvent; // For a later exercise with local components
  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_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 riid, void** ppv);
  69. // IClassFactory
  70. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, 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 riid, void** ppv)
  91. {
  92. if(riid == IID_IUnknown || riid == 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 riid, 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(riid, 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 riid, 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(riid, ppv);
  144. pFactory->Release();
  145. return hr;
  146. }
  147. HRESULT __stdcall DllRegisterServer()
  148. {
  149. char DllPath[256];
  150. OLECHAR wDllPath[256];
  151. GetModuleFileName(g_hInstance, DllPath, 256);
  152. mbstowcs(wDllPath, DllPath, 256);
  153. ITypeLib* pTypeLib;
  154. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  155. if(FAILED(hr))
  156. return hr;
  157. pTypeLib->Release();
  158. return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  159. }
  160. HRESULT __stdcall DllUnregisterServer()
  161. {
  162. HRESULT hr = UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  163. if(FAILED(hr))
  164. return hr;
  165. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  166. }
  167. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  168. {
  169. g_hInstance = hInstance;
  170. return TRUE;
  171. }