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

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. class CInsideCOM : public ISum
  8. {
  9. public:
  10. // IUnknown
  11. ULONG __stdcall AddRef();
  12. ULONG __stdcall Release();
  13. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  14. // ISum
  15. HRESULT __stdcall Sum(int x, int y, int* retval);
  16. };
  17. // No need for reference counting in a singleton
  18. ULONG CInsideCOM::AddRef()
  19. {
  20. return 2;
  21. }
  22. ULONG CInsideCOM::Release()
  23. {
  24. return 1;
  25. }
  26. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  27. {
  28. if(riid == IID_IUnknown)
  29. {
  30. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  31. *ppv = reinterpret_cast<IUnknown*>(this);
  32. }
  33. else if(riid == IID_ISum)
  34. {
  35. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  36. *ppv = (ISum*)this;
  37. }
  38. else 
  39. {
  40. *ppv = NULL;
  41. return E_NOINTERFACE;
  42. }
  43. return S_OK;
  44. }
  45. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  46. {
  47. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  48. *retval = x + y;
  49. return S_OK;
  50. }
  51. class CFactory : public IClassFactory
  52. {
  53. public:
  54. // IUnknown
  55. ULONG __stdcall AddRef();
  56. ULONG __stdcall Release();
  57. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  58. // IClassFactory
  59. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  60. HRESULT __stdcall LockServer(BOOL bLock);
  61. CFactory() : m_cRef(1) { }
  62. ~CFactory() { }
  63. private:
  64. ULONG m_cRef;
  65. };
  66. ULONG CFactory::AddRef()
  67. {
  68. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  69. return ++m_cRef;
  70. }
  71. ULONG CFactory::Release()
  72. {
  73. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  74. if(--m_cRef != 0)
  75. return m_cRef;
  76. delete this;
  77. return 0;
  78. }
  79. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  80. {
  81. if(riid == IID_IUnknown || riid == IID_IClassFactory)
  82. {
  83. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  84. *ppv = (IClassFactory*)this;
  85. }
  86. else
  87. {
  88. *ppv = NULL;
  89. return E_NOINTERFACE;
  90. }
  91. AddRef();
  92. return S_OK;
  93. }
  94. HRESULT CFactory::CreateInstance(IUnknown* pUnknownOuter, REFIID riid, void** ppv)
  95. {
  96. if(pUnknownOuter != NULL)
  97. return CLASS_E_NOAGGREGATION;
  98. // Always return the same instance
  99. static CInsideCOM InsideCOM;
  100. cout << "Component: CFactory::CreateInstance() " << &InsideCOM << endl;
  101. // QueryInterface probably for IID_IUnknown
  102. return InsideCOM.QueryInterface(riid, ppv);
  103. }
  104. HRESULT CFactory::LockServer(BOOL bLock)
  105. {
  106. return S_OK;
  107. }
  108. void RegisterComponent()
  109. {
  110. ITypeLib* pTypeLib;
  111. LoadTypeLibEx(L"Component.exe", REGKIND_DEFAULT, &pTypeLib);
  112. RegisterServer("Component.exe", CLSID_InsideCOM, "Inside COM Sample #1", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  113. }
  114. void CommandLineParameters(int argc, char** argv)
  115. {
  116. RegisterComponent();
  117. if(argc < 2)
  118. {
  119. cout << "No parameter, but registered anyway" << endl;
  120. exit(false);
  121. }
  122. char* szToken = strtok(argv[1], "-/"); 
  123. if(_stricmp(szToken, "RegServer") == 0)
  124. {
  125. RegisterComponent();
  126. cout << "RegServer" << endl;
  127. exit(true);
  128. }
  129. if(_stricmp(szToken, "UnregServer") == 0)
  130. {
  131. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  132. UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  133. cout << "UnregServer" << endl;
  134. exit(true);
  135. }
  136. if(_stricmp(szToken, "Embedding") != 0)
  137. {
  138. cout << "Invalid parameter" << endl;
  139. exit(false);
  140. }
  141. }
  142. void main(int argc, char** argv)
  143. {
  144. CommandLineParameters(argc, argv);
  145. cout << "Component: CoInitializeEx()" << endl;
  146. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  147. IClassFactory *pClassFactory = new CFactory();
  148. cout << "Component: CoRegisterClassObject()" << endl;
  149. DWORD dwRegister;
  150. CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
  151. HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  152. WaitForSingleObject(hEvent, INFINITE);
  153. CoRevokeClassObject(dwRegister);
  154. pClassFactory->Release();
  155. CoUninitialize();
  156. }