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.

1567 lines
48 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. connect.cxx
  5. Abstract:
  6. IIS MetaBase connection point code.
  7. Author:
  8. Michael W. Thomas 02-Oct-96
  9. Revision History:
  10. --*/
  11. /*+==========================================================================
  12. File: CONNECT.CPP
  13. Summary: Implementation file for the connection points (and their
  14. connections) offered by the connectable objects in the
  15. STOSERVE server sample. COM objects are implemented for
  16. Connection Point Enumerators, Connection Points, and
  17. Connection Enumerators.
  18. For a comprehensive tutorial code tour of this module's
  19. contents and offerings see the accompanying STOSERVE.TXT
  20. file. For more specific technical details on the internal
  21. workings see the comments dispersed throughout the module's
  22. source code.
  23. Classes: COEnumConnectionPoints, COConnectionPoint, and
  24. COEnumConnections.
  25. Functions: none.
  26. Origin: 6-10-96: atrent - Editor inheritance from CONSERVE OLE
  27. Tutorial Code Sample. Very little change was required.
  28. ----------------------------------------------------------------------------
  29. This file is part of the Microsoft OLE Tutorial Code Samples.
  30. Copyright (C) Microsoft Corporation, 1996. All rights reserved.
  31. This source code is intended only as a supplement to Microsoft
  32. Development Tools and/or on-line documentation. See these other
  33. materials for detailed information regarding Microsoft code samples.
  34. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  35. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  36. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  37. PARTICULAR PURPOSE.
  38. ==========================================================================+*/
  39. #include "precomp.hxx"
  40. /*---------------------------------------------------------------------------
  41. COEnumConnectionPoints's implementation of its main COM object class
  42. including Constructor, Destructor, QueryInterface, AddRef, Release,
  43. Next, Skip, Reset, and Clone.
  44. ---------------------------------------------------------------------------*/
  45. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  46. Method: COEnumConnectionPoints::COEnumConnectionPoints
  47. Summary: COEnumConnectionPoints Constructor.
  48. Args: IUnknown* pHostObj
  49. Pointer to the host object whose connection points are
  50. being enumerated.
  51. Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnPts, and m_paConnPts.
  52. Returns: void
  53. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  54. COEnumConnectionPoints::COEnumConnectionPoints(
  55. IUnknown * pHostObj)
  56. {
  57. // Zero the COM object's reference count.
  58. m_cRefs = 0;
  59. // Assign the Host Object pointer.
  60. m_pHostObj = pHostObj;
  61. // Initialize the Connection Point enumerator variables.
  62. m_iEnumIndex = 0;
  63. m_cConnPts = 0;
  64. m_paConnPts = NULL;
  65. }
  66. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  67. Method: COEnumConnectionPoints::~COEnumConnectionPoints
  68. Summary: COEnumConnectionPoints Destructor.
  69. Args: void
  70. Modifies: m_paConnPts.
  71. Returns: void
  72. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  73. COEnumConnectionPoints::~COEnumConnectionPoints(void)
  74. {
  75. if (NULL != m_paConnPts)
  76. {
  77. UINT i;
  78. // Release all the connection point interface pointers.
  79. for (i=0; i<m_cConnPts; i++)
  80. if (NULL != m_paConnPts[i])
  81. m_paConnPts[i]->Release();
  82. // Delete the array of interface pointers.
  83. delete [] m_paConnPts;
  84. }
  85. }
  86. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  87. Method: COEnumConnectionPoints::Init
  88. Summary: COEnumConnectionPoints Initialization method. Create any
  89. necessary arrays, structures, and objects.
  90. Args: ULONG cConnPts,
  91. Number of Connections Points.
  92. IConnectionPoint** paConnPts,
  93. Pointer to array of connection point interface pointers.
  94. ULONG iEnumIndex
  95. The initial Enumerator index value.
  96. Modifies: m_cConnPts, m_paConnPts, m_iEnumIndex.
  97. Returns: HRESULT
  98. Standard OLE result code. NOERROR for success.
  99. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  100. HRESULT COEnumConnectionPoints::Init(
  101. ULONG cConnPts,
  102. IConnectionPoint ** paConnPts,
  103. ULONG iEnumIndex)
  104. {
  105. HRESULT hr = NOERROR;
  106. UINT i;
  107. // Remember the number of Connection points.
  108. m_cConnPts = cConnPts;
  109. // Remember the initial enumerator index.
  110. m_iEnumIndex = iEnumIndex;
  111. // Create a copy of the array of connection points and keep it inside
  112. // this enumerator COM object.
  113. m_paConnPts = new IConnectionPoint* [(UINT) cConnPts];
  114. // Fill the array copy with the IConnectionPoint interface pointers from
  115. // the array passed. AddRef for each new Interface pointer copy made.
  116. if (NULL != m_paConnPts)
  117. {
  118. for (i=0; i<cConnPts; i++)
  119. {
  120. m_paConnPts[i] = paConnPts[i];
  121. m_paConnPts[i]->AddRef();
  122. }
  123. }
  124. else
  125. hr = E_OUTOFMEMORY;
  126. return (hr);
  127. }
  128. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  129. Method: COEnumConnectionPoints::QueryInterface
  130. Summary: QueryInterface of the COEnumConnectionPoints non-delegating
  131. IUnknown implementation.
  132. Args: REFIID riid,
  133. [in] GUID of the Interface being requested.
  134. PPVOID ppv)
  135. [out] Address of the caller's pointer variable that will
  136. receive the requested interface pointer.
  137. Modifies: .
  138. Returns: HRESULT
  139. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  140. STDMETHODIMP COEnumConnectionPoints::QueryInterface(
  141. REFIID riid,
  142. PPVOID ppv)
  143. {
  144. HRESULT hr = E_NOINTERFACE;
  145. *ppv = NULL;
  146. // The IEnumConnectionPoints interface is implemented directly in
  147. // this COM object rather than being a nested interface implementation.
  148. if (IID_IUnknown == riid || IID_IEnumConnectionPoints == riid)
  149. *ppv = (LPVOID)this;
  150. if (NULL != *ppv)
  151. {
  152. // We've handed out a pointer to the interface so obey the COM rules
  153. // and AddRef the reference count.
  154. ((LPUNKNOWN)*ppv)->AddRef();
  155. hr = NOERROR;
  156. }
  157. return (hr);
  158. }
  159. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  160. Method: COEnumConnectionPoints::AddRef
  161. Summary: AddRef of the COEnumConnectionPoints non-delegating IUnknown
  162. implementation.
  163. Args: void
  164. Modifies: m_cRefs.
  165. Returns: ULONG
  166. New value of m_cRefs (COM object's reference count).
  167. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  168. STDMETHODIMP_(ULONG) COEnumConnectionPoints::AddRef(void)
  169. {
  170. ULONG cRefs;
  171. cRefs = ++m_cRefs;
  172. // Also AddRef the host object to ensure it stays around as long as
  173. // this enumerator.
  174. m_pHostObj->AddRef();
  175. return cRefs;
  176. }
  177. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  178. Method: COEnumConnectionPoints::Release
  179. Summary: Release of the COEnumConnectionPoints non-delegating IUnknown
  180. implementation.
  181. Args: void
  182. Modifies: m_cRefs.
  183. Returns: ULONG
  184. New value of m_cRefs (COM object's reference count).
  185. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  186. STDMETHODIMP_(ULONG) COEnumConnectionPoints::Release(void)
  187. {
  188. ULONG cRefs;
  189. // Pass this release along to the Host object being enumerated.
  190. m_pHostObj->Release();
  191. cRefs = --m_cRefs;
  192. if (0 == cRefs)
  193. {
  194. // We artificially bump the main ref count to prevent reentrancy via
  195. // the main object destructor.
  196. m_cRefs++;
  197. delete this;
  198. }
  199. return cRefs;
  200. }
  201. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  202. Method: COEnumConnectionPoints::Next
  203. Summary: The Next member method of this IEnumConnectionPoints interface
  204. implementation. Called by outside clients of a
  205. COEnumConnectionPoints object to request that a number of next
  206. connection point interface pointers be deposited into an array
  207. supplied by the caller.
  208. Args: ULONG cReq
  209. Number of connection points requested for return (starting at
  210. the current Enumerator index).
  211. IConnectionPoint** paConnPts,
  212. Pointer to a caller's output array that will receive the
  213. enumerated IConnectionPoint interface pointers.
  214. ULONG* cEnumerated)
  215. Pointer to a ULONG variable that will contain the number of
  216. connection points actually enumerated by this call.
  217. Modifies: .
  218. Returns: HRESULT
  219. Standard OLE result code. NOERROR for success.
  220. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  221. STDMETHODIMP COEnumConnectionPoints::Next(
  222. ULONG cReq,
  223. IConnectionPoint ** paConnPts,
  224. ULONG * pcEnumerated)
  225. {
  226. HRESULT hr = NOERROR;
  227. ULONG cRet = 0;
  228. // Make sure the argument values passed are valid.
  229. if (NULL != m_paConnPts)
  230. {
  231. if (NULL != paConnPts)
  232. {
  233. if (NULL != *paConnPts && m_iEnumIndex < m_cConnPts)
  234. {
  235. if (NULL != pcEnumerated)
  236. *pcEnumerated = 0L;
  237. else
  238. if (1L != cReq)
  239. hr = E_POINTER;
  240. }
  241. else
  242. hr = S_FALSE;
  243. }
  244. else
  245. hr = E_POINTER;
  246. }
  247. else
  248. hr = S_FALSE;
  249. if (SUCCEEDED(hr))
  250. {
  251. // Starting at the current Enumerator index, loop to assign the
  252. // requested number of output connection point interface pointers.
  253. for (; m_iEnumIndex < m_cConnPts && cReq > 0;
  254. paConnPts++, cRet++, cReq--)
  255. {
  256. // Assign from the inside Enumerator array to the specified receiving
  257. // array.
  258. *paConnPts = m_paConnPts[m_iEnumIndex++];
  259. // After assigning a copy of an IConnectionPoint pointer, AddRef it.
  260. if (NULL != *paConnPts)
  261. (*paConnPts)->AddRef();
  262. }
  263. // Assign the output number of connection points enumerated.
  264. if (NULL != pcEnumerated)
  265. *pcEnumerated = cRet;
  266. }
  267. return hr;
  268. }
  269. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  270. Method: COEnumConnectionPoints::Skip
  271. Summary: The Skip member method of this IEnumConnectionPoints interface
  272. implementation. Starting at the current Enumerator index, skip
  273. the specified number of connection point items.
  274. Args: ULONG cSkip
  275. Number of Connection Point items to skip.
  276. Modifies: .
  277. Returns: HRESULT
  278. Standard OLE result code. NOERROR for success.
  279. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  280. STDMETHODIMP COEnumConnectionPoints::Skip(
  281. ULONG cSkip)
  282. {
  283. HRESULT hr = NOERROR;
  284. // If there really is a connection point array and the requested
  285. // amount of skip does not exceed the number of connection points,
  286. // then bump the index by the requested skip amount.
  287. if (NULL != m_paConnPts && (m_iEnumIndex + cSkip) < m_cConnPts)
  288. m_iEnumIndex += cSkip;
  289. else
  290. hr = S_FALSE;
  291. return hr;
  292. }
  293. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  294. Method: COEnumConnectionPoints::Reset
  295. Summary: The Reset member method of the IEnumConnectionPoints interface
  296. implementation. Resets the Enumeration index to the first
  297. connection point item in the array.
  298. Args: void.
  299. Modifies: .
  300. Returns: HRESULT
  301. Standard OLE result code. NOERROR for success.
  302. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  303. STDMETHODIMP COEnumConnectionPoints::Reset(
  304. void)
  305. {
  306. // Zero the main Enumerator index.
  307. m_iEnumIndex = 0;
  308. return NOERROR;
  309. }
  310. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  311. Method: COEnumConnectionPoints::Clone
  312. Summary: The Clone member method of this IEnumConnectionPoints
  313. interface implementation. Creates a new clone of this entire
  314. Connection Point enumerator COM object.
  315. Args: IEnumConnectionPoints** ppIEnum
  316. Address of the caller's output pointer variable that will
  317. receive the IEnumConnectionPoints interface pointer of the
  318. new enumerator clone.
  319. Modifies: ...
  320. Returns: HRESULT
  321. Standard OLE result code. NOERROR for success.
  322. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  323. STDMETHODIMP COEnumConnectionPoints::Clone(
  324. IEnumConnectionPoints ** ppIEnum)
  325. {
  326. HRESULT hr;
  327. COEnumConnectionPoints * pCOEnum;
  328. // NULL the output variable first.
  329. *ppIEnum = NULL;
  330. // Create the Clone Enumerator COM object.
  331. pCOEnum = new COEnumConnectionPoints(m_pHostObj);
  332. if (NULL != pCOEnum)
  333. {
  334. // Initialize it with same values as in this existing enumerator.
  335. hr = pCOEnum->Init(m_cConnPts, m_paConnPts, m_iEnumIndex);
  336. if (SUCCEEDED(hr))
  337. {
  338. // QueryInterface to return the requested interface pointer.
  339. // An AddRef will be conveniently done by the QI.
  340. hr = pCOEnum->QueryInterface(
  341. IID_IEnumConnectionPoints,
  342. (PPVOID)ppIEnum);
  343. }
  344. else
  345. {
  346. delete pCOEnum;
  347. }
  348. }
  349. else
  350. hr = E_OUTOFMEMORY;
  351. return hr;
  352. }
  353. /*---------------------------------------------------------------------------
  354. COConnectionPoint's implementation of its main COM object class
  355. including Constructor, Destructor, QueryInterface, AddRef, Release,
  356. GetConnectionInterface, GetConnectionPointContainer, Advise, Unadvise,
  357. and EnumConnections.
  358. ---------------------------------------------------------------------------*/
  359. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  360. Method: COConnectionPoint::COConnectionPoint
  361. Summary: COConnectionPoint Constructor.
  362. Args: IUnknown* pHostObj
  363. Pointer to IUnknown of the connectable object offering this
  364. connection point.
  365. Modifies: ...
  366. Returns: void
  367. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  368. COConnectionPoint::COConnectionPoint(
  369. IUnknown * pHostObj)
  370. {
  371. // Zero the COM object's reference count.
  372. m_cRefs = 0;
  373. // Remember an IUnknown pointer to the connectable object that offers
  374. // this connection point. Since this connection point object's lifetime
  375. // is geared to that of the connectable object there is no need to
  376. // AddRef the following copied pointer to the connectable object.
  377. m_pHostObj = pHostObj;
  378. // Initialize the Connection Point variables.
  379. m_dwNextCookie = COOKIE_START_VALUE;
  380. m_uiMaxIndex = 0;
  381. m_cConnections = 0;
  382. m_paConnections = NULL;
  383. }
  384. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  385. Method: COConnectionPoint::~COConnectionPoint
  386. Summary: COConnectionPoint Destructor.
  387. Args: void
  388. Modifies: m_paConnections.
  389. Returns: void
  390. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  391. COConnectionPoint::~COConnectionPoint(void)
  392. {
  393. UINT i;
  394. IUnknown * pUnk;
  395. if (NULL != m_paConnections)
  396. {
  397. // Release all the connection sink interface pointers.
  398. for (i=0; i<m_uiMaxIndex; i++)
  399. {
  400. pUnk = m_paConnections[i].pUnk;
  401. if (NULL != pUnk)
  402. pUnk->Release();
  403. }
  404. // Delete the array of interface pointers.
  405. delete [] m_paConnections;
  406. }
  407. }
  408. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  409. Method: COConnectionPoint::Init
  410. Summary: COConnectionPoint Initialization method. Create any
  411. necessary arrays, structures, and subordinate objects.
  412. Args: REFIID riid
  413. Reference to the IID of the Sink interface associated with
  414. this connection point.
  415. Modifies: ...
  416. Returns: HRESULT
  417. Standard OLE result code. NOERROR for success.
  418. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  419. HRESULT COConnectionPoint::Init(
  420. REFIID riid)
  421. {
  422. HRESULT hr = NOERROR;
  423. CONNECTDATA * paConns;
  424. // Keep a copy of the reference to the IID of the sink interface
  425. // associated with this connection point. Needed for later
  426. // use by the GetConnectionInterface method.
  427. m_iidSink = riid;
  428. // Build the initial dynamic array for connections.
  429. paConns = new CONNECTDATA[ALLOC_CONNECTIONS];
  430. if (NULL != paConns)
  431. {
  432. // Zero the array.
  433. memset(paConns, 0, ALLOC_CONNECTIONS * sizeof(CONNECTDATA));
  434. // Rig this connection point object so that it will use the
  435. // new internal array of connections.
  436. m_uiMaxIndex = ALLOC_CONNECTIONS;
  437. m_paConnections = paConns;
  438. }
  439. else
  440. hr = E_OUTOFMEMORY;
  441. return (hr);
  442. }
  443. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  444. Method: COConnectionPoint::QueryInterface
  445. Summary: QueryInterface of the COConnectionPoint non-delegating
  446. IUnknown implementation.
  447. Args: REFIID riid,
  448. [in] GUID of the Interface being requested.
  449. PPVOID ppv)
  450. [out] Address of the caller's pointer variable that will
  451. receive the requested interface pointer.
  452. Modifies: .
  453. Returns: HRESULT
  454. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  455. STDMETHODIMP COConnectionPoint::QueryInterface(
  456. REFIID riid,
  457. PPVOID ppv)
  458. {
  459. HRESULT hr = E_NOINTERFACE;
  460. *ppv = NULL;
  461. // The IConnectionPoint interface is implemented directly in this
  462. // COM object rather than being a nested interface implementation.
  463. if (IID_IUnknown == riid || IID_IConnectionPoint == riid)
  464. *ppv = (LPVOID)this;
  465. if (NULL != *ppv)
  466. {
  467. // We've handed out a pointer to the interface so obey the COM rules
  468. // and AddRef the reference count.
  469. ((LPUNKNOWN)*ppv)->AddRef();
  470. hr = NOERROR;
  471. }
  472. return (hr);
  473. }
  474. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  475. Method: COConnectionPoint::AddRef
  476. Summary: AddRef of the COConnectionPoint non-delegating IUnknown
  477. implementation.
  478. Args: void
  479. Modifies: m_cRefs.
  480. Returns: ULONG
  481. New value of m_cRefs (COM object's reference count).
  482. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  483. STDMETHODIMP_(ULONG) COConnectionPoint::AddRef(void)
  484. {
  485. ULONG cRefs;
  486. cRefs = InterlockedIncrement((long *)&m_cRefs);
  487. return cRefs;
  488. }
  489. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  490. Method: COConnectionPoint::Release
  491. Summary: Release of the COConnectionPoint non-delegating IUnknown
  492. implementation.
  493. Args: void
  494. Modifies: m_cRefs.
  495. Returns: ULONG
  496. New value of m_cRefs (COM object's reference count).
  497. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  498. STDMETHODIMP_(ULONG) COConnectionPoint::Release(void)
  499. {
  500. ULONG cRefs;
  501. cRefs = InterlockedDecrement((long *)&m_cRefs);
  502. if (0 == cRefs)
  503. {
  504. // We artificially bump the main ref count to prevent reentrancy via
  505. // the main object destructor. We relinquish thread ownership of this
  506. // object prior to deleting it--a good practice.
  507. InterlockedIncrement((long *)&m_cRefs);
  508. delete this;
  509. }
  510. return cRefs;
  511. }
  512. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  513. Method: COConnectionPoint::GetSlot
  514. Summary: An internal private utility member method to obtain a free
  515. slot in the dynamic connections array. GetSlot will expand the
  516. dynamic array for more entries if needed. To guarantee thread
  517. safety, this private method should always be called within the
  518. protection of a bracketed OwnThis, UnOwnThis pair.
  519. Args: UINT* puiFreeSlot
  520. Address of an output variable to receive the free slot index.
  521. Modifies: m_uiMaxIndex, m_paConnections.
  522. Returns: HRESULT
  523. Standard OLE result code. NOERROR for success.
  524. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  525. HRESULT COConnectionPoint::GetSlot(
  526. UINT * puiFreeSlot)
  527. {
  528. HRESULT hr = NOERROR;
  529. BOOL bOpen = FALSE;
  530. UINT i;
  531. CONNECTDATA * paConns;
  532. // Zero the output variable.
  533. *puiFreeSlot = 0;
  534. // Loop to find an empty slot.
  535. for (i=0; i<m_uiMaxIndex; i++)
  536. {
  537. if (m_paConnections[i].dwCookie == 0)
  538. {
  539. // We found an open empty slot.
  540. *puiFreeSlot = i;
  541. bOpen = TRUE;
  542. break;
  543. }
  544. }
  545. if (!bOpen)
  546. {
  547. // We didn't find an existing open slot in the array--it's full.
  548. // Expand the array by ALLOC_CONNECTIONS entries and assign the
  549. // appropriate output index.
  550. paConns = new CONNECTDATA[m_uiMaxIndex + ALLOC_CONNECTIONS];
  551. if (NULL != paConns)
  552. {
  553. // Copy the content of the old full array to the new larger array.
  554. for (i=0; i<m_uiMaxIndex; i++)
  555. {
  556. paConns[i].pUnk = m_paConnections[i].pUnk;
  557. paConns[i].dwCookie = m_paConnections[i].dwCookie;
  558. }
  559. // Zero (ie mark as empty) the expanded portion of the new array.
  560. for (i=m_uiMaxIndex; i<m_uiMaxIndex+ALLOC_CONNECTIONS; i++)
  561. {
  562. paConns[i].pUnk = NULL;
  563. paConns[i].dwCookie = 0;
  564. }
  565. // New larger array is ready--delete the old array.
  566. delete [] m_paConnections;
  567. // Rig the connection point to use the new larger array.
  568. m_paConnections = paConns;
  569. // Assign the output free slot as first entry in new expanded area.
  570. *puiFreeSlot = m_uiMaxIndex;
  571. // Calculate the new max index.
  572. m_uiMaxIndex += ALLOC_CONNECTIONS;
  573. }
  574. else
  575. hr = E_OUTOFMEMORY;
  576. }
  577. return hr;
  578. }
  579. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  580. Method: COConnectionPoint::FindSlot
  581. Summary: An internal private utility member method to find an existing
  582. slot (identified by the specified dwCookie value) in the
  583. dynamic connections array.
  584. Args: DWORD dwCookie,
  585. The connection key (cookie) to find.
  586. UINT* puiSlot)
  587. Address of an output variable to receive the slot index.
  588. Modifies: ...
  589. Returns: HRESULT
  590. Standard OLE result code. NOERROR for success.
  591. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  592. HRESULT COConnectionPoint::FindSlot(
  593. DWORD dwCookie,
  594. UINT * puiSlot)
  595. {
  596. HRESULT hr = CONNECT_E_NOCONNECTION;
  597. UINT i;
  598. // Loop to find the Cookie.
  599. for (i=0; i<m_uiMaxIndex; i++)
  600. {
  601. if (dwCookie == m_paConnections[i].dwCookie)
  602. {
  603. // If a cookie match is found, assign the output slot index.
  604. *puiSlot = i;
  605. hr = NOERROR;
  606. break;
  607. }
  608. }
  609. return hr;
  610. }
  611. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  612. Method: COConnectionPoint::GetConnectionInterface
  613. Summary: The GetConnectionInterface member method of this
  614. IConnectionPoint interface implementation. Called to get the
  615. IID of the Sink interface associated with this connection
  616. point.
  617. Args: IID* piidSink
  618. Pointer to the IID of the associated sink interface.
  619. Modifies: .
  620. Returns: HRESULT
  621. Standard OLE result code. NOERROR for success.
  622. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  623. STDMETHODIMP COConnectionPoint::GetConnectionInterface(
  624. IID * piidSink)
  625. {
  626. HRESULT hr = NOERROR;
  627. if (NULL != piidSink)
  628. *piidSink = m_iidSink;
  629. else
  630. hr = E_POINTER;
  631. return hr;
  632. }
  633. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  634. Method: COConnectionPoint::GetConnectionPointContainer
  635. Summary: The GetConnectionPointContainer member method of this
  636. IConnectionPoint interface implementation. Called to get the
  637. connection point container that contains this connection
  638. point.
  639. Args: IConnectionPointContainer** ppConnPtCon
  640. Address of the pointer variable that will recieve the
  641. IConnectionPointContainer interface pointer.
  642. Modifies: .
  643. Returns: HRESULT
  644. Standard OLE result code. NOERROR for success.
  645. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  646. STDMETHODIMP COConnectionPoint::GetConnectionPointContainer(
  647. IConnectionPointContainer ** ppConnPtCon)
  648. {
  649. HRESULT hr;
  650. // Use QueryInterface to get the interface pointer and to perform the
  651. // needed AddRef on the returned pointer.
  652. hr = m_pHostObj->QueryInterface(
  653. IID_IConnectionPointContainer,
  654. (PPVOID) ppConnPtCon);
  655. return hr;
  656. }
  657. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  658. Method: COConnectionPoint::Advise
  659. Summary: The Advise member method of this IConnectionPoint interface
  660. implementation. Called by clients to establish a connection of
  661. their sink to this connection point. Uses the CThreaded
  662. OwnThis mechanism to provide mutually exclusive access by
  663. multiple threads.
  664. Args: IUnknown* pUnkSink
  665. IUnknown interface pointer of the Sink object in the client.
  666. DWORD* pdwCookie
  667. Pointer to a DWORD in the client that will receive a unique
  668. key used by the client to refer to the connection established
  669. by this Advise call.
  670. Modifies: ...
  671. Returns: HRESULT
  672. Standard OLE result code. NOERROR for success.
  673. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  674. STDMETHODIMP COConnectionPoint::Advise(
  675. IUnknown * pUnkSink,
  676. DWORD * pdwCookie)
  677. {
  678. HRESULT hr = NOERROR;
  679. UINT uiFreeSlot = 0;
  680. IUnknown * pISink = NULL;
  681. g_rSinkResource->Lock(TSRES_LOCK_WRITE);
  682. // Zero the output connection key.
  683. *pdwCookie = 0;
  684. // Get the specific associated client Sink interface that this
  685. // connection point expects to use for notifications.
  686. hr = pUnkSink->QueryInterface(m_iidSink, (PPVOID)&pISink);
  687. if (SUCCEEDED(hr))
  688. {
  689. // Store the specific sink interface in this connection point's
  690. // array of live connections. First find a free slot (expand the
  691. // array if needed).
  692. hr = GetSlot(&uiFreeSlot);
  693. if (SUCCEEDED(hr))
  694. {
  695. // Assign the new slot with the connection entry.
  696. m_paConnections[uiFreeSlot].pUnk = pISink;
  697. m_paConnections[uiFreeSlot].dwCookie = m_dwNextCookie;
  698. // Assign the output Cookie value.
  699. *pdwCookie = m_dwNextCookie;
  700. // Increment the Cookie counter.
  701. m_dwNextCookie++;
  702. // Increment the number of live connections.
  703. m_cConnections++;
  704. }
  705. }
  706. else
  707. hr = CONNECT_E_CANNOTCONNECT;
  708. g_rSinkResource->Unlock();
  709. return hr;
  710. }
  711. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  712. Method: COConnectionPoint::Unadvise
  713. Summary: The Unadvise member method of this IConnectionPoint interface
  714. implementation. Called by clients to disconnect a connection
  715. of their sink to this connection point. The connection is
  716. identified by the dwCookie argument (obtained by a previous
  717. Advise call). Uses the CThreaded OwnThis mechanism to provide
  718. mutually exclusive access by multiple threads.
  719. Args: DWORD dwCookie
  720. Connection key that was obtained by a previous Advise call.
  721. Modifies: .
  722. Returns: HRESULT
  723. Standard OLE result code. NOERROR for success.
  724. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  725. STDMETHODIMP COConnectionPoint::Unadvise(
  726. DWORD dwCookie)
  727. {
  728. HRESULT hr = NOERROR;
  729. UINT uiSlot;
  730. if (0 != dwCookie)
  731. {
  732. g_rSinkResource->Lock(TSRES_LOCK_WRITE);
  733. hr = FindSlot(dwCookie, &uiSlot);
  734. if (SUCCEEDED(hr))
  735. {
  736. // Release the sink interface.
  737. RELEASE_INTERFACE(m_paConnections[uiSlot].pUnk);
  738. // Mark the array entry as empty.
  739. m_paConnections[uiSlot].dwCookie = 0;
  740. // Decrement the number of live connections.
  741. m_cConnections--;
  742. }
  743. g_rSinkResource->Unlock();
  744. }
  745. else
  746. hr = E_INVALIDARG;
  747. return hr;
  748. }
  749. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  750. Method: COConnectionPoint::EnumConnections
  751. Summary: The EnumConnections member method of this IConnectionPoint
  752. interface implementation. Called to obtain an IEnumConnections
  753. enumerator interface that can be used to enumerate the
  754. connections of this connection point. Uses the CThreaded
  755. OwnThis mechanism to ensure mutually exclusive access by
  756. multiple threads.
  757. Args: IEnumConnections** ppIEnum
  758. Address of the caller's output pointer variable that will
  759. receive the enumerator IEnumConnections interface pointer.
  760. Modifies: ...
  761. Returns: HRESULT
  762. Standard OLE result code. NOERROR for success.
  763. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  764. STDMETHODIMP COConnectionPoint::EnumConnections(
  765. IEnumConnections ** ppIEnum)
  766. {
  767. HRESULT hr = OLE_E_NOCONNECTION;
  768. CONNECTDATA * paConns;
  769. COEnumConnections * pCOEnum;
  770. UINT i,j;
  771. g_rSinkResource->Lock(TSRES_LOCK_READ);
  772. // Zero the output enumerator interface pointer.
  773. *ppIEnum = NULL;
  774. if (0 != m_cConnections)
  775. {
  776. // Create an array of CONNECTDATA structures.
  777. paConns = new CONNECTDATA[(UINT)m_cConnections];
  778. if (NULL != paConns)
  779. {
  780. for (i=0, j=0; i<m_uiMaxIndex && j<m_cConnections; i++)
  781. {
  782. // Copy non-empty entries only.
  783. if (0 != m_paConnections[i].dwCookie)
  784. {
  785. // Assign the occupied entry.
  786. paConns[j].pUnk = (IUnknown*)m_paConnections[i].pUnk;
  787. paConns[j].dwCookie = m_paConnections[i].dwCookie;
  788. j++;
  789. }
  790. }
  791. // Create a new COM object for enumerating connections. Pass
  792. // 'this' as a pHostObj pointer used later to ensure the host
  793. // connection point object stays alive as long as the enumerator
  794. // that enumerates connections to that connection point.
  795. pCOEnum = new COEnumConnections(this);
  796. if (NULL != pCOEnum)
  797. {
  798. // Use the previously constructed (paConns) array of connections
  799. // to init the new COEnumConnections COM object. The Init will
  800. // build yet another internal copy of this array. Set the
  801. // initial enumerator index to 0.
  802. hr = pCOEnum->Init(m_cConnections, paConns, 0);
  803. // QueryInterface to return the requested interface pointer.
  804. // An AddRef will be conveniently done by the QI.
  805. if (SUCCEEDED(hr))
  806. {
  807. hr = pCOEnum->QueryInterface(
  808. IID_IEnumConnections,
  809. (PPVOID)ppIEnum);
  810. }
  811. }
  812. else
  813. {
  814. hr = E_OUTOFMEMORY;
  815. }
  816. // We're done with the locally constructed array copy--delete it.
  817. delete [] paConns;
  818. }
  819. else
  820. {
  821. hr = E_OUTOFMEMORY;
  822. }
  823. }
  824. g_rSinkResource->Unlock();
  825. return hr;
  826. }
  827. STDMETHODIMP
  828. COConnectionPoint::InternalEnumSinks(
  829. CONNECTDATA **prgConnections,
  830. ULONG *pcConnections)
  831. {
  832. // Locals
  833. HRESULT hr = S_OK;
  834. BOOL fUnlock = FALSE;
  835. CONNECTDATA *pConns = NULL;
  836. ULONG cConns = 0;
  837. ULONG i;
  838. ULONG j;
  839. // Check args
  840. if ( ( prgConnections == NULL ) || ( pcConnections == NULL ) )
  841. {
  842. hr = E_INVALIDARG;
  843. goto exit;
  844. }
  845. // Init
  846. *prgConnections = NULL;
  847. *pcConnections = 0;
  848. g_rSinkResource->Lock(TSRES_LOCK_READ);
  849. fUnlock = TRUE;
  850. cConns = m_cConnections;
  851. // Any listeners?
  852. if ( cConns == 0 )
  853. {
  854. goto exit;
  855. }
  856. // Create an array of CONNECTDATA structures.
  857. pConns = new CONNECTDATA[cConns];
  858. if ( pConns == NULL )
  859. {
  860. hr = E_OUTOFMEMORY;
  861. goto exit;
  862. }
  863. // Set to 0.
  864. memset( pConns, 0, sizeof(CONNECTDATA)*cConns );
  865. // Copy
  866. for ( i = 0, j = 0; ( i<m_uiMaxIndex ) && ( j<cConns ); i++ )
  867. {
  868. // Copy non-empty entries only.
  869. if ( m_paConnections[i].dwCookie == 0 )
  870. {
  871. continue;
  872. }
  873. // Assign the occupied entry.
  874. pConns[j].pUnk = m_paConnections[i].pUnk;
  875. pConns[j].pUnk->AddRef();
  876. pConns[j].dwCookie = m_paConnections[i].dwCookie;
  877. j++;
  878. }
  879. // Return
  880. *prgConnections = pConns;
  881. *pcConnections = cConns;
  882. // Don't delete
  883. pConns = NULL;
  884. cConns = 0;
  885. exit:
  886. if ( fUnlock )
  887. {
  888. g_rSinkResource->Unlock();
  889. }
  890. if ( pConns != NULL )
  891. {
  892. for ( j = 0; j<cConns; j++ )
  893. {
  894. if ( pConns[j].pUnk != NULL )
  895. {
  896. pConns[j].pUnk->Release();
  897. pConns[j].pUnk = NULL;
  898. }
  899. }
  900. delete [] pConns;
  901. pConns = NULL;
  902. }
  903. return hr;
  904. }
  905. /*---------------------------------------------------------------------------
  906. COEnumConnections's implementation of its main COM object class
  907. including Constructor, Destructor, QueryInterface, AddRef, Release,
  908. Next, Skip, Reset, and Clone.
  909. ---------------------------------------------------------------------------*/
  910. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  911. Method: COEnumConnections::COEnumConnections
  912. Summary: COEnumConnections Constructor.
  913. Args: IUnknown* pHostObj
  914. Pointer to IUnknown interface of the host Connection Point
  915. COM object whose connections are being enumerated.
  916. Modifies: m_cRefs, m_pHostObj, m_iEnumIndex, m_cConnections,
  917. and m_paConnections.
  918. Returns: void
  919. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  920. COEnumConnections::COEnumConnections(
  921. IUnknown* pHostObj)
  922. {
  923. // Zero the COM object's reference count.
  924. m_cRefs = 0;
  925. // Assign the Host Object pointer.
  926. m_pHostObj = pHostObj;
  927. // Initialize the Connection Point enumerator variables.
  928. m_iEnumIndex = 0;
  929. m_cConnections = 0;
  930. m_paConnections = NULL;
  931. return;
  932. }
  933. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  934. Method: COEnumConnections::~COEnumConnections
  935. Summary: COEnumConnections Destructor.
  936. Args: void
  937. Modifies: m_paConnections.
  938. Returns: void
  939. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  940. COEnumConnections::~COEnumConnections(void)
  941. {
  942. if (NULL != m_paConnections)
  943. {
  944. UINT i;
  945. // Release all the connected sink interface pointers.
  946. for (i=0; i<m_cConnections; i++)
  947. m_paConnections[i].pUnk->Release();
  948. // Delete the array of connections.
  949. delete [] m_paConnections;
  950. }
  951. return;
  952. }
  953. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  954. Method: COEnumConnections::Init
  955. Summary: COEnumConnections Initialization method. Create any
  956. necessary arrays, structures, and objects.
  957. Args: ULONG cConnections
  958. Number of Connections.
  959. CONNECTDATA* paConnections,
  960. Pointer to array of connections.
  961. ULONG iEnumIndex
  962. The Enumerator index initial value.
  963. Modifies: m_cConnections, m_paConnections, m_pHostObj, m_iEnumIndex.
  964. Returns: HRESULT
  965. Standard OLE result code. NOERROR for success.
  966. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  967. HRESULT COEnumConnections::Init(
  968. ULONG cConnections,
  969. CONNECTDATA* paConnections,
  970. ULONG iEnumIndex)
  971. {
  972. HRESULT hr = NOERROR;
  973. UINT i;
  974. // Remember the number of live Connections.
  975. m_cConnections = cConnections;
  976. // Remember the initial enumerator index.
  977. m_iEnumIndex = iEnumIndex;
  978. // Create a copy of the array of connections and keep it inside
  979. // this enumerator COM object.
  980. m_paConnections = new CONNECTDATA [(UINT) cConnections];
  981. // Fill the array copy with the connection data from the connections
  982. // array passed. AddRef for each new sink Interface pointer copy made.
  983. if (NULL != m_paConnections)
  984. {
  985. for (i=0; i<cConnections; i++)
  986. {
  987. m_paConnections[i] = paConnections[i];
  988. m_paConnections[i].pUnk->AddRef();
  989. }
  990. }
  991. else
  992. hr = E_OUTOFMEMORY;
  993. return (hr);
  994. }
  995. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  996. Method: COEnumConnections::QueryInterface
  997. Summary: QueryInterface of the COEnumConnections non-delegating
  998. IUnknown implementation.
  999. Args: REFIID riid,
  1000. [in] GUID of the Interface being requested.
  1001. PPVOID ppv)
  1002. [out] Address of the caller's pointer variable that will
  1003. receive the requested interface pointer.
  1004. Modifies: .
  1005. Returns: HRESULT
  1006. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1007. STDMETHODIMP COEnumConnections::QueryInterface(
  1008. REFIID riid,
  1009. PPVOID ppv)
  1010. {
  1011. HRESULT hr = E_NOINTERFACE;
  1012. *ppv = NULL;
  1013. // The IEnumConnections interface is implemented directly in
  1014. // this COM object rather than being a nested interface implementation.
  1015. if (IID_IUnknown == riid || IID_IEnumConnections == riid)
  1016. *ppv = (LPVOID)this;
  1017. if (NULL != *ppv)
  1018. {
  1019. // We've handed out a pointer to the interface so obey the COM rules
  1020. // and AddRef the reference count.
  1021. ((LPUNKNOWN)*ppv)->AddRef();
  1022. hr = NOERROR;
  1023. }
  1024. return (hr);
  1025. }
  1026. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1027. Method: COEnumConnections::AddRef
  1028. Summary: AddRef of the COEnumConnections non-delegating IUnknown
  1029. implementation.
  1030. Args: void
  1031. Modifies: m_cRefs.
  1032. Returns: ULONG
  1033. New value of m_cRefs (COM object's reference count).
  1034. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1035. STDMETHODIMP_(ULONG) COEnumConnections::AddRef(void)
  1036. {
  1037. ULONG cRefs;
  1038. cRefs = InterlockedIncrement((long *)&m_cRefs);
  1039. // Also AddRef the host object to ensure it stays around as long as
  1040. // this enumerator.
  1041. m_pHostObj->AddRef();
  1042. return cRefs;
  1043. }
  1044. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1045. Method: COEnumConnections::Release
  1046. Summary: Release of the COEnumConnections non-delegating IUnknown
  1047. implementation.
  1048. Args: void
  1049. Modifies: m_cRefs.
  1050. Returns: ULONG
  1051. New value of m_cRefs (COM object's reference count).
  1052. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1053. STDMETHODIMP_(ULONG) COEnumConnections::Release(void)
  1054. {
  1055. ULONG cRefs;
  1056. // Pass this release along to the Host object being enumerated.
  1057. m_pHostObj->Release();
  1058. cRefs = InterlockedDecrement((long *)&m_cRefs);
  1059. if (0 == cRefs)
  1060. {
  1061. // We artificially bump the main ref count to prevent reentrancy via
  1062. // the main object destructor.
  1063. InterlockedIncrement((long *)&m_cRefs);
  1064. delete this;
  1065. }
  1066. return cRefs;
  1067. }
  1068. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1069. Method: COEnumConnections::Next
  1070. Summary: The Next member method of this IEnumConnections interface
  1071. implementation. Called by outside clients of a
  1072. COEnumConnections object to request a number of next
  1073. connections be returned in an array supplied by the caller.
  1074. Args: ULONG cReq
  1075. Number of connection points requested for return (starting at
  1076. the current Enumerator index).
  1077. CONNECTDATA* paConnections,
  1078. Pointer to a caller's output array that will receive the
  1079. enumerated connection data structures.
  1080. ULONG* pcEnumerated)
  1081. Pointer to a ULONG variable that will contain the number of
  1082. connection points actually enumerated by this call.
  1083. Modifies: .
  1084. Returns: HRESULT
  1085. Standard OLE result code. NOERROR for success.
  1086. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1087. STDMETHODIMP COEnumConnections::Next(
  1088. ULONG cReq,
  1089. CONNECTDATA* paConnections,
  1090. ULONG* pcEnumerated)
  1091. {
  1092. HRESULT hr = NOERROR;
  1093. ULONG cRet = 0;
  1094. // Make sure the argument values passed are valid.
  1095. if (NULL != m_paConnections)
  1096. {
  1097. if (NULL != paConnections)
  1098. {
  1099. if (m_iEnumIndex < m_cConnections)
  1100. {
  1101. if (NULL != pcEnumerated)
  1102. *pcEnumerated = 0L;
  1103. else
  1104. if (1L != cReq)
  1105. hr = E_POINTER;
  1106. }
  1107. else
  1108. hr = S_FALSE;
  1109. }
  1110. else
  1111. hr = E_POINTER;
  1112. }
  1113. else
  1114. hr = S_FALSE;
  1115. if (SUCCEEDED(hr))
  1116. {
  1117. // Starting at the current Enumerator index, loop to assign the
  1118. // requested number of output connection data structures.
  1119. for (; m_iEnumIndex < m_cConnections && cReq > 0;
  1120. paConnections++, m_iEnumIndex++, cRet++, cReq--)
  1121. {
  1122. // Because we are assigning a copy of a connection's data, AddRef
  1123. // its sink interface pointer.
  1124. if (NULL != m_paConnections[m_iEnumIndex].pUnk)
  1125. m_paConnections[m_iEnumIndex].pUnk->AddRef();
  1126. // Assign a connection's data from the inside Enumerator array to
  1127. // the specified output receiving array.
  1128. *paConnections = m_paConnections[m_iEnumIndex];
  1129. }
  1130. // Assign the output number of connections enumerated.
  1131. if (NULL != pcEnumerated)
  1132. *pcEnumerated = cRet;
  1133. }
  1134. return hr;
  1135. }
  1136. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1137. Method: COEnumConnections::Skip
  1138. Summary: The Skip member method of this IEnumConnections interface
  1139. implementation. Starting at the current Enumerator index, skip
  1140. the specified number of connection items.
  1141. Args: ULONG cSkip
  1142. Number of Connection items to skip.
  1143. Modifies: .
  1144. Returns: HRESULT
  1145. Standard OLE result code. NOERROR for success.
  1146. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1147. STDMETHODIMP COEnumConnections::Skip(
  1148. ULONG cSkip)
  1149. {
  1150. HRESULT hr = NOERROR;
  1151. // If there really is a connection array and the requested
  1152. // amount of skip does not exceed the number of connections,
  1153. // then bump the index by the requested skip amount.
  1154. if (NULL != m_paConnections && (m_iEnumIndex + cSkip) < m_cConnections)
  1155. m_iEnumIndex += cSkip;
  1156. else
  1157. hr = S_FALSE;
  1158. return hr;
  1159. }
  1160. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1161. Method: COEnumConnections::Reset
  1162. Summary: The Reset member method of the IEnumConnections interface
  1163. implementation. Resets the Enumeration index to the first
  1164. connection item in the array.
  1165. Args: void.
  1166. Modifies: .
  1167. Returns: HRESULT
  1168. Standard OLE result code. NOERROR for success.
  1169. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1170. STDMETHODIMP COEnumConnections::Reset(
  1171. void)
  1172. {
  1173. // Zero the main Enumerator index.
  1174. m_iEnumIndex = 0;
  1175. return NOERROR;
  1176. }
  1177. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  1178. Method: COEnumConnections::Clone
  1179. Summary: The Clone member method of this IEnumConnections interface
  1180. implementation. Creates a new clone of this entire Connection
  1181. enumerator COM object and returns a pointer to its
  1182. IEnumConnections interface.
  1183. Args: IEnumConnections** ppIEnum
  1184. Address of the caller's output pointer variable that will
  1185. receive the IEnumConnections interface pointer of the
  1186. new enumerator clone.
  1187. Modifies: ...
  1188. Returns: HRESULT
  1189. Standard OLE result code. NOERROR for success.
  1190. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  1191. STDMETHODIMP COEnumConnections::Clone(
  1192. IEnumConnections** ppIEnum)
  1193. {
  1194. HRESULT hr;
  1195. COEnumConnections* pCOEnum;
  1196. // NULL the output variable first.
  1197. *ppIEnum = NULL;
  1198. // Create the Clone Enumerator COM object.
  1199. pCOEnum = new COEnumConnections(m_pHostObj);
  1200. if (NULL != pCOEnum)
  1201. {
  1202. // Initialize it with same values as in this existing enumerator.
  1203. hr = pCOEnum->Init(m_cConnections, m_paConnections, m_iEnumIndex);
  1204. if (SUCCEEDED(hr))
  1205. {
  1206. // QueryInterface to return the requested interface pointer.
  1207. // An AddRef will be conveniently done by the QI.
  1208. hr = pCOEnum->QueryInterface(
  1209. IID_IEnumConnections,
  1210. (PPVOID)ppIEnum);
  1211. }
  1212. else
  1213. {
  1214. delete pCOEnum;
  1215. }
  1216. }
  1217. else
  1218. hr = E_OUTOFMEMORY;
  1219. return hr;
  1220. }
  1221.