Documentation ordinal fixes.
[wine] / dlls / ole32 / bindctx.c
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 DEFAULT_DEBUG_CHANNEL(ole);
20
21 /* represent the first size table and it's increment block size */
22 #define  BLOCK_TAB_SIZE 10 
23 #define  MAX_TAB_SIZE   0xFFFFFFFF
24
25 /* data structure of the BindCtx table elements */
26 typedef struct BindCtxObject{
27
28     IUnknown*   pObj; /* point on a bound object */
29
30     LPOLESTR  pkeyObj; /* key associated to this bound object */
31
32     BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
33
34 } BindCtxObject;
35
36 /* BindCtx data strucrture */
37 typedef struct BindCtxImpl{
38
39     ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
40                                      
41     ULONG ref; /* reference counter for this object */
42
43     BindCtxObject* bindCtxTable; /* this is a table in which all bounded objects are stored*/
44     DWORD          bindCtxTableLastIndex;  /* first free index in the table */
45     DWORD          bindCtxTableSize;   /* size table */
46
47     BIND_OPTS2 bindOption2; /* a structure which contains the bind options*/
48
49 } BindCtxImpl;
50
51 /* IBindCtx prototype functions : */
52
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);
72
73 /* Virtual function table for the BindCtx class. */
74 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
75     {
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
90 };
91
92 /*******************************************************************************
93  *        BindCtx_QueryInterface
94  *******************************************************************************/
95 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
96 {
97   ICOM_THIS(BindCtxImpl,iface);
98
99   TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
100
101   /* Perform a sanity check on the parameters.*/
102   if ( (This==0) || (ppvObject==0) )
103       return E_INVALIDARG;
104   
105   /* Initialize the return parameter.*/
106   *ppvObject = 0;
107
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;
114
115   /* Check that we obtained an interface.*/
116   if ((*ppvObject)==0)
117       return E_NOINTERFACE;
118   
119    /* Query Interface always increases the reference count by one when it is successful */
120   BindCtxImpl_AddRef(iface);
121
122   return S_OK;
123 }
124
125 /******************************************************************************
126  *       BindCtx_AddRef
127  ******************************************************************************/
128 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
129 {
130     ICOM_THIS(BindCtxImpl,iface);
131
132     TRACE("(%p)\n",This);
133
134     return ++(This->ref);
135 }
136
137 /******************************************************************************
138  *        BindCtx_Release
139  ******************************************************************************/
140 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
141 {
142     ICOM_THIS(BindCtxImpl,iface);
143
144     TRACE("(%p)\n",This);
145
146     This->ref--;
147
148     if (This->ref==0){
149
150         /* release all registered objects */
151         BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
152
153         BindCtxImpl_Destroy(This);
154
155         return 0;
156     }
157     return This->ref;;
158 }
159
160
161 /******************************************************************************
162  *         BindCtx_Construct (local function)
163  *******************************************************************************/
164 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
165 {
166     TRACE("(%p)\n",This);
167
168     /* Initialize the virtual function table.*/
169     ICOM_VTBL(This)    = &VT_BindCtxImpl;
170     This->ref          = 0;
171
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;
177
178     This->bindOption2.dwTrackFlags = 0;
179     This->bindOption2.dwClassContext = CLSCTX_SERVER;
180     This->bindOption2.locale = 1033;
181     This->bindOption2.pServerInfo = 0;
182
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));
187
188     if (This->bindCtxTable==NULL)
189         return E_OUTOFMEMORY;
190
191     return S_OK;
192 }
193
194 /******************************************************************************
195  *        BindCtx_Destroy    (local function)
196  *******************************************************************************/
197 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
198 {
199     TRACE("(%p)\n",This);
200
201     /* free the table space memory */
202     HeapFree(GetProcessHeap(),0,This->bindCtxTable);
203
204     /* free the bindctx structure */
205     HeapFree(GetProcessHeap(),0,This);
206
207     return S_OK;
208 }
209
210
211 /******************************************************************************
212  *        BindCtx_RegisterObjectBound
213  ******************************************************************************/
214 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
215 {
216
217     ICOM_THIS(BindCtxImpl,iface);
218     DWORD lastIndex=This->bindCtxTableLastIndex;
219     BindCtxObject cell;
220
221     TRACE("(%p,%p)\n",This,punk);
222
223     if (punk==NULL)
224         return E_POINTER;
225     
226     IUnknown_AddRef(punk);
227     
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;
234
235     if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
236
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;
240 }
241
242         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
243
244         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
245                                          This->bindCtxTableSize * sizeof(BindCtxObject));
246         if (!This->bindCtxTable)
247             return E_OUTOFMEMORY;
248     }
249     return S_OK;
250 }
251
252 /******************************************************************************
253  *        BindCtx_RevokeObjectBound
254  ******************************************************************************/
255 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
256 {
257     DWORD index,j;
258
259     ICOM_THIS(BindCtxImpl,iface);
260
261     TRACE("(%p,%p)\n",This,punk);
262
263     /* check if the object was registred or not */
264     if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
265         
266         return MK_E_NOTBOUND;
267
268     IUnknown_Release(This->bindCtxTable[index].pObj);
269     
270     /* left-shift all elements in the right side of the current revoked object */
271     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
272         This->bindCtxTable[j]= This->bindCtxTable[j+1];
273     
274     This->bindCtxTableLastIndex--;
275
276     return S_OK;
277 }
278
279 /******************************************************************************
280  *        BindCtx_ReleaseBoundObjects
281  ******************************************************************************/
282 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
283 {
284     DWORD i;
285
286     ICOM_THIS(BindCtxImpl,iface);
287
288     TRACE("(%p)\n",This);
289
290     for(i=0;i<This->bindCtxTableLastIndex;i++)
291        IUnknown_Release(This->bindCtxTable[i].pObj);
292
293     This->bindCtxTableLastIndex = 0;
294
295     return S_OK;
296 }
297
298 /******************************************************************************
299  *        BindCtx_SetBindOptions
300  ******************************************************************************/
301 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
302 {
303     ICOM_THIS(BindCtxImpl,iface);
304
305     TRACE("(%p,%p)\n",This,pbindopts);
306
307     if (pbindopts==NULL)
308         return E_POINTER;
309     
310     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
311     {
312         WARN("invalid size\n");
313         return E_INVALIDARG; /* FIXME : not verified */
314     }
315     memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
316     return S_OK;
317 }
318
319 /******************************************************************************
320  *        BindCtx_GetBindOptions
321  ******************************************************************************/
322 HRESULT WINAPI BindCtxImpl_GetBindOptions(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\n");
334         return E_INVALIDARG; /* FIXME : not verified */
335     }
336     memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
337     return S_OK;
338 }
339
340 /******************************************************************************
341  *        BindCtx_GetRunningObjectTable
342  ******************************************************************************/
343 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
344 {
345     HRESULT res;
346
347     ICOM_THIS(BindCtxImpl,iface);
348
349     TRACE("(%p,%p)\n",This,pprot);
350
351     if (pprot==NULL)
352         return E_POINTER;
353     
354     res=GetRunningObjectTable(0, pprot);
355
356     return res;
357 }
358
359 /******************************************************************************
360  *        BindCtx_RegisterObjectParam
361  ******************************************************************************/
362 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
363 {
364     ICOM_THIS(BindCtxImpl,iface);
365
366     TRACE("(%p,%p,%p)\n",This,pszkey,punk);
367
368     if (punk==NULL)
369         return E_INVALIDARG;
370     
371     IUnknown_AddRef(punk);
372
373     This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
374     This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
375
376     if (pszkey==NULL)
377
378         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
379
380     else{
381
382         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
383             HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
384
385         if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
386             return E_OUTOFMEMORY;
387         strcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
388 }
389
390     This->bindCtxTableLastIndex++;
391     
392     if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
393
394         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
395
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;
399         }
400         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
401                                          This->bindCtxTableSize * sizeof(BindCtxObject));
402         if (!This->bindCtxTable)
403             return E_OUTOFMEMORY;
404     }
405     return S_OK;
406 }
407 /******************************************************************************
408  *        BindCtx_GetObjectParam
409  ******************************************************************************/
410 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
411 {
412     DWORD index;
413     ICOM_THIS(BindCtxImpl,iface);
414
415     TRACE("(%p,%p,%p)\n",This,pszkey,punk);
416
417     if (punk==NULL)
418         return E_POINTER;
419
420     *punk=0;
421     
422     if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
423         return E_FAIL;
424
425     IUnknown_AddRef(This->bindCtxTable[index].pObj);
426     
427     *punk = This->bindCtxTable[index].pObj;
428
429     return S_OK;
430 }
431
432 /******************************************************************************
433  *        BindCtx_RevokeObjectParam
434  ******************************************************************************/
435 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
436 {
437     DWORD index,j;
438
439     ICOM_THIS(BindCtxImpl,iface);
440
441     TRACE("(%p,%p)\n",This,ppenum);
442
443     if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
444         return E_FAIL;
445
446     /* release the object if it's found */
447     IUnknown_Release(This->bindCtxTable[index].pObj);
448     
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];
452     
453     This->bindCtxTableLastIndex--;
454
455     return S_OK;
456 }
457
458 /******************************************************************************
459  *        BindCtx_EnumObjectParam
460  ******************************************************************************/
461 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
462 {
463     FIXME("(%p,%p),stub!\n",iface,pszkey);
464     return E_NOTIMPL;
465 }
466
467 /********************************************************************************
468  *        GetObjectIndex (local function)
469  ********************************************************************************/
470 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
471                                           IUnknown* punk,
472                                           LPOLESTR pszkey,
473                                           DWORD *index)
474 {
475
476     DWORD i;
477     BYTE found=0;
478     
479     TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
480
481     if (punk==NULL)
482         /* search object identified by a register key */
483         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
484
485             if(This->bindCtxTable[i].regType==1){
486
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)
491                      )
492                    )
493
494                     found=1;
495             }
496         }
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;
502
503     if (index != NULL)
504         *index=i-1;
505
506     if (found)
507         return S_OK;
508     else
509         return S_FALSE;
510 }
511
512 /******************************************************************************
513  *        CreateBindCtx16
514  ******************************************************************************/
515 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
516 {
517     FIXME("(%ld,%p),stub!\n",reserved,ppbc);
518     return E_NOTIMPL;
519 }
520
521 /******************************************************************************
522  *        CreateBindCtx (OLE32.52)
523  ******************************************************************************/
524 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
525 {
526     BindCtxImpl* newBindCtx = 0;
527     HRESULT hr;
528     IID riid=IID_IBindCtx;
529
530     TRACE("(%ld,%p)\n",reserved,ppbc);
531
532     newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
533
534     if (newBindCtx == 0)
535         return E_OUTOFMEMORY;
536
537     hr = BindCtxImpl_Construct(newBindCtx);
538
539     if (FAILED(hr)){
540
541         HeapFree(GetProcessHeap(),0,newBindCtx);
542         return hr;
543     }
544
545     hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
546
547     return hr;
548 }