-Wpointer-sign fixes for gcc 4.0.
[wine/multimedia.git] / dlls / ole32 / oleobj.c
blob0c77121694a93aa859f9cffc64a1db55bbc7d1f3
1 /*
2 * OLE2 COM objects
4 * Copyright 1998 Eric Kohl
5 * Copyright 1999 Francis Beaudet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <stdarg.h>
24 #include <string.h>
26 #define COBJMACROS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37 #define INITIAL_SINKS 10
39 /**************************************************************************
40 * OleAdviseHolderImpl Implementation
42 typedef struct OleAdviseHolderImpl
44 const IOleAdviseHolderVtbl *lpVtbl;
46 LONG ref;
48 DWORD maxSinks;
49 IAdviseSink** arrayOfSinks;
51 } OleAdviseHolderImpl;
53 /**************************************************************************
54 * OleAdviseHolderImpl_Destructor
56 static void OleAdviseHolderImpl_Destructor(
57 OleAdviseHolderImpl* ptrToDestroy)
59 DWORD index;
60 TRACE("%p\n", ptrToDestroy);
62 for (index = 0; index < ptrToDestroy->maxSinks; index++)
64 if (ptrToDestroy->arrayOfSinks[index]!=0)
66 IAdviseSink_Release(ptrToDestroy->arrayOfSinks[index]);
67 ptrToDestroy->arrayOfSinks[index] = NULL;
71 HeapFree(GetProcessHeap(),
73 ptrToDestroy->arrayOfSinks);
76 HeapFree(GetProcessHeap(),
78 ptrToDestroy);
81 /**************************************************************************
82 * OleAdviseHolderImpl_QueryInterface
84 static HRESULT WINAPI OleAdviseHolderImpl_QueryInterface(
85 LPOLEADVISEHOLDER iface,
86 REFIID riid,
87 LPVOID* ppvObj)
89 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
90 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObj);
92 * Sanity check
94 if (ppvObj==NULL)
95 return E_POINTER;
97 *ppvObj = NULL;
99 if (IsEqualIID(riid, &IID_IUnknown))
101 /* IUnknown */
102 *ppvObj = This;
104 else if(IsEqualIID(riid, &IID_IOleAdviseHolder))
106 /* IOleAdviseHolder */
107 *ppvObj = (IOleAdviseHolder*) This;
110 if(*ppvObj == NULL)
111 return E_NOINTERFACE;
114 * A successful QI always increments the reference count.
116 IUnknown_AddRef((IUnknown*)*ppvObj);
118 return S_OK;
121 /******************************************************************************
122 * OleAdviseHolderImpl_AddRef
124 static ULONG WINAPI OleAdviseHolderImpl_AddRef(
125 LPOLEADVISEHOLDER iface)
127 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
128 ULONG ref = InterlockedIncrement(&This->ref);
130 TRACE("(%p)->(ref=%ld)\n", This, ref - 1);
132 return ref;
135 /******************************************************************************
136 * OleAdviseHolderImpl_Release
138 static ULONG WINAPI OleAdviseHolderImpl_Release(
139 LPOLEADVISEHOLDER iface)
141 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
142 ULONG ref;
143 TRACE("(%p)->(ref=%ld)\n", This, This->ref);
144 ref = InterlockedDecrement(&This->ref);
146 if (ref == 0) OleAdviseHolderImpl_Destructor(This);
148 return ref;
151 /******************************************************************************
152 * OleAdviseHolderImpl_Advise
154 static HRESULT WINAPI OleAdviseHolderImpl_Advise(
155 LPOLEADVISEHOLDER iface,
156 IAdviseSink* pAdvise,
157 DWORD* pdwConnection)
159 DWORD index;
161 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
163 TRACE("(%p)->(%p, %p)\n", This, pAdvise, pdwConnection);
166 * Sanity check
168 if (pdwConnection==NULL)
169 return E_POINTER;
171 *pdwConnection = 0;
174 * Find a free spot in the array.
176 for (index = 0; index < This->maxSinks; index++)
178 if (This->arrayOfSinks[index]==NULL)
179 break;
183 * If the array is full, we need to grow it.
185 if (index == This->maxSinks)
187 DWORD i;
189 This->maxSinks+=INITIAL_SINKS;
191 This->arrayOfSinks = HeapReAlloc(GetProcessHeap(),
193 This->arrayOfSinks,
194 This->maxSinks*sizeof(IAdviseSink*));
196 for (i=index;i < This->maxSinks; i++)
197 This->arrayOfSinks[i]=0;
201 * Store the new sink
203 This->arrayOfSinks[index] = pAdvise;
205 if (This->arrayOfSinks[index]!=NULL)
206 IAdviseSink_AddRef(This->arrayOfSinks[index]);
209 * Return the index as the cookie.
210 * Since 0 is not a valid cookie, we will increment by
211 * 1 the index in the table.
213 *pdwConnection = index+1;
215 return S_OK;
218 /******************************************************************************
219 * OleAdviseHolderImpl_Unadvise
221 static HRESULT WINAPI OleAdviseHolderImpl_Unadvise(
222 LPOLEADVISEHOLDER iface,
223 DWORD dwConnection)
225 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
227 TRACE("(%p)->(%lu)\n", This, dwConnection);
230 * So we don't return 0 as a cookie, the index was
231 * incremented by 1 in OleAdviseHolderImpl_Advise
232 * we have to compensate.
234 dwConnection--;
237 * Check for invalid cookies.
239 if (dwConnection >= This->maxSinks)
240 return OLE_E_NOCONNECTION;
242 if (This->arrayOfSinks[dwConnection] == NULL)
243 return OLE_E_NOCONNECTION;
246 * Release the sink and mark the spot in the list as free.
248 IAdviseSink_Release(This->arrayOfSinks[dwConnection]);
249 This->arrayOfSinks[dwConnection] = NULL;
251 return S_OK;
254 /******************************************************************************
255 * OleAdviseHolderImpl_EnumAdvise
257 static HRESULT WINAPI
258 OleAdviseHolderImpl_EnumAdvise (LPOLEADVISEHOLDER iface, IEnumSTATDATA **ppenumAdvise)
260 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
261 FIXME("(%p)->(%p)\n", This, ppenumAdvise);
263 *ppenumAdvise = NULL;
265 return S_OK;
268 /******************************************************************************
269 * OleAdviseHolderImpl_SendOnRename
271 static HRESULT WINAPI
272 OleAdviseHolderImpl_SendOnRename (LPOLEADVISEHOLDER iface, IMoniker *pmk)
274 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
275 FIXME("(%p)->(%p)\n", This, pmk);
278 return S_OK;
281 /******************************************************************************
282 * OleAdviseHolderImpl_SendOnSave
284 static HRESULT WINAPI
285 OleAdviseHolderImpl_SendOnSave (LPOLEADVISEHOLDER iface)
287 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
288 FIXME("(%p)\n", This);
290 return S_OK;
293 /******************************************************************************
294 * OleAdviseHolderImpl_SendOnClose
296 static HRESULT WINAPI
297 OleAdviseHolderImpl_SendOnClose (LPOLEADVISEHOLDER iface)
299 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
300 FIXME("(%p)\n", This);
303 return S_OK;
306 /**************************************************************************
307 * OleAdviseHolderImpl_VTable
309 static const IOleAdviseHolderVtbl oahvt =
311 OleAdviseHolderImpl_QueryInterface,
312 OleAdviseHolderImpl_AddRef,
313 OleAdviseHolderImpl_Release,
314 OleAdviseHolderImpl_Advise,
315 OleAdviseHolderImpl_Unadvise,
316 OleAdviseHolderImpl_EnumAdvise,
317 OleAdviseHolderImpl_SendOnRename,
318 OleAdviseHolderImpl_SendOnSave,
319 OleAdviseHolderImpl_SendOnClose
322 /**************************************************************************
323 * OleAdviseHolderImpl_Constructor
326 static LPOLEADVISEHOLDER OleAdviseHolderImpl_Constructor(void)
328 OleAdviseHolderImpl* lpoah;
329 DWORD index;
331 lpoah = HeapAlloc(GetProcessHeap(), 0, sizeof(OleAdviseHolderImpl));
333 lpoah->lpVtbl = &oahvt;
334 lpoah->ref = 1;
335 lpoah->maxSinks = INITIAL_SINKS;
336 lpoah->arrayOfSinks = HeapAlloc(GetProcessHeap(),
338 lpoah->maxSinks * sizeof(IAdviseSink*));
340 for (index = 0; index < lpoah->maxSinks; index++)
341 lpoah->arrayOfSinks[index]=0;
343 TRACE("returning %p\n", lpoah);
344 return (LPOLEADVISEHOLDER)lpoah;
347 /**************************************************************************
348 * DataAdviseHolder Implementation
350 typedef struct DataAdviseConnection {
351 IAdviseSink *sink;
352 FORMATETC fmat;
353 DWORD advf;
354 } DataAdviseConnection;
356 typedef struct DataAdviseHolder
358 const IDataAdviseHolderVtbl *lpVtbl;
360 LONG ref;
361 DWORD maxCons;
362 DataAdviseConnection* Connections;
363 } DataAdviseHolder;
365 /******************************************************************************
366 * DataAdviseHolder_Destructor
368 static void DataAdviseHolder_Destructor(DataAdviseHolder* ptrToDestroy)
370 DWORD index;
371 TRACE("%p\n", ptrToDestroy);
373 for (index = 0; index < ptrToDestroy->maxCons; index++)
375 if (ptrToDestroy->Connections[index].sink != NULL)
377 IAdviseSink_Release(ptrToDestroy->Connections[index].sink);
378 ptrToDestroy->Connections[index].sink = NULL;
382 HeapFree(GetProcessHeap(), 0, ptrToDestroy->Connections);
383 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
386 /************************************************************************
387 * DataAdviseHolder_QueryInterface (IUnknown)
389 * See Windows documentation for more details on IUnknown methods.
391 static HRESULT WINAPI DataAdviseHolder_QueryInterface(
392 IDataAdviseHolder* iface,
393 REFIID riid,
394 void** ppvObject)
396 DataAdviseHolder *This = (DataAdviseHolder *)iface;
397 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
399 * Perform a sanity check on the parameters.
401 if ( (This==0) || (ppvObject==0) )
402 return E_INVALIDARG;
405 * Initialize the return parameter.
407 *ppvObject = 0;
410 * Compare the riid with the interface IDs implemented by this object.
412 if ( (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0) ||
413 (memcmp(&IID_IDataAdviseHolder, riid, sizeof(IID_IDataAdviseHolder)) == 0) )
415 *ppvObject = iface;
419 * Check that we obtained an interface.
421 if ((*ppvObject)==0)
423 return E_NOINTERFACE;
427 * Query Interface always increases the reference count by one when it is
428 * successful.
430 IUnknown_AddRef((IUnknown*)*ppvObject);
432 return S_OK;
435 /************************************************************************
436 * DataAdviseHolder_AddRef (IUnknown)
438 * See Windows documentation for more details on IUnknown methods.
440 static ULONG WINAPI DataAdviseHolder_AddRef(
441 IDataAdviseHolder* iface)
443 DataAdviseHolder *This = (DataAdviseHolder *)iface;
444 TRACE("(%p) (ref=%ld)\n", This, This->ref);
445 return InterlockedIncrement(&This->ref);
448 /************************************************************************
449 * DataAdviseHolder_Release (IUnknown)
451 * See Windows documentation for more details on IUnknown methods.
453 static ULONG WINAPI DataAdviseHolder_Release(
454 IDataAdviseHolder* iface)
456 DataAdviseHolder *This = (DataAdviseHolder *)iface;
457 ULONG ref;
458 TRACE("(%p) (ref=%ld)\n", This, This->ref);
461 * Decrease the reference count on this object.
463 ref = InterlockedDecrement(&This->ref);
466 * If the reference count goes down to 0, perform suicide.
468 if (ref==0) DataAdviseHolder_Destructor(This);
470 return ref;
473 /************************************************************************
474 * DataAdviseHolder_Advise
477 static HRESULT WINAPI DataAdviseHolder_Advise(
478 IDataAdviseHolder* iface,
479 IDataObject* pDataObject,
480 FORMATETC* pFetc,
481 DWORD advf,
482 IAdviseSink* pAdvise,
483 DWORD* pdwConnection)
485 DWORD index;
487 DataAdviseHolder *This = (DataAdviseHolder *)iface;
489 TRACE("(%p)->(%p, %p, %08lx, %p, %p)\n", This, pDataObject, pFetc, advf,
490 pAdvise, pdwConnection);
492 * Sanity check
494 if (pdwConnection==NULL)
495 return E_POINTER;
497 *pdwConnection = 0;
500 * Find a free spot in the array.
502 for (index = 0; index < This->maxCons; index++)
504 if (This->Connections[index].sink == NULL)
505 break;
509 * If the array is full, we need to grow it.
511 if (index == This->maxCons)
513 This->maxCons+=INITIAL_SINKS;
514 This->Connections = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
515 This->Connections,
516 This->maxCons*sizeof(DataAdviseConnection));
519 * Store the new sink
521 This->Connections[index].sink = pAdvise;
522 memcpy(&(This->Connections[index].fmat), pFetc, sizeof(FORMATETC));
523 This->Connections[index].advf = advf;
525 if (This->Connections[index].sink != NULL) {
526 IAdviseSink_AddRef(This->Connections[index].sink);
527 if(advf & ADVF_PRIMEFIRST) {
528 IDataAdviseHolder_SendOnDataChange(iface, pDataObject, 0, advf);
532 * Return the index as the cookie.
533 * Since 0 is not a valid cookie, we will increment by
534 * 1 the index in the table.
536 *pdwConnection = index+1;
538 return S_OK;
541 /******************************************************************************
542 * DataAdviseHolder_Unadvise
544 static HRESULT WINAPI DataAdviseHolder_Unadvise(
545 IDataAdviseHolder* iface,
546 DWORD dwConnection)
548 DataAdviseHolder *This = (DataAdviseHolder *)iface;
550 TRACE("(%p)->(%lu)\n", This, dwConnection);
553 * So we don't return 0 as a cookie, the index was
554 * incremented by 1 in OleAdviseHolderImpl_Advise
555 * we have to compensate.
557 dwConnection--;
560 * Check for invalid cookies.
562 if (dwConnection >= This->maxCons)
563 return OLE_E_NOCONNECTION;
565 if (This->Connections[dwConnection].sink == NULL)
566 return OLE_E_NOCONNECTION;
569 * Release the sink and mark the spot in the list as free.
571 IAdviseSink_Release(This->Connections[dwConnection].sink);
572 memset(&(This->Connections[dwConnection]), 0, sizeof(DataAdviseConnection));
573 return S_OK;
576 static HRESULT WINAPI DataAdviseHolder_EnumAdvise(
577 IDataAdviseHolder* iface,
578 IEnumSTATDATA** ppenumAdvise)
580 DataAdviseHolder *This = (DataAdviseHolder *)iface;
582 FIXME("(%p)->(%p)\n", This, ppenumAdvise);
583 return E_NOTIMPL;
586 /******************************************************************************
587 * DataAdviseHolder_SendOnDataChange
589 static HRESULT WINAPI DataAdviseHolder_SendOnDataChange(
590 IDataAdviseHolder* iface,
591 IDataObject* pDataObject,
592 DWORD dwReserved,
593 DWORD advf)
595 DataAdviseHolder *This = (DataAdviseHolder *)iface;
596 DWORD index;
597 STGMEDIUM stg;
598 HRESULT res;
600 TRACE("(%p)->(%p,%08lx,%08lx)\n", This, pDataObject, dwReserved, advf);
602 for(index = 0; index < This->maxCons; index++) {
603 if(This->Connections[index].sink != NULL) {
604 if(!(This->Connections[index].advf & ADVF_NODATA)) {
605 TRACE("Calling IDataObject_GetData\n");
606 res = IDataObject_GetData(pDataObject,
607 &(This->Connections[index].fmat),
608 &stg);
609 TRACE("returns %08lx\n", res);
611 TRACE("Calling IAdviseSink_OnDataChange\n");
612 IAdviseSink_OnDataChange(This->Connections[index].sink,
613 &(This->Connections[index].fmat),
614 &stg);
615 TRACE("Done IAdviseSink_OnDataChange\n");
616 if(This->Connections[index].advf & ADVF_ONLYONCE) {
617 TRACE("Removing connection\n");
618 DataAdviseHolder_Unadvise(iface, index+1);
622 return S_OK;
625 /**************************************************************************
626 * DataAdviseHolderImpl_VTable
628 static const IDataAdviseHolderVtbl DataAdviseHolderImpl_VTable =
630 DataAdviseHolder_QueryInterface,
631 DataAdviseHolder_AddRef,
632 DataAdviseHolder_Release,
633 DataAdviseHolder_Advise,
634 DataAdviseHolder_Unadvise,
635 DataAdviseHolder_EnumAdvise,
636 DataAdviseHolder_SendOnDataChange
639 /******************************************************************************
640 * DataAdviseHolder_Constructor
642 static IDataAdviseHolder* DataAdviseHolder_Constructor(void)
644 DataAdviseHolder* newHolder;
646 newHolder = HeapAlloc(GetProcessHeap(), 0, sizeof(DataAdviseHolder));
648 newHolder->lpVtbl = &DataAdviseHolderImpl_VTable;
649 newHolder->ref = 1;
650 newHolder->maxCons = INITIAL_SINKS;
651 newHolder->Connections = HeapAlloc(GetProcessHeap(),
652 HEAP_ZERO_MEMORY,
653 newHolder->maxCons *
654 sizeof(DataAdviseConnection));
656 TRACE("returning %p\n", newHolder);
657 return (IDataAdviseHolder*)newHolder;
660 /***********************************************************************
661 * API functions
664 /***********************************************************************
665 * CreateOleAdviseHolder [OLE32.@]
667 HRESULT WINAPI CreateOleAdviseHolder(
668 LPOLEADVISEHOLDER *ppOAHolder)
670 TRACE("(%p)\n", ppOAHolder);
673 * Sanity check,
675 if (ppOAHolder==NULL)
676 return E_POINTER;
678 *ppOAHolder = OleAdviseHolderImpl_Constructor ();
680 if (*ppOAHolder != NULL)
681 return S_OK;
683 return E_OUTOFMEMORY;
686 /******************************************************************************
687 * CreateDataAdviseHolder [OLE32.@]
689 HRESULT WINAPI CreateDataAdviseHolder(
690 LPDATAADVISEHOLDER* ppDAHolder)
692 TRACE("(%p)\n", ppDAHolder);
695 * Sanity check,
697 if (ppDAHolder==NULL)
698 return E_POINTER;
700 *ppDAHolder = DataAdviseHolder_Constructor();
702 if (*ppDAHolder != NULL)
703 return S_OK;
705 return E_OUTOFMEMORY;