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