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