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

Windows编程

开发平台:

Visual C++

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