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 ***************************************************************************************/
25 #include "wine/unicode.h"
26 #include "wine/obj_base.h"
27 #include "wine/obj_misc.h"
28 #include "wine/obj_storage.h"
29 #include "wine/obj_moniker.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ole);
34 /* represent the first size table and it's increment block size */
35 #define BLOCK_TAB_SIZE 10
36 #define MAX_TAB_SIZE 0xFFFFFFFF
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 */
49 /* BindCtx data strucrture */
50 typedef struct BindCtxImpl{
52 ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
54 ULONG 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*/
64 /* IBindCtx prototype functions : */
66 /* IUnknown functions*/
67 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
68 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
69 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
70 /* IBindCtx functions */
71 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
72 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
73 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
74 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
75 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
76 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
77 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
78 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
79 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
80 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
82 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
83 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
84 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
86 /* Virtual function table for the BindCtx class. */
87 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
89 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
90 BindCtxImpl_QueryInterface,
93 BindCtxImpl_RegisterObjectBound,
94 BindCtxImpl_RevokeObjectBound,
95 BindCtxImpl_ReleaseBoundObjects,
96 BindCtxImpl_SetBindOptions,
97 BindCtxImpl_GetBindOptions,
98 BindCtxImpl_GetRunningObjectTable,
99 BindCtxImpl_RegisterObjectParam,
100 BindCtxImpl_GetObjectParam,
101 BindCtxImpl_EnumObjectParam,
102 BindCtxImpl_RevokeObjectParam
105 /*******************************************************************************
106 * BindCtx_QueryInterface
107 *******************************************************************************/
108 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
110 ICOM_THIS(BindCtxImpl,iface);
112 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
114 /* Perform a sanity check on the parameters.*/
115 if ( (This==0) || (ppvObject==0) )
118 /* Initialize the return parameter.*/
121 /* Compare the riid with the interface IDs implemented by this object.*/
122 if (IsEqualIID(&IID_IUnknown, riid))
123 *ppvObject = (IBindCtx*)This;
125 if (IsEqualIID(&IID_IBindCtx, riid))
126 *ppvObject = (IBindCtx*)This;
128 /* Check that we obtained an interface.*/
130 return E_NOINTERFACE;
132 /* Query Interface always increases the reference count by one when it is successful */
133 BindCtxImpl_AddRef(iface);
138 /******************************************************************************
140 ******************************************************************************/
141 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
143 ICOM_THIS(BindCtxImpl,iface);
145 TRACE("(%p)\n",This);
147 return ++(This->ref);
150 /******************************************************************************
152 ******************************************************************************/
153 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
155 ICOM_THIS(BindCtxImpl,iface);
157 TRACE("(%p)\n",This);
163 /* release all registered objects */
164 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
166 BindCtxImpl_Destroy(This);
174 /******************************************************************************
175 * BindCtx_Construct (local function)
176 *******************************************************************************/
177 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
179 TRACE("(%p)\n",This);
181 /* Initialize the virtual function table.*/
182 ICOM_VTBL(This) = &VT_BindCtxImpl;
185 /* Initialize the BIND_OPTS2 structure */
186 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
187 This->bindOption2.grfFlags = 0;
188 This->bindOption2.grfMode = STGM_READWRITE;
189 This->bindOption2.dwTickCountDeadline = 0;
191 This->bindOption2.dwTrackFlags = 0;
192 This->bindOption2.dwClassContext = CLSCTX_SERVER;
193 This->bindOption2.locale = 1033;
194 This->bindOption2.pServerInfo = 0;
196 /* Initialize the bindctx table */
197 This->bindCtxTableSize=BLOCK_TAB_SIZE;
198 This->bindCtxTableLastIndex=0;
199 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
201 if (This->bindCtxTable==NULL)
202 return E_OUTOFMEMORY;
207 /******************************************************************************
208 * BindCtx_Destroy (local function)
209 *******************************************************************************/
210 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
212 TRACE("(%p)\n",This);
214 /* free the table space memory */
215 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
217 /* free the bindctx structure */
218 HeapFree(GetProcessHeap(),0,This);
224 /******************************************************************************
225 * BindCtx_RegisterObjectBound
226 ******************************************************************************/
227 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
230 ICOM_THIS(BindCtxImpl,iface);
231 DWORD lastIndex=This->bindCtxTableLastIndex;
234 TRACE("(%p,%p)\n",This,punk);
239 IUnknown_AddRef(punk);
241 /* put the object in the first free element in the table */
242 This->bindCtxTable[lastIndex].pObj = punk;
243 This->bindCtxTable[lastIndex].pkeyObj = NULL;
244 This->bindCtxTable[lastIndex].regType = 0;
245 cell=This->bindCtxTable[lastIndex];
246 lastIndex= ++This->bindCtxTableLastIndex;
248 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
250 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
251 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
255 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
257 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
258 This->bindCtxTableSize * sizeof(BindCtxObject));
259 if (!This->bindCtxTable)
260 return E_OUTOFMEMORY;
265 /******************************************************************************
266 * BindCtx_RevokeObjectBound
267 ******************************************************************************/
268 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
272 ICOM_THIS(BindCtxImpl,iface);
274 TRACE("(%p,%p)\n",This,punk);
276 /* check if the object was registred or not */
277 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
279 return MK_E_NOTBOUND;
281 IUnknown_Release(This->bindCtxTable[index].pObj);
283 /* left-shift all elements in the right side of the current revoked object */
284 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
285 This->bindCtxTable[j]= This->bindCtxTable[j+1];
287 This->bindCtxTableLastIndex--;
292 /******************************************************************************
293 * BindCtx_ReleaseBoundObjects
294 ******************************************************************************/
295 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
299 ICOM_THIS(BindCtxImpl,iface);
301 TRACE("(%p)\n",This);
303 for(i=0;i<This->bindCtxTableLastIndex;i++)
304 IUnknown_Release(This->bindCtxTable[i].pObj);
306 This->bindCtxTableLastIndex = 0;
311 /******************************************************************************
312 * BindCtx_SetBindOptions
313 ******************************************************************************/
314 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
316 ICOM_THIS(BindCtxImpl,iface);
318 TRACE("(%p,%p)\n",This,pbindopts);
323 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
325 WARN("invalid size\n");
326 return E_INVALIDARG; /* FIXME : not verified */
328 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
332 /******************************************************************************
333 * BindCtx_GetBindOptions
334 ******************************************************************************/
335 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
337 ICOM_THIS(BindCtxImpl,iface);
339 TRACE("(%p,%p)\n",This,pbindopts);
344 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
346 WARN("invalid size\n");
347 return E_INVALIDARG; /* FIXME : not verified */
349 memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
353 /******************************************************************************
354 * BindCtx_GetRunningObjectTable
355 ******************************************************************************/
356 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
360 ICOM_THIS(BindCtxImpl,iface);
362 TRACE("(%p,%p)\n",This,pprot);
367 res=GetRunningObjectTable(0, pprot);
372 /******************************************************************************
373 * BindCtx_RegisterObjectParam
374 ******************************************************************************/
375 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
377 ICOM_THIS(BindCtxImpl,iface);
379 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
384 IUnknown_AddRef(punk);
386 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
387 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
391 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
395 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
396 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
398 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
399 return E_OUTOFMEMORY;
400 strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
403 This->bindCtxTableLastIndex++;
405 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
407 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
409 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
410 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
413 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
414 This->bindCtxTableSize * sizeof(BindCtxObject));
415 if (!This->bindCtxTable)
416 return E_OUTOFMEMORY;
420 /******************************************************************************
421 * BindCtx_GetObjectParam
422 ******************************************************************************/
423 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
426 ICOM_THIS(BindCtxImpl,iface);
428 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
435 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
438 IUnknown_AddRef(This->bindCtxTable[index].pObj);
440 *punk = This->bindCtxTable[index].pObj;
445 /******************************************************************************
446 * BindCtx_RevokeObjectParam
447 ******************************************************************************/
448 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
452 ICOM_THIS(BindCtxImpl,iface);
454 TRACE("(%p,%p)\n",This,ppenum);
456 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
459 /* release the object if it's found */
460 IUnknown_Release(This->bindCtxTable[index].pObj);
462 /* remove the object from the table with a left-shifting of all objects in the right side */
463 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
464 This->bindCtxTable[j]= This->bindCtxTable[j+1];
466 This->bindCtxTableLastIndex--;
471 /******************************************************************************
472 * BindCtx_EnumObjectParam
473 ******************************************************************************/
474 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
476 FIXME("(%p,%p),stub!\n",iface,pszkey);
480 /********************************************************************************
481 * GetObjectIndex (local function)
482 ********************************************************************************/
483 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
492 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
495 /* search object identified by a register key */
496 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
498 if(This->bindCtxTable[i].regType==1){
500 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
501 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
503 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
511 /* search object identified by a moniker*/
512 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
513 if(This->bindCtxTable[i].pObj==punk)
525 /******************************************************************************
527 ******************************************************************************/
528 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
530 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
534 /******************************************************************************
535 * CreateBindCtx (OLE32.52)
536 ******************************************************************************/
537 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
539 BindCtxImpl* newBindCtx = 0;
541 IID riid=IID_IBindCtx;
543 TRACE("(%ld,%p)\n",reserved,ppbc);
545 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
548 return E_OUTOFMEMORY;
550 hr = BindCtxImpl_Construct(newBindCtx);
554 HeapFree(GetProcessHeap(),0,newBindCtx);
558 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);