Make reg the default output chanel.
[wine/dcerpc.git] / dlls / ole32 / bindctx.c
blob11034c70ef7b475b0a66966f4792f9daee61d7c2
1 /***************************************************************************************
2 * BindCtx implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
7 #include <string.h>
8 #include <assert.h>
9 #include "winerror.h"
10 #include "winbase.h"
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"
17 #include "heap.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 */
34 } BindCtxObject;
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*/
49 } BindCtxImpl;
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);
68 /* Local functions*/
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,
78 BindCtxImpl_AddRef,
79 BindCtxImpl_Release,
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) )
103 return E_INVALIDARG;
105 /* Initialize the return parameter.*/
106 *ppvObject = 0;
108 /* Compare the riid with the interface IDs implemented by this object.*/
109 if (IsEqualIID(&IID_IUnknown, riid))
110 *ppvObject = (IBindCtx*)This;
111 else
112 if (IsEqualIID(&IID_IBindCtx, riid))
113 *ppvObject = (IBindCtx*)This;
115 /* Check that we obtained an interface.*/
116 if ((*ppvObject)==0)
117 return E_NOINTERFACE;
119 /* Query Interface always increases the reference count by one when it is successful */
120 BindCtxImpl_AddRef(iface);
122 return S_OK;
125 /******************************************************************************
126 * BindCtx_AddRef
127 ******************************************************************************/
128 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
130 ICOM_THIS(BindCtxImpl,iface);
132 TRACE("(%p)\n",This);
134 return ++(This->ref);
137 /******************************************************************************
138 * BindCtx_Release
139 ******************************************************************************/
140 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
142 ICOM_THIS(BindCtxImpl,iface);
144 TRACE("(%p)\n",This);
146 This->ref--;
148 if (This->ref==0){
150 /* release all registered objects */
151 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
153 BindCtxImpl_Destroy(This);
155 return 0;
157 return This->ref;;
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;
170 This->ref = 0;
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;
191 return S_OK;
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);
207 return S_OK;
211 /******************************************************************************
212 * BindCtx_RegisterObjectBound
213 ******************************************************************************/
214 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
217 ICOM_THIS(BindCtxImpl,iface);
218 DWORD lastIndex=This->bindCtxTableLastIndex;
219 BindCtxObject cell;
221 TRACE("(%p,%p)\n",This,punk);
223 if (punk==NULL)
224 return E_POINTER;
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);
239 return E_FAIL;
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;
249 return S_OK;
252 /******************************************************************************
253 * BindCtx_RevokeObjectBound
254 ******************************************************************************/
255 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
257 DWORD index,j;
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--;
276 return S_OK;
279 /******************************************************************************
280 * BindCtx_ReleaseBoundObjects
281 ******************************************************************************/
282 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
284 DWORD i;
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;
295 return S_OK;
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);
307 if (pbindopts==NULL)
308 return E_POINTER;
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);
316 return S_OK;
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);
328 if (pbindopts==NULL)
329 return E_POINTER;
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);
337 return S_OK;
340 /******************************************************************************
341 * BindCtx_GetRunningObjectTable
342 ******************************************************************************/
343 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
345 HRESULT res;
347 ICOM_THIS(BindCtxImpl,iface);
349 TRACE("(%p,%p)\n",This,pprot);
351 if (pprot==NULL)
352 return E_POINTER;
354 res=GetRunningObjectTable(0, pprot);
356 return res;
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);
368 if (punk==NULL)
369 return E_INVALIDARG;
371 IUnknown_AddRef(punk);
373 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
374 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
376 if (pszkey==NULL)
378 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
380 else{
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);
398 return E_FAIL;
400 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
401 This->bindCtxTableSize * sizeof(BindCtxObject));
402 if (!This->bindCtxTable)
403 return E_OUTOFMEMORY;
405 return S_OK;
407 /******************************************************************************
408 * BindCtx_GetObjectParam
409 ******************************************************************************/
410 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
412 DWORD index;
413 ICOM_THIS(BindCtxImpl,iface);
415 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
417 if (punk==NULL)
418 return E_POINTER;
420 *punk=0;
422 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
423 return E_FAIL;
425 IUnknown_AddRef(This->bindCtxTable[index].pObj);
427 *punk = This->bindCtxTable[index].pObj;
429 return S_OK;
432 /******************************************************************************
433 * BindCtx_RevokeObjectParam
434 ******************************************************************************/
435 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
437 DWORD index,j;
439 ICOM_THIS(BindCtxImpl,iface);
441 TRACE("(%p,%p)\n",This,ppenum);
443 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
444 return E_FAIL;
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--;
455 return S_OK;
458 /******************************************************************************
459 * BindCtx_EnumObjectParam
460 ******************************************************************************/
461 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
463 FIXME("(%p,%p),stub!\n",iface,pszkey);
464 return E_NOTIMPL;
467 /********************************************************************************
468 * GetObjectIndex (local function)
469 ********************************************************************************/
470 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
471 IUnknown* punk,
472 LPOLESTR pszkey,
473 DWORD *index)
476 DWORD i;
477 BYTE found=0;
479 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
481 if (punk==NULL)
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) &&
489 (pszkey!=NULL) &&
490 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
494 found=1;
497 else
498 /* search object identified by a moniker*/
499 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
500 if(This->bindCtxTable[i].pObj==punk)
501 found=1;
503 if (index != NULL)
504 *index=i-1;
506 if (found)
507 return S_OK;
508 else
509 return S_FALSE;
512 /******************************************************************************
513 * CreateBindCtx16
514 ******************************************************************************/
515 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
517 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
518 return E_NOTIMPL;
521 /******************************************************************************
522 * CreateBindCtx
523 ******************************************************************************/
524 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
526 BindCtxImpl* newBindCtx = 0;
527 HRESULT hr;
528 IID riid=IID_IBindCtx;
530 TRACE("(%ld,%p)\n",reserved,ppbc);
532 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
534 if (newBindCtx == 0)
535 return E_OUTOFMEMORY;
537 hr = BindCtxImpl_Construct(newBindCtx);
539 if (FAILED(hr)){
541 HeapFree(GetProcessHeap(),0,newBindCtx);
542 return hr;
545 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
547 return hr;