Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: CSurrogate.cxx
  7. //
  8. // Contents: Class the implements the ISurrogate interface
  9. //
  10. //
  11. // History: 21-May-96 t-AdamE Created
  12. // 09-Apr-98 WilfR Updated for Unified Surrogate
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "csrgt.hxx"
  16. #include "srgtdeb.hxx"
  17. //+---------------------------------------------------------------------------
  18. //
  19. // Function: CSurrogate::~CSurrogate()
  20. //
  21. // Synopsis: destructor for CSurrogate
  22. //
  23. // History: 4-09-98 WilfR Created
  24. //
  25. //----------------------------------------------------------------------------
  26. CSurrogate::~CSurrogate()
  27. {
  28. if( _hEventSurrogateFree )
  29. CloseHandle( _hEventSurrogateFree );
  30. }
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Function: CSurrogate::Init()
  34. //
  35. // Synopsis: Initializes data structures with error results
  36. //
  37. // History: 4-09-98 WilfR Created
  38. //
  39. //----------------------------------------------------------------------------
  40. BOOL CSurrogate::Init()
  41. {
  42. // create the event to be signaled when we are freed
  43. return (_hEventSurrogateFree =
  44. CreateEvent(NULL,FALSE,FALSE,NULL)) != NULL ? TRUE : FALSE;
  45. }
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Function: CSurrogate::QueryInterface
  49. //
  50. // History: 6-21-96 t-Adame Created
  51. //
  52. //----------------------------------------------------------------------------
  53. STDMETHODIMP CSurrogate::QueryInterface(REFIID iid, LPVOID FAR * ppv)
  54. {
  55. if (iid == IID_IUnknown)
  56. {
  57. *ppv = (void*)(IUnknown*)this;
  58. AddRef();
  59. return S_OK;
  60. }
  61. else if (iid == IID_ISurrogate)
  62. {
  63. *ppv = (void*)(ISurrogate*)this;
  64. AddRef();
  65. return S_OK;
  66. }
  67. return E_NOINTERFACE;
  68. }
  69. //+---------------------------------------------------------------------------
  70. //
  71. // Function: CSurrogate::AddRef()
  72. //
  73. // History: 6-21-96 t-Adame Created
  74. //
  75. //----------------------------------------------------------------------------
  76. ULONG CSurrogate::AddRef()
  77. {
  78. InterlockedIncrement((LPLONG)&_cref);
  79. Win4Assert(_cref > 0);
  80. ULONG cref = _cref; // NOTE: not thread safe but not worth worrying about
  81. return cref;
  82. }
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Function: CSurrogate::Release()
  86. //
  87. // Synopsis: Decrements our Reference count -- note that this
  88. // implementation of ISurrogate does not delete the object
  89. // that implements it so that we can allocate it on the stack
  90. //
  91. // History: 6-21-96 t-Adame Created
  92. //
  93. //----------------------------------------------------------------------------
  94. ULONG CSurrogate::Release()
  95. {
  96. Win4Assert(_cref >0);
  97. InterlockedDecrement((LPLONG)&_cref);
  98. ULONG cref = _cref; // NOTE: not thread safe but not worth worrying about
  99. return cref;
  100. }
  101. //+---------------------------------------------------------------------------
  102. //
  103. // Function: CSurrogate::LoadDllServer
  104. //
  105. // Synopsis: Loads information about the dll corresponding to the specified
  106. // clsid into our table of loaded dlls, which implicitly
  107. // loads the dll into the surrogate process
  108. //
  109. // History: 6-21-96 t-Adame Created
  110. // 4-09-98 WilfR This is never called for the Unified
  111. // Surrogate
  112. //
  113. //----------------------------------------------------------------------------
  114. STDMETHODIMP CSurrogate::LoadDllServer(
  115. /* [in] */ REFCLSID rclsid)
  116. {
  117. SrgtDebugOut(DEB_SRGT_METHODCALL,"ISurrogate::LoadDllServer called\n",0);
  118. // TODO: I should probably put an assertion in here.
  119. return S_OK;
  120. }
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Function: CSurrogate::FreeSurrogate
  124. //
  125. // Synopsis: called by OLE when there are no external references to clients
  126. // of dll servers that were loaded by this object. A call
  127. // to this function signals the surrogate process that it should
  128. // terminate
  129. //
  130. // History: 6-21-96 t-Adame Created
  131. // 4-09-98 WilfR Modified for Unified Surrogate
  132. //
  133. //----------------------------------------------------------------------------
  134. STDMETHODIMP CSurrogate::FreeSurrogate()
  135. {
  136. SrgtDebugOut(DEB_SRGT_METHODCALL,"ISurrogate::FreeSurrogate called\n",0);
  137. Win4Assert(_hEventSurrogateFree != NULL);
  138. return SetEvent(_hEventSurrogateFree) ? S_OK : E_FAIL;
  139. }
  140. //+---------------------------------------------------------------------------
  141. //
  142. // Function: CSurrogate::WaitForSurrogateFree
  143. //
  144. // Synopsis: sleep the main thread of the surrogate process until OLE
  145. // signals us via a call to FreeSurrogate that we should terminate.
  146. //
  147. // History: 6-21-96 t-Adame Created
  148. // 4-09-98 WilfR Changed the name and implementation
  149. // for new Unified Surrogate. (implementation
  150. // was copied from
  151. //
  152. //----------------------------------------------------------------------------
  153. void CSurrogate::WaitForSurrogateFree()
  154. {
  155. Win4Assert(_hEventSurrogateFree != NULL);
  156. // wait for the FreeSurrogate method of ISurrogate to be called
  157. // NOTE: in the previous incarnation of the surrogate we use to
  158. // timeout every 60 seconds to call CoFreeUnusedLibraries().
  159. // Since this was being done in the MTA and we do nothing to
  160. // perform a similar tasks in the STAs we create its effectiveness
  161. // is minimal.
  162. WaitForSingleObject(_hEventSurrogateFree, INFINITE);
  163. }