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"
18 DEFAULT_DEBUG_CHANNEL(ole);
20 /* represent the first size table and it's increment block size */
21 #define BLOCK_TAB_SIZE 10
22 #define MAX_TAB_SIZE 0xFFFFFFFF
24 /* data structure of the BindCtx table elements */
25 typedef struct BindCtxObject{
27 IUnknown* pObj; /* point on a bound object */
29 LPOLESTR pkeyObj; /* key associated to this bound object */
31 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
35 /* BindCtx data strucrture */
36 typedef struct BindCtxImpl{
38 ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
40 ULONG ref; /* reference counter for this object */
42 BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
43 DWORD bindCtxTableLastIndex; /* first free index in the table */
44 DWORD bindCtxTableSize; /* size table */
46 BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
50 /* IBindCtx prototype functions : */
52 /* IUnknown functions*/
53 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
54 static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
55 static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
56 /* IBindCtx functions */
57 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
58 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
59 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
60 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
61 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
62 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
63 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
64 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
65 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
66 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
68 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
69 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
70 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
72 /* Virtual function table for the BindCtx class. */
73 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
75 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
76 BindCtxImpl_QueryInterface,
79 BindCtxImpl_RegisterObjectBound,
80 BindCtxImpl_RevokeObjectBound,
81 BindCtxImpl_ReleaseBoundObjects,
82 BindCtxImpl_SetBindOptions,
83 BindCtxImpl_GetBindOptions,
84 BindCtxImpl_GetRunningObjectTable,
85 BindCtxImpl_RegisterObjectParam,
86 BindCtxImpl_GetObjectParam,
87 BindCtxImpl_EnumObjectParam,
88 BindCtxImpl_RevokeObjectParam
91 /*******************************************************************************
92 * BindCtx_QueryInterface
93 *******************************************************************************/
94 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
96 ICOM_THIS(BindCtxImpl,iface);
98 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
100 /* Perform a sanity check on the parameters.*/
101 if ( (This==0) || (ppvObject==0) )
104 /* Initialize the return parameter.*/
107 /* Compare the riid with the interface IDs implemented by this object.*/
108 if (IsEqualIID(&IID_IUnknown, riid))
109 *ppvObject = (IBindCtx*)This;
111 if (IsEqualIID(&IID_IBindCtx, riid))
112 *ppvObject = (IBindCtx*)This;
114 /* Check that we obtained an interface.*/
116 return E_NOINTERFACE;
118 /* Query Interface always increases the reference count by one when it is successful */
119 BindCtxImpl_AddRef(iface);
124 /******************************************************************************
126 ******************************************************************************/
127 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
129 ICOM_THIS(BindCtxImpl,iface);
131 TRACE("(%p)\n",This);
133 return ++(This->ref);
136 /******************************************************************************
138 ******************************************************************************/
139 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
141 ICOM_THIS(BindCtxImpl,iface);
143 TRACE("(%p)\n",This);
149 /* release all registered objects */
150 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
152 BindCtxImpl_Destroy(This);
160 /******************************************************************************
161 * BindCtx_Construct (local function)
162 *******************************************************************************/
163 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
165 TRACE("(%p)\n",This);
167 /* Initialize the virtual function table.*/
168 ICOM_VTBL(This) = &VT_BindCtxImpl;
171 /* Initialize the BIND_OPTS2 structure */
172 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
173 This->bindOption2.grfFlags = 0;
174 This->bindOption2.grfMode = STGM_READWRITE;
175 This->bindOption2.dwTickCountDeadline = 0;
177 This->bindOption2.dwTrackFlags = 0;
178 This->bindOption2.dwClassContext = CLSCTX_SERVER;
179 This->bindOption2.locale = 1033;
180 This->bindOption2.pServerInfo = 0;
182 /* Initialize the bindctx table */
183 This->bindCtxTableSize=BLOCK_TAB_SIZE;
184 This->bindCtxTableLastIndex=0;
185 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
187 if (This->bindCtxTable==NULL)
188 return E_OUTOFMEMORY;
193 /******************************************************************************
194 * BindCtx_Destroy (local function)
195 *******************************************************************************/
196 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
198 TRACE("(%p)\n",This);
200 /* free the table space memory */
201 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
203 /* free the bindctx structure */
204 HeapFree(GetProcessHeap(),0,This);
210 /******************************************************************************
211 * BindCtx_RegisterObjectBound
212 ******************************************************************************/
213 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
216 ICOM_THIS(BindCtxImpl,iface);
217 DWORD lastIndex=This->bindCtxTableLastIndex;
220 TRACE("(%p,%p)\n",This,punk);
225 IUnknown_AddRef(punk);
227 /* put the object in the first free element in the table */
228 This->bindCtxTable[lastIndex].pObj = punk;
229 This->bindCtxTable[lastIndex].pkeyObj = NULL;
230 This->bindCtxTable[lastIndex].regType = 0;
231 cell=This->bindCtxTable[lastIndex];
232 lastIndex= ++This->bindCtxTableLastIndex;
234 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
236 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
237 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
241 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
243 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
244 This->bindCtxTableSize * sizeof(BindCtxObject));
245 if (!This->bindCtxTable)
246 return E_OUTOFMEMORY;
251 /******************************************************************************
252 * BindCtx_RevokeObjectBound
253 ******************************************************************************/
254 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
258 ICOM_THIS(BindCtxImpl,iface);
260 TRACE("(%p,%p)\n",This,punk);
262 /* check if the object was registred or not */
263 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
265 return MK_E_NOTBOUND;
267 IUnknown_Release(This->bindCtxTable[index].pObj);
269 /* left-shift all elements in the right side of the current revoked object */
270 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
271 This->bindCtxTable[j]= This->bindCtxTable[j+1];
273 This->bindCtxTableLastIndex--;
278 /******************************************************************************
279 * BindCtx_ReleaseBoundObjects
280 ******************************************************************************/
281 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
285 ICOM_THIS(BindCtxImpl,iface);
287 TRACE("(%p)\n",This);
289 for(i=0;i<This->bindCtxTableLastIndex;i++)
290 IUnknown_Release(This->bindCtxTable[i].pObj);
292 This->bindCtxTableLastIndex = 0;
297 /******************************************************************************
298 * BindCtx_SetBindOptions
299 ******************************************************************************/
300 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
302 ICOM_THIS(BindCtxImpl,iface);
304 TRACE("(%p,%p)\n",This,pbindopts);
309 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
311 WARN("invalid size\n");
312 return E_INVALIDARG; /* FIXME : not verified */
314 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
318 /******************************************************************************
319 * BindCtx_GetBindOptions
320 ******************************************************************************/
321 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
323 ICOM_THIS(BindCtxImpl,iface);
325 TRACE("(%p,%p)\n",This,pbindopts);
330 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
332 WARN("invalid size\n");
333 return E_INVALIDARG; /* FIXME : not verified */
335 memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
339 /******************************************************************************
340 * BindCtx_GetRunningObjectTable
341 ******************************************************************************/
342 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
346 ICOM_THIS(BindCtxImpl,iface);
348 TRACE("(%p,%p)\n",This,pprot);
353 res=GetRunningObjectTable(0, pprot);
358 /******************************************************************************
359 * BindCtx_RegisterObjectParam
360 ******************************************************************************/
361 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
363 ICOM_THIS(BindCtxImpl,iface);
365 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
370 IUnknown_AddRef(punk);
372 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
373 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
377 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
381 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
382 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
384 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
385 return E_OUTOFMEMORY;
386 strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
389 This->bindCtxTableLastIndex++;
391 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
393 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
395 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
396 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
399 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
400 This->bindCtxTableSize * sizeof(BindCtxObject));
401 if (!This->bindCtxTable)
402 return E_OUTOFMEMORY;
406 /******************************************************************************
407 * BindCtx_GetObjectParam
408 ******************************************************************************/
409 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
412 ICOM_THIS(BindCtxImpl,iface);
414 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
421 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
424 IUnknown_AddRef(This->bindCtxTable[index].pObj);
426 *punk = This->bindCtxTable[index].pObj;
431 /******************************************************************************
432 * BindCtx_RevokeObjectParam
433 ******************************************************************************/
434 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
438 ICOM_THIS(BindCtxImpl,iface);
440 TRACE("(%p,%p)\n",This,ppenum);
442 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
445 /* release the object if it's found */
446 IUnknown_Release(This->bindCtxTable[index].pObj);
448 /* remove the object from the table with a left-shifting of all objects in the right side */
449 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
450 This->bindCtxTable[j]= This->bindCtxTable[j+1];
452 This->bindCtxTableLastIndex--;
457 /******************************************************************************
458 * BindCtx_EnumObjectParam
459 ******************************************************************************/
460 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
462 FIXME("(%p,%p),stub!\n",iface,pszkey);
466 /********************************************************************************
467 * GetObjectIndex (local function)
468 ********************************************************************************/
469 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
478 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
481 /* search object identified by a register key */
482 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
484 if(This->bindCtxTable[i].regType==1){
486 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
487 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
489 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
497 /* search object identified by a moniker*/
498 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
499 if(This->bindCtxTable[i].pObj==punk)
511 /******************************************************************************
513 ******************************************************************************/
514 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
516 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
520 /******************************************************************************
521 * CreateBindCtx (OLE32.52)
522 ******************************************************************************/
523 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
525 BindCtxImpl* newBindCtx = 0;
527 IID riid=IID_IBindCtx;
529 TRACE("(%ld,%p)\n",reserved,ppbc);
531 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
534 return E_OUTOFMEMORY;
536 hr = BindCtxImpl_Construct(newBindCtx);
540 HeapFree(GetProcessHeap(),0,newBindCtx);
544 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);