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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #define _WIN32_DCOM
  3. #include <windows.h>
  4. #include <ocidl.h>
  5. #include <iostream.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include "Componentcomponent.h" // Generated by MIDL
  9. #include "registry.h"
  10. const REG_DATA g_regData[] = {
  11.     { "CLSID\{10000002-0000-0000-0000-000000000001}", 0, "Inside COM+: In Process Component" },
  12.     { "CLSID\{10000002-0000-0000-0000-000000000001}", "AppID", "{10000002-0000-0000-0000-000000000001}" },
  13. { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", 0, (const char*)-1 }, 
  14. { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", "ThreadingModel", "Both" }, 
  15. { "CLSID\{10000002-0000-0000-0000-000000000001}\ProgID", 0, "Component.InsideCOM.1" },
  16. { "CLSID\{10000002-0000-0000-0000-000000000001}\VersionIndependentProgID", 0, "Component.InsideCOM" },
  17. { "AppID\{10000002-0000-0000-0000-000000000001}", 0, "Inside COM+: In Process Component" },
  18. { "AppID\{10000002-0000-0000-0000-000000000001}", "DllSurrogate", "" },
  19. { "Component.InsideCOM", 0, "Inside COM+: In Process Component" },
  20. { "Component.InsideCOM\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
  21. { "Component.InsideCOM\CurVer", 0, "Component.InsideCOM.1" },
  22. { "Component.InsideCOM.1", 0, "Inside COM+: In Process Component" },
  23. { "Component.InsideCOM.1\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
  24. { 0, 0, 0 }
  25. };
  26. HINSTANCE g_hInstance;
  27. long g_cLocks = 0;
  28. class CInsideCOM : public IPrime, public IPipeLong
  29. {
  30. public:
  31. // IUnknown
  32. ULONG __stdcall AddRef();
  33. ULONG __stdcall Release();
  34. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  35. // IPrime
  36. HRESULT __stdcall IsPrime(int testnumber, int* retval);
  37. // IPipeLong
  38.     HRESULT __stdcall Pull(long* buf, ULONG cRequest, ULONG* pcReturned);
  39.     HRESULT __stdcall Push(long* buf, ULONG cSent);
  40. CInsideCOM() : m_cRef(1) { g_cLocks++; }
  41. ~CInsideCOM()
  42. {
  43. cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
  44. g_cLocks--;
  45. }
  46. private:
  47. ULONG m_cRef;
  48. };
  49. HRESULT CInsideCOM::Pull(long* buf, ULONG cRequest, ULONG* pcReturned)
  50. {
  51. long square = 1;
  52. for(ULONG count = 0; count < cRequest; count++)
  53. {
  54. buf[count] = square++;
  55. }
  56. *pcReturned = count;
  57. return S_OK;
  58. }
  59. HRESULT CInsideCOM::Push(long* buf, ULONG cSent)
  60. {
  61. return S_OK;
  62. }
  63. HRESULT CInsideCOM::IsPrime(int testnumber, int* retval)
  64. {
  65. unsigned long count;
  66. unsigned long HalfNumber = testnumber / 2 + 1;
  67. for(count = 2; count < HalfNumber; count++)
  68. if(testnumber % count == 0)
  69. {
  70. *retval = false;
  71. return S_OK;
  72. }
  73. *retval = true;
  74. return S_OK;
  75. }
  76. ULONG CInsideCOM::AddRef()
  77. {
  78. return ++m_cRef;
  79. }
  80. ULONG CInsideCOM::Release()
  81. {
  82. if(--m_cRef != 0)
  83. return m_cRef;
  84. delete this;
  85. return 0;
  86. }
  87. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  88. {
  89. if(riid == IID_IUnknown)
  90. *ppv = (IPrime*)this;
  91. else if(riid == IID_IPipeLong)
  92. *ppv = (IPipeLong*)this;
  93. else if(riid == IID_IPrime)
  94. *ppv = (IPrime*)this;
  95. else
  96. {
  97. *ppv = NULL;
  98. return E_NOINTERFACE;
  99. }
  100. AddRef();
  101. return S_OK;
  102. }
  103. class CFactory : public IClassFactory
  104. {
  105. public:
  106. // IUnknown
  107. ULONG __stdcall AddRef();
  108. ULONG __stdcall Release();
  109. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  110. // IClassFactory
  111. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  112. HRESULT __stdcall LockServer(BOOL bLock);
  113. CFactory() : m_cRef(1) { g_cLocks++; }
  114. ~CFactory() { g_cLocks--; }
  115. private:
  116. ULONG m_cRef;
  117. };
  118. ULONG CFactory::AddRef()
  119. {
  120. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  121. return ++m_cRef;
  122. }
  123. ULONG CFactory::Release()
  124. {
  125. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  126. if(--m_cRef != 0)
  127. return m_cRef;
  128. delete this;
  129. return 0;
  130. }
  131. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  132. {
  133. if(riid == IID_IUnknown || riid == IID_IClassFactory)
  134. *ppv = (IClassFactory*)this;
  135. else
  136. {
  137. *ppv = NULL;
  138. return E_NOINTERFACE;
  139. }
  140. AddRef();
  141. return S_OK;
  142. }
  143. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  144. {
  145. if(pUnknownOuter != NULL)
  146. return CLASS_E_NOAGGREGATION;
  147. CInsideCOM *pInsideCOM = new CInsideCOM;
  148. if(pInsideCOM == NULL)
  149. return E_OUTOFMEMORY;
  150. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  151. pInsideCOM->Release();
  152. return hr;
  153. }
  154. HRESULT CFactory::LockServer(BOOL bLock)
  155. {
  156. if(bLock)
  157. g_cLocks++;
  158. else
  159. g_cLocks--;
  160. return S_OK;
  161. }
  162. HRESULT __stdcall DllCanUnloadNow()
  163. {
  164. cout << "Component: DllCanUnloadNow() " << (g_cLocks == 0 ? "Yes" : "No") << endl;
  165. if(g_cLocks == 0)
  166. return S_OK;
  167. else
  168.     return S_FALSE;
  169. }
  170. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
  171. {
  172. if(clsid != CLSID_InsideCOM)
  173. return CLASS_E_CLASSNOTAVAILABLE;
  174. CFactory* pFactory = new CFactory;
  175. if(pFactory == NULL)
  176. return E_OUTOFMEMORY;
  177. HRESULT hr = pFactory->QueryInterface(riid, ppv);
  178. pFactory->Release();
  179. return hr;
  180. }
  181. HRESULT __stdcall DllRegisterServer()
  182. {
  183. char DllPath[MAX_PATH];
  184. GetModuleFileName(g_hInstance, DllPath, sizeof(DllPath));
  185. OLECHAR wDllPath[MAX_PATH];
  186. mbstowcs(wDllPath, DllPath, sizeof(wDllPath));
  187. ITypeLib* pTypeLib;
  188. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  189. if(FAILED(hr))
  190. return hr;
  191. pTypeLib->Release();
  192. return RegisterServerEx(g_regData, DllPath);
  193. }
  194. HRESULT __stdcall DllUnregisterServer()
  195. {
  196. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  197. return UnregisterServerEx(g_regData);
  198. }
  199. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  200. {
  201. g_hInstance = hInstance;
  202. return TRUE;
  203. }