Make our ascii strings static const.
[wine] / dlls / ole32 / itemmoniker.c
1 /***************************************************************************************
2  *                            ItemMonikers 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 NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "wine/unicode.h"
35 #include "moniker.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 const CLSID CLSID_ItemMoniker = {
40   0x304, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
41 };
42
43 /* ItemMoniker data structure */
44 typedef struct ItemMonikerImpl{
45
46     ICOM_VTABLE(IMoniker)*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
47
48     /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
49      * two monikers are equal. That's whay IROTData interface is implemented by monikers.
50      */
51     ICOM_VTABLE(IROTData)*  lpvtbl2;  /* VTable relative to the IROTData interface.*/
52
53     ULONG ref; /* reference counter for this object */
54
55     LPOLESTR itemName; /* item name identified by this ItemMoniker */
56
57     LPOLESTR itemDelimiter; /* Delimiter string */
58
59 } ItemMonikerImpl;
60
61 /********************************************************************************/
62 /* ItemMoniker prototype functions :                                            */
63
64 /* IUnknown prototype functions */
65 static HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
66 static ULONG   WINAPI ItemMonikerImpl_AddRef(IMoniker* iface);
67 static ULONG   WINAPI ItemMonikerImpl_Release(IMoniker* iface);
68
69 /* IPersist prototype functions */
70 static HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
71
72 /* IPersistStream prototype functions */
73 static HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface);
74 static HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface, IStream* pStm);
75 static HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
76 static HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
77
78 /* IMoniker prototype functions */
79 static HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
80 static HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
81 static HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
82 static HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
83 static HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
84 static HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
85 static HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
86 static HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
87 static HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pItemTime);
88 static HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
89 static HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
90 static HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
91 static HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
92 static HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
93 static HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
94
95 /* Local function used by ItemMoniker implementation */
96 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* iface, LPCOLESTR lpszDelim,LPCOLESTR lpszPathName);
97 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* iface);
98
99 /********************************************************************************/
100 /* IROTData prototype functions                                                 */
101
102 /* IUnknown prototype functions */
103 static HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
104 static ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData* iface);
105 static ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface);
106
107 /* IROTData prototype function */
108 static HRESULT WINAPI ItemMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
109
110 /********************************************************************************/
111 /* Virtual function table for the ItemMonikerImpl class which  include IPersist,*/
112 /* IPersistStream and IMoniker functions.                                       */
113 static ICOM_VTABLE(IMoniker) VT_ItemMonikerImpl =
114     {
115     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
116     ItemMonikerImpl_QueryInterface,
117     ItemMonikerImpl_AddRef,
118     ItemMonikerImpl_Release,
119     ItemMonikerImpl_GetClassID,
120     ItemMonikerImpl_IsDirty,
121     ItemMonikerImpl_Load,
122     ItemMonikerImpl_Save,
123     ItemMonikerImpl_GetSizeMax,
124     ItemMonikerImpl_BindToObject,
125     ItemMonikerImpl_BindToStorage,
126     ItemMonikerImpl_Reduce,
127     ItemMonikerImpl_ComposeWith,
128     ItemMonikerImpl_Enum,
129     ItemMonikerImpl_IsEqual,
130     ItemMonikerImpl_Hash,
131     ItemMonikerImpl_IsRunning,
132     ItemMonikerImpl_GetTimeOfLastChange,
133     ItemMonikerImpl_Inverse,
134     ItemMonikerImpl_CommonPrefixWith,
135     ItemMonikerImpl_RelativePathTo,
136     ItemMonikerImpl_GetDisplayName,
137     ItemMonikerImpl_ParseDisplayName,
138     ItemMonikerImpl_IsSystemMoniker
139 };
140
141 /********************************************************************************/
142 /* Virtual function table for the IROTData class.                               */
143 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
144 {
145     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
146     ItemMonikerROTDataImpl_QueryInterface,
147     ItemMonikerROTDataImpl_AddRef,
148     ItemMonikerROTDataImpl_Release,
149     ItemMonikerROTDataImpl_GetComparaisonData
150 };
151
152 /*******************************************************************************
153  *        ItemMoniker_QueryInterface
154  *******************************************************************************/
155 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
156 {
157     ICOM_THIS(ItemMonikerImpl,iface);
158
159   TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
160
161   /* Perform a sanity check on the parameters.*/
162     if ( (This==0) || (ppvObject==0) )
163         return E_INVALIDARG;
164
165   /* Initialize the return parameter */
166   *ppvObject = 0;
167
168   /* Compare the riid with the interface IDs implemented by this object.*/
169   if (IsEqualIID(&IID_IUnknown, riid) ||
170       IsEqualIID(&IID_IPersist, riid) ||
171       IsEqualIID(&IID_IPersistStream, riid) ||
172       IsEqualIID(&IID_IMoniker, riid)
173      )
174       *ppvObject = iface;
175
176     else if (IsEqualIID(&IID_IROTData, riid))
177         *ppvObject = (IROTData*)&(This->lpvtbl2);
178
179   /* Check that we obtained an interface.*/
180     if ((*ppvObject)==0)
181         return E_NOINTERFACE;
182
183    /* Query Interface always increases the reference count by one when it is successful */
184   ItemMonikerImpl_AddRef(iface);
185
186   return S_OK;
187 }
188
189 /******************************************************************************
190  *        ItemMoniker_AddRef
191  ******************************************************************************/
192 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
193 {
194     ICOM_THIS(ItemMonikerImpl,iface);
195
196     TRACE("(%p)\n",This);
197
198     return ++(This->ref);
199 }
200
201 /******************************************************************************
202  *        ItemMoniker_Release
203  ******************************************************************************/
204 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
205 {
206     ICOM_THIS(ItemMonikerImpl,iface);
207
208     TRACE("(%p)\n",This);
209
210     This->ref--;
211
212     /* destroy the object if there's no more reference on it */
213     if (This->ref==0){
214
215         ItemMonikerImpl_Destroy(This);
216
217         return 0;
218     }
219     return This->ref;
220 }
221
222 /******************************************************************************
223  *        ItemMoniker_GetClassID
224  ******************************************************************************/
225 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
226 {
227     TRACE("(%p,%p),stub!\n",iface,pClassID);
228
229     if (pClassID==NULL)
230         return E_POINTER;
231
232     *pClassID = CLSID_ItemMoniker;
233
234     return S_OK;
235 }
236
237 /******************************************************************************
238  *        ItemMoniker_IsDirty
239  ******************************************************************************/
240 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
241 {
242     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
243        method in the OLE-provided moniker interfaces always return S_FALSE because
244        their internal state never changes. */
245
246     TRACE("(%p)\n",iface);
247
248     return S_FALSE;
249 }
250
251 /******************************************************************************
252  *        ItemMoniker_Load
253  ******************************************************************************/
254 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
255 {
256
257     ICOM_THIS(ItemMonikerImpl,iface);
258     HRESULT res;
259     DWORD delimiterLength,nameLength,lenW;
260     CHAR *itemNameA,*itemDelimiterA;
261     ULONG bread;
262
263     /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
264
265     /* read item delimiter string length + 1 */
266     res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
267     if (bread != sizeof(DWORD))
268         return E_FAIL;
269
270     /* read item delimiter string */
271     if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
272         return E_OUTOFMEMORY;
273     res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
274     if (bread != delimiterLength)
275     {
276         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
277         return E_FAIL;
278     }
279
280     lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
281     This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
282     if (!This->itemDelimiter)
283     {
284         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
285         return E_OUTOFMEMORY;
286     }
287     MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
288     HeapFree( GetProcessHeap(), 0, itemDelimiterA );
289
290     /* read item name string length + 1*/
291     res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
292     if (bread != sizeof(DWORD))
293         return E_FAIL;
294
295     /* read item name string */
296     if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
297         return E_OUTOFMEMORY;
298     res=IStream_Read(pStm,itemNameA,nameLength,&bread);
299     if (bread != nameLength)
300     {
301         HeapFree( GetProcessHeap(), 0, itemNameA );
302         return E_FAIL;
303     }
304
305     lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
306     This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
307     if (!This->itemName)
308     {
309         HeapFree( GetProcessHeap(), 0, itemNameA );
310         return E_OUTOFMEMORY;
311     }
312     MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
313     HeapFree( GetProcessHeap(), 0, itemNameA );
314
315     return res;
316 }
317
318 /******************************************************************************
319  *        ItemMoniker_Save
320  ******************************************************************************/
321 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
322                                     IStream* pStm,/* pointer to the stream where the object is to be saved */
323                                     BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
324 {
325     ICOM_THIS(ItemMonikerImpl,iface);
326     HRESULT res;
327     CHAR *itemNameA,*itemDelimiterA;
328
329     /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
330     /*                                    2) String (type A): item delimiter string ('\0' included)          */
331     /*                                    3) DWORD : size of item name string ('\0' included)       */
332     /*                                    4) String (type A): item name string ('\0' included)               */
333
334     DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
335     DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
336     itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
337     itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
338     WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
339     WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
340
341     res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
342     res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
343     res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
344     res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
345
346     return res;
347 }
348
349 /******************************************************************************
350  *        ItemMoniker_GetSizeMax
351  ******************************************************************************/
352 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
353                                           ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
354 {
355     ICOM_THIS(ItemMonikerImpl,iface);
356     DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
357     DWORD nameLength=lstrlenW(This->itemName)+1;
358
359     TRACE("(%p,%p)\n",iface,pcbSize);
360
361     if (pcbSize!=NULL)
362         return E_POINTER;
363
364     /* for more details see ItemMonikerImpl_Save coments */
365
366     pcbSize->u.LowPart =  sizeof(DWORD) + /* DWORD which contains delimiter length */
367                         delimiterLength + /* item delimiter string */
368                         sizeof(DWORD) + /* DWORD which contains item name length */
369                         nameLength + /* item name string */
370                         34; /* this constant was added ! because when I tested this function it usually */
371                             /*  returns 34 bytes more than the number of bytes used by IMoniker::Save function */
372     pcbSize->u.HighPart=0;
373
374     return S_OK;
375 }
376
377 /******************************************************************************
378  *         ItemMoniker_Construct (local function)
379  *******************************************************************************/
380 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
381 {
382
383     int sizeStr1=lstrlenW(lpszItem), sizeStr2;
384     static const OLECHAR emptystr[1];
385     LPCOLESTR   delim;
386
387     TRACE("(%p,%p)\n",This,lpszItem);
388
389     /* Initialize the virtual fgunction table. */
390     This->lpvtbl1      = &VT_ItemMonikerImpl;
391     This->lpvtbl2      = &VT_ROTDataImpl;
392     This->ref          = 0;
393
394     This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
395     if (!This->itemName)
396         return E_OUTOFMEMORY;
397     lstrcpyW(This->itemName,lpszItem);
398
399     if (!lpszDelim)
400         FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
401
402     delim = lpszDelim ? lpszDelim : emptystr;
403
404     sizeStr2=lstrlenW(delim);
405     This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
406     if (!This->itemDelimiter) {
407         HeapFree(GetProcessHeap(),0,This->itemName);
408         return E_OUTOFMEMORY;
409     }
410     lstrcpyW(This->itemDelimiter,delim);
411     return S_OK;
412 }
413
414 /******************************************************************************
415  *        ItemMoniker_Destroy (local function)
416  *******************************************************************************/
417 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
418 {
419     TRACE("(%p)\n",This);
420
421     if (This->itemName)
422         HeapFree(GetProcessHeap(),0,This->itemName);
423
424     if (This->itemDelimiter)
425         HeapFree(GetProcessHeap(),0,This->itemDelimiter);
426
427     HeapFree(GetProcessHeap(),0,This);
428
429     return S_OK;
430 }
431
432 /******************************************************************************
433  *                  ItemMoniker_BindToObject
434  ******************************************************************************/
435 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
436                                             IBindCtx* pbc,
437                                             IMoniker* pmkToLeft,
438                                             REFIID riid,
439                                             VOID** ppvResult)
440 {
441     ICOM_THIS(ItemMonikerImpl,iface);
442
443     HRESULT   res;
444     IID    refid=IID_IOleItemContainer;
445     IOleItemContainer *poic=0;
446
447     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
448
449     if(ppvResult ==NULL)
450         return E_POINTER;
451
452     if(pmkToLeft==NULL)
453         return E_INVALIDARG;
454
455     *ppvResult=0;
456
457     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)(char*)&poic);
458
459     if (SUCCEEDED(res)){
460
461         res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
462
463         IOleItemContainer_Release(poic);
464     }
465
466     return res;
467 }
468
469 /******************************************************************************
470  *        ItemMoniker_BindToStorage
471  ******************************************************************************/
472 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
473                                              IBindCtx* pbc,
474                                              IMoniker* pmkToLeft,
475                                              REFIID riid,
476                                              VOID** ppvResult)
477 {
478     ICOM_THIS(ItemMonikerImpl,iface);
479
480     HRESULT   res;
481     IOleItemContainer *poic=0;
482
483     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
484
485     *ppvResult=0;
486
487     if(pmkToLeft==NULL)
488         return E_INVALIDARG;
489
490     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)(char*)&poic);
491
492     if (SUCCEEDED(res)){
493
494         res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
495
496         IOleItemContainer_Release(poic);
497     }
498
499     return res;
500 }
501
502 /******************************************************************************
503  *        ItemMoniker_Reduce
504  ******************************************************************************/
505 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
506                                       IBindCtx* pbc,
507                                       DWORD dwReduceHowFar,
508                                       IMoniker** ppmkToLeft,
509                                       IMoniker** ppmkReduced)
510 {
511     TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
512
513     if (ppmkReduced==NULL)
514         return E_POINTER;
515
516     ItemMonikerImpl_AddRef(iface);
517
518     *ppmkReduced=iface;
519
520     return MK_S_REDUCED_TO_SELF;
521 }
522 /******************************************************************************
523  *        ItemMoniker_ComposeWith
524  ******************************************************************************/
525 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
526                                            IMoniker* pmkRight,
527                                            BOOL fOnlyIfNotGeneric,
528                                            IMoniker** ppmkComposite)
529 {
530     HRESULT res=S_OK;
531     DWORD mkSys,mkSys2;
532     IEnumMoniker* penumMk=0;
533     IMoniker *pmostLeftMk=0;
534     IMoniker* tempMkComposite=0;
535
536     TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
537
538     if ((ppmkComposite==NULL)||(pmkRight==NULL))
539         return E_POINTER;
540
541     *ppmkComposite=0;
542
543     IMoniker_IsSystemMoniker(pmkRight,&mkSys);
544
545     /* If pmkRight is an anti-moniker, the returned moniker is NULL */
546     if(mkSys==MKSYS_ANTIMONIKER)
547         return res;
548
549     else
550         /* if pmkRight is a composite whose leftmost component is an anti-moniker,           */
551         /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
552
553          if(mkSys==MKSYS_GENERICCOMPOSITE){
554
555             res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
556
557             if (FAILED(res))
558                 return res;
559
560             res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
561
562             IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
563
564             if(mkSys2==MKSYS_ANTIMONIKER){
565
566                 IMoniker_Release(pmostLeftMk);
567
568                 tempMkComposite=iface;
569                 IMoniker_AddRef(iface);
570
571                 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
572
573                     res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
574
575                     IMoniker_Release(tempMkComposite);
576                     IMoniker_Release(pmostLeftMk);
577
578                     tempMkComposite=*ppmkComposite;
579                     IMoniker_AddRef(tempMkComposite);
580                 }
581                 return res;
582             }
583             else
584                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
585          }
586          /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
587           composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
588           a NULL moniker and a return value of MK_E_NEEDGENERIC */
589           else
590             if (!fOnlyIfNotGeneric)
591                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
592
593             else
594                 return MK_E_NEEDGENERIC;
595 }
596
597 /******************************************************************************
598  *        ItemMoniker_Enum
599  ******************************************************************************/
600 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
601 {
602     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
603
604     if (ppenumMoniker == NULL)
605         return E_POINTER;
606
607     *ppenumMoniker = NULL;
608
609     return S_OK;
610 }
611
612 /******************************************************************************
613  *        ItemMoniker_IsEqual
614  ******************************************************************************/
615 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
616 {
617
618     CLSID clsid;
619     LPOLESTR dispName1,dispName2;
620     IBindCtx* bind;
621     HRESULT res = S_FALSE;
622
623     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
624
625     if (!pmkOtherMoniker) return S_FALSE;
626
627
628     /* check if both are ItemMoniker */
629     if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
630     if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
631
632     /* check if both displaynames are the same */
633     if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
634         if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
635             if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
636                 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
637                 CoTaskMemFree(dispName2);
638             }
639             CoTaskMemFree(dispName1);
640         }
641     }
642     return res;
643 }
644
645 /******************************************************************************
646  *        ItemMoniker_Hash
647  ******************************************************************************/
648 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
649 {
650     ICOM_THIS(ItemMonikerImpl,iface);
651
652     int  h = 0,i,skip,len;
653     int  off = 0;
654     LPOLESTR val;
655
656     if (pdwHash==NULL)
657         return E_POINTER;
658
659     val =  This->itemName;
660     len = lstrlenW(val);
661
662     if (len < 16) {
663         for (i = len ; i > 0; i--) {
664             h = (h * 37) + val[off++];
665         }
666     } else {
667         /* only sample some characters */
668         skip = len / 8;
669         for (i = len ; i > 0; i -= skip, off += skip) {
670             h = (h * 39) + val[off];
671         }
672     }
673
674     *pdwHash=h;
675
676     return S_OK;
677 }
678
679 /******************************************************************************
680  *        ItemMoniker_IsRunning
681  ******************************************************************************/
682 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
683                                          IBindCtx* pbc,
684                                          IMoniker* pmkToLeft,
685                                          IMoniker* pmkNewlyRunning)
686 {
687     IRunningObjectTable* rot;
688     HRESULT res;
689     IOleItemContainer *poic=0;
690     ICOM_THIS(ItemMonikerImpl,iface);
691
692     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
693
694     /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
695     /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running.              */
696     if (pmkToLeft==NULL)
697         if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
698             return S_OK;
699         else {
700             if (pbc==NULL)
701                 return E_POINTER;
702
703             res=IBindCtx_GetRunningObjectTable(pbc,&rot);
704
705             if (FAILED(res))
706                 return res;
707
708             res = IRunningObjectTable_IsRunning(rot,iface);
709
710             IRunningObjectTable_Release(rot);
711         }
712     else{
713
714         /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter,         */
715         /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
716         /* passing the string contained within this moniker. */
717
718         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)(char*)&poic);
719
720         if (SUCCEEDED(res)){
721
722             res=IOleItemContainer_IsRunning(poic,This->itemName);
723
724             IOleItemContainer_Release(poic);
725         }
726     }
727
728     return res;
729 }
730
731 /******************************************************************************
732  *        ItemMoniker_GetTimeOfLastChange
733  ******************************************************************************/
734 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
735                                                    IBindCtx* pbc,
736                                                    IMoniker* pmkToLeft,
737                                                    FILETIME* pItemTime)
738 {
739     IRunningObjectTable* rot;
740     HRESULT res;
741     IMoniker *compositeMk;
742
743     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
744
745     if (pItemTime==NULL)
746         return E_INVALIDARG;
747
748     /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
749     if (pmkToLeft==NULL)
750
751         return MK_E_NOTBINDABLE;
752     else {
753
754         /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access  */
755         /* the time of last change. If the object is not in the ROT, the method calls                          */
756         /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter.                                            */
757
758         res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
759
760         res=IBindCtx_GetRunningObjectTable(pbc,&rot);
761
762         if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
763
764             res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
765
766         IMoniker_Release(compositeMk);
767     }
768
769     return res;
770 }
771
772 /******************************************************************************
773  *        ItemMoniker_Inverse
774  ******************************************************************************/
775 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
776 {
777     TRACE("(%p,%p)\n",iface,ppmk);
778
779     if (ppmk==NULL)
780         return E_POINTER;
781
782     return CreateAntiMoniker(ppmk);
783 }
784
785 /******************************************************************************
786  *        ItemMoniker_CommonPrefixWith
787  ******************************************************************************/
788 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
789 {
790     DWORD mkSys;
791     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
792     /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
793     /* to this moniker and returns MK_S_US */
794
795     if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
796
797         *ppmkPrefix=iface;
798
799         IMoniker_AddRef(iface);
800
801         return MK_S_US;
802     }
803     else
804         /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
805         /* the case where the other moniker is a generic composite. */
806         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
807 }
808
809 /******************************************************************************
810  *        ItemMoniker_RelativePathTo
811  ******************************************************************************/
812 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
813 {
814     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
815
816     if (ppmkRelPath==NULL)
817         return E_POINTER;
818
819     *ppmkRelPath=0;
820
821     return MK_E_NOTBINDABLE;
822 }
823
824 /******************************************************************************
825  *        ItemMoniker_GetDisplayName
826  ******************************************************************************/
827 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
828                                               IBindCtx* pbc,
829                                               IMoniker* pmkToLeft,
830                                               LPOLESTR *ppszDisplayName)
831 {
832     ICOM_THIS(ItemMonikerImpl,iface);
833
834     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
835
836     if (ppszDisplayName==NULL)
837         return E_POINTER;
838
839     if (pmkToLeft!=NULL){
840         return E_INVALIDARG;
841     }
842
843     *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
844
845     if (*ppszDisplayName==NULL)
846         return E_OUTOFMEMORY;
847
848     lstrcpyW(*ppszDisplayName,This->itemDelimiter);
849     lstrcatW(*ppszDisplayName,This->itemName);
850
851     return S_OK;
852 }
853
854 /******************************************************************************
855  *        ItemMoniker_ParseDisplayName
856  ******************************************************************************/
857 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
858                                                 IBindCtx* pbc,
859                                                 IMoniker* pmkToLeft,
860                                                 LPOLESTR pszDisplayName,
861                                                 ULONG* pchEaten,
862                                                 IMoniker** ppmkOut)
863 {
864     IOleItemContainer* poic=0;
865     IParseDisplayName* ppdn=0;
866     LPOLESTR displayName;
867     HRESULT res;
868     ICOM_THIS(ItemMonikerImpl,iface);
869
870     /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
871     if (pmkToLeft==NULL)
872
873         return MK_E_SYNTAX;
874
875     else{
876         /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
877         /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
878         /* name to IParseDisplayName::ParseDisplayName */
879         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)(char*)&poic);
880
881         if (SUCCEEDED(res)){
882
883             res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)(char*)&ppdn);
884
885             res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
886
887             res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
888
889             IOleItemContainer_Release(poic);
890             IParseDisplayName_Release(ppdn);
891         }
892     }
893     return res;
894 }
895
896 /******************************************************************************
897  *        ItemMoniker_IsSystemMoniker
898  ******************************************************************************/
899 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
900 {
901     TRACE("(%p,%p)\n",iface,pwdMksys);
902
903     if (!pwdMksys)
904         return E_POINTER;
905
906     (*pwdMksys)=MKSYS_ITEMMONIKER;
907
908     return S_OK;
909 }
910
911 /*******************************************************************************
912  *        ItemMonikerIROTData_QueryInterface
913  *******************************************************************************/
914 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
915 {
916
917     ICOM_THIS_From_IROTData(IMoniker, iface);
918
919     TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
920
921     return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
922 }
923
924 /***********************************************************************
925  *        ItemMonikerIROTData_AddRef
926  */
927 ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
928 {
929     ICOM_THIS_From_IROTData(IMoniker, iface);
930
931     TRACE("(%p)\n",iface);
932
933     return ItemMonikerImpl_AddRef(This);
934 }
935
936 /***********************************************************************
937  *        ItemMonikerIROTData_Release
938  */
939 ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
940 {
941     ICOM_THIS_From_IROTData(IMoniker, iface);
942
943     TRACE("(%p)\n",iface);
944
945     return ItemMonikerImpl_Release(This);
946 }
947
948 /******************************************************************************
949  *        ItemMonikerIROTData_GetComparaisonData
950  ******************************************************************************/
951 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
952                                                          BYTE* pbData,
953                                                          ULONG cbMax,
954                                                          ULONG* pcbData)
955 {
956     FIXME("(),stub!\n");
957     return E_NOTIMPL;
958 }
959
960 /******************************************************************************
961  *        CreateItemMoniker     [OLE32.@]
962  ******************************************************************************/
963 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR  lpszItem, LPMONIKER * ppmk)
964 {
965     ItemMonikerImpl* newItemMoniker = 0;
966     HRESULT        hr = S_OK;
967     IID riid=IID_IMoniker;
968
969     TRACE("(%p,%p,%p)\n",lpszDelim,lpszItem,ppmk);
970
971     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
972
973     if (newItemMoniker == 0)
974         return STG_E_INSUFFICIENTMEMORY;
975
976     hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
977
978     if (FAILED(hr)){
979
980         HeapFree(GetProcessHeap(),0,newItemMoniker);
981     return hr;
982     }
983
984     return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&riid,(void**)ppmk);
985 }