Do not check for non NULL pointer before HeapFree'ing it. It's
[wine.git] / dlls / ole32 / bindctx.c
blobe13d2a9a942d6dd9d2998b2f8c371e71eced5f56
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>
25 #define COBJMACROS
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/unicode.h"
31 #include "objbase.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ole);
36 /* represent the first size table and it's increment block size */
37 #define BLOCK_TAB_SIZE 10
38 #define MAX_TAB_SIZE 0xFFFFFFFF
40 /* data structure of the BindCtx table elements */
41 typedef struct BindCtxObject{
43 IUnknown* pObj; /* point on a bound object */
45 LPOLESTR pkeyObj; /* key associated to this bound object */
47 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
49 } BindCtxObject;
51 /* BindCtx data strucrture */
52 typedef struct BindCtxImpl{
54 IBindCtxVtbl *lpVtbl; /* VTable relative to the IBindCtx interface.*/
56 ULONG ref; /* reference counter for this object */
58 BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
59 DWORD bindCtxTableLastIndex; /* first free index in the table */
60 DWORD bindCtxTableSize; /* size table */
62 BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
64 } BindCtxImpl;
66 /* IBindCtx prototype functions : */
68 /* IUnknown functions*/
69 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
70 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
71 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
72 /* IBindCtx functions */
73 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
74 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
75 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
76 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
77 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,BIND_OPTS *pbindopts);
78 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
79 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
80 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
81 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
82 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
83 /* Local functions*/
84 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
85 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
86 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
88 /* Virtual function table for the BindCtx class. */
89 static IBindCtxVtbl VT_BindCtxImpl =
91 BindCtxImpl_QueryInterface,
92 BindCtxImpl_AddRef,
93 BindCtxImpl_Release,
94 BindCtxImpl_RegisterObjectBound,
95 BindCtxImpl_RevokeObjectBound,
96 BindCtxImpl_ReleaseBoundObjects,
97 BindCtxImpl_SetBindOptions,
98 BindCtxImpl_GetBindOptions,
99 BindCtxImpl_GetRunningObjectTable,
100 BindCtxImpl_RegisterObjectParam,
101 BindCtxImpl_GetObjectParam,
102 BindCtxImpl_EnumObjectParam,
103 BindCtxImpl_RevokeObjectParam
106 /*******************************************************************************
107 * BindCtx_QueryInterface
108 *******************************************************************************/
109 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
111 BindCtxImpl *This = (BindCtxImpl *)iface;
113 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
115 /* Perform a sanity check on the parameters.*/
116 if ( (This==0) || (ppvObject==0) )
117 return E_INVALIDARG;
119 /* Initialize the return parameter.*/
120 *ppvObject = 0;
122 /* Compare the riid with the interface IDs implemented by this object.*/
123 if (IsEqualIID(&IID_IUnknown, riid))
124 *ppvObject = (IBindCtx*)This;
125 else
126 if (IsEqualIID(&IID_IBindCtx, riid))
127 *ppvObject = (IBindCtx*)This;
129 /* Check that we obtained an interface.*/
130 if ((*ppvObject)==0)
131 return E_NOINTERFACE;
133 /* Query Interface always increases the reference count by one when it is successful */
134 BindCtxImpl_AddRef(iface);
136 return S_OK;
139 /******************************************************************************
140 * BindCtx_AddRef
141 ******************************************************************************/
142 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
144 BindCtxImpl *This = (BindCtxImpl *)iface;
146 TRACE("(%p)\n",This);
148 return InterlockedIncrement(&This->ref);
151 /******************************************************************************
152 * BindCtx_Release
153 ******************************************************************************/
154 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
156 BindCtxImpl *This = (BindCtxImpl *)iface;
157 ULONG ref;
159 TRACE("(%p)\n",This);
161 ref = InterlockedDecrement(&This->ref);
163 if (ref == 0){
164 /* release all registered objects */
165 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
167 BindCtxImpl_Destroy(This);
169 return ref;
173 /******************************************************************************
174 * BindCtx_Construct (local function)
175 *******************************************************************************/
176 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
178 TRACE("(%p)\n",This);
180 /* Initialize the virtual function table.*/
181 This->lpVtbl = &VT_BindCtxImpl;
182 This->ref = 0;
184 /* Initialize the BIND_OPTS2 structure */
185 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
186 This->bindOption2.grfFlags = 0;
187 This->bindOption2.grfMode = STGM_READWRITE;
188 This->bindOption2.dwTickCountDeadline = 0;
190 This->bindOption2.dwTrackFlags = 0;
191 This->bindOption2.dwClassContext = CLSCTX_SERVER;
192 This->bindOption2.locale = 1033;
193 This->bindOption2.pServerInfo = 0;
195 /* Initialize the bindctx table */
196 This->bindCtxTableSize=BLOCK_TAB_SIZE;
197 This->bindCtxTableLastIndex=0;
198 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
200 if (This->bindCtxTable==NULL)
201 return E_OUTOFMEMORY;
203 return S_OK;
206 /******************************************************************************
207 * BindCtx_Destroy (local function)
208 *******************************************************************************/
209 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
211 TRACE("(%p)\n",This);
213 /* free the table space memory */
214 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
216 /* free the bindctx structure */
217 HeapFree(GetProcessHeap(),0,This);
219 return S_OK;
223 /******************************************************************************
224 * BindCtx_RegisterObjectBound
225 ******************************************************************************/
226 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
228 BindCtxImpl *This = (BindCtxImpl *)iface;
229 DWORD lastIndex=This->bindCtxTableLastIndex;
231 TRACE("(%p,%p)\n",This,punk);
233 if (punk==NULL)
234 return E_POINTER;
236 IUnknown_AddRef(punk);
238 /* put the object in the first free element in the table */
239 This->bindCtxTable[lastIndex].pObj = punk;
240 This->bindCtxTable[lastIndex].pkeyObj = NULL;
241 This->bindCtxTable[lastIndex].regType = 0;
242 lastIndex= ++This->bindCtxTableLastIndex;
244 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
246 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
247 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
248 return E_FAIL;
251 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
253 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
254 This->bindCtxTableSize * sizeof(BindCtxObject));
255 if (!This->bindCtxTable)
256 return E_OUTOFMEMORY;
258 return S_OK;
261 /******************************************************************************
262 * BindCtx_RevokeObjectBound
263 ******************************************************************************/
264 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
266 DWORD index,j;
268 BindCtxImpl *This = (BindCtxImpl *)iface;
270 TRACE("(%p,%p)\n",This,punk);
272 /* check if the object was registered or not */
273 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
274 return MK_E_NOTBOUND;
276 if(This->bindCtxTable[index].pObj)
277 IUnknown_Release(This->bindCtxTable[index].pObj);
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 BindCtxImpl *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 BindCtxImpl *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 BindCtxImpl *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 BindCtxImpl *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 BindCtxImpl *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 BindCtxImpl *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 BindCtxImpl *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 HeapFree(GetProcessHeap(),0,This->bindCtxTable[index].pkeyObj);
476 /* remove the object from the table with a left-shifting of all objects in the right side */
477 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
478 This->bindCtxTable[j]= This->bindCtxTable[j+1];
480 This->bindCtxTableLastIndex--;
482 return S_OK;
485 /******************************************************************************
486 * BindCtx_EnumObjectParam
487 ******************************************************************************/
488 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
490 FIXME("(%p,%p),stub!\n",iface,pszkey);
491 return E_NOTIMPL;
494 /********************************************************************************
495 * GetObjectIndex (local function)
496 ********************************************************************************/
497 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
498 IUnknown* punk,
499 LPOLESTR pszkey,
500 DWORD *index)
503 DWORD i;
504 BYTE found=0;
506 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
508 if (punk==NULL)
509 /* search object identified by a register key */
510 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
512 if(This->bindCtxTable[i].regType==1){
514 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
515 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
516 (pszkey!=NULL) &&
517 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
521 found=1;
524 else
525 /* search object identified by a moniker*/
526 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
527 if(This->bindCtxTable[i].pObj==punk)
528 found=1;
530 if (index != NULL)
531 *index=i-1;
533 if (found)
534 return S_OK;
535 TRACE("key not found\n");
536 return S_FALSE;
539 /******************************************************************************
540 * CreateBindCtx16
541 ******************************************************************************/
542 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
544 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
545 return E_NOTIMPL;
548 /******************************************************************************
549 * CreateBindCtx (OLE32.@)
550 ******************************************************************************/
551 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
553 BindCtxImpl* newBindCtx = 0;
554 HRESULT hr;
555 IID riid=IID_IBindCtx;
557 TRACE("(%ld,%p)\n",reserved,ppbc);
559 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
561 if (newBindCtx == 0)
562 return E_OUTOFMEMORY;
564 hr = BindCtxImpl_Construct(newBindCtx);
566 if (FAILED(hr)){
568 HeapFree(GetProcessHeap(),0,newBindCtx);
569 return hr;
572 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
574 return hr;
577 HRESULT WINAPI BindMoniker(LPMONIKER pmk, DWORD grfOpt, REFIID riid, LPVOID * ppvResult)
579 HRESULT res;
580 IBindCtx * pbc;
582 TRACE("(%p, %lx, %s, %p)\n", pmk, grfOpt, debugstr_guid(riid), ppvResult);
584 res = CreateBindCtx(grfOpt, &pbc);
585 if (SUCCEEDED(res))
586 res = IMoniker_BindToObject(pmk, pbc, NULL, riid, ppvResult);
587 return res;