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 <conio.h>     // For getch
  6. #include "registry.h"  // For registry functions
  7. #include "Componentcomponent.h" // Generated by MIDL
  8. long g_cComponents = 0;
  9. long g_cServerLocks = 0;
  10. HANDLE g_hEvent;
  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 Test(DaysOfTheWeek day);
  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::Test(DaysOfTheWeek day)
  60. {
  61. cout << "Today is " << day << endl;
  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 riid, void** ppv);
  71. // IClassFactory
  72. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, 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 riid, void** ppv)
  93. {
  94. if((riid == IID_IUnknown) || (riid == 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 riid, 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(riid, 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. RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  133. }
  134. void CommandLineParameters(int argc, char** argv)
  135. {
  136. RegisterComponent();
  137. if(argc < 2)
  138. {
  139. cout << "No parameter, but registered anyway" << endl;
  140. exit(false);
  141. }
  142. char* szToken = strtok(argv[1], "-/"); 
  143. if(_stricmp(szToken, "RegServer") == 0)
  144. {
  145. RegisterComponent();
  146. cout << "RegServer" << endl;
  147. exit(true);
  148. }
  149. if(_stricmp(szToken, "UnregServer") == 0)
  150. {
  151. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  152. UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  153. cout << "UnregServer" << endl;
  154. exit(true);
  155. }
  156. if(_stricmp(szToken, "Embedding") != 0)
  157. {
  158. cout << "Invalid parameter" << endl;
  159. exit(false);
  160. }
  161. }
  162. void main(int argc, char** argv)
  163. {
  164. CommandLineParameters(argc, argv);
  165. cout << "Component: CoInitializeEx()" << endl;
  166. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  167. IClassFactory *pClassFactory = new CFactory();
  168. cout << "Component: CoRegisterClassObject()" << endl;
  169. DWORD dwRegister;
  170. CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
  171. g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  172. WaitForSingleObject(g_hEvent, INFINITE);
  173. CoRevokeClassObject(dwRegister);
  174. pClassFactory->Release();
  175. CoUninitialize();
  176. _getch();
  177. }