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