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