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