source: trunk/src/ole32/old/bindctx.cpp

Last change on this file was 5601, checked in by sandervl, 24 years ago

created

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