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