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