2 * Implementation of a generic ConnectionPoint object.
4 * Copyright 2000 Huw D M Davies for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * See one exported function here is CreateConnectionPoint, see
22 * comments just above that function for information.
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 /************************************************************************
47 * Implementation of IConnectionPoint
49 typedef struct ConnectionPointImpl {
51 IConnectionPoint IConnectionPoint_iface;
53 /* IUnknown of our main object*/
59 /* IID of sink interface */
62 /* Array of sink IUnknowns */
67 } ConnectionPointImpl;
69 static const IConnectionPointVtbl ConnectionPointImpl_VTable;
72 /************************************************************************
73 * Implementation of IEnumConnections
75 typedef struct EnumConnectionsImpl {
77 IEnumConnections IEnumConnections_iface;
81 /* IUnknown of ConnectionPoint, used for ref counting */
88 /* Next connection to enumerate from */
91 } EnumConnectionsImpl;
93 static EnumConnectionsImpl *EnumConnectionsImpl_Construct(IUnknown *pUnk,
97 static inline ConnectionPointImpl *impl_from_IConnectionPoint(IConnectionPoint *iface)
99 return CONTAINING_RECORD(iface, ConnectionPointImpl, IConnectionPoint_iface);
102 static inline EnumConnectionsImpl *impl_from_IEnumConnections(IEnumConnections *iface)
104 return CONTAINING_RECORD(iface, EnumConnectionsImpl, IEnumConnections_iface);
107 /************************************************************************
108 * ConnectionPointImpl_Construct
110 static ConnectionPointImpl *ConnectionPointImpl_Construct(IUnknown *pUnk,
113 ConnectionPointImpl *Obj;
115 Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
116 Obj->IConnectionPoint_iface.lpVtbl = &ConnectionPointImpl_VTable;
120 Obj->maxSinks = MAXSINKS;
121 Obj->sinks = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122 sizeof(IUnknown*) * MAXSINKS);
127 /************************************************************************
128 * ConnectionPointImpl_Destroy
130 static void ConnectionPointImpl_Destroy(ConnectionPointImpl *Obj)
133 for(i = 0; i < Obj->maxSinks; i++) {
135 IUnknown_Release(Obj->sinks[i]);
136 Obj->sinks[i] = NULL;
139 HeapFree(GetProcessHeap(), 0, Obj->sinks);
140 HeapFree(GetProcessHeap(), 0, Obj);
144 static ULONG WINAPI ConnectionPointImpl_AddRef(IConnectionPoint* iface);
145 /************************************************************************
146 * ConnectionPointImpl_QueryInterface (IUnknown)
148 * See Windows documentation for more details on IUnknown methods.
150 static HRESULT WINAPI ConnectionPointImpl_QueryInterface(
151 IConnectionPoint* iface,
155 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
156 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
159 * Perform a sanity check on the parameters.
161 if ( (This==0) || (ppvObject==0) )
165 * Initialize the return parameter.
170 * Compare the riid with the interface IDs implemented by this object.
172 if (IsEqualIID(&IID_IUnknown, riid))
174 else if (IsEqualIID(&IID_IConnectionPoint, riid))
178 * Check that we obtained an interface.
182 FIXME("() : asking for un supported interface %s\n",debugstr_guid(riid));
183 return E_NOINTERFACE;
187 * Query Interface always increases the reference count by one when it is
190 ConnectionPointImpl_AddRef(&This->IConnectionPoint_iface);
196 /************************************************************************
197 * ConnectionPointImpl_AddRef (IUnknown)
199 * See Windows documentation for more details on IUnknown methods.
201 static ULONG WINAPI ConnectionPointImpl_AddRef(IConnectionPoint* iface)
203 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
204 ULONG refCount = InterlockedIncrement(&This->ref);
206 TRACE("(%p)->(ref before=%d)\n", This, refCount - 1);
211 /************************************************************************
212 * ConnectionPointImpl_Release (IUnknown)
214 * See Windows documentation for more details on IUnknown methods.
216 static ULONG WINAPI ConnectionPointImpl_Release(
217 IConnectionPoint* iface)
219 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
220 ULONG refCount = InterlockedDecrement(&This->ref);
222 TRACE("(%p)->(ref before=%d)\n", This, refCount + 1);
225 * If the reference count goes down to 0, perform suicide.
227 if (!refCount) ConnectionPointImpl_Destroy(This);
232 /************************************************************************
233 * ConnectionPointImpl_GetConnectionInterface (IConnectionPoint)
236 static HRESULT WINAPI ConnectionPointImpl_GetConnectionInterface(
237 IConnectionPoint *iface,
240 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
241 TRACE("(%p)->(%p) returning %s\n", This, piid, debugstr_guid(&(This->iid)));
246 /************************************************************************
247 * ConnectionPointImpl_GetConnectionPointContainer (IConnectionPoint)
250 static HRESULT WINAPI ConnectionPointImpl_GetConnectionPointContainer(
251 IConnectionPoint *iface,
252 IConnectionPointContainer **ppCPC)
254 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
255 TRACE("(%p)->(%p)\n", This, ppCPC);
257 return IUnknown_QueryInterface(This->Obj,
258 &IID_IConnectionPointContainer,
262 /************************************************************************
263 * ConnectionPointImpl_Advise (IConnectionPoint)
266 static HRESULT WINAPI ConnectionPointImpl_Advise(IConnectionPoint *iface,
271 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
273 TRACE("(%p)->(%p, %p)\n", This, lpUnk, pdwCookie);
276 if(FAILED(IUnknown_QueryInterface(lpUnk, &This->iid, (LPVOID)&lpSink)))
277 return CONNECT_E_CANNOTCONNECT;
279 for(i = 0; i < This->maxSinks; i++) {
280 if(This->sinks[i] == NULL)
283 if(i == This->maxSinks) {
284 This->maxSinks += MAXSINKS;
285 This->sinks = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sinks,
286 This->maxSinks * sizeof(IUnknown *));
288 This->sinks[i] = lpSink;
295 /************************************************************************
296 * ConnectionPointImpl_Unadvise (IConnectionPoint)
299 static HRESULT WINAPI ConnectionPointImpl_Unadvise(IConnectionPoint *iface,
302 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
303 TRACE("(%p)->(%d)\n", This, dwCookie);
305 if(dwCookie == 0 || dwCookie > This->maxSinks) return E_INVALIDARG;
307 if(This->sinks[dwCookie-1] == NULL) return CONNECT_E_NOCONNECTION;
309 IUnknown_Release(This->sinks[dwCookie-1]);
310 This->sinks[dwCookie-1] = NULL;
315 /************************************************************************
316 * ConnectionPointImpl_EnumConnections (IConnectionPoint)
319 static HRESULT WINAPI ConnectionPointImpl_EnumConnections(
320 IConnectionPoint *iface,
321 LPENUMCONNECTIONS *ppEnum)
323 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
326 EnumConnectionsImpl *EnumObj;
329 TRACE("(%p)->(%p)\n", This, ppEnum);
333 if(This->nSinks == 0) return OLE_E_NOCONNECTION;
335 pCD = HeapAlloc(GetProcessHeap(), 0, sizeof(CONNECTDATA) * This->nSinks);
337 for(i = 0, nextslot = 0; i < This->maxSinks; i++) {
338 if(This->sinks[i] != NULL) {
339 pCD[nextslot].pUnk = This->sinks[i];
340 pCD[nextslot].dwCookie = i + 1;
344 assert(nextslot == This->nSinks);
346 /* Bump the ref count of this object up by one. It gets Released in
347 IEnumConnections_Release */
348 IUnknown_AddRef((IUnknown*)This);
350 EnumObj = EnumConnectionsImpl_Construct((IUnknown*)This, This->nSinks, pCD);
351 hr = IEnumConnections_QueryInterface(&EnumObj->IEnumConnections_iface,
352 &IID_IEnumConnections, (LPVOID)ppEnum);
353 IEnumConnections_Release(&EnumObj->IEnumConnections_iface);
355 HeapFree(GetProcessHeap(), 0, pCD);
359 static const IConnectionPointVtbl ConnectionPointImpl_VTable =
361 ConnectionPointImpl_QueryInterface,
362 ConnectionPointImpl_AddRef,
363 ConnectionPointImpl_Release,
364 ConnectionPointImpl_GetConnectionInterface,
365 ConnectionPointImpl_GetConnectionPointContainer,
366 ConnectionPointImpl_Advise,
367 ConnectionPointImpl_Unadvise,
368 ConnectionPointImpl_EnumConnections
372 static const IEnumConnectionsVtbl EnumConnectionsImpl_VTable;
373 static ULONG WINAPI EnumConnectionsImpl_AddRef(IEnumConnections* iface);
375 /************************************************************************
376 * EnumConnectionsImpl_Construct
378 static EnumConnectionsImpl *EnumConnectionsImpl_Construct(IUnknown *pUnk,
382 EnumConnectionsImpl *Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
385 Obj->IEnumConnections_iface.lpVtbl = &EnumConnectionsImpl_VTable;
388 Obj->pCD = HeapAlloc(GetProcessHeap(), 0, nSinks * sizeof(CONNECTDATA));
389 Obj->nConns = nSinks;
392 for(i = 0; i < nSinks; i++) {
393 Obj->pCD[i] = pCD[i];
394 IUnknown_AddRef(Obj->pCD[i].pUnk);
399 /************************************************************************
400 * EnumConnectionsImpl_Destroy
402 static void EnumConnectionsImpl_Destroy(EnumConnectionsImpl *Obj)
406 for(i = 0; i < Obj->nConns; i++)
407 IUnknown_Release(Obj->pCD[i].pUnk);
409 HeapFree(GetProcessHeap(), 0, Obj->pCD);
410 HeapFree(GetProcessHeap(), 0, Obj);
414 /************************************************************************
415 * EnumConnectionsImpl_QueryInterface (IUnknown)
417 * See Windows documentation for more details on IUnknown methods.
419 static HRESULT WINAPI EnumConnectionsImpl_QueryInterface(
420 IEnumConnections* iface,
424 ConnectionPointImpl *This = (ConnectionPointImpl *)iface;
425 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);
428 * Perform a sanity check on the parameters.
430 if ( (This==0) || (ppvObject==0) )
434 * Initialize the return parameter.
439 * Compare the riid with the interface IDs implemented by this object.
441 if (IsEqualIID(&IID_IUnknown, riid))
443 else if (IsEqualIID(&IID_IEnumConnections, riid))
447 * Check that we obtained an interface.
451 FIXME("() : asking for un supported interface %s\n",debugstr_guid(riid));
452 return E_NOINTERFACE;
456 * Query Interface always increases the reference count by one when it is
459 EnumConnectionsImpl_AddRef((IEnumConnections*)This);
465 /************************************************************************
466 * EnumConnectionsImpl_AddRef (IUnknown)
468 * See Windows documentation for more details on IUnknown methods.
470 static ULONG WINAPI EnumConnectionsImpl_AddRef(IEnumConnections* iface)
472 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
473 ULONG refCount = InterlockedIncrement(&This->ref);
475 TRACE("(%p)->(ref before=%d)\n", This, refCount - 1);
477 IUnknown_AddRef(This->pUnk);
481 /************************************************************************
482 * EnumConnectionsImpl_Release (IUnknown)
484 * See Windows documentation for more details on IUnknown methods.
486 static ULONG WINAPI EnumConnectionsImpl_Release(IEnumConnections* iface)
488 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
489 ULONG refCount = InterlockedDecrement(&This->ref);
491 TRACE("(%p)->(ref before=%d)\n", This, refCount + 1);
493 IUnknown_Release(This->pUnk);
496 * If the reference count goes down to 0, perform suicide.
498 if (!refCount) EnumConnectionsImpl_Destroy(This);
503 /************************************************************************
504 * EnumConnectionsImpl_Next (IEnumConnections)
507 static HRESULT WINAPI EnumConnectionsImpl_Next(IEnumConnections* iface,
508 ULONG cConn, LPCONNECTDATA pCD,
511 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
513 TRACE("(%p)->(%d, %p, %p)\n", This, cConn, pCD, pEnum);
521 if(This->nCur >= This->nConns)
524 while(This->nCur < This->nConns && cConn) {
525 *pCD++ = This->pCD[This->nCur];
526 IUnknown_AddRef(This->pCD[This->nCur].pUnk);
539 /************************************************************************
540 * EnumConnectionsImpl_Skip (IEnumConnections)
543 static HRESULT WINAPI EnumConnectionsImpl_Skip(IEnumConnections* iface,
546 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
547 TRACE("(%p)->(%d)\n", This, cSkip);
549 if(This->nCur + cSkip >= This->nConns)
558 /************************************************************************
559 * EnumConnectionsImpl_Reset (IEnumConnections)
562 static HRESULT WINAPI EnumConnectionsImpl_Reset(IEnumConnections* iface)
564 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
565 TRACE("(%p)\n", This);
573 /************************************************************************
574 * EnumConnectionsImpl_Clone (IEnumConnections)
577 static HRESULT WINAPI EnumConnectionsImpl_Clone(IEnumConnections* iface,
578 LPENUMCONNECTIONS *ppEnum)
580 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
581 EnumConnectionsImpl *newObj;
582 TRACE("(%p)->(%p)\n", This, ppEnum);
584 newObj = EnumConnectionsImpl_Construct(This->pUnk, This->nConns, This->pCD);
585 newObj->nCur = This->nCur;
586 *ppEnum = (LPENUMCONNECTIONS)newObj;
587 IUnknown_AddRef(This->pUnk);
591 static const IEnumConnectionsVtbl EnumConnectionsImpl_VTable =
593 EnumConnectionsImpl_QueryInterface,
594 EnumConnectionsImpl_AddRef,
595 EnumConnectionsImpl_Release,
596 EnumConnectionsImpl_Next,
597 EnumConnectionsImpl_Skip,
598 EnumConnectionsImpl_Reset,
599 EnumConnectionsImpl_Clone
602 /************************************************************************
604 * The exported function to create the connection point.
605 * NB not a windows API
608 * pUnk [in] IUnknown of object to which the ConnectionPoint is associated.
609 * Needed to access IConnectionPointContainer.
611 * riid [in] IID of sink interface that this ConnectionPoint manages
613 * pCP [out] returns IConnectionPoint
616 HRESULT CreateConnectionPoint(IUnknown *pUnk, REFIID riid,
617 IConnectionPoint **pCP)
619 ConnectionPointImpl *Obj;
622 Obj = ConnectionPointImpl_Construct(pUnk, riid);
623 if(!Obj) return E_OUTOFMEMORY;
625 hr = IConnectionPoint_QueryInterface(&Obj->IConnectionPoint_iface,
626 &IID_IConnectionPoint, (LPVOID)pCP);
627 IConnectionPoint_Release(&Obj->IConnectionPoint_iface);