Leaked source code of windows server 2003
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.

2206 lines
64 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. connect.cxx
  5. Abstract:
  6. IIS DCOM Admin connection point code.
  7. Author:
  8. Michael W. Thomas 02-Oct-96
  9. Revision History:
  10. --*/
  11. #include "precomp.hxx"
  12. #include <iadm.h>
  13. #include "coiadm.hxx"
  14. /*+==========================================================================
  15. File: CONNECT.CPP
  16. Summary: Implementation file for the connection points (and their
  17. connections) offered by the connectable objects in the
  18. STOSERVE server sample. COM objects are implemented for
  19. Connection Point Enumerators, Connection Points, and
  20. Connection Enumerators.
  21. For a comprehensive tutorial code tour of this module's
  22. contents and offerings see the accompanying STOSERVE.TXT
  23. file. For more specific technical details on the internal
  24. workings see the comments dispersed throughout the module's
  25. source code.
  26. Classes: COEnumConnectionPoints, COConnectionPoint, and
  27. COEnumConnections.
  28. Functions: none.
  29. Origin: 6-10-96: atrent - Editor inheritance from CONSERVE OLE
  30. Tutorial Code Sample. Very little change was required.
  31. ----------------------------------------------------------------------------
  32. This file is part of the Microsoft OLE Tutorial Code Samples.
  33. Copyright (C) Microsoft Corporation, 1996. All rights reserved.
  34. This source code is intended only as a supplement to Microsoft
  35. Development Tools and/or on-line documentation. See these other
  36. materials for detailed information regarding Microsoft code samples.
  37. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  38. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  39. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  40. PARTICULAR PURPOSE.
  41. ==========================================================================+*/
  42. /*---------------------------------------------------------------------------
  43. We include WINDOWS.H for all Win32 applications.
  44. We include OLE2.H because we will be making calls to the OLE Libraries.
  45. We include OLECTL.H because it has definitions for connectable objects.
  46. We include APPUTIL.H because we will be building this application using
  47. the convenient Virtual Window and Dialog classes and other
  48. utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  49. We include IPAPER.H and PAPGUIDS.H for the common paper-related
  50. Interface class, GUID, and CLSID specifications.
  51. We include SERVER.H because it has internal class declarations and
  52. resource ID definitions specific for this DLL.
  53. We include CONNECT.H for object class declarations for the various
  54. connection point and connection COM objects used in CONSERVE.
  55. We include PAPER.H because it has the class COEnumConnectionPoints
  56. declarations as well as the COPaper class declaration.
  57. ---------------------------------------------------------------------------*/
  58. // Helper functions
  59. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  60. Method: SetSinkCallbackSecurityBlanket
  61. Summary: Sink callbacks are vulnerable to privilege elevation attack
  62. It must be prevented to make callbacks through proxies that enable
  63. impersonation level IMPERSONATE because callbacks are happening
  64. on thead with SYSTEM user.
  65. SetSinkCallbackSecurityBlanket must be called on any sink proxy before
  66. any call as made
  67. Note: See details on previous implementation and related bugs in
  68. WinSE 5611, 7579, 10575.
  69. Function introduced for fixing Windows Bugs 431282
  70. Args: IUnknown *
  71. Modifies: security blanket
  72. Returns: void
  73. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  74. HRESULT SetSinkCallbackSecurityBlanket(
  75. IUnknown * pUnkSink )
  76. {
  77. HRESULT hr = S_OK;
  78. IClientSecurity * pICS = NULL;
  79. DBG_ASSERT( pUnkSink != NULL );
  80. //
  81. // Check to see if we have a ClientSecurity interface
  82. // (meaning the sink is oop or on another thread).
  83. //
  84. hr = pUnkSink->QueryInterface( IID_IClientSecurity,
  85. (void **) &pICS );
  86. if ((SUCCEEDED(hr)) && (pICS != NULL))
  87. {
  88. hr = pICS->SetBlanket(pUnkSink,
  89. RPC_C_AUTHN_DEFAULT, // use NT default security
  90. RPC_C_AUTHZ_DEFAULT, // use NT default authentication
  91. COLE_DEFAULT_PRINCIPAL, //
  92. RPC_C_AUTHN_LEVEL_DEFAULT,
  93. RPC_C_IMP_LEVEL_IDENTIFY, // THIS IS VERY IMPORTANT
  94. // TO KEEP IMPERSONATION LEVEL
  95. // on IDENTIFY to PREVENT privilege
  96. // elevation
  97. COLE_DEFAULT_AUTHINFO,
  98. EOAC_DEFAULT );
  99. pICS->Release();
  100. pICS = NULL;
  101. }
  102. else
  103. {
  104. //
  105. // in the inproc case it is OK to not to set blanket because there is no proxy
  106. //
  107. hr = S_OK;
  108. }
  109. return hr;
  110. }
  111. /*---------------------------------------------------------------------------
  112. COEnumConnectionPoints's implementation of its main COM object class
  113. including Constructor, Destructor, QueryInterface, AddRef, Release,
  114. Next, Skip, Reset, and Clone.
  115. ---------------------------------------------------------------------------*/
  116. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  117. Method: COEnumConnectionPoints::COEnumConnectionPoints
  118. Summary: COEnumConnectionPoints Constructor.
  119. Args: IUnknown* pHostObj
  120. Pointer to the host object whose connection points are
  121. being enumerated.
  122. Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnPts, and m_paConnPts.
  123. Returns: void
  124. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  125. COEnumConnectionPoints::COEnumConnectionPoints(
  126. IUnknown * pHostObj)
  127. {
  128. // Zero the COM object's reference count.
  129. m_cRefs = 0;
  130. // Assign the Host Object pointer.
  131. m_pHostObj = pHostObj;
  132. // Initialize the Connection Point enumerator variables.
  133. m_iEnumIndex = 0;
  134. m_cConnPts = 0;
  135. m_paConnPts = NULL;
  136. }
  137. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  138. Method: COEnumConnectionPoints::~COEnumConnectionPoints
  139. Summary: COEnumConnectionPoints Destructor.
  140. Args: void
  141. Modifies: m_paConnPts.
  142. Returns: void
  143. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  144. COEnumConnectionPoints::~COEnumConnectionPoints(void)
  145. {
  146. if (NULL != m_paConnPts)
  147. {
  148. UINT i;
  149. // Release all the connection point interface pointers.
  150. for (i=0; i<m_cConnPts; i++)
  151. {
  152. if (NULL != m_paConnPts[i])
  153. {
  154. m_paConnPts[i]->Release();
  155. }
  156. }
  157. // Delete the array of interface pointers.
  158. delete [] m_paConnPts;
  159. }
  160. }
  161. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  162. Method: COEnumConnectionPoints::Init
  163. Summary: COEnumConnectionPoints Initialization method. Create any
  164. necessary arrays, structures, and objects.
  165. Args: ULONG cConnPts,
  166. Number of Connections Points.
  167. IConnectionPoint** paConnPts,
  168. Pointer to array of connection point interface pointers.
  169. ULONG iEnumIndex
  170. The initial Enumerator index value.
  171. Modifies: m_cConnPts, m_paConnPts, m_iEnumIndex.
  172. Returns: HRESULT
  173. Standard OLE result code. S_OK for success.
  174. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  175. HRESULT COEnumConnectionPoints::Init(
  176. ULONG cConnPts,
  177. IConnectionPoint ** paConnPts,
  178. ULONG iEnumIndex)
  179. {
  180. HRESULT hr = S_OK;
  181. UINT i;
  182. // Remember the number of Connection points.
  183. m_cConnPts = cConnPts;
  184. // Remember the initial enumerator index.
  185. m_iEnumIndex = iEnumIndex;
  186. // Create a copy of the array of connection points and keep it inside
  187. // this enumerator COM object.
  188. m_paConnPts = new IConnectionPoint* [(UINT) cConnPts];
  189. // Fill the array copy with the IConnectionPoint interface pointers from
  190. // the array passed. AddRef for each new Interface pointer copy made.
  191. if (NULL != m_paConnPts)
  192. {
  193. for (i=0; i<cConnPts; i++)
  194. {
  195. m_paConnPts[i] = paConnPts[i];
  196. m_paConnPts[i]->AddRef();
  197. }
  198. }
  199. else
  200. {
  201. hr = E_OUTOFMEMORY;
  202. }
  203. return (hr);
  204. }
  205. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  206. Method: COEnumConnectionPoints::QueryInterface
  207. Summary: QueryInterface of the COEnumConnectionPoints non-delegating
  208. IUnknown implementation.
  209. Args: REFIID riid,
  210. [in] GUID of the Interface being requested.
  211. PPVOID ppv)
  212. [out] Address of the caller's pointer variable that will
  213. receive the requested interface pointer.
  214. Modifies: .
  215. Returns: HRESULT
  216. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  217. STDMETHODIMP COEnumConnectionPoints::QueryInterface(
  218. REFIID riid,
  219. PPVOID ppv)
  220. {
  221. HRESULT hr = E_NOINTERFACE;
  222. *ppv = NULL;
  223. // The IEnumConnectionPoints interface is implemented directly in
  224. // this COM object rather than being a nested interface implementation.
  225. if (IID_IUnknown == riid || IID_IEnumConnectionPoints == riid)
  226. {
  227. *ppv = (LPVOID)this;
  228. }
  229. if (NULL != *ppv)
  230. {
  231. // We've handed out a pointer to the interface so obey the COM rules
  232. // and AddRef the reference count.
  233. ((LPUNKNOWN)*ppv)->AddRef();
  234. hr = S_OK;
  235. }
  236. return (hr);
  237. }
  238. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  239. Method: COEnumConnectionPoints::AddRef
  240. Summary: AddRef of the COEnumConnectionPoints non-delegating IUnknown
  241. implementation.
  242. Args: void
  243. Modifies: m_cRefs.
  244. Returns: ULONG
  245. New value of m_cRefs (COM object's reference count).
  246. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  247. STDMETHODIMP_(ULONG) COEnumConnectionPoints::AddRef(void)
  248. {
  249. ULONG cRefs;
  250. cRefs = ++m_cRefs;
  251. // Also AddRef the host object to ensure it stays around as long as
  252. // this enumerator.
  253. m_pHostObj->AddRef();
  254. return cRefs;
  255. }
  256. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  257. Method: COEnumConnectionPoints::Release
  258. Summary: Release of the COEnumConnectionPoints non-delegating IUnknown
  259. implementation.
  260. Args: void
  261. Modifies: m_cRefs.
  262. Returns: ULONG
  263. New value of m_cRefs (COM object's reference count).
  264. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  265. STDMETHODIMP_(ULONG) COEnumConnectionPoints::Release(void)
  266. {
  267. ULONG cRefs;
  268. // Pass this release along to the Host object being enumerated.
  269. m_pHostObj->Release();
  270. cRefs = --m_cRefs;
  271. if (0 == cRefs)
  272. {
  273. // We artificially bump the main ref count to prevent reentrancy via
  274. // the main object destructor.
  275. m_cRefs++;
  276. delete this;
  277. }
  278. return cRefs;
  279. }
  280. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  281. Method: COEnumConnectionPoints::Next
  282. Summary: The Next member method of this IEnumConnectionPoints interface
  283. implementation. Called by outside clients of a
  284. COEnumConnectionPoints object to request that a number of next
  285. connection point interface pointers be deposited into an array
  286. supplied by the caller.
  287. Args: ULONG cReq
  288. Number of connection points requested for return (starting at
  289. the current Enumerator index).
  290. IConnectionPoint** paConnPts,
  291. Pointer to a caller's output array that will receive the
  292. enumerated IConnectionPoint interface pointers.
  293. ULONG* cEnumerated)
  294. Pointer to a ULONG variable that will contain the number of
  295. connection points actually enumerated by this call.
  296. Modifies: .
  297. Returns: HRESULT
  298. Standard OLE result code. S_OK for success.
  299. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  300. STDMETHODIMP COEnumConnectionPoints::Next(
  301. ULONG cReq,
  302. IConnectionPoint ** paConnPts,
  303. ULONG * pcEnumerated)
  304. {
  305. HRESULT hr = S_OK;
  306. ULONG cRet = 0;
  307. // Make sure the argument values passed are valid.
  308. if (NULL != m_paConnPts)
  309. {
  310. if (NULL != paConnPts)
  311. {
  312. if (NULL != *paConnPts && m_iEnumIndex < m_cConnPts)
  313. {
  314. if (NULL != pcEnumerated)
  315. {
  316. *pcEnumerated = 0L;
  317. }
  318. else
  319. {
  320. if (1L != cReq)
  321. {
  322. hr = E_POINTER;
  323. }
  324. }
  325. }
  326. else
  327. {
  328. hr = S_FALSE;
  329. }
  330. }
  331. else
  332. {
  333. hr = E_POINTER;
  334. }
  335. }
  336. else
  337. {
  338. hr = S_FALSE;
  339. }
  340. if (SUCCEEDED(hr))
  341. {
  342. // Starting at the current Enumerator index, loop to assign the
  343. // requested number of output connection point interface pointers.
  344. for (; m_iEnumIndex < m_cConnPts && cReq > 0;
  345. paConnPts++, cRet++, cReq--)
  346. {
  347. // Assign from the inside Enumerator array to the specified receiving
  348. // array.
  349. *paConnPts = m_paConnPts[m_iEnumIndex++];
  350. // After assigning a copy of an IConnectionPoint pointer, AddRef it.
  351. if (NULL != *paConnPts)
  352. {
  353. (*paConnPts)->AddRef();
  354. }
  355. }
  356. // Assign the output number of connection points enumerated.
  357. if (NULL != pcEnumerated)
  358. {
  359. *pcEnumerated = cRet;
  360. }
  361. }
  362. return hr;
  363. }
  364. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  365. Method: COEnumConnectionPoints::Skip
  366. Summary: The Skip member method of this IEnumConnectionPoints interface
  367. implementation. Starting at the current Enumerator index, skip
  368. the specified number of connection point items.
  369. Args: ULONG cSkip
  370. Number of Connection Point items to skip.
  371. Modifies: .
  372. Returns: HRESULT
  373. Standard OLE result code. S_OK for success.
  374. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  375. STDMETHODIMP COEnumConnectionPoints::Skip(
  376. ULONG cSkip)
  377. {
  378. HRESULT hr = S_OK;
  379. // If there really is a connection point array and the requested
  380. // amount of skip does not exceed the number of connection points,
  381. // then bump the index by the requested skip amount.
  382. if (NULL != m_paConnPts && (m_iEnumIndex + cSkip) < m_cConnPts)
  383. {
  384. m_iEnumIndex += cSkip;
  385. }
  386. else
  387. {
  388. hr = S_FALSE;
  389. }
  390. return hr;
  391. }
  392. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  393. Method: COEnumConnectionPoints::Reset
  394. Summary: The Reset member method of the IEnumConnectionPoints interface
  395. implementation. Resets the Enumeration index to the first
  396. connection point item in the array.
  397. Args: void.
  398. Modifies: .
  399. Returns: HRESULT
  400. Standard OLE result code. S_OK for success.
  401. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  402. STDMETHODIMP COEnumConnectionPoints::Reset(void)
  403. {
  404. // Zero the main Enumerator index.
  405. m_iEnumIndex = 0;
  406. return S_OK;
  407. }
  408. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  409. Method: COEnumConnectionPoints::Clone
  410. Summary: The Clone member method of this IEnumConnectionPoints
  411. interface implementation. Creates a new clone of this entire
  412. Connection Point enumerator COM object.
  413. Args: IEnumConnectionPoints** ppIEnum
  414. Address of the caller's output pointer variable that will
  415. receive the IEnumConnectionPoints interface pointer of the
  416. new enumerator clone.
  417. Modifies: ...
  418. Returns: HRESULT
  419. Standard OLE result code. S_OK for success.
  420. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  421. STDMETHODIMP COEnumConnectionPoints::Clone(
  422. IEnumConnectionPoints ** ppIEnum)
  423. {
  424. HRESULT hr;
  425. COEnumConnectionPoints * pCOEnum;
  426. // NULL the output variable first.
  427. *ppIEnum = NULL;
  428. // Create the Clone Enumerator COM object.
  429. pCOEnum = new COEnumConnectionPoints(m_pHostObj);
  430. if (NULL != pCOEnum)
  431. {
  432. // Initialize it with same values as in this existing enumerator.
  433. hr = pCOEnum->Init(m_cConnPts, m_paConnPts, m_iEnumIndex);
  434. if (SUCCEEDED(hr))
  435. {
  436. // QueryInterface to return the requested interface pointer.
  437. // An AddRef will be conveniently done by the QI.
  438. hr = pCOEnum->QueryInterface(
  439. IID_IEnumConnectionPoints,
  440. (PPVOID)ppIEnum);
  441. }
  442. if( FAILED( hr ) )
  443. {
  444. delete pCOEnum;
  445. pCOEnum = NULL;
  446. }
  447. }
  448. else
  449. {
  450. hr = E_OUTOFMEMORY;
  451. }
  452. return hr;
  453. }
  454. /*---------------------------------------------------------------------------
  455. COConnectionPoint's implementation of its main COM object class
  456. including Constructor, Destructor, QueryInterface, AddRef, Release,
  457. GetConnectionInterface, GetConnectionPointContainer, Advise, Unadvise,
  458. and EnumConnections.
  459. ---------------------------------------------------------------------------*/
  460. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  461. Method: COConnectionPoint::COConnectionPoint
  462. Summary: COConnectionPoint Constructor.
  463. Args: IUnknown* pHostObj
  464. Pointer to IUnknown of the connectable object offering this
  465. connection point.
  466. Modifies: ...
  467. Returns: void
  468. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  469. COConnectionPoint::COConnectionPoint(void)
  470. {
  471. // Initialize the Connection Point variables.
  472. m_pHostObj = NULL;
  473. m_uiMaxIndex = 0;
  474. m_cConnections = 0;
  475. m_paConnections = NULL;
  476. m_bTerminated = FALSE;
  477. m_bEnabled = TRUE;
  478. m_pGIT = NULL;
  479. }
  480. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  481. Method: COConnectionPoint::~COConnectionPoint
  482. Summary: COConnectionPoint Destructor.
  483. Args: void
  484. Modifies: m_paConnections, m_bTerminated.
  485. Returns: void
  486. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  487. COConnectionPoint::~COConnectionPoint(void)
  488. {
  489. Terminate();
  490. }
  491. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  492. Method: COConnectionPoint::Terminate
  493. Summary: Shuts down the object. Can be called from the destructor or from the
  494. outer object Terminate
  495. Args: void
  496. Modifies: m_paConnections, m_bTerminated.
  497. Returns: Right now only S_OK
  498. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  499. STDMETHODIMP COConnectionPoint::Terminate(void)
  500. {
  501. HRESULT hr = S_OK;
  502. UINT i;
  503. m_Lock.WriteLock();
  504. if (!m_bTerminated)
  505. {
  506. if (NULL != m_paConnections)
  507. {
  508. DBG_ASSERT(m_pGIT != NULL);
  509. if (m_pGIT != NULL)
  510. {
  511. // Release all the connection sink interface pointers.
  512. for (i=0; i<m_uiMaxIndex; i++)
  513. {
  514. if (m_paConnections[i].dwCookie != 0)
  515. {
  516. m_pGIT->RevokeInterfaceFromGlobal (m_paConnections[i].dwCookie);
  517. }
  518. }
  519. }
  520. // Delete the array of interface pointers.
  521. delete [] m_paConnections;
  522. m_paConnections=NULL;
  523. }
  524. DBG_ASSERT(m_pGIT != NULL);
  525. if (m_pGIT)
  526. {
  527. m_pGIT->Release();
  528. m_pGIT = NULL;
  529. }
  530. m_uiMaxIndex = 0;
  531. m_cConnections = 0;
  532. m_bTerminated = TRUE;
  533. }
  534. m_Lock.WriteUnlock();
  535. return hr;
  536. }
  537. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  538. Method: COConnectionPoint::Init
  539. Summary: COConnectionPoint Initialization method. Create any
  540. necessary arrays, structures, and subordinate objects.
  541. Args: IUnknown* pHostObj
  542. Pointer to IUnknown of the connectable object offering this
  543. connection point.
  544. REFIID riid
  545. Reference to the IID of the Sink interface associated with
  546. this connection point.
  547. Modifies: ...
  548. Returns: HRESULT
  549. Standard OLE result code. S_OK for success.
  550. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  551. HRESULT COConnectionPoint::Init(
  552. IUnknown * pHostObj,
  553. REFIID riid)
  554. {
  555. HRESULT hr = S_OK;
  556. CONNECTDATA * paConns;
  557. if (m_bTerminated)
  558. {
  559. hr=E_UNEXPECTED;
  560. goto exit;
  561. }
  562. // Remember an IUnknown pointer to the connectable object that offers
  563. // this connection point. Since this connection point object's lifetime
  564. // is geared to that of the connectable object there is no need to
  565. // AddRef the following copied pointer to the connectable object.
  566. m_pHostObj = pHostObj;
  567. // Keep a copy of the reference to the IID of the sink interface
  568. // associated with this connection point. Needed for later
  569. // use by the GetConnectionInterface method.
  570. m_iidSink = riid;
  571. DBG_ASSERT(m_iidSink == IID_IMSAdminBaseSink_W);
  572. // Build the initial dynamic array for connections.
  573. paConns = new CONNECTDATA[ALLOC_CONNECTIONS];
  574. if (NULL != paConns)
  575. {
  576. // Zero the array.
  577. memset(paConns, 0, ALLOC_CONNECTIONS * sizeof(CONNECTDATA));
  578. // Rig this connection point object so that it will use the
  579. // new internal array of connections.
  580. m_uiMaxIndex = ALLOC_CONNECTIONS;
  581. m_paConnections = paConns;
  582. hr = CoCreateInstance (
  583. CLSID_StdGlobalInterfaceTable,
  584. NULL,
  585. CLSCTX_INPROC_SERVER,
  586. IID_IGlobalInterfaceTable,
  587. (void **)&m_pGIT
  588. );
  589. }
  590. else
  591. {
  592. hr = E_OUTOFMEMORY;
  593. }
  594. exit:
  595. return (hr);
  596. }
  597. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  598. Method: COConnectionPoint::QueryInterface
  599. Summary: QueryInterface of the COConnectionPoint non-delegating
  600. IUnknown implementation.
  601. Args: REFIID riid,
  602. [in] GUID of the Interface being requested.
  603. PPVOID ppv)
  604. [out] Address of the caller's pointer variable that will
  605. receive the requested interface pointer.
  606. Modifies: .
  607. Returns: HRESULT
  608. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  609. STDMETHODIMP COConnectionPoint::QueryInterface(
  610. REFIID riid,
  611. PPVOID ppv)
  612. {
  613. HRESULT hr = E_NOINTERFACE;
  614. if (ppv)
  615. {
  616. *ppv = NULL;
  617. }
  618. else
  619. {
  620. hr=E_POINTER;
  621. goto exit;
  622. }
  623. if (m_bTerminated)
  624. {
  625. hr=E_UNEXPECTED;
  626. goto exit;
  627. }
  628. // The IConnectionPoint interface is implemented directly in this
  629. // COM object rather than being a nested interface implementation.
  630. if (IID_IUnknown == riid || IID_IConnectionPoint == riid)
  631. {
  632. *ppv = (LPVOID)this;
  633. }
  634. if (NULL != *ppv)
  635. {
  636. // We've handed out a pointer to the interface so obey the COM rules
  637. // and AddRef the reference count.
  638. ((LPUNKNOWN)*ppv)->AddRef();
  639. hr = S_OK;
  640. }
  641. exit:
  642. return (hr);
  643. }
  644. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  645. Method: COConnectionPoint::AddRef
  646. Summary: AddRef of the COConnectionPoint non-delegating IUnknown
  647. implementation.
  648. Args: void
  649. Modifies: .
  650. Returns: ULONG
  651. New value of the host objects m_cRefs (COM object's reference count).
  652. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  653. STDMETHODIMP_(ULONG) COConnectionPoint::AddRef(void)
  654. {
  655. if (m_bTerminated)
  656. {
  657. return 1;
  658. }
  659. else
  660. {
  661. return m_pHostObj->AddRef();
  662. }
  663. }
  664. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  665. Method: COConnectionPoint::Release
  666. Summary: Release of the COConnectionPoint non-delegating IUnknown
  667. implementation.
  668. Args: void
  669. Modifies: .
  670. Returns: ULONG
  671. New value of m_cRefs (COM object's reference count).
  672. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  673. STDMETHODIMP_(ULONG) COConnectionPoint::Release(void)
  674. {
  675. if (m_bTerminated)
  676. {
  677. return 0;
  678. }
  679. else
  680. {
  681. return m_pHostObj->Release();
  682. }
  683. }
  684. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  685. Method: COConnectionPoint::GetSlot
  686. Summary: An internal private utility member method to obtain a free
  687. slot in the dynamic connections array. GetSlot will expand the
  688. dynamic array for more entries if needed. To guarantee thread
  689. safety, this private method should always be called within the
  690. protection of a bracketed OwnThis, UnOwnThis pair.
  691. Args: UINT* puiFreeSlot
  692. Address of an output variable to receive the free slot index.
  693. Modifies: m_uiMaxIndex, m_paConnections.
  694. Returns: HRESULT
  695. Standard OLE result code. S_OK for success.
  696. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  697. HRESULT COConnectionPoint::GetSlot(
  698. UINT * puiFreeSlot)
  699. {
  700. HRESULT hr = S_OK;
  701. BOOL bOpen = FALSE;
  702. UINT i;
  703. CONNECTDATA * paConns;
  704. // Zero the output variable.
  705. *puiFreeSlot = 0;
  706. if (m_bTerminated)
  707. {
  708. hr = E_UNEXPECTED;
  709. goto exit;
  710. }
  711. // Loop to find an empty slot.
  712. for (i=0; i<m_uiMaxIndex; i++)
  713. {
  714. if (m_paConnections[i].dwCookie == 0)
  715. {
  716. // We found an open empty slot.
  717. *puiFreeSlot = i;
  718. bOpen = TRUE;
  719. break;
  720. }
  721. }
  722. if (!bOpen)
  723. {
  724. // We didn't find an existing open slot in the array--it's full.
  725. // Expand the array by ALLOC_CONNECTIONS entries and assign the
  726. // appropriate output index.
  727. paConns = new CONNECTDATA[m_uiMaxIndex + ALLOC_CONNECTIONS];
  728. if (NULL != paConns)
  729. {
  730. // Copy the content of the old full array to the new larger array.
  731. for (i=0; i<m_uiMaxIndex; i++)
  732. {
  733. paConns[i].pUnk = m_paConnections[i].pUnk;
  734. paConns[i].dwCookie = m_paConnections[i].dwCookie;
  735. }
  736. // Zero (ie mark as empty) the expanded portion of the new array.
  737. for (i=m_uiMaxIndex; i<m_uiMaxIndex+ALLOC_CONNECTIONS; i++)
  738. {
  739. paConns[i].pUnk = NULL;
  740. paConns[i].dwCookie = 0;
  741. }
  742. // New larger array is ready--delete the old array.
  743. delete [] m_paConnections;
  744. // Rig the connection point to use the new larger array.
  745. m_paConnections = paConns;
  746. // Assign the output free slot as first entry in new expanded area.
  747. *puiFreeSlot = m_uiMaxIndex;
  748. // Calculate the new max index.
  749. m_uiMaxIndex += ALLOC_CONNECTIONS;
  750. }
  751. else
  752. {
  753. hr = E_OUTOFMEMORY;
  754. }
  755. }
  756. exit:
  757. return hr;
  758. }
  759. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  760. Method: COConnectionPoint::FindSlot
  761. Summary: An internal private utility member method to find an existing
  762. slot (identified by the specified dwCookie value) in the
  763. dynamic connections array.
  764. Args: DWORD dwCookie,
  765. The connection key (cookie) to find.
  766. UINT* puiSlot)
  767. Address of an output variable to receive the slot index.
  768. Modifies: ...
  769. Returns: HRESULT
  770. Standard OLE result code. S_OK for success.
  771. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  772. HRESULT COConnectionPoint::FindSlot(
  773. DWORD dwCookie,
  774. UINT * puiSlot)
  775. {
  776. HRESULT hr = CONNECT_E_NOCONNECTION;
  777. UINT i;
  778. if (m_bTerminated)
  779. {
  780. hr = E_UNEXPECTED;
  781. goto exit;
  782. }
  783. // Loop to find the Cookie.
  784. for (i=0; i<m_uiMaxIndex; i++)
  785. {
  786. if (dwCookie == m_paConnections[i].dwCookie)
  787. {
  788. // If a cookie match is found, assign the output slot index.
  789. *puiSlot = i;
  790. hr = S_OK;
  791. break;
  792. }
  793. }
  794. exit:
  795. return hr;
  796. }
  797. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  798. Method: COConnectionPoint::GetConnectionInterface
  799. Summary: The GetConnectionInterface member method of this
  800. IConnectionPoint interface implementation. Called to get the
  801. IID of the Sink interface associated with this connection
  802. point.
  803. Args: IID* piidSink
  804. Pointer to the IID of the associated sink interface.
  805. Modifies: .
  806. Returns: HRESULT
  807. Standard OLE result code. S_OK for success.
  808. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  809. STDMETHODIMP COConnectionPoint::GetConnectionInterface(
  810. IID * piidSink)
  811. {
  812. HRESULT hr = S_OK;
  813. if (m_bTerminated)
  814. {
  815. hr = E_UNEXPECTED;
  816. goto exit;
  817. }
  818. if (NULL != piidSink)
  819. {
  820. *piidSink = m_iidSink;
  821. }
  822. else
  823. {
  824. hr = E_POINTER;
  825. }
  826. exit:
  827. return hr;
  828. }
  829. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  830. Method: COConnectionPoint::GetConnectionPointContainer
  831. Summary: The GetConnectionPointContainer member method of this
  832. IConnectionPoint interface implementation. Called to get the
  833. connection point container that contains this connection
  834. point.
  835. Args: IConnectionPointContainer** ppConnPtCon
  836. Address of the pointer variable that will recieve the
  837. IConnectionPointContainer interface pointer.
  838. Modifies: .
  839. Returns: HRESULT
  840. Standard OLE result code. S_OK for success.
  841. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  842. STDMETHODIMP COConnectionPoint::GetConnectionPointContainer(
  843. IConnectionPointContainer ** ppConnPtCon)
  844. {
  845. HRESULT hr;
  846. if (m_bTerminated)
  847. {
  848. hr = E_UNEXPECTED;
  849. }
  850. else
  851. {
  852. // Use QueryInterface to get the interface pointer and to perform the
  853. // needed AddRef on the returned pointer.
  854. hr = m_pHostObj->QueryInterface(
  855. IID_IConnectionPointContainer,
  856. (PPVOID) ppConnPtCon);
  857. }
  858. return hr;
  859. }
  860. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  861. Method: COConnectionPoint::Advise
  862. Summary: The Advise member method of this IConnectionPoint interface
  863. implementation. Called by clients to establish a connection of
  864. their sink to this connection point. Uses the CThreaded
  865. OwnThis mechanism to provide mutually exclusive access by
  866. multiple threads.
  867. Args: IUnknown* pUnkSink
  868. IUnknown interface pointer of the Sink object in the client.
  869. DWORD* pdwCookie
  870. Pointer to a DWORD in the client that will receive a unique
  871. key used by the client to refer to the connection established
  872. by this Advise call.
  873. Modifies: ...
  874. Returns: HRESULT
  875. Standard OLE result code. S_OK for success.
  876. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  877. STDMETHODIMP COConnectionPoint::Advise(
  878. IUnknown * pUnkSink,
  879. DWORD * pdwCookie)
  880. {
  881. HRESULT hr = S_OK;
  882. UINT uiFreeSlot = 0;
  883. IUnknown * pISink = NULL;
  884. DWORD dwCookie = 0;
  885. //
  886. // Lock was moved after the QI calls to eliminate long calls under lock
  887. //
  888. // Zero the output connection key.
  889. *pdwCookie = 0;
  890. if (m_bTerminated)
  891. {
  892. hr = E_UNEXPECTED;
  893. goto exit;
  894. }
  895. // It is necessary to set ProxyBlanket
  896. // to prevent privilege elevation by client
  897. // since callbacks would otherwise happen with impersonated SYSTEM
  898. // user
  899. hr = SetSinkCallbackSecurityBlanket( pUnkSink );
  900. if (SUCCEEDED(hr))
  901. {
  902. //
  903. // Verify if m_iidSink is supported
  904. //
  905. hr = pUnkSink->QueryInterface(m_iidSink, (PPVOID)&pISink);
  906. if (hr == E_NOINTERFACE)
  907. {
  908. hr = CONNECT_E_CANNOTCONNECT;
  909. }
  910. }
  911. if (FAILED(hr))
  912. {
  913. goto exit;
  914. }
  915. //
  916. // We will lock only after the QI call was made
  917. // to eliminate locking the resource for the long time
  918. // So far there was no data requiring synchronization.
  919. //
  920. m_Lock.WriteLock();
  921. if (SUCCEEDED(hr)&&m_bTerminated)
  922. {
  923. hr = E_UNEXPECTED;
  924. }
  925. if (SUCCEEDED(hr))
  926. {
  927. // Store the specific sink interface in this connection point's
  928. // array of live connections. First find a free slot (expand the
  929. // array if needed).
  930. hr = GetSlot(&uiFreeSlot);
  931. if (SUCCEEDED(hr))
  932. {
  933. //
  934. // store interface ref in GIP
  935. //
  936. if (pISink != NULL)
  937. {
  938. DBG_ASSERT(m_pGIT != NULL);
  939. hr = m_pGIT->RegisterInterfaceInGlobal (pUnkSink, IID_IUnknown, &dwCookie);
  940. if (SUCCEEDED(hr))
  941. {
  942. m_paConnections[uiFreeSlot].pUnk = NULL;
  943. m_paConnections[uiFreeSlot].dwCookie = dwCookie;
  944. // Assign the output Cookie value.
  945. *pdwCookie = dwCookie;
  946. // Increment the number of live connections.
  947. m_cConnections++;
  948. }
  949. }
  950. }
  951. }
  952. m_Lock.WriteUnlock();
  953. exit:
  954. //
  955. // Cleanup
  956. //
  957. if ( pISink != NULL )
  958. {
  959. pISink->Release();
  960. pISink = NULL;
  961. }
  962. return hr;
  963. }
  964. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  965. Method: COConnectionPoint::Unadvise
  966. Summary: The Unadvise member method of this IConnectionPoint interface
  967. implementation. Called by clients to disconnect a connection
  968. of their sink to this connection point. The connection is
  969. identified by the dwCookie argument (obtained by a previous
  970. Advise call). Uses the CThreaded OwnThis mechanism to provide
  971. mutually exclusive access by multiple threads.
  972. Args: DWORD dwCookie
  973. Connection key that was obtained by a previous Advise call.
  974. Modifies: .
  975. Returns: HRESULT
  976. Standard OLE result code. S_OK for success.
  977. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  978. STDMETHODIMP COConnectionPoint::Unadvise(
  979. DWORD dwCookie)
  980. {
  981. HRESULT hr;
  982. if ( m_bTerminated )
  983. {
  984. hr = E_UNEXPECTED;
  985. goto exit;
  986. }
  987. m_Lock.WriteLock();
  988. if ( m_bTerminated )
  989. {
  990. hr = E_UNEXPECTED;
  991. }
  992. else
  993. {
  994. hr = Unadvise_Worker( dwCookie );
  995. }
  996. // If we unadvised the last listener remove all notifications for the
  997. // host ABO object in the notifications queue
  998. if ( ( hr != E_UNEXPECTED ) && ( m_cConnections == 0 ) )
  999. {
  1000. m_Lock.ConvertExclusiveToShared();
  1001. if ( m_cConnections == 0 )
  1002. {
  1003. ((CADMCOMW*)m_pHostObj)->RemoveAllPendingNotifications( FALSE );
  1004. }
  1005. m_Lock.ReadUnlock();
  1006. }
  1007. else
  1008. {
  1009. m_Lock.WriteUnlock();
  1010. }
  1011. exit:
  1012. // Done
  1013. return hr;
  1014. }
  1015. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1016. Method: COConnectionPoint::ListenersPresent
  1017. Summary: Checks whether there are any listeners (sinks) presently registered
  1018. for notifications. Acquires read lock.
  1019. Args: None
  1020. Modifies: None
  1021. Returns: HRESULT
  1022. S_OK if sending notifications is enabled and there is at least one registered listener sink for notifications.
  1023. S_FALSE if sending notifications is disabled or there are no registered listeners.
  1024. E_UNEXPECTED if the object was already terminated.
  1025. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1026. STDMETHODIMP
  1027. COConnectionPoint::ListenersPresent(VOID)
  1028. {
  1029. HRESULT hr = S_OK;
  1030. m_Lock.ReadLock();
  1031. // Already terminated?
  1032. if ( m_bTerminated )
  1033. {
  1034. hr = E_UNEXPECTED;
  1035. goto exit;
  1036. }
  1037. // If disabled
  1038. if ( !m_bEnabled )
  1039. {
  1040. // Don't send any notifications
  1041. hr = S_FALSE;
  1042. goto exit;
  1043. }
  1044. // Any listeners currently?
  1045. if ( m_cConnections == 0 )
  1046. {
  1047. // No listeners
  1048. hr = S_FALSE;
  1049. goto exit;
  1050. }
  1051. exit:
  1052. m_Lock.ReadUnlock();
  1053. // Done
  1054. return hr;
  1055. }
  1056. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1057. Method: COConnectionPoint::Disable
  1058. Summary: Set m_bEnabled to FALSE. This will cause ListenersPresent to return S_FALSE ever after.
  1059. Acquires write lock.
  1060. Args: None
  1061. Modifies: None
  1062. Returns: HRESULT
  1063. S_OK
  1064. E_UNEXPECTED if the object was already terminated.
  1065. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1066. STDMETHODIMP
  1067. COConnectionPoint::Disable(VOID)
  1068. {
  1069. HRESULT hr = S_OK;
  1070. m_Lock.WriteLock();
  1071. // Already terminated?
  1072. if ( m_bTerminated )
  1073. {
  1074. hr = E_UNEXPECTED;
  1075. goto exit;
  1076. }
  1077. // Disable
  1078. m_bEnabled = FALSE;
  1079. exit:
  1080. m_Lock.WriteUnlock();
  1081. // Done
  1082. return hr;
  1083. }
  1084. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1085. Method: COConnectionPoint::Unadvise_Worker
  1086. Summary: Does the actual work of Unadvise. Assume a write lock is already
  1087. held.
  1088. Args: DWORD dwCookie
  1089. Connection key that was obtained by a previous Advise call.
  1090. Modifies: .
  1091. Returns: HRESULT
  1092. Standard OLE result code. S_OK for success.
  1093. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1094. STDMETHODIMP
  1095. COConnectionPoint::Unadvise_Worker(
  1096. DWORD dwCookie)
  1097. {
  1098. HRESULT hr = S_OK;
  1099. UINT uiSlot;
  1100. if (m_bTerminated)
  1101. {
  1102. hr = E_UNEXPECTED;
  1103. goto exit;
  1104. }
  1105. if ( 0 == dwCookie )
  1106. {
  1107. hr = E_INVALIDARG;
  1108. goto exit;
  1109. }
  1110. hr = FindSlot(dwCookie, &uiSlot);
  1111. if ( FAILED( hr ) )
  1112. {
  1113. goto exit;
  1114. }
  1115. DBG_ASSERT(m_pGIT != NULL);
  1116. if (m_pGIT != NULL)
  1117. {
  1118. m_pGIT->RevokeInterfaceFromGlobal (dwCookie);
  1119. }
  1120. // Mark the array entry as empty.
  1121. m_paConnections[uiSlot].dwCookie = 0;
  1122. // nothing is supposed to be stored in m_paConnections[uiSlot].pUnk
  1123. DBG_ASSERT( m_paConnections[uiSlot].pUnk == NULL );
  1124. // Decrement the number of live connections.
  1125. m_cConnections--;
  1126. exit:
  1127. return hr;
  1128. }
  1129. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1130. Method: COConnectionPoint::EnumConnections
  1131. Summary: The EnumConnections member method of this IConnectionPoint
  1132. interface implementation. Called to obtain an IEnumConnections
  1133. enumerator interface that can be used to enumerate the
  1134. connections of this connection point. Uses the CThreaded
  1135. OwnThis mechanism to ensure mutually exclusive access by
  1136. multiple threads.
  1137. Args: IEnumConnections** ppIEnum
  1138. Address of the caller's output pointer variable that will
  1139. receive the enumerator IEnumConnections interface pointer.
  1140. Modifies: ...
  1141. Returns: HRESULT
  1142. Standard OLE result code. S_OK for success.
  1143. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1144. STDMETHODIMP COConnectionPoint::EnumConnections(
  1145. IEnumConnections ** ppIEnum)
  1146. {
  1147. HRESULT hr = OLE_E_NOCONNECTION;
  1148. CONNECTDATA * paConns;
  1149. COEnumConnections * pCOEnum;
  1150. UINT i,j;
  1151. if (ppIEnum)
  1152. {
  1153. // Zero the output enumerator interface pointer.
  1154. *ppIEnum = NULL;
  1155. }
  1156. else
  1157. {
  1158. hr = E_INVALIDARG;
  1159. goto exit;
  1160. }
  1161. if (m_bTerminated)
  1162. {
  1163. hr = E_UNEXPECTED;
  1164. goto exit;
  1165. }
  1166. m_Lock.ReadLock();
  1167. if (m_bTerminated)
  1168. {
  1169. hr = E_UNEXPECTED;
  1170. }
  1171. else
  1172. {
  1173. if (0 != m_cConnections)
  1174. {
  1175. // Create an array of CONNECTDATA structures.
  1176. paConns = new CONNECTDATA[(UINT)m_cConnections];
  1177. if (NULL != paConns)
  1178. {
  1179. for (i=0, j=0; i<m_uiMaxIndex && j<m_cConnections; i++)
  1180. {
  1181. // Copy non-empty entries only.
  1182. if (0 != m_paConnections[i].dwCookie)
  1183. {
  1184. //
  1185. // Assign the occupied entry
  1186. //
  1187. paConns[j].dwCookie = m_paConnections[i].dwCookie;
  1188. paConns[j].pUnk = m_paConnections[i].pUnk;
  1189. j++;
  1190. }
  1191. }
  1192. //
  1193. // Create a new COM object for enumerating connections. Pass
  1194. // 'this' as a pHostObj pointer used later to ensure the host
  1195. // connection point object stays alive as long as the enumerator
  1196. // that enumerates connections to that connection point.
  1197. //
  1198. pCOEnum = new COEnumConnections(this);
  1199. if (NULL != pCOEnum)
  1200. {
  1201. // Use the previously constructed (paConns) array of connections
  1202. // to init the new COEnumConnections COM object. The Init will
  1203. // build yet another internal copy of this array. Set the
  1204. // initial enumerator index to 0.
  1205. hr = pCOEnum->Init(m_cConnections, paConns, 0, m_pGIT);
  1206. // QueryInterface to return the requested interface pointer.
  1207. // An AddRef will be conveniently done by the QI.
  1208. if (SUCCEEDED(hr))
  1209. {
  1210. hr = pCOEnum->QueryInterface(
  1211. IID_IEnumConnections,
  1212. (PPVOID)ppIEnum);
  1213. }
  1214. if (FAILED(hr))
  1215. {
  1216. delete pCOEnum;
  1217. }
  1218. }
  1219. else
  1220. {
  1221. hr = E_OUTOFMEMORY;
  1222. }
  1223. // We're done with the locally constructed array copy--delete it.
  1224. delete [] paConns;
  1225. }
  1226. else
  1227. {
  1228. hr = E_OUTOFMEMORY;
  1229. }
  1230. }
  1231. }
  1232. m_Lock.ReadUnlock();
  1233. exit:
  1234. return hr;
  1235. }
  1236. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1237. Method: COConnectionPoint::InternalEnumSinks
  1238. Summary: Returns a copy of the CONNECTDATA array.
  1239. Returns: HRESULT
  1240. Standard OLE result code. S_OK for success.
  1241. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1242. STDMETHODIMP
  1243. COConnectionPoint::InternalEnumSinks(
  1244. CONNECTDATA **prgConnections,
  1245. ULONG *pcConnections)
  1246. {
  1247. // Locals
  1248. HRESULT hr = S_OK;
  1249. BOOL fUnlock = FALSE;
  1250. CONNECTDATA *pConns = NULL;
  1251. ULONG cConns = 0;
  1252. ULONG i;
  1253. ULONG j;
  1254. IUnknown *pUnkSink = NULL;
  1255. // Check args
  1256. if ( ( prgConnections == NULL ) || ( pcConnections == NULL ) )
  1257. {
  1258. hr = E_INVALIDARG;
  1259. goto exit;
  1260. }
  1261. // Init
  1262. *prgConnections = NULL;
  1263. *pcConnections = 0;
  1264. m_Lock.ReadLock();
  1265. fUnlock = TRUE;
  1266. cConns = m_cConnections;
  1267. if ( m_pGIT == NULL )
  1268. {
  1269. DBG_ASSERT( m_pGIT != NULL );
  1270. hr = E_FAIL;
  1271. goto exit;
  1272. }
  1273. // Any listeners?
  1274. if ( cConns == 0 )
  1275. {
  1276. goto exit;
  1277. }
  1278. // Create an array of CONNECTDATA structures.
  1279. pConns = new CONNECTDATA[cConns];
  1280. if ( pConns == NULL )
  1281. {
  1282. hr = E_OUTOFMEMORY;
  1283. goto exit;
  1284. }
  1285. // Set to 0.
  1286. memset( pConns, 0, sizeof(CONNECTDATA)*cConns );
  1287. // Copy
  1288. for ( i = 0, j = 0; ( i<m_uiMaxIndex ) && ( j<cConns ); i++ )
  1289. {
  1290. // Copy non-empty entries only.
  1291. if ( m_paConnections[i].dwCookie == 0 )
  1292. {
  1293. continue;
  1294. }
  1295. // Unmarshal
  1296. hr = m_pGIT->GetInterfaceFromGlobal( m_paConnections[i].dwCookie,
  1297. IID_IUnknown,
  1298. (VOID**)&pUnkSink );
  1299. if ( FAILED( hr ) )
  1300. {
  1301. goto exit;
  1302. }
  1303. DBG_ASSERT( pUnkSink != NULL );
  1304. // It is necessary to set ProxyBlanket
  1305. // to prevent privilege elevation by client
  1306. // since callbacks would otherwise happen with impersonated SYSTEM user
  1307. hr = SetSinkCallbackSecurityBlanket( pUnkSink );
  1308. if ( FAILED( hr ) )
  1309. {
  1310. goto exit;
  1311. }
  1312. // Assign the occupied entry.
  1313. pConns[j].pUnk = pUnkSink;
  1314. pUnkSink = NULL;
  1315. pConns[j].dwCookie = m_paConnections[i].dwCookie;
  1316. j++;
  1317. }
  1318. // Return
  1319. *prgConnections = pConns;
  1320. *pcConnections = cConns;
  1321. // Don't delete
  1322. pConns = NULL;
  1323. cConns = 0;
  1324. exit:
  1325. if ( fUnlock )
  1326. {
  1327. m_Lock.ReadUnlock();
  1328. }
  1329. if ( pUnkSink != NULL )
  1330. {
  1331. pUnkSink->Release();
  1332. pUnkSink = NULL;
  1333. }
  1334. if ( pConns != NULL )
  1335. {
  1336. for ( j = 0; j<cConns; j++ )
  1337. {
  1338. if ( pConns[j].pUnk != NULL )
  1339. {
  1340. pConns[j].pUnk->Release();
  1341. pConns[j].pUnk = NULL;
  1342. }
  1343. }
  1344. delete [] pConns;
  1345. pConns = NULL;
  1346. }
  1347. return hr;
  1348. }
  1349. /*---------------------------------------------------------------------------
  1350. COEnumConnections's implementation of its main COM object class
  1351. including Constructor, Destructor, QueryInterface, AddRef, Release,
  1352. Next, Skip, Reset, and Clone.
  1353. ---------------------------------------------------------------------------*/
  1354. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1355. Method: COEnumConnections::COEnumConnections
  1356. Summary: COEnumConnections Constructor.
  1357. Args: IUnknown* pHostObj
  1358. Pointer to IUnknown interface of the host Connection Point
  1359. COM object whose connections are being enumerated.
  1360. Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnections,
  1361. and m_paConnections.
  1362. Returns: void
  1363. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1364. COEnumConnections::COEnumConnections(
  1365. IUnknown* pHostObj)
  1366. {
  1367. // Zero the COM object's reference count.
  1368. m_cRefs = 0;
  1369. // Assign the Host Object pointer.
  1370. m_pHostObj = pHostObj;
  1371. // Initialize the Connection Point enumerator variables.
  1372. m_iEnumIndex = 0;
  1373. m_cConnections = 0;
  1374. m_paConnections = NULL;
  1375. }
  1376. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1377. Method: COEnumConnections::~COEnumConnections
  1378. Summary: COEnumConnections Destructor.
  1379. Args: void
  1380. Modifies: m_paConnections.
  1381. Returns: void
  1382. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1383. COEnumConnections::~COEnumConnections(void)
  1384. {
  1385. if (NULL != m_paConnections)
  1386. {
  1387. UINT i;
  1388. // Release all the connected sink interface pointers.
  1389. for (i=0; i<m_cConnections; i++)
  1390. {
  1391. if (m_paConnections[i].pUnk != NULL)
  1392. {
  1393. m_paConnections[i].pUnk->Release();
  1394. }
  1395. }
  1396. // Delete the array of connections.
  1397. delete [] m_paConnections;
  1398. }
  1399. }
  1400. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1401. Method: COEnumConnections::Init
  1402. Summary: COEnumConnections Initialization method. Create any
  1403. necessary arrays, structures, and objects.
  1404. Args: ULONG cConnections
  1405. Number of Connections.
  1406. CONNECTDATA* paConnections,
  1407. Pointer to array of connections.
  1408. ULONG iEnumIndex
  1409. The Enumerator index initial value.
  1410. Modifies: m_cConnections, m_paConnections, m_pHostObj, m_iEnumIndex.
  1411. Returns: HRESULT
  1412. Standard OLE result code. S_OK for success.
  1413. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1414. HRESULT COEnumConnections::Init(
  1415. ULONG cConnections,
  1416. CONNECTDATA * paConnections,
  1417. ULONG iEnumIndex,
  1418. IGlobalInterfaceTable * pGIT)
  1419. {
  1420. HRESULT hr = S_OK;
  1421. UINT i;
  1422. IUnknown * pUnkSink = NULL;
  1423. IUnknown * pISink = NULL;
  1424. // Remember the number of live Connections.
  1425. m_cConnections = cConnections;
  1426. // Remember the initial enumerator index.
  1427. m_iEnumIndex = iEnumIndex;
  1428. // Create a copy of the array of connections and keep it inside
  1429. // this enumerator COM object.
  1430. m_paConnections = new CONNECTDATA [(UINT) cConnections];
  1431. // Fill the array copy with the connection data from the connections
  1432. // array passed. AddRef for each new sink Interface pointer copy made.
  1433. if (NULL != m_paConnections)
  1434. {
  1435. for (i=0; (i < cConnections) ; i++)
  1436. {
  1437. m_paConnections[i].dwCookie = paConnections[i].dwCookie;
  1438. m_paConnections[i].pUnk = NULL;
  1439. if (SUCCEEDED(hr))
  1440. {
  1441. //
  1442. // Get the interface.
  1443. // Don't do this after a failure, to avoid overriding a previous
  1444. // error code.
  1445. //
  1446. if (pGIT != NULL)
  1447. {
  1448. hr = pGIT->GetInterfaceFromGlobal(m_paConnections[i].dwCookie,
  1449. IID_IUnknown,
  1450. (void**)&( pUnkSink ));
  1451. if(SUCCEEDED(hr) && pUnkSink != NULL)
  1452. {
  1453. //
  1454. // It is necessary to set ProxyBlanket
  1455. // to prevent privilege elevation by client
  1456. // since callbacks would otherwise happen with impersonated SYSTEM user
  1457. //
  1458. hr = SetSinkCallbackSecurityBlanket( pUnkSink );
  1459. if ( SUCCEEDED( hr ) )
  1460. {
  1461. hr = pUnkSink->QueryInterface(IID_IMSAdminBaseSink_W,
  1462. (void **)&(pISink) );
  1463. if ( SUCCEEDED( hr ) )
  1464. {
  1465. //
  1466. // We have to set blanket again for pISink
  1467. //
  1468. hr = SetSinkCallbackSecurityBlanket( pISink );
  1469. if ( SUCCEEDED( hr ) )
  1470. {
  1471. DBG_ASSERT( m_paConnections[i].pUnk == NULL );
  1472. m_paConnections[i].pUnk = pISink;
  1473. pISink = NULL;
  1474. }
  1475. }
  1476. }
  1477. pUnkSink->Release();
  1478. pUnkSink = NULL;
  1479. }
  1480. }
  1481. else
  1482. {
  1483. //
  1484. // This case only occurs when Clone is called, in which case the pUnk field
  1485. // is valid.
  1486. //
  1487. DBG_ASSERT(paConnections[i].pUnk != NULL);
  1488. hr = paConnections[i].pUnk->QueryInterface(IID_IMSAdminBaseSink_W,
  1489. (void **)&(m_paConnections[i].pUnk));
  1490. }
  1491. }
  1492. }
  1493. }
  1494. else
  1495. {
  1496. hr = E_OUTOFMEMORY;
  1497. }
  1498. if ( pUnkSink != NULL )
  1499. {
  1500. pUnkSink->Release();
  1501. pUnkSink = NULL;
  1502. }
  1503. if ( pISink != NULL )
  1504. {
  1505. pISink->Release();
  1506. pISink = NULL;
  1507. }
  1508. return (hr);
  1509. }
  1510. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1511. Method: COEnumConnections::QueryInterface
  1512. Summary: QueryInterface of the COEnumConnections non-delegating
  1513. IUnknown implementation.
  1514. Args: REFIID riid,
  1515. [in] GUID of the Interface being requested.
  1516. PPVOID ppv)
  1517. [out] Address of the caller's pointer variable that will
  1518. receive the requested interface pointer.
  1519. Modifies: .
  1520. Returns: HRESULT
  1521. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1522. STDMETHODIMP COEnumConnections::QueryInterface(
  1523. REFIID riid,
  1524. PPVOID ppv)
  1525. {
  1526. HRESULT hr = E_NOINTERFACE;
  1527. *ppv = NULL;
  1528. // The IEnumConnections interface is implemented directly in
  1529. // this COM object rather than being a nested interface implementation.
  1530. if (IID_IUnknown == riid || IID_IEnumConnections == riid)
  1531. {
  1532. *ppv = (LPVOID)this;
  1533. }
  1534. if (NULL != *ppv)
  1535. {
  1536. // We've handed out a pointer to the interface so obey the COM rules
  1537. // and AddRef the reference count.
  1538. ((LPUNKNOWN)*ppv)->AddRef();
  1539. hr = S_OK;
  1540. }
  1541. return (hr);
  1542. }
  1543. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1544. Method: COEnumConnections::AddRef
  1545. Summary: AddRef of the COEnumConnections non-delegating IUnknown
  1546. implementation.
  1547. Args: void
  1548. Modifies: m_cRefs.
  1549. Returns: ULONG
  1550. New value of m_cRefs (COM object's reference count).
  1551. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1552. STDMETHODIMP_(ULONG) COEnumConnections::AddRef(void)
  1553. {
  1554. ULONG cRefs;
  1555. cRefs = InterlockedIncrement((long *)&m_cRefs);
  1556. // Also AddRef the host object to ensure it stays around as long as
  1557. // this enumerator.
  1558. m_pHostObj->AddRef();
  1559. return cRefs;
  1560. }
  1561. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1562. Method: COEnumConnections::Release
  1563. Summary: Release of the COEnumConnections non-delegating IUnknown
  1564. implementation.
  1565. Args: void
  1566. Modifies: m_cRefs.
  1567. Returns: ULONG
  1568. New value of m_cRefs (COM object's reference count).
  1569. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1570. STDMETHODIMP_(ULONG) COEnumConnections::Release(void)
  1571. {
  1572. ULONG cRefs;
  1573. // Pass this release along to the Host object being enumerated.
  1574. m_pHostObj->Release();
  1575. cRefs = InterlockedDecrement((long *)&m_cRefs);
  1576. if (0 == cRefs)
  1577. {
  1578. // We artificially bump the main ref count to prevent reentrancy via
  1579. // the main object destructor.
  1580. InterlockedIncrement((long *)&m_cRefs);
  1581. delete this;
  1582. }
  1583. return cRefs;
  1584. }
  1585. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1586. Method: COEnumConnections::Next
  1587. Summary: The Next member method of this IEnumConnections interface
  1588. implementation. Called by outside clients of a
  1589. COEnumConnections object to request a number of next
  1590. connections be returned in an array supplied by the caller.
  1591. Args: ULONG cReq
  1592. Number of connection points requested for return (starting at
  1593. the current Enumerator index).
  1594. CONNECTDATA* paConnections,
  1595. Pointer to a caller's output array that will receive the
  1596. enumerated connection data structures.
  1597. ULONG* pcEnumerated)
  1598. Pointer to a ULONG variable that will contain the number of
  1599. connection points actually enumerated by this call.
  1600. Modifies: .
  1601. Returns: HRESULT
  1602. Standard OLE result code. S_OK for success.
  1603. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1604. STDMETHODIMP COEnumConnections::Next(
  1605. ULONG cReq,
  1606. CONNECTDATA * paConnections,
  1607. ULONG * pcEnumerated)
  1608. {
  1609. HRESULT hr = S_OK;
  1610. ULONG cRet = 0;
  1611. // Make sure the argument values passed are valid.
  1612. if (NULL != m_paConnections)
  1613. {
  1614. if (NULL != paConnections)
  1615. {
  1616. if (m_iEnumIndex < m_cConnections)
  1617. {
  1618. if (NULL != pcEnumerated)
  1619. {
  1620. *pcEnumerated = 0L;
  1621. }
  1622. else
  1623. {
  1624. if (1L != cReq)
  1625. {
  1626. hr = E_POINTER;
  1627. }
  1628. }
  1629. }
  1630. else
  1631. {
  1632. hr = S_FALSE;
  1633. }
  1634. }
  1635. else
  1636. {
  1637. hr = E_POINTER;
  1638. }
  1639. }
  1640. else
  1641. {
  1642. hr = S_FALSE;
  1643. }
  1644. if (SUCCEEDED(hr))
  1645. {
  1646. // Starting at the current Enumerator index, loop to assign the
  1647. // requested number of output connection data structures.
  1648. for (; m_iEnumIndex < m_cConnections && cReq > 0;
  1649. paConnections++, m_iEnumIndex++, cRet++, cReq--)
  1650. {
  1651. // Because we are assigning a copy of a connection's data, AddRef
  1652. // its sink interface pointer.
  1653. if (NULL != m_paConnections[m_iEnumIndex].pUnk)
  1654. {
  1655. m_paConnections[m_iEnumIndex].pUnk->AddRef();
  1656. }
  1657. // Assign a connection's data from the inside Enumerator array to
  1658. // the specified output receiving array.
  1659. *paConnections = m_paConnections[m_iEnumIndex];
  1660. }
  1661. // Assign the output number of connections enumerated.
  1662. if (NULL != pcEnumerated)
  1663. {
  1664. *pcEnumerated = cRet;
  1665. }
  1666. }
  1667. return hr;
  1668. }
  1669. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1670. Method: COEnumConnections::Skip
  1671. Summary: The Skip member method of this IEnumConnections interface
  1672. implementation. Starting at the current Enumerator index, skip
  1673. the specified number of connection items.
  1674. Args: ULONG cSkip
  1675. Number of Connection items to skip.
  1676. Modifies: .
  1677. Returns: HRESULT
  1678. Standard OLE result code. S_OK for success.
  1679. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1680. STDMETHODIMP COEnumConnections::Skip(
  1681. ULONG cSkip)
  1682. {
  1683. HRESULT hr = S_OK;
  1684. // If there really is a connection array and the requested
  1685. // amount of skip does not exceed the number of connections,
  1686. // then bump the index by the requested skip amount.
  1687. if (NULL != m_paConnections && (m_iEnumIndex + cSkip) < m_cConnections)
  1688. {
  1689. m_iEnumIndex += cSkip;
  1690. }
  1691. else
  1692. {
  1693. hr = S_FALSE;
  1694. }
  1695. return hr;
  1696. }
  1697. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1698. Method: COEnumConnections::Reset
  1699. Summary: The Reset member method of the IEnumConnections interface
  1700. implementation. Resets the Enumeration index to the first
  1701. connection item in the array.
  1702. Args: void.
  1703. Modifies: .
  1704. Returns: HRESULT
  1705. Standard OLE result code. S_OK for success.
  1706. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1707. STDMETHODIMP COEnumConnections::Reset(void)
  1708. {
  1709. // Zero the main Enumerator index.
  1710. m_iEnumIndex = 0;
  1711. return S_OK;
  1712. }
  1713. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1714. Method: COEnumConnections::Clone
  1715. Summary: The Clone member method of this IEnumConnections interface
  1716. implementation. Creates a new clone of this entire Connection
  1717. enumerator COM object and returns a pointer to its
  1718. IEnumConnections interface.
  1719. Args: IEnumConnections** ppIEnum
  1720. Address of the caller's output pointer variable that will
  1721. receive the IEnumConnections interface pointer of the
  1722. new enumerator clone.
  1723. Modifies: ...
  1724. Returns: HRESULT
  1725. Standard OLE result code. S_OK for success.
  1726. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1727. STDMETHODIMP COEnumConnections::Clone(
  1728. IEnumConnections ** ppIEnum)
  1729. {
  1730. HRESULT hr;
  1731. COEnumConnections * pCOEnum;
  1732. // NULL the output variable first.
  1733. *ppIEnum = NULL;
  1734. // Create the Clone Enumerator COM object.
  1735. pCOEnum = new COEnumConnections(m_pHostObj);
  1736. if (NULL != pCOEnum)
  1737. {
  1738. // Initialize it with same values as in this existing enumerator.
  1739. hr = pCOEnum->Init(m_cConnections, m_paConnections, m_iEnumIndex);
  1740. if (SUCCEEDED(hr))
  1741. {
  1742. // QueryInterface to return the requested interface pointer.
  1743. // An AddRef will be conveniently done by the QI.
  1744. hr = pCOEnum->QueryInterface(
  1745. IID_IEnumConnections,
  1746. (PPVOID)ppIEnum);
  1747. }
  1748. if( FAILED( hr ) )
  1749. {
  1750. delete pCOEnum;
  1751. pCOEnum = NULL;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. hr = E_OUTOFMEMORY;
  1757. }
  1758. return hr;
  1759. }