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

Windows编程

开发平台:

Visual C++

  1. // local.cpp
  2. #define _WIN32_DCOM
  3. #include <iostream.h>
  4. #include "Componentcomponent.h"
  5. #include "registry.h"
  6. HANDLE g_hEvent;
  7. class CInsideCOM : public ISum
  8. {
  9. public:
  10. // IUnknown
  11. ULONG __stdcall AddRef();
  12. ULONG __stdcall Release();
  13. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  14. // ISum
  15. HRESULT __stdcall Sum(int x, int y, int *retval);
  16. CInsideCOM();
  17. ~CInsideCOM();
  18. private:
  19. long m_cRef;
  20. };
  21. void InitiateComponentShutdown()
  22. {
  23. cout << "InitiateComponentShutdown()" << endl;
  24. PostQuitMessage(0);
  25. }
  26. CInsideCOM::CInsideCOM() : m_cRef(1)
  27. {
  28. CoAddRefServerProcess();
  29. }
  30. CInsideCOM::~CInsideCOM()
  31. {
  32. cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
  33. if(CoReleaseServerProcess() == 0)
  34. InitiateComponentShutdown();
  35. }
  36. ULONG CInsideCOM::AddRef()
  37. {
  38. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  39. return InterlockedIncrement(&m_cRef);
  40. }
  41. ULONG CInsideCOM::Release()
  42. {
  43. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  44. unsigned cRef = InterlockedDecrement(&m_cRef);
  45. if(cRef != 0)
  46. return cRef;
  47. delete this;
  48. return 0;
  49. }
  50. HRESULT CInsideCOM::QueryInterface(REFIID riid, void **ppv)
  51. {
  52. if(riid == IID_IUnknown)
  53. {
  54. cout << "Component: CInsideCOM::QueryInterface() for IUnknown" << endl;
  55. *ppv = (IUnknown*)this;
  56. }
  57. else if(riid == IID_ISum)
  58. {
  59. cout << "Component: CInsideCOM::QueryInterface() for ISum" << endl;
  60. *ppv = (ISum*)this;
  61. }
  62. else
  63. {
  64. *ppv = NULL;
  65. return E_NOINTERFACE;
  66. }
  67. AddRef();
  68. return S_OK;
  69. }
  70. HRESULT CInsideCOM::Sum(int x, int y, int *retval)
  71. {
  72. *retval = x + y;
  73. return S_OK;
  74. }
  75. class CFactory : public IClassFactory
  76. {
  77. public:
  78. // IUnknown
  79. ULONG __stdcall AddRef();
  80. ULONG __stdcall Release();
  81. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  82. // IClassFactory
  83. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  84. HRESULT __stdcall LockServer(BOOL bLock);
  85. CFactory() : m_cRef(1) { }
  86. ~CFactory() { cout << "Component: CFactory::~CFactory()" << endl; }
  87. private:
  88. long m_cRef;
  89. };
  90. ULONG CFactory::AddRef()
  91. {
  92. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  93. return InterlockedIncrement(&m_cRef);
  94. }
  95. ULONG CFactory::Release()
  96. {
  97. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  98. unsigned cRef = InterlockedDecrement(&m_cRef);
  99. if(cRef != 0)
  100. return cRef;
  101. delete this;
  102. return 0;
  103. }
  104. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  105. {
  106. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  107. {
  108. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  109. *ppv = (IClassFactory *)this;
  110. }
  111. else 
  112. {
  113. *ppv = NULL;
  114. return E_NOINTERFACE;
  115. }
  116. AddRef();
  117. return S_OK;
  118. }
  119. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void **ppv)
  120. {
  121. if(pUnknownOuter != NULL)
  122. return CLASS_E_NOAGGREGATION;
  123. CInsideCOM *pInsideCOM = new CInsideCOM;
  124. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  125. if(pInsideCOM == NULL)
  126. return E_OUTOFMEMORY;
  127. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  128. pInsideCOM->Release();
  129. return hr;
  130. }
  131. HRESULT CFactory::LockServer(BOOL bLock)
  132. {
  133. if(bLock)
  134. CoAddRefServerProcess();
  135. else
  136. if(CoReleaseServerProcess() == 0)
  137. InitiateComponentShutdown();
  138. return S_OK;
  139. }
  140. void RegisterComponent()
  141. {
  142. ITypeLib* pTypeLib;
  143. LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
  144. pTypeLib->Release();
  145. RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.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_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.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. CommandLineParameters(argc, argv);
  178. CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  179. DWORD dwRegister;
  180. IClassFactory *pIFactory = new CFactory();
  181. CoRegisterClassObject(CLSID_InsideCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
  182. CoResumeClassObjects();
  183. MSG msg;
  184. while(GetMessage(&msg, NULL, 0, 0))
  185. DispatchMessage(&msg);
  186. CoRevokeClassObject(dwRegister);
  187. pIFactory->Release();
  188. CoUninitialize();
  189. }