crypt32: Store properties directly in link contexts and get rid of Context_GetProperties.
[wine.git] / dlls / oleaut32 / connpt.c
blob59768a029820ff62d915313a3262b315e9759fbc
1 /*
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
20 * NOTES:
21 * See one exported function here is CreateConnectionPoint, see
22 * comments just above that function for information.
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #define COBJMACROS
31 #include "winerror.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "ole2.h"
37 #include "olectl.h"
38 #include "connpt.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 #define MAXSINKS 10
46 /************************************************************************
47 * Implementation of IConnectionPoint
49 typedef struct ConnectionPointImpl {
51 IConnectionPoint IConnectionPoint_iface;
53 /* IUnknown of our main object*/
54 IUnknown *Obj;
56 /* Reference count */
57 LONG ref;
59 /* IID of sink interface */
60 IID iid;
62 /* Array of sink IUnknowns */
63 IUnknown **sinks;
64 DWORD maxSinks;
66 DWORD nSinks;
67 } ConnectionPointImpl;
69 static const IConnectionPointVtbl ConnectionPointImpl_VTable;
72 /************************************************************************
73 * Implementation of IEnumConnections
75 typedef struct EnumConnectionsImpl {
77 IEnumConnections IEnumConnections_iface;
79 LONG ref;
81 /* IUnknown of ConnectionPoint, used for ref counting */
82 IUnknown *pUnk;
84 /* Connection Data */
85 CONNECTDATA *pCD;
86 DWORD nConns;
88 /* Next connection to enumerate from */
89 DWORD nCur;
91 } EnumConnectionsImpl;
93 static EnumConnectionsImpl *EnumConnectionsImpl_Construct(IUnknown *pUnk,
94 DWORD nSinks,
95 CONNECTDATA *pCD);
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,
111 REFIID riid)
113 ConnectionPointImpl *Obj;
115 Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
116 Obj->IConnectionPoint_iface.lpVtbl = &ConnectionPointImpl_VTable;
117 Obj->Obj = pUnk;
118 Obj->ref = 1;
119 Obj->iid = *riid;
120 Obj->maxSinks = MAXSINKS;
121 Obj->sinks = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122 sizeof(IUnknown*) * MAXSINKS);
123 Obj->nSinks = 0;
124 return Obj;
127 /************************************************************************
128 * ConnectionPointImpl_Destroy
130 static void ConnectionPointImpl_Destroy(ConnectionPointImpl *Obj)
132 DWORD i;
133 for(i = 0; i < Obj->maxSinks; i++) {
134 if(Obj->sinks[i]) {
135 IUnknown_Release(Obj->sinks[i]);
136 Obj->sinks[i] = NULL;
139 HeapFree(GetProcessHeap(), 0, Obj->sinks);
140 HeapFree(GetProcessHeap(), 0, Obj);
141 return;
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,
152 REFIID riid,
153 void** ppvObject)
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) )
162 return E_INVALIDARG;
165 * Initialize the return parameter.
167 *ppvObject = 0;
170 * Compare the riid with the interface IDs implemented by this object.
172 if (IsEqualIID(&IID_IUnknown, riid))
173 *ppvObject = This;
174 else if (IsEqualIID(&IID_IConnectionPoint, riid))
175 *ppvObject = This;
178 * Check that we obtained an interface.
180 if ((*ppvObject)==0)
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
188 * successful
190 ConnectionPointImpl_AddRef(&This->IConnectionPoint_iface);
192 return S_OK;
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);
208 return refCount;
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);
229 return refCount;
232 /************************************************************************
233 * ConnectionPointImpl_GetConnectionInterface (IConnectionPoint)
236 static HRESULT WINAPI ConnectionPointImpl_GetConnectionInterface(
237 IConnectionPoint *iface,
238 IID *piid)
240 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
241 TRACE("(%p)->(%p) returning %s\n", This, piid, debugstr_guid(&(This->iid)));
242 *piid = This->iid;
243 return S_OK;
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,
259 (LPVOID)ppCPC);
262 /************************************************************************
263 * ConnectionPointImpl_Advise (IConnectionPoint)
266 static HRESULT WINAPI ConnectionPointImpl_Advise(IConnectionPoint *iface,
267 IUnknown *lpUnk,
268 DWORD *pdwCookie)
270 DWORD i;
271 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
272 IUnknown *lpSink;
273 TRACE("(%p)->(%p, %p)\n", This, lpUnk, pdwCookie);
275 *pdwCookie = 0;
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)
281 break;
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;
289 This->nSinks++;
290 *pdwCookie = i + 1;
291 return S_OK;
295 /************************************************************************
296 * ConnectionPointImpl_Unadvise (IConnectionPoint)
299 static HRESULT WINAPI ConnectionPointImpl_Unadvise(IConnectionPoint *iface,
300 DWORD dwCookie)
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;
311 This->nSinks--;
312 return S_OK;
315 /************************************************************************
316 * ConnectionPointImpl_EnumConnections (IConnectionPoint)
319 static HRESULT WINAPI ConnectionPointImpl_EnumConnections(
320 IConnectionPoint *iface,
321 LPENUMCONNECTIONS *ppEnum)
323 ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
324 CONNECTDATA *pCD;
325 DWORD i, nextslot;
326 EnumConnectionsImpl *EnumObj;
327 HRESULT hr;
329 TRACE("(%p)->(%p)\n", This, ppEnum);
331 *ppEnum = NULL;
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;
341 nextslot++;
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);
356 return hr;
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,
379 DWORD nSinks,
380 CONNECTDATA *pCD)
382 EnumConnectionsImpl *Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
383 DWORD i;
385 Obj->IEnumConnections_iface.lpVtbl = &EnumConnectionsImpl_VTable;
386 Obj->ref = 1;
387 Obj->pUnk = pUnk;
388 Obj->pCD = HeapAlloc(GetProcessHeap(), 0, nSinks * sizeof(CONNECTDATA));
389 Obj->nConns = nSinks;
390 Obj->nCur = 0;
392 for(i = 0; i < nSinks; i++) {
393 Obj->pCD[i] = pCD[i];
394 IUnknown_AddRef(Obj->pCD[i].pUnk);
396 return Obj;
399 /************************************************************************
400 * EnumConnectionsImpl_Destroy
402 static void EnumConnectionsImpl_Destroy(EnumConnectionsImpl *Obj)
404 DWORD i;
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);
411 return;
414 /************************************************************************
415 * EnumConnectionsImpl_QueryInterface (IUnknown)
417 * See Windows documentation for more details on IUnknown methods.
419 static HRESULT WINAPI EnumConnectionsImpl_QueryInterface(
420 IEnumConnections* iface,
421 REFIID riid,
422 void** ppvObject)
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) )
431 return E_INVALIDARG;
434 * Initialize the return parameter.
436 *ppvObject = 0;
439 * Compare the riid with the interface IDs implemented by this object.
441 if (IsEqualIID(&IID_IUnknown, riid))
442 *ppvObject = This;
443 else if (IsEqualIID(&IID_IEnumConnections, riid))
444 *ppvObject = This;
447 * Check that we obtained an interface.
449 if ((*ppvObject)==0)
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
457 * successful
459 EnumConnectionsImpl_AddRef((IEnumConnections*)This);
461 return S_OK;
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);
478 return refCount;
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);
500 return refCount;
503 /************************************************************************
504 * EnumConnectionsImpl_Next (IEnumConnections)
507 static HRESULT WINAPI EnumConnectionsImpl_Next(IEnumConnections* iface,
508 ULONG cConn, LPCONNECTDATA pCD,
509 ULONG *pEnum)
511 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
512 DWORD nRet = 0;
513 TRACE("(%p)->(%d, %p, %p)\n", This, cConn, pCD, pEnum);
515 if(pEnum == NULL) {
516 if(cConn != 1)
517 return E_POINTER;
518 } else
519 *pEnum = 0;
521 if(This->nCur >= This->nConns)
522 return S_FALSE;
524 while(This->nCur < This->nConns && cConn) {
525 *pCD++ = This->pCD[This->nCur];
526 IUnknown_AddRef(This->pCD[This->nCur].pUnk);
527 This->nCur++;
528 cConn--;
529 nRet++;
532 if(pEnum)
533 *pEnum = nRet;
535 return S_OK;
539 /************************************************************************
540 * EnumConnectionsImpl_Skip (IEnumConnections)
543 static HRESULT WINAPI EnumConnectionsImpl_Skip(IEnumConnections* iface,
544 ULONG cSkip)
546 EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
547 TRACE("(%p)->(%d)\n", This, cSkip);
549 if(This->nCur + cSkip >= This->nConns)
550 return S_FALSE;
552 This->nCur += cSkip;
554 return S_OK;
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);
567 This->nCur = 0;
569 return S_OK;
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);
588 return S_OK;
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
607 * PARAMS
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;
620 HRESULT hr;
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);
628 return hr;