Get rid of the no longer used ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
[wine/multimedia.git] / dlls / ole32 / bindctx.c
blobba737c731033ccdb53157b970cd23256582faf61
1 /***************************************************************************************
2 * BindCtx implementation
4 * Copyright 1999 Noomen Hamza
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************************/
21 #include <stdarg.h>
22 #include <string.h>
23 #include <assert.h>
24 #include "winerror.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/unicode.h"
28 #include "objbase.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ole);
33 /* represent the first size table and it's increment block size */
34 #define BLOCK_TAB_SIZE 10
35 #define MAX_TAB_SIZE 0xFFFFFFFF
37 /* data structure of the BindCtx table elements */
38 typedef struct BindCtxObject{
40 IUnknown* pObj; /* point on a bound object */
42 LPOLESTR pkeyObj; /* key associated to this bound object */
44 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
46 } BindCtxObject;
48 /* BindCtx data strucrture */
49 typedef struct BindCtxImpl{
51 IBindCtxVtbl *lpVtbl; /* VTable relative to the IBindCtx interface.*/
53 ULONG ref; /* reference counter for this object */
55 BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
56 DWORD bindCtxTableLastIndex; /* first free index in the table */
57 DWORD bindCtxTableSize; /* size table */
59 BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
61 } BindCtxImpl;
63 /* IBindCtx prototype functions : */
65 /* IUnknown functions*/
66 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
67 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
68 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
69 /* IBindCtx functions */
70 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
71 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
72 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
73 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
74 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
75 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
76 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
77 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
78 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
79 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
80 /* Local functions*/
81 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
82 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
83 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
85 /* Virtual function table for the BindCtx class. */
86 static IBindCtxVtbl VT_BindCtxImpl =
88 BindCtxImpl_QueryInterface,
89 BindCtxImpl_AddRef,
90 BindCtxImpl_Release,
91 BindCtxImpl_RegisterObjectBound,
92 BindCtxImpl_RevokeObjectBound,
93 BindCtxImpl_ReleaseBoundObjects,
94 BindCtxImpl_SetBindOptions,
95 BindCtxImpl_GetBindOptions,
96 BindCtxImpl_GetRunningObjectTable,
97 BindCtxImpl_RegisterObjectParam,
98 BindCtxImpl_GetObjectParam,
99 BindCtxImpl_EnumObjectParam,
100 BindCtxImpl_RevokeObjectParam
103 /*******************************************************************************
104 * BindCtx_QueryInterface
105 *******************************************************************************/
106 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
108 ICOM_THIS(BindCtxImpl,iface);
110 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
112 /* Perform a sanity check on the parameters.*/
113 if ( (This==0) || (ppvObject==0) )
114 return E_INVALIDARG;
116 /* Initialize the return parameter.*/
117 *ppvObject = 0;
119 /* Compare the riid with the interface IDs implemented by this object.*/
120 if (IsEqualIID(&IID_IUnknown, riid))
121 *ppvObject = (IBindCtx*)This;
122 else
123 if (IsEqualIID(&IID_IBindCtx, riid))
124 *ppvObject = (IBindCtx*)This;
126 /* Check that we obtained an interface.*/
127 if ((*ppvObject)==0)
128 return E_NOINTERFACE;
130 /* Query Interface always increases the reference count by one when it is successful */
131 BindCtxImpl_AddRef(iface);
133 return S_OK;
136 /******************************************************************************
137 * BindCtx_AddRef
138 ******************************************************************************/
139 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
141 ICOM_THIS(BindCtxImpl,iface);
143 TRACE("(%p)\n",This);
145 return ++(This->ref);
148 /******************************************************************************
149 * BindCtx_Release
150 ******************************************************************************/
151 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
153 ICOM_THIS(BindCtxImpl,iface);
155 TRACE("(%p)\n",This);
157 This->ref--;
159 if (This->ref==0){
161 /* release all registered objects */
162 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
164 BindCtxImpl_Destroy(This);
166 return 0;
168 return This->ref;
172 /******************************************************************************
173 * BindCtx_Construct (local function)
174 *******************************************************************************/
175 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
177 TRACE("(%p)\n",This);
179 /* Initialize the virtual function table.*/
180 This->lpVtbl = &VT_BindCtxImpl;
181 This->ref = 0;
183 /* Initialize the BIND_OPTS2 structure */
184 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
185 This->bindOption2.grfFlags = 0;
186 This->bindOption2.grfMode = STGM_READWRITE;
187 This->bindOption2.dwTickCountDeadline = 0;
189 This->bindOption2.dwTrackFlags = 0;
190 This->bindOption2.dwClassContext = CLSCTX_SERVER;
191 This->bindOption2.locale = 1033;
192 This->bindOption2.pServerInfo = 0;
194 /* Initialize the bindctx table */
195 This->bindCtxTableSize=BLOCK_TAB_SIZE;
196 This->bindCtxTableLastIndex=0;
197 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
199 if (This->bindCtxTable==NULL)
200 return E_OUTOFMEMORY;
202 return S_OK;
205 /******************************************************************************
206 * BindCtx_Destroy (local function)
207 *******************************************************************************/
208 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
210 TRACE("(%p)\n",This);
212 /* free the table space memory */
213 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
215 /* free the bindctx structure */
216 HeapFree(GetProcessHeap(),0,This);
218 return S_OK;
222 /******************************************************************************
223 * BindCtx_RegisterObjectBound
224 ******************************************************************************/
225 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
227 ICOM_THIS(BindCtxImpl,iface);
228 DWORD lastIndex=This->bindCtxTableLastIndex;
230 TRACE("(%p,%p)\n",This,punk);
232 if (punk==NULL)
233 return E_POINTER;
235 IUnknown_AddRef(punk);
237 /* put the object in the first free element in the table */
238 This->bindCtxTable[lastIndex].pObj = punk;
239 This->bindCtxTable[lastIndex].pkeyObj = NULL;
240 This->bindCtxTable[lastIndex].regType = 0;
241 lastIndex= ++This->bindCtxTableLastIndex;
243 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
245 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
246 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
247 return E_FAIL;
250 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
252 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
253 This->bindCtxTableSize * sizeof(BindCtxObject));
254 if (!This->bindCtxTable)
255 return E_OUTOFMEMORY;
257 return S_OK;
260 /******************************************************************************
261 * BindCtx_RevokeObjectBound
262 ******************************************************************************/
263 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
265 DWORD index,j;
267 ICOM_THIS(BindCtxImpl,iface);
269 TRACE("(%p,%p)\n",This,punk);
271 /* check if the object was registered or not */
272 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
273 return MK_E_NOTBOUND;
275 if(This->bindCtxTable[index].pObj)
276 IUnknown_Release(This->bindCtxTable[index].pObj);
277 if(This->bindCtxTable[index].pkeyObj)
278 HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
280 /* left-shift all elements in the right side of the current revoked object */
281 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
282 This->bindCtxTable[j]= This->bindCtxTable[j+1];
284 This->bindCtxTableLastIndex--;
286 return S_OK;
289 /******************************************************************************
290 * BindCtx_ReleaseBoundObjects
291 ******************************************************************************/
292 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
294 DWORD i;
296 ICOM_THIS(BindCtxImpl,iface);
298 TRACE("(%p)\n",This);
300 for(i=0;i<This->bindCtxTableLastIndex;i++)
302 if(This->bindCtxTable[i].pObj)
303 IUnknown_Release(This->bindCtxTable[i].pObj);
304 if(This->bindCtxTable[i].pkeyObj)
305 HeapFree(GetProcessHeap(),0,This->bindCtxTable[i].pkeyObj);
308 This->bindCtxTableLastIndex = 0;
310 return S_OK;
313 /******************************************************************************
314 * BindCtx_SetBindOptions
315 ******************************************************************************/
316 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
318 ICOM_THIS(BindCtxImpl,iface);
320 TRACE("(%p,%p)\n",This,pbindopts);
322 if (pbindopts==NULL)
323 return E_POINTER;
325 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
327 WARN("invalid size\n");
328 return E_INVALIDARG; /* FIXME : not verified */
330 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
331 return S_OK;
334 /******************************************************************************
335 * BindCtx_GetBindOptions
336 ******************************************************************************/
337 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
339 ICOM_THIS(BindCtxImpl,iface);
341 TRACE("(%p,%p)\n",This,pbindopts);
343 if (pbindopts==NULL)
344 return E_POINTER;
346 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
348 WARN("invalid size\n");
349 return E_INVALIDARG; /* FIXME : not verified */
351 memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
352 return S_OK;
355 /******************************************************************************
356 * BindCtx_GetRunningObjectTable
357 ******************************************************************************/
358 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
360 HRESULT res;
362 ICOM_THIS(BindCtxImpl,iface);
364 TRACE("(%p,%p)\n",This,pprot);
366 if (pprot==NULL)
367 return E_POINTER;
369 res=GetRunningObjectTable(0, pprot);
371 return res;
374 /******************************************************************************
375 * BindCtx_RegisterObjectParam
376 ******************************************************************************/
377 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
379 DWORD index=0;
380 ICOM_THIS(BindCtxImpl,iface);
382 TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
384 if (punk==NULL)
385 return E_INVALIDARG;
387 if (pszkey!=NULL && BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_OK)
389 TRACE("Overwriting existing key\n");
390 if(This->bindCtxTable[index].pObj!=NULL)
391 IUnknown_Release(This->bindCtxTable[index].pObj);
392 This->bindCtxTable[index].pObj=punk;
393 IUnknown_AddRef(punk);
394 return S_OK;
396 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
397 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
399 if (pszkey==NULL)
401 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
403 else{
405 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
406 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
408 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
409 return E_OUTOFMEMORY;
410 strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
413 This->bindCtxTableLastIndex++;
415 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
417 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
419 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
420 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
421 return E_FAIL;
423 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
424 This->bindCtxTableSize * sizeof(BindCtxObject));
425 if (!This->bindCtxTable)
426 return E_OUTOFMEMORY;
428 IUnknown_AddRef(punk);
429 return S_OK;
432 /******************************************************************************
433 * BindCtx_GetObjectParam
434 ******************************************************************************/
435 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
437 DWORD index;
438 ICOM_THIS(BindCtxImpl,iface);
440 TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
442 if (punk==NULL)
443 return E_POINTER;
445 *punk=0;
447 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
448 return E_FAIL;
450 IUnknown_AddRef(This->bindCtxTable[index].pObj);
452 *punk = This->bindCtxTable[index].pObj;
454 return S_OK;
457 /******************************************************************************
458 * BindCtx_RevokeObjectParam
459 ******************************************************************************/
460 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
462 DWORD index,j;
464 ICOM_THIS(BindCtxImpl,iface);
466 TRACE("(%p,%s)\n",This,debugstr_w(ppenum));
468 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
469 return E_FAIL;
471 /* release the object if it's found */
472 if(This->bindCtxTable[index].pObj)
473 IUnknown_Release(This->bindCtxTable[index].pObj);
474 if(This->bindCtxTable[index].pkeyObj)
475 HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
477 /* remove the object from the table with a left-shifting of all objects in the right side */
478 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
479 This->bindCtxTable[j]= This->bindCtxTable[j+1];
481 This->bindCtxTableLastIndex--;
483 return S_OK;
486 /******************************************************************************
487 * BindCtx_EnumObjectParam
488 ******************************************************************************/
489 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
491 FIXME("(%p,%p),stub!\n",iface,pszkey);
492 return E_NOTIMPL;
495 /********************************************************************************
496 * GetObjectIndex (local function)
497 ********************************************************************************/
498 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
499 IUnknown* punk,
500 LPOLESTR pszkey,
501 DWORD *index)
504 DWORD i;
505 BYTE found=0;
507 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
509 if (punk==NULL)
510 /* search object identified by a register key */
511 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
513 if(This->bindCtxTable[i].regType==1){
515 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
516 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
517 (pszkey!=NULL) &&
518 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
522 found=1;
525 else
526 /* search object identified by a moniker*/
527 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
528 if(This->bindCtxTable[i].pObj==punk)
529 found=1;
531 if (index != NULL)
532 *index=i-1;
534 if (found)
535 return S_OK;
536 TRACE("key not found\n");
537 return S_FALSE;
540 /******************************************************************************
541 * CreateBindCtx16
542 ******************************************************************************/
543 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
545 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
546 return E_NOTIMPL;
549 /******************************************************************************
550 * CreateBindCtx (OLE32.@)
551 ******************************************************************************/
552 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
554 BindCtxImpl* newBindCtx = 0;
555 HRESULT hr;
556 IID riid=IID_IBindCtx;
558 TRACE("(%ld,%p)\n",reserved,ppbc);
560 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
562 if (newBindCtx == 0)
563 return E_OUTOFMEMORY;
565 hr = BindCtxImpl_Construct(newBindCtx);
567 if (FAILED(hr)){
569 HeapFree(GetProcessHeap(),0,newBindCtx);
570 return hr;
573 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
575 return hr;
578 HRESULT WINAPI BindMoniker(LPMONIKER pmk, DWORD grfOpt, REFIID riid, LPVOID * ppvResult)
580 HRESULT res;
581 IBindCtx * pbc;
583 TRACE("(%p, %lx, %s, %p)\n", pmk, grfOpt, debugstr_guid(riid), ppvResult);
585 res = CreateBindCtx(grfOpt, &pbc);
586 if (SUCCEEDED(res))
587 res = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppvResult);
588 return res;