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

Windows编程

开发平台:

Visual C++

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