Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ole32 / bindctx.c
blob1492393b57f6e8806f5783cfe256836bdf0cfba3
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
24 #define COBJMACROS
26 #include "winerror.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "objbase.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ole);
36 #define BINDCTX_FIRST_TABLE_SIZE 4
38 /* data structure of the BindCtx table elements */
39 typedef struct BindCtxObject{
41 IUnknown* pObj; /* point on a bound object */
43 LPOLESTR pkeyObj; /* key associated to this bound object */
45 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
47 } BindCtxObject;
49 /* BindCtx data structure */
50 typedef struct BindCtxImpl{
52 const IBindCtxVtbl *lpVtbl; /* VTable relative to the IBindCtx interface.*/
54 LONG ref; /* reference counter for this object */
56 BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
57 DWORD bindCtxTableLastIndex; /* first free index in the table */
58 DWORD bindCtxTableSize; /* size table */
60 BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
62 } BindCtxImpl;
64 /* IBindCtx prototype functions : */
65 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx*);
66 static HRESULT BindCtxImpl_GetObjectIndex(BindCtxImpl*, IUnknown*, LPOLESTR, DWORD *);
67 static HRESULT BindCtxImpl_ExpandTable(BindCtxImpl *);
69 /*******************************************************************************
70 * BindCtx_QueryInterface
71 *******************************************************************************/
72 static HRESULT WINAPI
73 BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
75 BindCtxImpl *This = (BindCtxImpl *)iface;
77 TRACE("(%p %s %p)\n",This, debugstr_guid(riid), ppvObject);
79 /* Perform a sanity check on the parameters.*/
80 if (!ppvObject)
81 return E_POINTER;
83 /* Initialize the return parameter.*/
84 *ppvObject = 0;
86 /* Compare the riid with the interface IDs implemented by this object.*/
87 if (IsEqualIID(&IID_IUnknown, riid) ||
88 IsEqualIID(&IID_IBindCtx, riid))
90 *ppvObject = This;
91 IBindCtx_AddRef(iface);
92 return S_OK;
95 return E_NOINTERFACE;
98 /******************************************************************************
99 * BindCtx_AddRef
100 ******************************************************************************/
101 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
103 BindCtxImpl *This = (BindCtxImpl *)iface;
105 TRACE("(%p)\n",This);
107 return InterlockedIncrement(&This->ref);
110 /******************************************************************************
111 * BindCtx_Destroy (local function)
112 *******************************************************************************/
113 static HRESULT BindCtxImpl_Destroy(BindCtxImpl* This)
115 TRACE("(%p)\n",This);
117 /* free the table space memory */
118 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
120 /* free the bindctx structure */
121 HeapFree(GetProcessHeap(),0,This);
123 return S_OK;
126 /******************************************************************************
127 * BindCtx_Release
128 ******************************************************************************/
129 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
131 BindCtxImpl *This = (BindCtxImpl *)iface;
132 ULONG ref;
134 TRACE("(%p)\n",This);
136 ref = InterlockedDecrement(&This->ref);
137 if (ref == 0)
139 /* release all registered objects */
140 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
142 BindCtxImpl_Destroy(This);
144 return ref;
148 /******************************************************************************
149 * BindCtx_RegisterObjectBound
150 ******************************************************************************/
151 static HRESULT WINAPI
152 BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
154 BindCtxImpl *This = (BindCtxImpl *)iface;
155 DWORD lastIndex=This->bindCtxTableLastIndex;
157 TRACE("(%p,%p)\n",This,punk);
159 if (punk==NULL)
160 return S_OK;
162 if (lastIndex == This->bindCtxTableSize)
164 HRESULT hr = BindCtxImpl_ExpandTable(This);
165 if (FAILED(hr))
166 return hr;
169 IUnknown_AddRef(punk);
171 /* put the object in the first free element in the table */
172 This->bindCtxTable[lastIndex].pObj = punk;
173 This->bindCtxTable[lastIndex].pkeyObj = NULL;
174 This->bindCtxTable[lastIndex].regType = 0;
175 lastIndex= ++This->bindCtxTableLastIndex;
177 return S_OK;
180 /******************************************************************************
181 * BindCtx_RevokeObjectBound
182 ******************************************************************************/
183 static HRESULT WINAPI
184 BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
186 DWORD index,j;
188 BindCtxImpl *This = (BindCtxImpl *)iface;
190 TRACE("(%p,%p)\n",This,punk);
192 if (!punk)
193 return E_INVALIDARG;
195 /* check if the object was registered or not */
196 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
197 return MK_E_NOTBOUND;
199 if(This->bindCtxTable[index].pObj)
200 IUnknown_Release(This->bindCtxTable[index].pObj);
201 HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
203 /* left-shift all elements in the right side of the current revoked object */
204 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
205 This->bindCtxTable[j]= This->bindCtxTable[j+1];
207 This->bindCtxTableLastIndex--;
209 return S_OK;
212 /******************************************************************************
213 * BindCtx_ReleaseBoundObjects
214 ******************************************************************************/
215 static HRESULT WINAPI
216 BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
218 DWORD i;
220 BindCtxImpl *This = (BindCtxImpl *)iface;
222 TRACE("(%p)\n",This);
224 for(i=0;i<This->bindCtxTableLastIndex;i++)
226 if(This->bindCtxTable[i].pObj)
227 IUnknown_Release(This->bindCtxTable[i].pObj);
228 HeapFree(GetProcessHeap(),0,This->bindCtxTable[i].pkeyObj);
231 This->bindCtxTableLastIndex = 0;
233 return S_OK;
236 /******************************************************************************
237 * BindCtx_SetBindOptions
238 ******************************************************************************/
239 static HRESULT WINAPI
240 BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
242 BindCtxImpl *This = (BindCtxImpl *)iface;
244 TRACE("(%p,%p)\n",This,pbindopts);
246 if (pbindopts==NULL)
247 return E_POINTER;
249 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
251 WARN("invalid size\n");
252 return E_INVALIDARG; /* FIXME : not verified */
254 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
255 return S_OK;
258 /******************************************************************************
259 * BindCtx_GetBindOptions
260 ******************************************************************************/
261 static HRESULT WINAPI
262 BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts)
264 BindCtxImpl *This = (BindCtxImpl *)iface;
265 ULONG cbStruct;
267 TRACE("(%p,%p)\n",This,pbindopts);
269 if (pbindopts==NULL)
270 return E_POINTER;
272 cbStruct = pbindopts->cbStruct;
273 if (cbStruct > sizeof(BIND_OPTS2))
274 cbStruct = sizeof(BIND_OPTS2);
276 memcpy(pbindopts, &This->bindOption2, cbStruct);
277 pbindopts->cbStruct = cbStruct;
279 return S_OK;
282 /******************************************************************************
283 * BindCtx_GetRunningObjectTable
284 ******************************************************************************/
285 static HRESULT WINAPI
286 BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
288 BindCtxImpl *This = (BindCtxImpl *)iface;
290 TRACE("(%p,%p)\n",This,pprot);
292 if (pprot==NULL)
293 return E_POINTER;
295 return GetRunningObjectTable(0, pprot);
298 /******************************************************************************
299 * BindCtx_RegisterObjectParam
300 ******************************************************************************/
301 static HRESULT WINAPI
302 BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
304 DWORD index=0;
305 BindCtxImpl *This = (BindCtxImpl *)iface;
307 TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
309 if (punk==NULL)
310 return E_INVALIDARG;
312 if (pszkey!=NULL && BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_OK)
314 TRACE("Overwriting existing key\n");
315 if(This->bindCtxTable[index].pObj!=NULL)
316 IUnknown_Release(This->bindCtxTable[index].pObj);
317 This->bindCtxTable[index].pObj=punk;
318 IUnknown_AddRef(punk);
319 return S_OK;
322 if (This->bindCtxTableLastIndex == This->bindCtxTableSize)
324 HRESULT hr = BindCtxImpl_ExpandTable(This);
325 if (FAILED(hr))
326 return hr;
329 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
330 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
332 if (pszkey==NULL)
334 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
336 else
339 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
340 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
342 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
343 return E_OUTOFMEMORY;
344 lstrcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
347 This->bindCtxTableLastIndex++;
349 IUnknown_AddRef(punk);
350 return S_OK;
353 /******************************************************************************
354 * BindCtx_GetObjectParam
355 ******************************************************************************/
356 static HRESULT WINAPI
357 BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
359 DWORD index;
360 BindCtxImpl *This = (BindCtxImpl *)iface;
362 TRACE("(%p,%s,%p)\n",This,debugstr_w(pszkey),punk);
364 if (punk==NULL)
365 return E_POINTER;
367 *punk=0;
369 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
370 return E_FAIL;
372 IUnknown_AddRef(This->bindCtxTable[index].pObj);
374 *punk = This->bindCtxTable[index].pObj;
376 return S_OK;
379 /******************************************************************************
380 * BindCtx_RevokeObjectParam
381 ******************************************************************************/
382 static HRESULT WINAPI
383 BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
385 DWORD index,j;
387 BindCtxImpl *This = (BindCtxImpl *)iface;
389 TRACE("(%p,%s)\n",This,debugstr_w(ppenum));
391 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
392 return E_FAIL;
394 /* release the object if it's found */
395 if(This->bindCtxTable[index].pObj)
396 IUnknown_Release(This->bindCtxTable[index].pObj);
397 HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
399 /* remove the object from the table with a left-shifting of all objects in the right side */
400 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
401 This->bindCtxTable[j]= This->bindCtxTable[j+1];
403 This->bindCtxTableLastIndex--;
405 return S_OK;
408 /******************************************************************************
409 * BindCtx_EnumObjectParam
410 ******************************************************************************/
411 static HRESULT WINAPI
412 BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
414 TRACE("(%p,%p)\n",iface,pszkey);
416 *pszkey = NULL;
418 /* not implemented in native either */
419 return E_NOTIMPL;
422 /********************************************************************************
423 * GetObjectIndex (local function)
424 ********************************************************************************/
425 static HRESULT BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
426 IUnknown* punk,
427 LPOLESTR pszkey,
428 DWORD *index)
431 DWORD i;
432 BYTE found=0;
434 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
436 if (punk==NULL)
437 /* search object identified by a register key */
438 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
440 if(This->bindCtxTable[i].regType==1){
442 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
443 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
444 (pszkey!=NULL) &&
445 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
449 found=1;
452 else
453 /* search object identified by a moniker*/
454 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
455 if(This->bindCtxTable[i].pObj==punk)
456 found=1;
458 if (index != NULL)
459 *index=i-1;
461 if (found)
462 return S_OK;
463 TRACE("key not found\n");
464 return S_FALSE;
467 static HRESULT BindCtxImpl_ExpandTable(BindCtxImpl *This)
469 if (!This->bindCtxTableSize)
471 This->bindCtxTableSize = BINDCTX_FIRST_TABLE_SIZE;
472 This->bindCtxTable = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
473 This->bindCtxTableSize * sizeof(BindCtxObject));
475 else
477 This->bindCtxTableSize *= 2;
479 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
480 This->bindCtxTableSize * sizeof(BindCtxObject));
483 if (!This->bindCtxTable)
484 return E_OUTOFMEMORY;
486 return S_OK;
490 /* Virtual function table for the BindCtx class. */
491 static const IBindCtxVtbl VT_BindCtxImpl =
493 BindCtxImpl_QueryInterface,
494 BindCtxImpl_AddRef,
495 BindCtxImpl_Release,
496 BindCtxImpl_RegisterObjectBound,
497 BindCtxImpl_RevokeObjectBound,
498 BindCtxImpl_ReleaseBoundObjects,
499 BindCtxImpl_SetBindOptions,
500 BindCtxImpl_GetBindOptions,
501 BindCtxImpl_GetRunningObjectTable,
502 BindCtxImpl_RegisterObjectParam,
503 BindCtxImpl_GetObjectParam,
504 BindCtxImpl_EnumObjectParam,
505 BindCtxImpl_RevokeObjectParam
508 /******************************************************************************
509 * BindCtx_Construct (local function)
510 *******************************************************************************/
511 static HRESULT BindCtxImpl_Construct(BindCtxImpl* This)
513 TRACE("(%p)\n",This);
515 /* Initialize the virtual function table.*/
516 This->lpVtbl = &VT_BindCtxImpl;
517 This->ref = 0;
519 /* Initialize the BIND_OPTS2 structure */
520 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
521 This->bindOption2.grfFlags = 0;
522 This->bindOption2.grfMode = STGM_READWRITE;
523 This->bindOption2.dwTickCountDeadline = 0;
525 This->bindOption2.dwTrackFlags = 0;
526 This->bindOption2.dwClassContext = CLSCTX_SERVER;
527 This->bindOption2.locale = GetThreadLocale();
528 This->bindOption2.pServerInfo = 0;
530 /* Initialize the bindctx table */
531 This->bindCtxTableSize=0;
532 This->bindCtxTableLastIndex=0;
533 This->bindCtxTable = NULL;
535 return S_OK;
538 /******************************************************************************
539 * CreateBindCtx (OLE32.@)
541 * Creates a bind context. A bind context encompasses information and options
542 * used when binding to a moniker.
544 * PARAMS
545 * reserved [I] Reserved. Set to 0.
546 * ppbc [O] Address that receives the bind context object.
548 * RETURNS
549 * Success: S_OK.
550 * Failure: Any HRESULT code.
552 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
554 BindCtxImpl* newBindCtx;
555 HRESULT hr;
557 TRACE("(%d,%p)\n",reserved,ppbc);
559 if (!ppbc) return E_INVALIDARG;
561 *ppbc = NULL;
563 if (reserved != 0)
565 ERR("reserved should be 0, not 0x%x\n", reserved);
566 return E_INVALIDARG;
569 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
570 if (newBindCtx == 0)
571 return E_OUTOFMEMORY;
573 hr = BindCtxImpl_Construct(newBindCtx);
574 if (FAILED(hr))
576 HeapFree(GetProcessHeap(),0,newBindCtx);
577 return hr;
580 return BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&IID_IBindCtx,(void**)ppbc);
583 /******************************************************************************
584 * BindMoniker [OLE32.@]
586 * Binds to a moniker.
588 * PARAMS
589 * pmk [I] Moniker to bind to.
590 * grfOpt [I] Reserved option flags. Set to 0.
591 * riid [I] ID of the interface to bind to.
592 * pvResult [O] Address that receives the interface of the object that was bound to.
594 * RETURNS
595 * Success: S_OK.
596 * Failure: Any HRESULT code.
598 HRESULT WINAPI BindMoniker(LPMONIKER pmk, DWORD grfOpt, REFIID riid, LPVOID * ppvResult)
600 HRESULT res;
601 IBindCtx * pbc;
603 TRACE("(%p, %x, %s, %p)\n", pmk, grfOpt, debugstr_guid(riid), ppvResult);
605 res = CreateBindCtx(grfOpt, &pbc);
606 if (SUCCEEDED(res))
608 res = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppvResult);
609 IBindCtx_Release(pbc);
611 return res;