Fix gcc 4.0 -Wpointer-sign warnings.
[wine] / dlls / ole32 / antimoniker.c
1 /***************************************************************************************
2  *                            AntiMonikers 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 <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "objbase.h"
33 #include "wine/debug.h"
34 #include "moniker.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37
38 const CLSID CLSID_AntiMoniker = {
39   0x305, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
40 };
41
42 /* AntiMoniker data structure */
43 typedef struct AntiMonikerImpl{
44
45     const IMonikerVtbl*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
46
47     /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
48      * two monikers are equal. That's whay IROTData interface is implemented by monikers.
49      */
50     const IROTDataVtbl*  lpvtbl2;  /* VTable relative to the IROTData interface.*/
51
52     LONG ref; /* reference counter for this object */
53
54 } AntiMonikerImpl;
55
56
57 /*******************************************************************************
58  *        AntiMoniker_QueryInterface
59  *******************************************************************************/
60 static HRESULT WINAPI
61 AntiMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
62 {
63     AntiMonikerImpl *This = (AntiMonikerImpl *)iface;
64
65     TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
66
67     /* Perform a sanity check on the parameters.*/
68     if ( (This==0) || (ppvObject==0) )
69         return E_INVALIDARG;
70
71     /* Initialize the return parameter */
72     *ppvObject = 0;
73
74     /* Compare the riid with the interface IDs implemented by this object.*/
75     if (IsEqualIID(&IID_IUnknown, riid) ||
76         IsEqualIID(&IID_IPersist, riid) ||
77         IsEqualIID(&IID_IPersistStream, riid) ||
78         IsEqualIID(&IID_IMoniker, riid))
79         *ppvObject = iface;
80     else if (IsEqualIID(&IID_IROTData, riid))
81         *ppvObject = (IROTData*)&(This->lpvtbl2);
82
83     /* Check that we obtained an interface.*/
84     if ((*ppvObject)==0)
85         return E_NOINTERFACE;
86
87     /* always increase the reference count by one when it is successful */
88     IMoniker_AddRef(iface);
89
90     return S_OK;
91 }
92
93 /******************************************************************************
94  *        AntiMoniker_AddRef
95  ******************************************************************************/
96 static ULONG WINAPI
97 AntiMonikerImpl_AddRef(IMoniker* iface)
98 {
99     AntiMonikerImpl *This = (AntiMonikerImpl *)iface;
100
101     TRACE("(%p)\n",This);
102
103     return InterlockedIncrement(&This->ref);
104 }
105
106 /******************************************************************************
107  *        AntiMoniker_Release
108  ******************************************************************************/
109 static ULONG WINAPI
110 AntiMonikerImpl_Release(IMoniker* iface)
111 {
112     AntiMonikerImpl *This = (AntiMonikerImpl *)iface;
113     ULONG ref;
114
115     TRACE("(%p)\n",This);
116
117     ref = InterlockedDecrement(&This->ref);
118
119     /* destroy the object if there's no more reference on it */
120     if (ref == 0) HeapFree(GetProcessHeap(),0,This);
121
122     return ref;
123 }
124
125 /******************************************************************************
126  *        AntiMoniker_GetClassID
127  ******************************************************************************/
128 static HRESULT WINAPI
129 AntiMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
130 {
131     TRACE("(%p,%p),stub!\n",iface,pClassID);
132
133     if (pClassID==NULL)
134         return E_POINTER;
135
136     *pClassID = CLSID_AntiMoniker;
137
138     return S_OK;
139 }
140
141 /******************************************************************************
142  *        AntiMoniker_IsDirty
143  ******************************************************************************/
144 static HRESULT WINAPI
145 AntiMonikerImpl_IsDirty(IMoniker* iface)
146 {
147     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
148        method in the OLE-provided moniker interfaces always return S_FALSE because
149        their internal state never changes. */
150
151     TRACE("(%p)\n",iface);
152
153     return S_FALSE;
154 }
155
156 /******************************************************************************
157  *        AntiMoniker_Load
158  ******************************************************************************/
159 static HRESULT WINAPI
160 AntiMonikerImpl_Load(IMoniker* iface,IStream* pStm)
161 {
162     DWORD constant=1,dwbuffer;
163     HRESULT res;
164
165     /* data read by this function is only a DWORD constant (must be 1) ! */
166     res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),NULL);
167
168     if (SUCCEEDED(res)&& dwbuffer!=constant)
169         return E_FAIL;
170
171     return res;
172 }
173
174 /******************************************************************************
175  *        AntiMoniker_Save
176  ******************************************************************************/
177 static HRESULT WINAPI
178 AntiMonikerImpl_Save(IMoniker* iface,IStream* pStm,BOOL fClearDirty)
179 {
180     DWORD constant=1;
181     HRESULT res;
182
183     /* data written by this function is only a DWORD constant set to 1 ! */
184     res=IStream_Write(pStm,&constant,sizeof(constant),NULL);
185
186     return res;
187 }
188
189 /******************************************************************************
190  *        AntiMoniker_GetSizeMax
191  *
192  * PARAMS
193  * pcbSize [out] Pointer to size of stream needed to save object
194  ******************************************************************************/
195 static HRESULT WINAPI
196 AntiMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
197 {
198     TRACE("(%p,%p)\n",iface,pcbSize);
199
200     if (pcbSize!=NULL)
201         return E_POINTER;
202
203     /* for more details see AntiMonikerImpl_Save coments */
204
205     /*
206      * Normally the sizemax must be sizeof DWORD, but
207      * I tested this function it usually return 16 bytes
208      * more than the number of bytes used by AntiMoniker::Save function
209      */
210     pcbSize->u.LowPart =  sizeof(DWORD)+16;
211
212     pcbSize->u.HighPart=0;
213
214     return S_OK;
215 }
216
217 /******************************************************************************
218  *                  AntiMoniker_BindToObject
219  ******************************************************************************/
220 static HRESULT WINAPI
221 AntiMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
222                              REFIID riid, VOID** ppvResult)
223 {
224     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
225     return E_NOTIMPL;
226 }
227
228 /******************************************************************************
229  *        AntiMoniker_BindToStorage
230  ******************************************************************************/
231 static HRESULT WINAPI
232 AntiMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
233                               REFIID riid, VOID** ppvResult)
234 {
235     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
236     return E_NOTIMPL;
237 }
238
239 /******************************************************************************
240  *        AntiMoniker_Reduce
241  ******************************************************************************/
242 static HRESULT WINAPI
243 AntiMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
244                        IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
245 {
246     TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
247
248     if (ppmkReduced==NULL)
249         return E_POINTER;
250
251     AntiMonikerImpl_AddRef(iface);
252
253     *ppmkReduced=iface;
254
255     return MK_S_REDUCED_TO_SELF;
256 }
257 /******************************************************************************
258  *        AntiMoniker_ComposeWith
259  ******************************************************************************/
260 static HRESULT WINAPI
261 AntiMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
262                             BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
263 {
264
265     TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
266
267     if ((ppmkComposite==NULL)||(pmkRight==NULL))
268         return E_POINTER;
269
270     *ppmkComposite=0;
271
272     if (fOnlyIfNotGeneric)
273         return MK_E_NEEDGENERIC;
274     else
275         return CreateGenericComposite(iface,pmkRight,ppmkComposite);
276 }
277
278 /******************************************************************************
279  *        AntiMoniker_Enum
280  ******************************************************************************/
281 static HRESULT WINAPI
282 AntiMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
283 {
284     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
285
286     if (ppenumMoniker == NULL)
287         return E_POINTER;
288
289     *ppenumMoniker = NULL;
290
291     return S_OK;
292 }
293
294 /******************************************************************************
295  *        AntiMoniker_IsEqual
296  ******************************************************************************/
297 static HRESULT WINAPI
298 AntiMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
299 {
300     DWORD mkSys;
301
302     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
303
304     if (pmkOtherMoniker==NULL)
305         return S_FALSE;
306
307     IMoniker_IsSystemMoniker(pmkOtherMoniker,&mkSys);
308
309     if (mkSys==MKSYS_ANTIMONIKER)
310         return S_OK;
311     else
312         return S_FALSE;
313 }
314
315 /******************************************************************************
316  *        AntiMoniker_Hash
317  ******************************************************************************/
318 static HRESULT WINAPI AntiMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
319 {
320     if (pdwHash==NULL)
321         return E_POINTER;
322
323     *pdwHash=0;
324
325     return S_OK;
326 }
327
328 /******************************************************************************
329  *        AntiMoniker_IsRunning
330  ******************************************************************************/
331 static HRESULT WINAPI
332 AntiMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
333                           IMoniker* pmkNewlyRunning)
334 {
335     IRunningObjectTable* rot;
336     HRESULT res;
337
338     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
339
340     if (pbc==NULL)
341         return E_INVALIDARG;
342
343     res=IBindCtx_GetRunningObjectTable(pbc,&rot);
344
345     if (FAILED(res))
346         return res;
347
348     res = IRunningObjectTable_IsRunning(rot,iface);
349
350     IRunningObjectTable_Release(rot);
351
352     return res;
353 }
354
355 /******************************************************************************
356  *        AntiMoniker_GetTimeOfLastChange
357  ******************************************************************************/
358 static HRESULT WINAPI AntiMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
359                                                    IBindCtx* pbc,
360                                                    IMoniker* pmkToLeft,
361                                                    FILETIME* pAntiTime)
362 {
363     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pAntiTime);
364     return E_NOTIMPL;
365 }
366
367 /******************************************************************************
368  *        AntiMoniker_Inverse
369  ******************************************************************************/
370 static HRESULT WINAPI
371 AntiMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
372 {
373     TRACE("(%p,%p)\n",iface,ppmk);
374
375     if (ppmk==NULL)
376         return E_POINTER;
377
378     *ppmk=0;
379
380     return MK_E_NOINVERSE;
381 }
382
383 /******************************************************************************
384  *        AntiMoniker_CommonPrefixWith
385  ******************************************************************************/
386 static HRESULT WINAPI
387 AntiMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
388 {
389     DWORD mkSys;
390
391     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
392
393     if(mkSys==MKSYS_ITEMMONIKER){
394
395         IMoniker_AddRef(iface);
396
397         *ppmkPrefix=iface;
398
399         IMoniker_AddRef(iface);
400
401         return MK_S_US;
402     }
403     else
404         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
405 }
406
407 /******************************************************************************
408  *        AntiMoniker_RelativePathTo
409  ******************************************************************************/
410 static HRESULT WINAPI
411 AntiMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
412 {
413     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
414
415     if (ppmkRelPath==NULL)
416         return E_POINTER;
417
418     IMoniker_AddRef(pmOther);
419
420     *ppmkRelPath=pmOther;
421
422     return MK_S_HIM;
423 }
424
425 /******************************************************************************
426  *        AntiMoniker_GetDisplayName
427  ******************************************************************************/
428 static HRESULT WINAPI
429 AntiMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
430                                IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
431 {
432     static const WCHAR back[]={'\\','.','.',0};
433
434     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
435
436     if (ppszDisplayName==NULL)
437         return E_POINTER;
438
439     if (pmkToLeft!=NULL){
440         FIXME("() pmkToLeft!=NULL not implemented \n");
441         return E_NOTIMPL;
442     }
443
444     *ppszDisplayName=CoTaskMemAlloc(sizeof(back));
445
446     if (*ppszDisplayName==NULL)
447         return E_OUTOFMEMORY;
448
449     lstrcpyW(*ppszDisplayName,back);
450
451     return S_OK;
452 }
453
454 /******************************************************************************
455  *        AntiMoniker_ParseDisplayName
456  ******************************************************************************/
457 static HRESULT WINAPI
458 AntiMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc,
459                                  IMoniker* pmkToLeft, LPOLESTR pszDisplayName,
460                                  ULONG* pchEaten, IMoniker** ppmkOut)
461 {
462     TRACE("(%p,%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
463     return E_NOTIMPL;
464 }
465
466 /******************************************************************************
467  *        AntiMoniker_IsSystemMoniker
468  ******************************************************************************/
469 static HRESULT WINAPI
470 AntiMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
471 {
472     TRACE("(%p,%p)\n",iface,pwdMksys);
473
474     if (!pwdMksys)
475         return E_POINTER;
476
477     (*pwdMksys)=MKSYS_ANTIMONIKER;
478
479     return S_OK;
480 }
481
482 /*******************************************************************************
483  *        AntiMonikerIROTData_QueryInterface
484  *******************************************************************************/
485 static HRESULT WINAPI
486 AntiMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
487 {
488     ICOM_THIS_From_IROTData(IMoniker, iface);
489
490     TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
491
492     return AntiMonikerImpl_QueryInterface(This, riid, ppvObject);
493 }
494
495 /***********************************************************************
496  *        AntiMonikerIROTData_AddRef
497  */
498 static ULONG WINAPI AntiMonikerROTDataImpl_AddRef(IROTData *iface)
499 {
500     ICOM_THIS_From_IROTData(IMoniker, iface);
501
502     TRACE("(%p)\n",iface);
503
504     return AntiMonikerImpl_AddRef(This);
505 }
506
507 /***********************************************************************
508  *        AntiMonikerIROTData_Release
509  */
510 static ULONG WINAPI AntiMonikerROTDataImpl_Release(IROTData* iface)
511 {
512     ICOM_THIS_From_IROTData(IMoniker, iface);
513
514     TRACE("(%p)\n",iface);
515
516     return AntiMonikerImpl_Release(This);
517 }
518
519 /******************************************************************************
520  *        AntiMonikerIROTData_GetComparaisonData
521  ******************************************************************************/
522 static HRESULT WINAPI
523 AntiMonikerROTDataImpl_GetComparaisonData(IROTData* iface, BYTE* pbData,
524                                           ULONG cbMax, ULONG* pcbData)
525 {
526     FIXME("(),stub!\n");
527     return E_NOTIMPL;
528 }
529
530 /********************************************************************************/
531 /* Virtual function table for the AntiMonikerImpl class which  include IPersist,*/
532 /* IPersistStream and IMoniker functions.                                       */
533 static const IMonikerVtbl VT_AntiMonikerImpl =
534 {
535     AntiMonikerImpl_QueryInterface,
536     AntiMonikerImpl_AddRef,
537     AntiMonikerImpl_Release,
538     AntiMonikerImpl_GetClassID,
539     AntiMonikerImpl_IsDirty,
540     AntiMonikerImpl_Load,
541     AntiMonikerImpl_Save,
542     AntiMonikerImpl_GetSizeMax,
543     AntiMonikerImpl_BindToObject,
544     AntiMonikerImpl_BindToStorage,
545     AntiMonikerImpl_Reduce,
546     AntiMonikerImpl_ComposeWith,
547     AntiMonikerImpl_Enum,
548     AntiMonikerImpl_IsEqual,
549     AntiMonikerImpl_Hash,
550     AntiMonikerImpl_IsRunning,
551     AntiMonikerImpl_GetTimeOfLastChange,
552     AntiMonikerImpl_Inverse,
553     AntiMonikerImpl_CommonPrefixWith,
554     AntiMonikerImpl_RelativePathTo,
555     AntiMonikerImpl_GetDisplayName,
556     AntiMonikerImpl_ParseDisplayName,
557     AntiMonikerImpl_IsSystemMoniker
558 };
559
560 /********************************************************************************/
561 /* Virtual function table for the IROTData class.                               */
562 static const IROTDataVtbl VT_ROTDataImpl =
563 {
564     AntiMonikerROTDataImpl_QueryInterface,
565     AntiMonikerROTDataImpl_AddRef,
566     AntiMonikerROTDataImpl_Release,
567     AntiMonikerROTDataImpl_GetComparaisonData
568 };
569
570 /******************************************************************************
571  *         AntiMoniker_Construct (local function)
572  *******************************************************************************/
573 static HRESULT AntiMonikerImpl_Construct(AntiMonikerImpl* This)
574 {
575
576     TRACE("(%p)\n",This);
577
578     /* Initialize the virtual fgunction table. */
579     This->lpvtbl1      = &VT_AntiMonikerImpl;
580     This->lpvtbl2      = &VT_ROTDataImpl;
581     This->ref          = 0;
582
583     return S_OK;
584 }
585
586 /******************************************************************************
587  *        CreateAntiMoniker     [OLE32.@]
588  ******************************************************************************/
589 HRESULT WINAPI CreateAntiMoniker(LPMONIKER * ppmk)
590 {
591     AntiMonikerImpl* newAntiMoniker = 0;
592     HRESULT        hr = S_OK;
593     IID riid=IID_IMoniker;
594
595     TRACE("(%p)\n",ppmk);
596
597     newAntiMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(AntiMonikerImpl));
598
599     if (newAntiMoniker == 0)
600         return STG_E_INSUFFICIENTMEMORY;
601
602     hr = AntiMonikerImpl_Construct(newAntiMoniker);
603     if (FAILED(hr))
604     {
605         HeapFree(GetProcessHeap(),0,newAntiMoniker);
606         return hr;
607     }
608
609     hr = AntiMonikerImpl_QueryInterface((IMoniker*)newAntiMoniker,&riid,(void**)ppmk);
610
611     return hr;
612 }