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