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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include "Componentcomponent.h"
  5. #include "registry.h"
  6. #include <conio.h>
  7. HANDLE g_hEvent;
  8. class CInsideDCOM : public ISum
  9. {
  10. public:
  11. // IUnknown
  12. ULONG __stdcall AddRef();
  13. ULONG __stdcall Release();
  14. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  15. // ISum
  16. HRESULT __stdcall Sum(int x, int y, int *retval);
  17. CInsideDCOM();
  18. ~CInsideDCOM();
  19. private:
  20. long m_cRef;
  21. };
  22. void InitiateComponentShutdown()
  23. {
  24. cout << "InitiateComponentShutdown()" << endl;
  25. SetEvent(g_hEvent);
  26. }
  27. CInsideDCOM::CInsideDCOM() : m_cRef(1)
  28. {
  29. CoAddRefServerProcess();
  30. }
  31. CInsideDCOM::~CInsideDCOM()
  32. {
  33. cout << "Component: CInsideDCOM::~CInsideDCOM()" << endl;
  34. if(CoReleaseServerProcess() == 0)
  35. InitiateComponentShutdown();
  36. }
  37. ULONG CInsideDCOM::AddRef()
  38. {
  39. cout << "Component: CInsideDCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  40. return InterlockedIncrement(&m_cRef);
  41. }
  42. ULONG CInsideDCOM::Release()
  43. {
  44. cout << "Component: CInsideDCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  45. unsigned cRef = InterlockedDecrement(&m_cRef);
  46. if(cRef != 0)
  47. return cRef;
  48. delete this;
  49. return 0;
  50. }
  51. HRESULT CInsideDCOM::QueryInterface(REFIID iid, void **ppv)
  52. {
  53. if(iid == IID_IUnknown)
  54. {
  55. cout << "Component: CInsideDCOM::QueryInterface() for IUnknown" << endl;
  56. *ppv = (IUnknown*)this;
  57. }
  58. else if(iid == IID_ISum)
  59. {
  60. cout << "Component: CInsideDCOM::QueryInterface() for ISum" << endl;
  61. *ppv = (ISum*)this;
  62. }
  63. else
  64. {
  65. *ppv = NULL;
  66. return E_NOINTERFACE;
  67. }
  68. AddRef();
  69. return S_OK;
  70. }
  71. HRESULT CInsideDCOM::Sum(int x, int y, int *retval)
  72. {
  73. *retval = x + y;
  74. return S_OK;
  75. }
  76. class CFactory : public IClassFactory
  77. {
  78. public:
  79. // IUnknown
  80. ULONG __stdcall AddRef();
  81. ULONG __stdcall Release();
  82. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  83. // IClassFactory
  84. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  85. HRESULT __stdcall LockServer(BOOL bLock);
  86. CFactory() : m_cRef(1) { }
  87. ~CFactory() { cout << "Component: CFactory::~CFactory()" << endl; }
  88. private:
  89. long m_cRef;
  90. };
  91. ULONG CFactory::AddRef()
  92. {
  93. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  94. return InterlockedIncrement(&m_cRef);
  95. }
  96. ULONG CFactory::Release()
  97. {
  98. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  99. unsigned cRef = InterlockedDecrement(&m_cRef);
  100. if(cRef != 0)
  101. return cRef;
  102. delete this;
  103. return 0;
  104. }
  105. HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
  106. {
  107. if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  108. {
  109. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  110. *ppv = (IClassFactory *)this;
  111. }
  112. else 
  113. {
  114. *ppv = NULL;
  115. return E_NOINTERFACE;
  116. }
  117. AddRef();
  118. return S_OK;
  119. }
  120. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void **ppv)
  121. {
  122. if(pUnknownOuter != NULL)
  123. return CLASS_E_NOAGGREGATION;
  124. CInsideDCOM *pInsideDCOM = new CInsideDCOM;
  125. cout << "Component: CFactory::CreateInstance() " << pInsideDCOM << endl;
  126. if(pInsideDCOM == NULL)
  127. return E_OUTOFMEMORY;
  128. HRESULT hr = pInsideDCOM->QueryInterface(iid, ppv);
  129. pInsideDCOM->Release();
  130. return hr;
  131. }
  132. HRESULT CFactory::LockServer(BOOL bLock)
  133. {
  134. if(bLock)
  135. CoAddRefServerProcess();
  136. else
  137. if(CoReleaseServerProcess() == 0)
  138. InitiateComponentShutdown();
  139. return S_OK;
  140. }
  141. void RegisterComponent()
  142. {
  143. ITypeLib* pTypeLib;
  144. LoadTypeLibEx(L"Component.exe", REGKIND_DEFAULT, &pTypeLib);
  145. RegisterServer("Component.exe", CLSID_InsideDCOM, "Inside DCOM Sample #1", "Component.InsideDCOM", "Component.InsideDCOM.1", NULL);
  146. }
  147. void CommandLineParameters(int argc, char** argv)
  148. {
  149. RegisterComponent();
  150. if(argc < 2)
  151. {
  152. cout << "No parameter, but registered anyway..." << endl;
  153. exit(false);
  154. }
  155. char* szToken = strtok(argv[1], "-/"); 
  156. if(_stricmp(szToken, "RegServer") == 0)
  157. {
  158. RegisterComponent();
  159. cout << "RegServer" << endl;
  160. exit(true);
  161. }
  162. if(_stricmp(szToken, "UnregServer") == 0)
  163. {
  164. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  165. UnregisterServer(CLSID_InsideDCOM, "Component.InsideDCOM", "Component.InsideDCOM.1");
  166. cout << "UnregServer" << endl;
  167. exit(true);
  168. }
  169. if(_stricmp(szToken, "Embedding") != 0)
  170. {
  171. cout << "Invalid parameter" << endl;
  172. exit(false);
  173. }
  174. }
  175. void main(int argc, char** argv)
  176. {
  177. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  178. CommandLineParameters(argc, argv);
  179. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  180. DWORD dwRegister;
  181. IClassFactory *pIFactory = new CFactory();
  182. CoRegisterClassObject(CLSID_InsideDCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
  183. IRunningObjectTable* pRunningObjectTable;
  184. GetRunningObjectTable(NULL, &pRunningObjectTable);
  185. unsigned long handle;
  186. RegisterActiveObject((IUnknown*)pIFactory, CLSID_InsideDCOM, ACTIVEOBJECT_WEAK, &handle);
  187. CoResumeClassObjects();
  188. WaitForSingleObject(g_hEvent, INFINITE);
  189. CloseHandle(g_hEvent);
  190. _getch();
  191. CoRevokeClassObject(dwRegister);
  192. RevokeActiveObject(handle, NULL);
  193. pRunningObjectTable->Release();
  194. pIFactory->Release();
  195. CoUninitialize();
  196. }