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

Windows编程

开发平台:

Visual C++

  1. // local.cpp
  2. #define _WIN32_DCOM
  3. #include <windows.h>
  4. #include <iostream.h>  // For cout
  5. #include "registry.h"  // For registry functions
  6. #include "Componentcomponent.h" // Generated by MIDL
  7. #include <stdio.h>
  8. #include <conio.h>
  9. long g_cComponents = 0;
  10. long g_cServerLocks = 0;
  11. HANDLE g_hEvent;
  12. class CTestIDL : public IPointerTest
  13. {
  14. public:
  15. // IUnknown
  16. ULONG __stdcall AddRef();
  17. ULONG __stdcall Release();
  18. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  19. HRESULT __stdcall GetInterfacePointer2(REFIID riid, IUnknown** ppv);
  20. HRESULT __stdcall GetInterfacePointer3(IPointerTest** ppv);
  21. HRESULT __stdcall GetInterfacePointer4(REFIID riid, void** ppv);
  22. CTestIDL() : m_cRef(1) { g_cComponents++; }
  23. ~CTestIDL() { cout << "Component: CTestIDL::~CTestIDL()" << endl, g_cComponents--; }
  24. private:
  25. ULONG m_cRef;
  26. };
  27. // HRESULT GetInterfacePointer2([in] REFIID riid, [out, iid_is(riid)] IUnknown** ppv);
  28. HRESULT CTestIDL::GetInterfacePointer2(REFIID riid, IUnknown** ppv)
  29. {
  30. return QueryInterface(riid, (void**)ppv);
  31. }
  32. // HRESULT GetInterfacePointer3([out] IPointerTest** ppv);
  33. HRESULT CTestIDL::GetInterfacePointer3(IPointerTest** ppv)
  34. {
  35. return QueryInterface(IID_IPointerTest, (void**)ppv);
  36. }
  37. // [local] HRESULT GetInterfacePointer4([in] REFIID riid, [out, iid_is(riid)] void** ppv);
  38. // [call_as(GetInterfacePointer4)] HRESULT RemoteGetInterfacePointer4([in] REFIID riid, [out, iid_is(riid)] IUnknown** ppv);
  39. HRESULT CTestIDL::GetInterfacePointer4(REFIID riid, void** ppv)
  40. {
  41. return QueryInterface(riid, (void**)ppv);
  42. }
  43. ULONG CTestIDL::AddRef()
  44. {
  45. cout << "Component: CTestIDL::AddRef() m_cRef = " << m_cRef + 1 << endl;
  46. return ++m_cRef;
  47. }
  48. ULONG CTestIDL::Release()
  49. {
  50. cout << "Component: CTestIDL::Release() m_cRef = " << m_cRef - 1 << endl;
  51. if(--m_cRef != 0)
  52. return m_cRef;
  53. SetEvent(g_hEvent); // ADD THIS!!!
  54. delete this;
  55. return 0;
  56. }
  57. HRESULT CTestIDL::QueryInterface(REFIID riid, void** ppv)
  58. {
  59. if(riid == IID_IUnknown)
  60. {
  61. cout << "Component: CTestIDL::QueryInterface() for IUnknown returning " << this << endl;
  62. *ppv = reinterpret_cast<IUnknown*>(this);
  63. }
  64. else if(riid == IID_IPointerTest)
  65. {
  66. cout << "Component: CTestIDL::QueryInterface() ASKING FOR IPointerTest " << this << endl;
  67. *ppv = (IPointerTest*)this;
  68. }
  69. else 
  70. {
  71. *ppv = NULL;
  72. return E_NOINTERFACE;
  73. }
  74. AddRef();
  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() { }
  89. private:
  90. ULONG m_cRef;
  91. };
  92. ULONG CFactory::AddRef()
  93. {
  94. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  95. return ++m_cRef;
  96. }
  97. ULONG CFactory::Release()
  98. {
  99. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  100. if(--m_cRef != 0)
  101. return m_cRef;
  102. delete this;
  103. return 0;
  104. }
  105. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  106. {
  107. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  108. {
  109. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  110. *ppv = (IClassFactory*)this;
  111. }
  112. else
  113. {
  114. *ppv = NULL;
  115. return E_NOINTERFACE;
  116. }
  117. AddRef();
  118. return S_OK;
  119. }
  120. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  121. {
  122. if(pUnknownOuter != NULL)
  123. return CLASS_E_NOAGGREGATION;
  124. CTestIDL *pTestIDL = new CTestIDL;
  125. cout << "Component: CFactory::CreateInstance() " << pTestIDL << endl;
  126. if(pTestIDL == NULL)
  127. return E_OUTOFMEMORY;
  128. // QueryInterface probably for IID_IUNKNOWN
  129. HRESULT hr = pTestIDL->QueryInterface(riid, ppv);
  130. pTestIDL->Release();
  131. return hr;
  132. }
  133. HRESULT CFactory::LockServer(BOOL bLock)
  134. {
  135. if(bLock)
  136. g_cServerLocks++;
  137. else
  138. g_cServerLocks--;
  139. return S_OK;
  140. }
  141. void RegisterComponent()
  142. {
  143. ITypeLib* pTypeLib;
  144. LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
  145. RegisterServer("component.exe", CLSID_PointerTest, "Test IDL Sample", "Component.InterfacePointer", "Component.InterfacePointer.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_PointerTest, "Component.InterfacePointer", "Component.InterfacePointer.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. cout << "Component: CoInitializeEx()" << endl;
  179. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  180. IClassFactory *pClassFactory = new CFactory();
  181. cout << "Component: CoRegisterClassObject()" << endl;
  182. DWORD dwRegister;
  183. CoRegisterClassObject(CLSID_PointerTest, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
  184. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  185. WaitForSingleObject(g_hEvent, INFINITE);
  186. CoRevokeClassObject(dwRegister);
  187. pClassFactory->Release();
  188. CoUninitialize();
  189. _getch();
  190. }