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

Windows编程

开发平台:

Visual C++

  1. // local.cpp
  2. #define _WIN32_DCOM
  3. #include <windows.h>
  4. #include <conio.h>
  5. #include <iostream.h>  // For cout
  6. #include "registry.h"  // For registry functions
  7. #include "Componentcomponent.h" // Generated by MIDL
  8. #include "ChannelHookchannelhook.h" // Generated by MIDL
  9. long g_cComponents = 0;
  10. long g_cServerLocks = 0;
  11. HANDLE g_hEvent;
  12. class CInsideCOM : public ISum
  13. {
  14. public:
  15. // IUnknown
  16. ULONG __stdcall AddRef();
  17. ULONG __stdcall Release();
  18. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  19. // ISum
  20. HRESULT __stdcall Sum(int x, int y, int* retval);
  21. CInsideCOM() : m_cRef(1) { g_cComponents++; }
  22. ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
  23. private:
  24. ULONG m_cRef;
  25. };
  26. ULONG CInsideCOM::AddRef()
  27. {
  28. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  29. return ++m_cRef;
  30. }
  31. ULONG CInsideCOM::Release()
  32. {
  33. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  34. if(--m_cRef != 0)
  35. return m_cRef;
  36. SetEvent(g_hEvent); // ADD THIS!!!
  37. delete this;
  38. return 0;
  39. }
  40. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  41. {
  42. if(riid == IID_IUnknown)
  43. {
  44. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  45. *ppv = reinterpret_cast<IUnknown*>(this);
  46. }
  47. else if(riid == IID_ISum)
  48. {
  49. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  50. *ppv = (ISum*)this;
  51. }
  52. else 
  53. {
  54. *ppv = NULL;
  55. return E_NOINTERFACE;
  56. }
  57. AddRef();
  58. return S_OK;
  59. }
  60. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  61. {
  62. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  63. IClientInfo* pClientInfo;
  64. CoCreateInstance(CLSID_ClientChannelHook, NULL, CLSCTX_INPROC_SERVER, IID_IClientInfo, (void**)&pClientInfo);
  65. BSTR bstr = 0;
  66. pClientInfo->GetClientComputerName(&bstr);
  67. MessageBox(NULL, (char*)bstr, "GetClientComputerName", MB_OK);
  68. SysFreeString(bstr);
  69. *retval = x + y;
  70. return S_OK;
  71. }
  72. class CFactory : public IClassFactory
  73. {
  74. public:
  75. // IUnknown
  76. ULONG __stdcall AddRef();
  77. ULONG __stdcall Release();
  78. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  79. // IClassFactory
  80. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  81. HRESULT __stdcall LockServer(BOOL bLock);
  82. CFactory() : m_cRef(1) { }
  83. ~CFactory() { }
  84. private:
  85. ULONG m_cRef;
  86. };
  87. ULONG CFactory::AddRef()
  88. {
  89. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  90. return ++m_cRef;
  91. }
  92. ULONG CFactory::Release()
  93. {
  94. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  95. if(--m_cRef != 0)
  96. return m_cRef;
  97. delete this;
  98. return 0;
  99. }
  100. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  101. {
  102. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  103. {
  104. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  105. *ppv = (IClassFactory *)this;
  106. }
  107. else
  108. {
  109. *ppv = NULL;
  110. return E_NOINTERFACE;
  111. }
  112. AddRef();
  113. return S_OK;
  114. }
  115. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  116. {
  117. if(pUnknownOuter != NULL)
  118. return CLASS_E_NOAGGREGATION;
  119. CInsideCOM *pInsideCOM = new CInsideCOM;
  120. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  121. if(pInsideCOM == NULL)
  122. return E_OUTOFMEMORY;
  123. // QueryInterface probably for IID_IUNKNOWN
  124. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  125. pInsideCOM->Release();
  126. return hr;
  127. }
  128. HRESULT CFactory::LockServer(BOOL bLock)
  129. {
  130. if(bLock)
  131. g_cServerLocks++;
  132. else
  133. g_cServerLocks--;
  134. return S_OK;
  135. }
  136. void RegisterComponent()
  137. {
  138. ITypeLib* pTypeLib;
  139. LoadTypeLibEx(L"Component.exe", REGKIND_DEFAULT, &pTypeLib);
  140. RegisterServer("Component.exe", CLSID_InsideCOM, "Inside COM Sample #1", "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. cout << "Component: CoInitializeEx()" << endl;
  174. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  175. // Load the channel hook
  176. void* silly;
  177. CoCreateInstance(CLSID_ClientChannelHook, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, &silly);
  178. IClassFactory *pClassFactory = new CFactory();
  179. cout << "Component: CoRegisterClassObject()" << endl;
  180. DWORD dwRegister;
  181. CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
  182. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  183. WaitForSingleObject(g_hEvent, INFINITE);
  184. CoRevokeClassObject(dwRegister);
  185. pClassFactory->Release();
  186. CoUninitialize();
  187. _getch();
  188. }