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

Windows编程

开发平台:

Visual C++

  1. // local.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include "Componentcomponent.h"
  7. #include "registry.h"
  8. HANDLE g_hEvent;
  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();
  19. ~CInsideCOM();
  20. private:
  21. long m_cRef;
  22. };
  23. void InitiateComponentShutdown()
  24. {
  25. cout << "InitiateComponentShutdown()" << endl;
  26. PostQuitMessage(0);
  27. }
  28. CInsideCOM::CInsideCOM() : m_cRef(1)
  29. {
  30. CoAddRefServerProcess();
  31. }
  32. CInsideCOM::~CInsideCOM()
  33. {
  34. cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
  35. if(CoReleaseServerProcess() == 0)
  36. InitiateComponentShutdown();
  37. }
  38. ULONG CInsideCOM::AddRef()
  39. {
  40. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  41. return InterlockedIncrement(&m_cRef);
  42. }
  43. ULONG CInsideCOM::Release()
  44. {
  45. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  46. unsigned cRef = InterlockedDecrement(&m_cRef);
  47. if(cRef != 0)
  48. return cRef;
  49. delete this;
  50. return 0;
  51. }
  52. HRESULT CInsideCOM::QueryInterface(REFIID riid, void **ppv)
  53. {
  54. if(riid == IID_IUnknown)
  55. {
  56. cout << "Component: CInsideCOM::QueryInterface() for IUnknown" << endl;
  57. *ppv = (IUnknown*)this;
  58. }
  59. else if(riid == IID_ISum)
  60. {
  61. cout << "Component: CInsideCOM::QueryInterface() for ISum" << endl;
  62. *ppv = (ISum*)this;
  63. }
  64. else
  65. {
  66. *ppv = NULL;
  67. return E_NOINTERFACE;
  68. }
  69. AddRef();
  70. return S_OK;
  71. }
  72. HRESULT CInsideCOM::Sum(int x, int y, int *retval)
  73. {
  74. *retval = x + y;
  75. return S_OK;
  76. }
  77. class CFactory : public IClassFactory
  78. {
  79. public:
  80. // IUnknown
  81. ULONG __stdcall AddRef();
  82. ULONG __stdcall Release();
  83. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  84. // IClassFactory
  85. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  86. HRESULT __stdcall LockServer(BOOL bLock);
  87. CFactory() : m_cRef(1) { }
  88. ~CFactory() { cout << "Component: CFactory::~CFactory()" << endl; }
  89. private:
  90. long m_cRef;
  91. };
  92. ULONG CFactory::AddRef()
  93. {
  94. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  95. return InterlockedIncrement(&m_cRef);
  96. }
  97. ULONG CFactory::Release()
  98. {
  99. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  100. unsigned cRef = InterlockedDecrement(&m_cRef);
  101. if(cRef != 0)
  102. return cRef;
  103. delete this;
  104. return 0;
  105. }
  106. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  107. {
  108. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  109. {
  110. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  111. *ppv = (IClassFactory *)this;
  112. }
  113. else 
  114. {
  115. *ppv = NULL;
  116. return E_NOINTERFACE;
  117. }
  118. AddRef();
  119. return S_OK;
  120. }
  121. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void **ppv)
  122. {
  123. if(pUnknownOuter != NULL)
  124. return CLASS_E_NOAGGREGATION;
  125. CInsideCOM *pInsideCOM = new CInsideCOM;
  126. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  127. if(pInsideCOM == NULL)
  128. return E_OUTOFMEMORY;
  129. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  130. pInsideCOM->Release();
  131. return hr;
  132. }
  133. HRESULT CFactory::LockServer(BOOL bLock)
  134. {
  135. if(bLock)
  136. CoAddRefServerProcess();
  137. else
  138. if(CoReleaseServerProcess() == 0)
  139. InitiateComponentShutdown();
  140. return S_OK;
  141. }
  142. void RegisterComponent()
  143. {
  144. ITypeLib* pTypeLib;
  145. HRESULT hr = LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
  146. pTypeLib->Release();
  147. RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  148. }
  149. void CommandLineParameters(int argc, char** argv)
  150. {
  151. RegisterComponent();
  152. if(argc < 2)
  153. {
  154. cout << "No parameter, but registered anyway..." << endl;
  155. exit(false);
  156. }
  157. char* szToken = strtok(argv[1], "-/"); 
  158. if(_stricmp(szToken, "RegServer") == 0)
  159. {
  160. RegisterComponent();
  161. cout << "RegServer" << endl;
  162. exit(true);
  163. }
  164. if(_stricmp(szToken, "UnregServer") == 0)
  165. {
  166. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  167. UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  168. cout << "UnregServer" << endl;
  169. exit(true);
  170. }
  171. if(_stricmp(szToken, "Embedding") != 0)
  172. {
  173. cout << "Invalid parameter" << endl;
  174. exit(false);
  175. }
  176. }
  177. class CMessageFilter : public IMessageFilter
  178. {
  179. public:
  180. // IUnknown
  181. ULONG __stdcall AddRef();
  182. ULONG __stdcall Release();
  183. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  184. // IMessageFilter
  185.     DWORD __stdcall HandleInComingCall(DWORD dwCallType, HTASK htaskCaller, DWORD dwTickCount, LPINTERFACEINFO lpInterfaceInfo);
  186.     DWORD __stdcall RetryRejectedCall(HTASK htaskCallee, DWORD dwTickCount, DWORD dwRejectType);
  187.     DWORD __stdcall MessagePending(HTASK htaskCallee, DWORD dwTickCount, DWORD dwPendingType);
  188. };
  189. ULONG CMessageFilter::AddRef()
  190. {
  191. return 2;
  192. }
  193. ULONG CMessageFilter::Release()
  194. {
  195. return 1;
  196. }
  197. HRESULT CMessageFilter::QueryInterface(REFIID riid, void **ppv)
  198. {
  199. if(riid == IID_IUnknown || riid == IID_IMessageFilter)
  200. *ppv = (IMessageFilter*)this;
  201. else
  202. {
  203. *ppv = NULL;
  204. return E_NOINTERFACE;
  205. }
  206. return S_OK;
  207. }
  208. DWORD CMessageFilter::HandleInComingCall(DWORD dwCallType, HTASK htaskCaller, DWORD dwTickCount, LPINTERFACEINFO lpInterfaceInfo)
  209. {
  210. cout << "Component: CMessageFilter::HandleInComingCall" << endl;
  211. if(lpInterfaceInfo->iid == IID_ISum)
  212. {
  213. static int counter = 0;
  214. static int randa = (int)((((float)rand())/RAND_MAX)*10);
  215. cout << "Number of times = " << randa << endl;
  216. if(counter++ < randa)
  217. return SERVERCALL_RETRYLATER;
  218. }
  219. return SERVERCALL_ISHANDLED;
  220. }
  221. DWORD CMessageFilter::RetryRejectedCall(HTASK htaskCallee, DWORD dwTickCount, DWORD dwRejectType)
  222. {
  223. return E_NOTIMPL;
  224. }
  225. DWORD CMessageFilter::MessagePending(HTASK htaskCallee, DWORD dwTickCount, DWORD dwPendingType)
  226. {
  227. return E_NOTIMPL;
  228. }
  229. void main(int argc, char** argv)
  230. {
  231. srand(GetTickCount());
  232. srand(rand());
  233. CommandLineParameters(argc, argv);
  234. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  235. IMessageFilter* pMF = new CMessageFilter;
  236. IMessageFilter* pOldMF;
  237. CoRegisterMessageFilter(pMF, &pOldMF);
  238. DWORD dwRegister;
  239. IClassFactory *pIFactory = new CFactory();
  240. CoRegisterClassObject(CLSID_InsideCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
  241. CoResumeClassObjects();
  242. MSG msg;
  243. while(GetMessage(&msg, NULL, 0, 0))
  244. DispatchMessage(&msg);
  245. CoRevokeClassObject(dwRegister);
  246. pIFactory->Release();
  247. CoUninitialize();
  248. Sleep(10000);
  249. }