source: trunk/src/ole32/bindctx.c@ 7502

Last change on this file since 7502 was 7502, checked in by phaller, 24 years ago

Fixed out-of-scope FIXME,TRACE,WARN macros

File size: 17.9 KB
Line 
1/***************************************************************************************
2 * BindCtx implementation
3 *
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
6
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"
18
19#ifdef __WIN32OS2__
20#undef FIXME
21#undef TRACE
22#ifdef DEBUG
23// PH 2001-11-30
24// this macro definition causes the control leave the scope of a
25// non-curly-braced preceeding if statement. Therefore,
26// if (p!=NULL)
27// TRACE("p->a=%d", p->a)
28// crashes.
29//
30// !!! ENSURE TRACES AND FIXMES WITH PRECEEDING IF STATEMENT
31// !!! ARE PUT INTO CURLY BRACES
32#define TRACE WriteLog("OLE32: %s", __FUNCTION__); WriteLog
33#define FIXME WriteLog("FIXME OLE32: %s", __FUNCTION__); WriteLog
34#else
35#define TRACE 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
36#define FIXME 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
37#endif
38#endif
39
40DEFAULT_DEBUG_CHANNEL(ole);
41
42/* represent the first size table and it's increment block size */
43#define BLOCK_TAB_SIZE 10
44#define MAX_TAB_SIZE 0xFFFFFFFF
45
46/* data structure of the BindCtx table elements */
47typedef struct BindCtxObject{
48
49 IUnknown* pObj; /* point on a bound object */
50
51 LPOLESTR pkeyObj; /* key associated to this bound object */
52
53 BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
54
55} BindCtxObject;
56
57/* BindCtx data strucrture */
58typedef struct BindCtxImpl{
59
60 ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
61
62 ULONG ref; /* reference counter for this object */
63
64 BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
65 DWORD bindCtxTableLastIndex; /* first free index in the table */
66 DWORD bindCtxTableSize; /* size table */
67
68 BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
69
70} BindCtxImpl;
71
72/* IBindCtx prototype functions : */
73
74/* IUnknown functions*/
75static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
76static ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
77static ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface);
78/* IBindCtx functions */
79static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
80static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
81static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
82static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
83static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
84static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
85static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
86static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
87static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
88static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
89/* Local functions*/
90HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
91HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
92HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
93
94/* Virtual function table for the BindCtx class. */
95static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
96 {
97 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
98 BindCtxImpl_QueryInterface,
99 BindCtxImpl_AddRef,
100 BindCtxImpl_Release,
101 BindCtxImpl_RegisterObjectBound,
102 BindCtxImpl_RevokeObjectBound,
103 BindCtxImpl_ReleaseBoundObjects,
104 BindCtxImpl_SetBindOptions,
105 BindCtxImpl_GetBindOptions,
106 BindCtxImpl_GetRunningObjectTable,
107 BindCtxImpl_RegisterObjectParam,
108 BindCtxImpl_GetObjectParam,
109 BindCtxImpl_EnumObjectParam,
110 BindCtxImpl_RevokeObjectParam
111};
112
113/*******************************************************************************
114 * BindCtx_QueryInterface
115 *******************************************************************************/
116HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
117{
118 ICOM_THIS(BindCtxImpl,iface);
119
120 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
121
122 /* Perform a sanity check on the parameters.*/
123 if ( (This==0) || (ppvObject==0) )
124 return E_INVALIDARG;
125
126 /* Initialize the return parameter.*/
127 *ppvObject = 0;
128
129 /* Compare the riid with the interface IDs implemented by this object.*/
130 if (IsEqualIID(&IID_IUnknown, riid))
131 *ppvObject = (IBindCtx*)This;
132 else
133 if (IsEqualIID(&IID_IBindCtx, riid))
134 *ppvObject = (IBindCtx*)This;
135
136 /* Check that we obtained an interface.*/
137 if ((*ppvObject)==0)
138 return E_NOINTERFACE;
139
140 /* Query Interface always increases the reference count by one when it is successful */
141 BindCtxImpl_AddRef(iface);
142
143 return S_OK;
144}
145
146/******************************************************************************
147 * BindCtx_AddRef
148 ******************************************************************************/
149ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
150{
151 ICOM_THIS(BindCtxImpl,iface);
152
153 TRACE("(%p)\n",This);
154
155 return ++(This->ref);
156}
157
158/******************************************************************************
159 * BindCtx_Release
160 ******************************************************************************/
161ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
162{
163 ICOM_THIS(BindCtxImpl,iface);
164
165 TRACE("(%p)\n",This);
166
167 This->ref--;
168
169 if (This->ref==0){
170
171 /* release all registered objects */
172 BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
173
174 BindCtxImpl_Destroy(This);
175
176 return 0;
177 }
178 return This->ref;;
179}
180
181
182/******************************************************************************
183 * BindCtx_Construct (local function)
184 *******************************************************************************/
185HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
186{
187 TRACE("(%p)\n",This);
188
189 /* Initialize the virtual function table.*/
190 ICOM_VTBL(This) = &VT_BindCtxImpl;
191 This->ref = 0;
192
193 /* Initialize the BIND_OPTS2 structure */
194 This->bindOption2.cbStruct = sizeof(BIND_OPTS2);
195 This->bindOption2.grfFlags = 0;
196 This->bindOption2.grfMode = STGM_READWRITE;
197 This->bindOption2.dwTickCountDeadline = 0;
198
199 This->bindOption2.dwTrackFlags = 0;
200 This->bindOption2.dwClassContext = CLSCTX_SERVER;
201 This->bindOption2.locale = 1033;
202 This->bindOption2.pServerInfo = 0;
203
204 /* Initialize the bindctx table */
205 This->bindCtxTableSize=BLOCK_TAB_SIZE;
206 This->bindCtxTableLastIndex=0;
207 This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
208
209 if (This->bindCtxTable==NULL)
210 return E_OUTOFMEMORY;
211
212 return S_OK;
213}
214
215/******************************************************************************
216 * BindCtx_Destroy (local function)
217 *******************************************************************************/
218HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
219{
220 TRACE("(%p)\n",This);
221
222 /* free the table space memory */
223 HeapFree(GetProcessHeap(),0,This->bindCtxTable);
224
225 /* free the bindctx structure */
226 HeapFree(GetProcessHeap(),0,This);
227
228 return S_OK;
229}
230
231
232/******************************************************************************
233 * BindCtx_RegisterObjectBound
234 ******************************************************************************/
235HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
236{
237
238 ICOM_THIS(BindCtxImpl,iface);
239 DWORD lastIndex=This->bindCtxTableLastIndex;
240 BindCtxObject cell;
241
242 TRACE("(%p,%p)\n",This,punk);
243
244 if (punk==NULL)
245 return E_POINTER;
246
247 IUnknown_AddRef(punk);
248
249 /* put the object in the first free element in the table */
250 This->bindCtxTable[lastIndex].pObj = punk;
251 This->bindCtxTable[lastIndex].pkeyObj = NULL;
252 This->bindCtxTable[lastIndex].regType = 0;
253 cell=This->bindCtxTable[lastIndex];
254 lastIndex= ++This->bindCtxTableLastIndex;
255
256 if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
257
258 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
259 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
260 return E_FAIL;
261}
262
263 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
264
265 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
266 This->bindCtxTableSize * sizeof(BindCtxObject));
267 if (!This->bindCtxTable)
268 return E_OUTOFMEMORY;
269 }
270 return S_OK;
271}
272
273/******************************************************************************
274 * BindCtx_RevokeObjectBound
275 ******************************************************************************/
276HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
277{
278 DWORD index,j;
279
280 ICOM_THIS(BindCtxImpl,iface);
281
282 TRACE("(%p,%p)\n",This,punk);
283
284 /* check if the object was registred or not */
285 if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
286
287 return MK_E_NOTBOUND;
288
289 IUnknown_Release(This->bindCtxTable[index].pObj);
290
291 /* left-shift all elements in the right side of the current revoked object */
292 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
293 This->bindCtxTable[j]= This->bindCtxTable[j+1];
294
295 This->bindCtxTableLastIndex--;
296
297 return S_OK;
298}
299
300/******************************************************************************
301 * BindCtx_ReleaseBoundObjects
302 ******************************************************************************/
303HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
304{
305 DWORD i;
306
307 ICOM_THIS(BindCtxImpl,iface);
308
309 TRACE("(%p)\n",This);
310
311 for(i=0;i<This->bindCtxTableLastIndex;i++)
312 IUnknown_Release(This->bindCtxTable[i].pObj);
313
314 This->bindCtxTableLastIndex = 0;
315
316 return S_OK;
317}
318
319/******************************************************************************
320 * BindCtx_SetBindOptions
321 ******************************************************************************/
322HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
323{
324 ICOM_THIS(BindCtxImpl,iface);
325
326 TRACE("(%p,%p)\n",This,pbindopts);
327
328 if (pbindopts==NULL)
329 return E_POINTER;
330
331 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
332 {
333 WARN("invalid size");
334 return E_INVALIDARG; /* FIXME : not verified */
335 }
336 memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
337 return S_OK;
338}
339
340/******************************************************************************
341 * BindCtx_GetBindOptions
342 ******************************************************************************/
343HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
344{
345 ICOM_THIS(BindCtxImpl,iface);
346
347 TRACE("(%p,%p)\n",This,pbindopts);
348
349 if (pbindopts==NULL)
350 return E_POINTER;
351
352 if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
353 {
354 WARN("invalid size");
355 return E_INVALIDARG; /* FIXME : not verified */
356 }
357 memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
358 return S_OK;
359}
360
361/******************************************************************************
362 * BindCtx_GetRunningObjectTable
363 ******************************************************************************/
364HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
365{
366 HRESULT res;
367
368 ICOM_THIS(BindCtxImpl,iface);
369
370 TRACE("(%p,%p)\n",This,pprot);
371
372 if (pprot==NULL)
373 return E_POINTER;
374
375 res=GetRunningObjectTable(0, pprot);
376
377 return res;
378}
379
380/******************************************************************************
381 * BindCtx_RegisterObjectParam
382 ******************************************************************************/
383HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
384{
385 ICOM_THIS(BindCtxImpl,iface);
386
387 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
388
389 if (punk==NULL)
390 return E_INVALIDARG;
391
392 IUnknown_AddRef(punk);
393
394 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
395 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
396
397 if (pszkey==NULL)
398
399 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
400
401 else{
402
403 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
404 HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
405
406 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
407 return E_OUTOFMEMORY;
408 strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
409}
410
411 This->bindCtxTableLastIndex++;
412
413 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
414
415 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
416
417 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
418 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
419 return E_FAIL;
420 }
421 This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
422 This->bindCtxTableSize * sizeof(BindCtxObject));
423 if (!This->bindCtxTable)
424 return E_OUTOFMEMORY;
425 }
426 return S_OK;
427}
428/******************************************************************************
429 * BindCtx_GetObjectParam
430 ******************************************************************************/
431HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
432{
433 DWORD index;
434 ICOM_THIS(BindCtxImpl,iface);
435
436 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
437
438 if (punk==NULL)
439 return E_POINTER;
440
441 *punk=0;
442
443 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
444 return E_FAIL;
445
446 IUnknown_AddRef(This->bindCtxTable[index].pObj);
447
448 *punk = This->bindCtxTable[index].pObj;
449
450 return S_OK;
451}
452
453/******************************************************************************
454 * BindCtx_RevokeObjectParam
455 ******************************************************************************/
456HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
457{
458 DWORD index,j;
459
460 ICOM_THIS(BindCtxImpl,iface);
461
462 TRACE("(%p,%p)\n",This,ppenum);
463
464 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
465 return E_FAIL;
466
467 /* release the object if it's found */
468 IUnknown_Release(This->bindCtxTable[index].pObj);
469
470 /* remove the object from the table with a left-shifting of all objects in the right side */
471 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
472 This->bindCtxTable[j]= This->bindCtxTable[j+1];
473
474 This->bindCtxTableLastIndex--;
475
476 return S_OK;
477}
478
479/******************************************************************************
480 * BindCtx_EnumObjectParam
481 ******************************************************************************/
482HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
483{
484 FIXME("(%p,%p),stub!\n",iface,pszkey);
485 return E_NOTIMPL;
486}
487
488/********************************************************************************
489 * GetObjectIndex (local function)
490 ********************************************************************************/
491HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
492 IUnknown* punk,
493 LPOLESTR pszkey,
494 DWORD *index)
495{
496
497 DWORD i;
498 BYTE found=0;
499
500 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
501
502 if (punk==NULL)
503 /* search object identified by a register key */
504 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
505
506 if(This->bindCtxTable[i].regType==1){
507
508 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
509 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
510 (pszkey!=NULL) &&
511 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
512 )
513 )
514
515 found=1;
516 }
517 }
518 else
519 /* search object identified by a moniker*/
520 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
521 if(This->bindCtxTable[i].pObj==punk)
522 found=1;
523
524 if (index != NULL)
525 *index=i-1;
526
527 if (found)
528 return S_OK;
529 else
530 return S_FALSE;
531}
532
533/******************************************************************************
534 * CreateBindCtx16
535 ******************************************************************************/
536HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
537{
538 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
539 return E_NOTIMPL;
540}
541
542/******************************************************************************
543 * CreateBindCtx
544 ******************************************************************************/
545HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
546{
547 BindCtxImpl* newBindCtx = 0;
548 HRESULT hr;
549 IID riid=IID_IBindCtx;
550
551 TRACE("(%ld,%p)\n",reserved,ppbc);
552
553 newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
554
555 if (newBindCtx == 0)
556 return E_OUTOFMEMORY;
557
558 hr = BindCtxImpl_Construct(newBindCtx);
559
560 if (FAILED(hr)){
561
562 HeapFree(GetProcessHeap(),0,newBindCtx);
563 return hr;
564 }
565
566 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
567
568 return hr;
569}
Note: See TracBrowser for help on using the repository browser.