source: trunk/src/ole32/bindctx.cpp@ 1036

Last change on this file since 1036 was 1033, checked in by davidr, 26 years ago

Ported remaining files pertaining to OLE32 from WINE

File size: 16.8 KB
Line 
1/* $Id: bindctx.cpp,v 1.1 1999-09-24 21:49:42 davidr 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_VTABLE(IBindCtx)* lpvtbl; /* 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 This->lpvtbl = &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 ICOM_THIS(BindCtxImpl,iface);
303
304 TRACE("(%p,%p)\n",This,pbindopts);
305
306 if (pbindopts==NULL)
307 return E_POINTER;
308
309 This->bindOption2=*pbindopts;
310
311 return S_OK;
312}
313
314/******************************************************************************
315 * BindCtx_GetBindOptions
316 ******************************************************************************/
317HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
318{
319 ICOM_THIS(BindCtxImpl,iface);
320
321 TRACE("(%p,%p)\n",This,pbindopts);
322
323 if (pbindopts==NULL)
324 return E_POINTER;
325
326 *pbindopts=This->bindOption2;
327
328 return S_OK;
329}
330
331/******************************************************************************
332 * BindCtx_GetRunningObjectTable
333 ******************************************************************************/
334HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
335{
336 HRESULT res;
337
338 ICOM_THIS(BindCtxImpl,iface);
339
340 TRACE("(%p,%p)\n",This,pprot);
341
342 if (pprot==NULL)
343 return E_POINTER;
344
345 res=GetRunningObjectTable(0, pprot);
346
347 return res;
348}
349
350/******************************************************************************
351 * BindCtx_RegisterObjectParam
352 ******************************************************************************/
353HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
354{
355 ICOM_THIS(BindCtxImpl,iface);
356
357 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
358
359 if (punk==NULL)
360 return E_INVALIDARG;
361
362 IUnknown_AddRef(punk);
363
364 This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
365 This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
366
367 if (pszkey==NULL)
368
369 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
370
371 else{
372
373 This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
374 (WCHAR *)HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
375
376 if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
377 return E_OUTOFMEMORY;
378 lstrcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
379}
380
381 This->bindCtxTableLastIndex++;
382
383 if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
384
385 This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
386
387 if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
388 FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
389 return E_FAIL;
390 }
391 This->bindCtxTable =
392 (BindCtxObject *)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
393 This->bindCtxTableSize * sizeof(BindCtxObject));
394 if (!This->bindCtxTable)
395 return E_OUTOFMEMORY;
396 }
397 return S_OK;
398}
399/******************************************************************************
400 * BindCtx_GetObjectParam
401 ******************************************************************************/
402HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
403{
404 DWORD index;
405 ICOM_THIS(BindCtxImpl,iface);
406
407 TRACE("(%p,%p,%p)\n",This,pszkey,punk);
408
409 if (punk==NULL)
410 return E_POINTER;
411
412 *punk=0;
413
414 if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
415 return E_FAIL;
416
417 IUnknown_AddRef(This->bindCtxTable[index].pObj);
418
419 *punk = This->bindCtxTable[index].pObj;
420
421 return S_OK;
422}
423
424/******************************************************************************
425 * BindCtx_RevokeObjectParam
426 ******************************************************************************/
427HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
428{
429 DWORD index,j;
430
431 ICOM_THIS(BindCtxImpl,iface);
432
433 TRACE("(%p,%p)\n",This,ppenum);
434
435 if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
436 return E_FAIL;
437
438 /* release the object if it's found */
439 IUnknown_Release(This->bindCtxTable[index].pObj);
440
441 /* remove the object from the table with a left-shifting of all objects in the right side */
442 for(j=index; j<This->bindCtxTableLastIndex-1; j++)
443 This->bindCtxTable[j]= This->bindCtxTable[j+1];
444
445 This->bindCtxTableLastIndex--;
446
447 return S_OK;
448}
449
450/******************************************************************************
451 * BindCtx_EnumObjectParam
452 ******************************************************************************/
453HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
454{
455 FIXME("(%p,%p),stub!\n",iface,pszkey);
456 return E_NOTIMPL;
457}
458
459/********************************************************************************
460 * GetObjectIndex (local function)
461 ********************************************************************************/
462HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
463 IUnknown* punk,
464 LPOLESTR pszkey,
465 DWORD *index)
466{
467
468 DWORD i;
469 BYTE found=0;
470
471 TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
472
473 if (punk==NULL)
474 /* search object identified by a register key */
475 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
476
477 if(This->bindCtxTable[i].regType==1){
478
479 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
480 ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
481 (pszkey!=NULL) &&
482 (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
483 )
484 )
485
486 found=1;
487 }
488 }
489 else
490 /* search object identified by a moniker*/
491 for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
492 if(This->bindCtxTable[i].pObj==punk)
493 found=1;
494
495 if (index != NULL)
496 *index=i-1;
497
498 if (found)
499 return S_OK;
500 else
501 return S_FALSE;
502}
503
504/******************************************************************************
505 * CreateBindCtx16
506 ******************************************************************************/
507HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
508{
509 FIXME("(%ld,%p),stub!\n",reserved,ppbc);
510 return E_NOTIMPL;
511}
512
513/******************************************************************************
514 * CreateBindCtx
515 ******************************************************************************/
516HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
517{
518 BindCtxImpl* newBindCtx = 0;
519 HRESULT hr;
520 IID riid=IID_IBindCtx;
521
522 TRACE("(%ld,%p)\n",reserved,ppbc);
523
524 newBindCtx = (BindCtxImpl *)HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
525
526 if (newBindCtx == 0)
527 return E_OUTOFMEMORY;
528
529 hr = BindCtxImpl_Construct(newBindCtx);
530
531 if (FAILED(hr)){
532
533 HeapFree(GetProcessHeap(),0,newBindCtx);
534 return hr;
535 }
536
537 hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
538
539 return hr;
540}
Note: See TracBrowser for help on using the repository browser.