1 /***************************************************************************************
2 * BindCtx implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
11 #include "wine/unicode.h"
12 #include "wine/obj_base.h"
13 #include "wine/obj_misc.h"
14 #include "wine/obj_storage.h"
15 #include "wine/obj_moniker.h"
16 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(ole);
21 /* represent the first size table and it's increment block size */
22 #define BLOCK_TAB_SIZE 10
23 #define MAX_TAB_SIZE 0xFFFFFFFF
25 /* data structure of the BindCtx table elements */
26 typedef struct BindCtxObject{
28 IUnknown* pObj; /* point on a bound object */
30 LPOLESTR pkeyObj; /* key associated to this bound object */
32 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
36 /* BindCtx data strucrture */
37 typedef struct BindCtxImpl{
39 ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
41 ULONG ref; /* reference counter for this object */
43 BindCtxObject* bindCtxTable; /* this is a table in witch all bounded objects are stored*/
44 DWORD bindCtxTableLastIndex; /* first free index in the table */
45 DWORD bindCtxTableSize; /* size table */
47 BIND_OPTS2 bindOption2; /* a structure witch contains the bind options*/
51 /* IBindCtx prototype functions : */
53 /* IUnknown functions*/
54 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
55 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
56 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
57 /* IBindCtx functions */
58 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
59 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
60 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
61 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
62 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
63 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
64 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
65 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
66 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
67 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
69 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
70 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
71 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
73 /* Virtual function table for the BindCtx class. */
74 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
76 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
77 BindCtxImpl_QueryInterface,
80 BindCtxImpl_RegisterObjectBound,
81 BindCtxImpl_RevokeObjectBound,
82 BindCtxImpl_ReleaseBoundObjects,
83 BindCtxImpl_SetBindOptions,
84 BindCtxImpl_GetBindOptions,
85 BindCtxImpl_GetRunningObjectTable,
86 BindCtxImpl_RegisterObjectParam,
87 BindCtxImpl_GetObjectParam,
88 BindCtxImpl_EnumObjectParam,
89 BindCtxImpl_RevokeObjectParam
92 /*******************************************************************************
93 * BindCtx_QueryInterface
94 *******************************************************************************/
95 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
97 ICOM_THIS(BindCtxImpl,iface);
99 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
101 /* Perform a sanity check on the parameters.*/
102 if ( (This==0) || (ppvObject==0) )
105 /* Initialize the return parameter.*/
108 /* Compare the riid with the interface IDs implemented by this object.*/
109 if (IsEqualIID(&IID_IUnknown, riid))
110 *ppvObject = (IBindCtx*)This;
112 if (IsEqualIID(&IID_IBindCtx, riid))
113 *ppvObject = (IBindCtx*)This;
115 /* Check that we obtained an interface.*/
117 return E_NOINTERFACE;
119 /* Query Interface always increases the reference count by one when it is successful */
120 BindCtxImpl_AddRef(iface);
125 /******************************************************************************
127 ******************************************************************************/
128 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
130 ICOM_THIS(BindCtxImpl,iface);
132 TRACE("(%p)\n",This);
134 return ++(This->ref);
137 /******************************************************************************
139 ******************************************************************************/
140 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
142 ICOM_THIS(BindCtxImpl,iface);
144 TRACE("(%p)\n",This);
150 /* release all registered objects */
151 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
153 BindCtxImpl_Destroy(This);
161 /******************************************************************************
162 * BindCtx_Construct (local function)
163 *******************************************************************************/
164 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
166 TRACE("(%p)\n",This);
168 /* Initialize the virtual function table.*/
169 ICOM_VTBL(This) = &VT_BindCtxImpl;
172 /* Initialize the BIND_OPTS2 structure */
173 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
174 This->bindOption2.grfFlags = 0;
175 This->bindOption2.grfMode = STGM_READWRITE;
176 This->bindOption2.dwTickCountDeadline = 0;
178 This->bindOption2.dwTrackFlags = 0;
179 This->bindOption2.dwClassContext = CLSCTX_SERVER;
180 This->bindOption2.locale = 1033;
181 This->bindOption2.pServerInfo = 0;
183 /* Initialize the bindctx table */
184 This->bindCtxTableSize=BLOCK_TAB_SIZE;
185 This->bindCtxTableLastIndex=0;
186 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
188 if (This->bindCtxTable==NULL)
189 return E_OUTOFMEMORY;
194 /******************************************************************************
195 * BindCtx_Destroy (local function)
196 *******************************************************************************/
197 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
199 TRACE("(%p)\n",This);
201 /* free the table space memory */
202 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
204 /* free the bindctx structure */
205 HeapFree(GetProcessHeap(),0,This);
211 /******************************************************************************
212 * BindCtx_RegisterObjectBound
213 ******************************************************************************/
214 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
217 ICOM_THIS(BindCtxImpl,iface);
218 DWORD lastIndex=This->bindCtxTableLastIndex;
221 TRACE("(%p,%p)\n",This,punk);
226 IUnknown_AddRef(punk);
228 /* put the object in the first free element in the table */
229 This->bindCtxTable[lastIndex].pObj = punk;
230 This->bindCtxTable[lastIndex].pkeyObj = NULL;
231 This->bindCtxTable[lastIndex].regType = 0;
232 cell=This->bindCtxTable[lastIndex];
233 lastIndex= ++This->bindCtxTableLastIndex;
235 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
237 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
238 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
242 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
244 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
245 This->bindCtxTableSize * sizeof(BindCtxObject));
246 if (!This->bindCtxTable)
247 return E_OUTOFMEMORY;
252 /******************************************************************************
253 * BindCtx_RevokeObjectBound
254 ******************************************************************************/
255 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
259 ICOM_THIS(BindCtxImpl,iface);
261 TRACE("(%p,%p)\n",This,punk);
263 /* check if the object was registred or not */
264 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
266 return MK_E_NOTBOUND;
268 IUnknown_Release(This->bindCtxTable[index].pObj);
270 /* left-shift all elements in the rigth side of the curent revoked object */
271 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
272 This->bindCtxTable[j]= This->bindCtxTable[j+1];
274 This->bindCtxTableLastIndex--;
279 /******************************************************************************
280 * BindCtx_ReleaseBoundObjects
281 ******************************************************************************/
282 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
286 ICOM_THIS(BindCtxImpl,iface);
288 TRACE("(%p)\n",This);
290 for(i=0;i<This->bindCtxTableLastIndex;i++)
291 IUnknown_Release(This->bindCtxTable[i].pObj);
293 This->bindCtxTableLastIndex = 0;
298 /******************************************************************************
299 * BindCtx_SetBindOptions
300 ******************************************************************************/
301 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
303 ICOM_THIS(BindCtxImpl,iface);
305 TRACE("(%p,%p)\n",This,pbindopts);
310 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
312 WARN("invalid size");
313 return E_INVALIDARG; /* FIXME : not verified */
315 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
319 /******************************************************************************
320 * BindCtx_GetBindOptions
321 ******************************************************************************/
322 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
324 ICOM_THIS(BindCtxImpl,iface);
326 TRACE("(%p,%p)\n",This,pbindopts);
331 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
333 WARN("invalid size");
334 return E_INVALIDARG; /* FIXME : not verified */
336 memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
340 /******************************************************************************
341 * BindCtx_GetRunningObjectTable
342 ******************************************************************************/
343 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
347 ICOM_THIS(BindCtxImpl,iface);
349 TRACE("(%p,%p)\n",This,pprot);
354 res=GetRunningObjectTable(0, pprot);
359 /******************************************************************************
360 * BindCtx_RegisterObjectParam
361 ******************************************************************************/
362 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
364 ICOM_THIS(BindCtxImpl,iface);
366 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
371 IUnknown_AddRef(punk);
373 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
374 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
378 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
382 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
383 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
385 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
386 return E_OUTOFMEMORY;
387 strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
390 This->bindCtxTableLastIndex++;
392 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
394 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
396 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
397 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
400 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
401 This->bindCtxTableSize * sizeof(BindCtxObject));
402 if (!This->bindCtxTable)
403 return E_OUTOFMEMORY;
407 /******************************************************************************
408 * BindCtx_GetObjectParam
409 ******************************************************************************/
410 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
413 ICOM_THIS(BindCtxImpl,iface);
415 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
422 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
425 IUnknown_AddRef(This->bindCtxTable[index].pObj);
427 *punk = This->bindCtxTable[index].pObj;
432 /******************************************************************************
433 * BindCtx_RevokeObjectParam
434 ******************************************************************************/
435 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
439 ICOM_THIS(BindCtxImpl,iface);
441 TRACE("(%p,%p)\n",This,ppenum);
443 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
446 /* release the object if it's found */
447 IUnknown_Release(This->bindCtxTable[index].pObj);
449 /* remove the object from the table with a left-shifting of all objects in the right side */
450 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
451 This->bindCtxTable[j]= This->bindCtxTable[j+1];
453 This->bindCtxTableLastIndex--;
458 /******************************************************************************
459 * BindCtx_EnumObjectParam
460 ******************************************************************************/
461 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
463 FIXME("(%p,%p),stub!\n",iface,pszkey);
467 /********************************************************************************
468 * GetObjectIndex (local function)
469 ********************************************************************************/
470 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
479 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
482 /* search object identified by a register key */
483 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
485 if(This->bindCtxTable[i].regType==1){
487 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
488 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
490 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
498 /* search object identified by a moniker*/
499 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
500 if(This->bindCtxTable[i].pObj==punk)
512 /******************************************************************************
514 ******************************************************************************/
515 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
517 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
521 /******************************************************************************
523 ******************************************************************************/
524 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
526 BindCtxImpl* newBindCtx = 0;
528 IID riid=IID_IBindCtx;
530 TRACE("(%ld,%p)\n",reserved,ppbc);
532 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
535 return E_OUTOFMEMORY;
537 hr = BindCtxImpl_Construct(newBindCtx);
541 HeapFree(GetProcessHeap(),0,newBindCtx);
545 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);