shell32: Remove unused variable.
[wine] / dlls / ole32 / filemoniker.c
1 /*
2  * FileMonikers implementation
3  *
4  * Copyright 1999  Noomen Hamza
5  * Copyright 2007  Robert Shearman
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <string.h>
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winnls.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "objbase.h"
37 #include "moniker.h"
38
39 #include "compobj_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 /* filemoniker data structure */
44 typedef struct FileMonikerImpl{
45     IMoniker IMoniker_iface;
46     IROTData IROTData_iface;
47     LONG ref;
48     LPOLESTR filePathName; /* path string identified by this filemoniker */
49     IUnknown *pMarshal; /* custom marshaler */
50 } FileMonikerImpl;
51
52 static inline FileMonikerImpl *impl_from_IMoniker(IMoniker *iface)
53 {
54     return CONTAINING_RECORD(iface, FileMonikerImpl, IMoniker_iface);
55 }
56
57 static inline FileMonikerImpl *impl_from_IROTData(IROTData *iface)
58 {
59     return CONTAINING_RECORD(iface, FileMonikerImpl, IROTData_iface);
60 }
61
62 /* Local function used by filemoniker implementation */
63 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
64 static HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* iface);
65
66 /*******************************************************************************
67  *        FileMoniker_QueryInterface
68  */
69 static HRESULT WINAPI
70 FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
71 {
72     FileMonikerImpl *This = impl_from_IMoniker(iface);
73
74     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
75
76     /* Perform a sanity check on the parameters.*/
77     if ( ppvObject==0 )
78         return E_INVALIDARG;
79
80     /* Initialize the return parameter */
81     *ppvObject = 0;
82
83     /* Compare the riid with the interface IDs implemented by this object.*/
84     if (IsEqualIID(&IID_IUnknown, riid)      ||
85         IsEqualIID(&IID_IPersist, riid)      ||
86         IsEqualIID(&IID_IPersistStream,riid) ||
87         IsEqualIID(&IID_IMoniker, riid)
88        )
89         *ppvObject = iface;
90
91     else if (IsEqualIID(&IID_IROTData, riid))
92         *ppvObject = &This->IROTData_iface;
93     else if (IsEqualIID(&IID_IMarshal, riid))
94     {
95         HRESULT hr = S_OK;
96         if (!This->pMarshal)
97             hr = MonikerMarshal_Create(iface, &This->pMarshal);
98         if (hr != S_OK)
99             return hr;
100         return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
101     }
102
103     /* Check that we obtained an interface.*/
104     if ((*ppvObject)==0)
105         return E_NOINTERFACE;
106
107     /* Query Interface always increases the reference count by one when it is successful */
108     IMoniker_AddRef(iface);
109
110     return S_OK;
111 }
112
113 /******************************************************************************
114  *        FileMoniker_AddRef
115  */
116 static ULONG WINAPI
117 FileMonikerImpl_AddRef(IMoniker* iface)
118 {
119     FileMonikerImpl *This = impl_from_IMoniker(iface);
120
121     TRACE("(%p)\n",iface);
122
123     return InterlockedIncrement(&This->ref);
124 }
125
126 /******************************************************************************
127  *        FileMoniker_Release
128  */
129 static ULONG WINAPI
130 FileMonikerImpl_Release(IMoniker* iface)
131 {
132     FileMonikerImpl *This = impl_from_IMoniker(iface);
133     ULONG ref;
134
135     TRACE("(%p)\n",iface);
136
137     ref = InterlockedDecrement(&This->ref);
138
139     /* destroy the object if there's no more reference on it */
140     if (ref == 0) FileMonikerImpl_Destroy(This);
141
142     return ref;
143 }
144
145 /******************************************************************************
146  *        FileMoniker_GetClassID
147  */
148 static HRESULT WINAPI
149 FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID)
150 {
151     TRACE("(%p,%p)\n",iface,pClassID);
152
153     if (pClassID==NULL)
154         return E_POINTER;
155
156     *pClassID = CLSID_FileMoniker;
157
158     return S_OK;
159 }
160
161 /******************************************************************************
162  *        FileMoniker_IsDirty
163  *
164  * Note that the OLE-provided implementations of the IPersistStream::IsDirty
165  * method in the OLE-provided moniker interfaces always return S_FALSE because
166  * their internal state never changes.
167  */
168 static HRESULT WINAPI
169 FileMonikerImpl_IsDirty(IMoniker* iface)
170 {
171
172     TRACE("(%p)\n",iface);
173
174     return S_FALSE;
175 }
176
177 /******************************************************************************
178  *        FileMoniker_Load
179  *
180  * this function locates and reads from the stream the filePath string
181  * written by FileMonikerImpl_Save
182  */
183 static HRESULT WINAPI
184 FileMonikerImpl_Load(IMoniker* iface, IStream* pStm)
185 {
186     FileMonikerImpl *This = impl_from_IMoniker(iface);
187     HRESULT res;
188     CHAR* filePathA = NULL;
189     WCHAR* filePathW = NULL;
190     ULONG bread;
191     WORD  wbuffer;
192     DWORD dwbuffer, bytesA, bytesW, len;
193     int i;
194
195
196     TRACE("(%p,%p)\n",iface,pStm);
197
198     /* first WORD */
199     res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
200     if (bread!=sizeof(WORD))
201     {
202         WARN("Couldn't read 0 word\n");
203         goto fail;
204     }
205
206     /* read filePath string length (plus one) */
207     res=IStream_Read(pStm,&bytesA,sizeof(DWORD),&bread);
208     if (bread != sizeof(DWORD))
209     {
210         WARN("Couldn't read file string length\n");
211         goto fail;
212     }
213
214     /* read filePath string */
215     filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
216     if (!filePathA)
217     {
218         res = E_OUTOFMEMORY;
219         goto fail;
220     }
221
222     res=IStream_Read(pStm,filePathA,bytesA,&bread);
223     if (bread != bytesA)
224     {
225         WARN("Couldn't read file path string\n");
226         goto fail;
227     }
228
229     /* read the unknown value */
230     IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
231     if (bread != sizeof(WORD))
232     {
233         WARN("Couldn't read unknown value\n");
234         goto fail;
235     }
236
237     /* read the DEAD constant */
238     IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
239     if (bread != sizeof(WORD))
240     {
241         WARN("Couldn't read DEAD constant\n");
242         goto fail;
243     }
244
245     for(i=0;i<5;i++)
246     {
247         res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
248         if (bread!=sizeof(DWORD))
249         {
250             WARN("Couldn't read 0 padding\n");
251             goto fail;
252         }
253     }
254
255     res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
256     if (bread!=sizeof(DWORD))
257         goto fail;
258
259     if (!dwbuffer) /* No W-string */
260     {        
261         bytesA--;
262         len=MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, bytesA, NULL, 0);
263         if (!len)
264             goto fail;
265
266         filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
267         if (!filePathW)
268         {
269             res = E_OUTOFMEMORY;
270             goto fail;
271         }
272         MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, -1, filePathW, len+1);
273         goto succeed;
274     }
275
276     if (dwbuffer < 6)
277         goto fail;
278
279     bytesW=dwbuffer - 6;
280
281     res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
282     if (bread!=sizeof(DWORD) || dwbuffer!=bytesW)
283         goto fail;
284
285     res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
286     if (bread!=sizeof(WORD) || wbuffer!=0x3)
287         goto fail;
288
289     len=bytesW/sizeof(WCHAR);
290     filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
291     if(!filePathW)
292     {
293          res = E_OUTOFMEMORY;
294          goto fail;
295     }
296     res=IStream_Read(pStm,filePathW,bytesW,&bread);
297     if (bread!=bytesW)
298          goto fail;
299
300     filePathW[len]=0;
301
302  succeed:
303     HeapFree(GetProcessHeap(),0,filePathA);
304     HeapFree(GetProcessHeap(),0,This->filePathName);
305     This->filePathName=filePathW;
306
307     return S_OK;
308
309  fail:
310     HeapFree(GetProcessHeap(), 0, filePathA);
311     HeapFree(GetProcessHeap(), 0, filePathW);
312
313     if (SUCCEEDED(res))
314          res = E_FAIL;
315     return res;
316 }
317
318 /******************************************************************************
319  *        FileMoniker_Save
320  *
321  * This function saves data of this object. In the beginning I thought
322  * that I have just to write the filePath string on Stream. But, when I
323  * tested this function with windows program samples, I noticed that it
324  * was not the case. This implementation is based on XP SP2. Other versions
325  * of Windows have minor variations.
326  *
327  * Data which must be written on stream is:
328  * 1) WORD constant: zero (not validated by Windows)
329  * 2) length of the path string ("\0" included)
330  * 3) path string type A
331  * 4) Unknown WORD value: Frequently 0xFFFF, but not always. If set very large,
332  *     Windows returns E_OUTOFMEMORY
333  * 5) WORD Constant: 0xDEAD (not validated by Windows)
334  * 6) five DWORD constant: zero (not validated by Windows)
335  * 7) If we're only writing the multibyte version,
336  *     write a zero DWORD and finish.
337  *
338  * 8) DWORD: double-length of the path string type W ("\0" not
339  *    included)
340  * 9) WORD constant: 0x3
341  * 10) filePath unicode string.
342  *
343  */
344 static HRESULT WINAPI
345 FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
346 {
347     FileMonikerImpl *This = impl_from_IMoniker(iface);
348     HRESULT res;
349     LPOLESTR filePathW=This->filePathName;
350     CHAR*    filePathA;
351     DWORD bytesA, bytesW, len;
352
353     static const WORD FFFF = 0xFFFF; /* Constants */
354     static const WORD DEAD = 0xDEAD;
355     static const DWORD ZERO     = 0;
356     static const WORD  THREE    = 0x3;
357
358     int i;
359     BOOL bUsedDefault, bWriteWide;
360
361     TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
362
363     if (pStm==NULL)
364         return E_POINTER;
365
366     /* write a 0 WORD */
367     res=IStream_Write(pStm,&ZERO,sizeof(WORD),NULL);
368     if (FAILED(res)) return res;
369
370     /* write length of filePath string ( 0 included )*/
371     bytesA = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
372     res=IStream_Write(pStm,&bytesA,sizeof(DWORD),NULL);
373     if (FAILED(res)) return res;
374
375     /* write A string (with '\0') */
376     filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
377     if (!filePathA)
378         return E_OUTOFMEMORY;
379     WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, bytesA, NULL, &bUsedDefault);
380     res=IStream_Write(pStm,filePathA,bytesA,NULL);
381     HeapFree(GetProcessHeap(),0,filePathA);
382     if (FAILED(res)) return res;
383
384     /* write a WORD 0xFFFF */
385     res=IStream_Write(pStm,&FFFF,sizeof(WORD),NULL);
386     if (FAILED(res)) return res;
387
388     /* write a WORD 0xDEAD */
389     res=IStream_Write(pStm,&DEAD,sizeof(WORD),NULL);
390     if (FAILED(res)) return res;
391
392     /* write 5 zero DWORDs */
393     for(i=0;i<5;i++)
394     {
395         res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
396         if (FAILED(res)) return res;
397     }
398
399     /* Write the wide version if:
400      *    + couldn't convert to CP_ACP, 
401      * or + it's a directory, 
402      * or + there's a character > 0xFF 
403      */
404     len = lstrlenW(filePathW);
405     bWriteWide = (bUsedDefault || (len > 0 && filePathW[len-1]=='\\' ));
406     if (!bWriteWide)
407     {
408         WCHAR* pch;
409         for(pch=filePathW;*pch;++pch) 
410         {
411             if (*pch > 0xFF)
412             {
413                 bWriteWide = TRUE;
414                 break;
415             }
416         }
417     }
418
419     if (!bWriteWide)
420         return IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
421
422     /* write bytes needed for the filepathW (without 0) + 6 */
423     bytesW = len*sizeof(WCHAR) + 6;
424     res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
425     if (FAILED(res)) return res;
426
427     /* try again, without the extra 6 */
428     bytesW -= 6;
429     res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
430     if (FAILED(res)) return res;
431
432     /* write a WORD 3 */
433     res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
434     if (FAILED(res)) return res;
435
436     /* write W string (no 0) */
437     return IStream_Write(pStm,filePathW,bytesW,NULL);
438 }
439
440 /******************************************************************************
441  *        FileMoniker_GetSizeMax
442  */
443 static HRESULT WINAPI
444 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
445 {
446     FileMonikerImpl *This = impl_from_IMoniker(iface);
447
448     TRACE("(%p,%p)\n",iface,pcbSize);
449
450     if (!pcbSize)
451         return E_POINTER;
452
453     /* We could calculate exactly (see ...::Save()) but instead
454      * we'll make a quick over-estimate, like Windows (NT4, XP) does.
455      */
456     pcbSize->u.LowPart  = 0x38 + 4 * lstrlenW(This->filePathName);
457     pcbSize->u.HighPart = 0;
458
459     return S_OK;
460 }
461
462 /******************************************************************************
463  *        FileMoniker_Destroy (local function)
464  *******************************************************************************/
465 HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* This)
466 {
467     TRACE("(%p)\n",This);
468
469     if (This->pMarshal) IUnknown_Release(This->pMarshal);
470     HeapFree(GetProcessHeap(),0,This->filePathName);
471     HeapFree(GetProcessHeap(),0,This);
472
473     return S_OK;
474 }
475
476 /******************************************************************************
477  *                  FileMoniker_BindToObject
478  */
479 static HRESULT WINAPI
480 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
481                              REFIID riid, VOID** ppvResult)
482 {
483     FileMonikerImpl *This = impl_from_IMoniker(iface);
484     HRESULT   res=E_FAIL;
485     CLSID     clsID;
486     IUnknown* pObj=0;
487     IRunningObjectTable *prot=0;
488     IPersistFile  *ppf=0;
489     IClassFactory *pcf=0;
490     IClassActivator *pca=0;
491
492     *ppvResult=0;
493
494     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
495
496     if(pmkToLeft==NULL){
497
498         res=IBindCtx_GetRunningObjectTable(pbc,&prot);
499
500         if (SUCCEEDED(res)){
501             /* if the requested class was loaded before ! we don't need to reload it */
502             res = IRunningObjectTable_GetObject(prot,iface,&pObj);
503
504             if (res==S_FALSE){
505                 /* first activation of this class */
506                 res=GetClassFile(This->filePathName,&clsID);
507                 if (SUCCEEDED(res)){
508
509                     res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
510                     if (SUCCEEDED(res)){
511
512                         res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
513                         if (SUCCEEDED(res)){
514
515                             pObj=(IUnknown*)ppf;
516                             IUnknown_AddRef(pObj);
517                         }
518                     }
519                 }
520             }
521         }
522     }
523     else{
524         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
525
526         if (res==E_NOINTERFACE){
527
528             res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
529
530             if (res==E_NOINTERFACE)
531                 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
532         }
533         if (pcf!=NULL){
534
535             IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)&ppf);
536
537             res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
538
539             if (SUCCEEDED(res)){
540
541                 pObj=(IUnknown*)ppf;
542                 IUnknown_AddRef(pObj);
543             }
544         }
545         if (pca!=NULL){
546
547             FIXME("()\n");
548
549             /*res=GetClassFile(This->filePathName,&clsID);
550
551             if (SUCCEEDED(res)){
552
553                 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
554
555                 if (SUCCEEDED(res)){
556
557                     pObj=(IUnknown*)ppf;
558                     IUnknown_AddRef(pObj);
559                 }
560             }*/
561         }
562     }
563
564     if (pObj!=NULL){
565         /* get the requested interface from the loaded class */
566         res= IUnknown_QueryInterface(pObj,riid,ppvResult);
567
568         IBindCtx_RegisterObjectBound(pbc,*ppvResult);
569
570         IUnknown_Release(pObj);
571     }
572
573     if (prot!=NULL)
574         IRunningObjectTable_Release(prot);
575
576     if (ppf!=NULL)
577         IPersistFile_Release(ppf);
578
579     if (pca!=NULL)
580         IClassActivator_Release(pca);
581
582     if (pcf!=NULL)
583         IClassFactory_Release(pcf);
584
585     return res;
586 }
587
588 /******************************************************************************
589  *        FileMoniker_BindToStorage
590  */
591 static HRESULT WINAPI
592 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
593                               REFIID riid, VOID** ppvObject)
594 {
595     LPOLESTR filePath=0;
596     IStorage *pstg=0;
597     HRESULT res;
598
599     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
600
601     if (pmkToLeft==NULL){
602
603         if (IsEqualIID(&IID_IStorage, riid)){
604
605             /* get the file name */
606             IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
607
608             /* verify if the file contains a storage object */
609             res=StgIsStorageFile(filePath);
610
611             if(res==S_OK){
612
613                 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
614
615                 if (SUCCEEDED(res)){
616
617                     *ppvObject=pstg;
618
619                     IStorage_AddRef(pstg);
620
621                     return res;
622                 }
623             }
624             CoTaskMemFree(filePath);
625         }
626         else
627             if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
628                 return E_FAIL;
629             else
630                 return E_NOINTERFACE;
631     }
632     else {
633
634         FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
635
636         return E_NOTIMPL;
637     }
638     return res;
639 }
640
641 /******************************************************************************
642  *        FileMoniker_Reduce
643  ******************************************************************************/
644 static HRESULT WINAPI
645 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
646                        IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
647 {
648     TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
649
650     if (ppmkReduced==NULL)
651         return E_POINTER;
652
653     IMoniker_AddRef(iface);
654
655     *ppmkReduced=iface;
656
657     return MK_S_REDUCED_TO_SELF;
658 }
659
660 /******************************************************************************
661  *        FileMoniker_ComposeWith
662  */
663 static HRESULT WINAPI
664 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
665                             BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
666 {
667     HRESULT res;
668     LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
669     static const WCHAR twoPoint[]={'.','.',0};
670     static const WCHAR bkSlash[]={'\\',0};
671     IBindCtx *bind=0;
672     int i=0,j=0,lastIdx1=0,lastIdx2=0;
673     DWORD mkSys;
674
675     TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
676
677     if (ppmkComposite==NULL)
678         return E_POINTER;
679
680     if (pmkRight==NULL)
681         return E_INVALIDARG;
682
683     *ppmkComposite=0;
684
685     IMoniker_IsSystemMoniker(pmkRight,&mkSys);
686
687     /* check if we have two FileMonikers to compose or not */
688     if(mkSys==MKSYS_FILEMONIKER){
689
690         CreateBindCtx(0,&bind);
691
692         IMoniker_GetDisplayName(iface,bind,NULL,&str1);
693         IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
694
695         /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
696         lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
697         lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
698
699         if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
700             return MK_E_SYNTAX;
701
702         if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
703             lastIdx1--;
704
705         /* for etch "..\" in the left of str2 remove the right element from str1 */
706         for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
707
708             lastIdx1-=2;
709         }
710
711         /* the length of the composed path string  is raised by the sum of the two paths lengths  */
712         newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
713
714         if (newStr)
715         {
716             /* new path is the concatenation of the rest of str1 and str2 */
717             for(*newStr=0,j=0;j<=lastIdx1;j++)
718                 strcatW(newStr,strDec1[j]);
719
720             if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
721                 strcatW(newStr,bkSlash);
722
723             for(j=i;j<=lastIdx2;j++)
724                 strcatW(newStr,strDec2[j]);
725
726             /* create a new moniker with the new string */
727             res=CreateFileMoniker(newStr,ppmkComposite);
728
729             /* free all strings space memory used by this function */
730             HeapFree(GetProcessHeap(),0,newStr);
731         }
732         else res = E_OUTOFMEMORY;
733
734         for(i=0; strDec1[i]!=NULL;i++)
735             CoTaskMemFree(strDec1[i]);
736         for(i=0; strDec2[i]!=NULL;i++)
737             CoTaskMemFree(strDec2[i]);
738         CoTaskMemFree(strDec1);
739         CoTaskMemFree(strDec2);
740
741         CoTaskMemFree(str1);
742         CoTaskMemFree(str2);
743
744         return res;
745     }
746     else if(mkSys==MKSYS_ANTIMONIKER){
747
748         *ppmkComposite=NULL;
749         return S_OK;
750     }
751     else if (fOnlyIfNotGeneric){
752
753         *ppmkComposite=NULL;
754         return MK_E_NEEDGENERIC;
755     }
756     else
757
758         return CreateGenericComposite(iface,pmkRight,ppmkComposite);
759 }
760
761 /******************************************************************************
762  *        FileMoniker_Enum
763  */
764 static HRESULT WINAPI
765 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
766 {
767     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
768
769     if (ppenumMoniker == NULL)
770         return E_POINTER;
771
772     *ppenumMoniker = NULL;
773
774     return S_OK;
775 }
776
777 /******************************************************************************
778  *        FileMoniker_IsEqual
779  */
780 static HRESULT WINAPI
781 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
782 {
783     FileMonikerImpl *This = impl_from_IMoniker(iface);
784     CLSID clsid;
785     LPOLESTR filePath;
786     IBindCtx* bind;
787     HRESULT res;
788
789     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
790
791     if (pmkOtherMoniker==NULL)
792         return S_FALSE;
793
794     IMoniker_GetClassID(pmkOtherMoniker,&clsid);
795
796     if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
797         return S_FALSE;
798
799     res = CreateBindCtx(0,&bind);
800     if (FAILED(res)) return res;
801
802     res = S_FALSE;
803     if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
804         if (!lstrcmpiW(filePath, This->filePathName))
805             res = S_OK;
806         CoTaskMemFree(filePath);
807     }
808
809     IBindCtx_Release(bind);
810     return res;
811 }
812
813 /******************************************************************************
814  *        FileMoniker_Hash
815  */
816 static HRESULT WINAPI
817 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
818 {
819     FileMonikerImpl *This = impl_from_IMoniker(iface);
820     int  h = 0,i,skip,len;
821     int  off = 0;
822     LPOLESTR val;
823
824     if (pdwHash==NULL)
825         return E_POINTER;
826
827     val =  This->filePathName;
828     len = lstrlenW(val);
829
830     if (len < 16) {
831         for (i = len ; i > 0; i--) {
832             h = (h * 37) + val[off++];
833         }
834     } else {
835         /* only sample some characters */
836         skip = len / 8;
837         for (i = len ; i > 0; i -= skip, off += skip) {
838             h = (h * 39) + val[off];
839         }
840     }
841
842     *pdwHash=h;
843
844     return S_OK;
845 }
846
847 /******************************************************************************
848  *        FileMoniker_IsRunning
849  */
850 static HRESULT WINAPI
851 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
852                           IMoniker* pmkNewlyRunning)
853 {
854     IRunningObjectTable* rot;
855     HRESULT res;
856
857     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
858
859     if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
860         return S_OK;
861
862     if (pbc==NULL)
863         return E_POINTER;
864
865     res=IBindCtx_GetRunningObjectTable(pbc,&rot);
866
867     if (FAILED(res))
868         return res;
869
870     res = IRunningObjectTable_IsRunning(rot,iface);
871
872     IRunningObjectTable_Release(rot);
873
874     return res;
875 }
876
877 /******************************************************************************
878  *        FileMoniker_GetTimeOfLastChange
879  ******************************************************************************/
880 static HRESULT WINAPI
881 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
882                                     IMoniker* pmkToLeft, FILETIME* pFileTime)
883 {
884     FileMonikerImpl *This = impl_from_IMoniker(iface);
885     IRunningObjectTable* rot;
886     HRESULT res;
887     WIN32_FILE_ATTRIBUTE_DATA info;
888
889     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
890
891     if (pFileTime==NULL)
892         return E_POINTER;
893
894     if (pmkToLeft!=NULL)
895         return E_INVALIDARG;
896
897     res=IBindCtx_GetRunningObjectTable(pbc,&rot);
898
899     if (FAILED(res))
900         return res;
901
902     res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
903
904     if (FAILED(res)){ /* the moniker is not registered */
905
906         if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
907             return MK_E_NOOBJECT;
908
909         *pFileTime=info.ftLastWriteTime;
910     }
911
912     return S_OK;
913 }
914
915 /******************************************************************************
916  *        FileMoniker_Inverse
917  */
918 static HRESULT WINAPI
919 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
920 {
921     TRACE("(%p,%p)\n",iface,ppmk);
922
923     return CreateAntiMoniker(ppmk);
924 }
925
926 /******************************************************************************
927  *        FileMoniker_CommonPrefixWith
928  */
929 static HRESULT WINAPI
930 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
931 {
932
933     LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
934     IBindCtx *pbind;
935     DWORD mkSys;
936     ULONG nb1,nb2,i,sameIdx;
937     BOOL machimeNameCase=FALSE;
938
939     if (ppmkPrefix==NULL)
940         return E_POINTER;
941
942     if (pmkOther==NULL)
943         return E_INVALIDARG;
944
945     *ppmkPrefix=0;
946
947     /* check if we have the same type of moniker */
948     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
949
950     if(mkSys==MKSYS_FILEMONIKER){
951         HRESULT ret;
952
953         ret = CreateBindCtx(0,&pbind);
954         if (FAILED(ret))
955             return ret;
956
957         /* create a string based on common part of the two paths */
958
959         ret = IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
960         if (FAILED(ret))
961             return ret;
962         ret = IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
963         if (FAILED(ret))
964             return ret;
965
966         nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
967         if (FAILED(nb1))
968             return nb1;
969         nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
970         if (FAILED(nb2))
971             return nb2;
972
973         if (nb1==0 || nb2==0)
974             return MK_E_NOPREFIX;
975
976         commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
977         if (!commonPath)
978             return E_OUTOFMEMORY;
979
980         *commonPath=0;
981
982         for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
983                          (stringTable2[sameIdx]!=NULL) &&
984                          (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
985
986         if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
987
988             machimeNameCase=TRUE;
989
990             for(i=2;i<sameIdx;i++)
991
992                 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
993                     machimeNameCase=FALSE;
994                     break;
995             }
996         }
997
998         if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
999             sameIdx--;
1000
1001         if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1002             ret = MK_E_NOPREFIX;
1003         else
1004         {
1005             for(i=0;i<sameIdx;i++)
1006                 strcatW(commonPath,stringTable1[i]);
1007     
1008             for(i=0;i<nb1;i++)
1009                 CoTaskMemFree(stringTable1[i]);
1010     
1011             CoTaskMemFree(stringTable1);
1012     
1013             for(i=0;i<nb2;i++)
1014                 CoTaskMemFree(stringTable2[i]);
1015     
1016             CoTaskMemFree(stringTable2);
1017     
1018             ret = CreateFileMoniker(commonPath,ppmkPrefix);
1019         }
1020         HeapFree(GetProcessHeap(),0,commonPath);
1021         return ret;
1022     }
1023     else
1024         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1025 }
1026
1027 /******************************************************************************
1028  *        DecomposePath (local function)
1029  */
1030 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1031 {
1032     static const WCHAR bSlash[] = {'\\',0};
1033     LPOLESTR word;
1034     int i=0,j,tabIndex=0, ret=0;
1035     LPOLESTR *strgtable ;
1036
1037     int len=lstrlenW(str);
1038
1039     TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1040
1041     strgtable = CoTaskMemAlloc((len + 1)*sizeof(*strgtable));
1042
1043     if (strgtable==NULL)
1044         return E_OUTOFMEMORY;
1045
1046     word = CoTaskMemAlloc((len + 1)*sizeof(WCHAR));
1047
1048     if (word==NULL)
1049     {
1050         ret = E_OUTOFMEMORY;
1051         goto lend;
1052     }
1053
1054     while(str[i]!=0){
1055
1056         if(str[i]==bSlash[0]){
1057
1058             strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1059
1060             if (strgtable[tabIndex]==NULL)
1061             {
1062                 ret = E_OUTOFMEMORY;
1063                 goto lend;
1064             }
1065
1066             strcpyW(strgtable[tabIndex++],bSlash);
1067
1068             i++;
1069
1070         }
1071         else {
1072
1073             for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1074                 word[j]=str[i];
1075
1076             word[j]=0;
1077
1078             strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1079
1080             if (strgtable[tabIndex]==NULL)
1081             {
1082                 ret = E_OUTOFMEMORY;
1083                 goto lend;
1084             }
1085
1086             strcpyW(strgtable[tabIndex++],word);
1087         }
1088     }
1089     strgtable[tabIndex]=NULL;
1090
1091     *stringTable=strgtable;
1092
1093     ret = tabIndex;
1094
1095 lend:
1096     if (ret < 0)
1097     {
1098         for (i = 0; i < tabIndex; i++)
1099             CoTaskMemFree(strgtable[i]);
1100
1101         CoTaskMemFree(strgtable);
1102     }
1103
1104     if (word)
1105         CoTaskMemFree(word);
1106
1107     return ret;
1108 }
1109
1110 /******************************************************************************
1111  *        FileMoniker_RelativePathTo
1112  */
1113 static HRESULT WINAPI
1114 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1115 {
1116     IBindCtx *bind;
1117     HRESULT res;
1118     LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1119     DWORD len1=0,len2=0,sameIdx=0,j=0;
1120     static const WCHAR back[] ={'.','.','\\',0};
1121
1122     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1123
1124     if (ppmkRelPath==NULL)
1125         return E_POINTER;
1126
1127     if (pmOther==NULL)
1128         return E_INVALIDARG;
1129
1130     res=CreateBindCtx(0,&bind);
1131     if (FAILED(res))
1132         return res;
1133
1134     res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1135     if (FAILED(res))
1136         return res;
1137     res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1138     if (FAILED(res))
1139         return res;
1140
1141     len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1142     len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1143
1144     if (FAILED(len1) || FAILED(len2))
1145         return E_OUTOFMEMORY;
1146
1147     /* count the number of similar items from the begin of the two paths */
1148     for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1149                    (tabStr2[sameIdx]!=NULL) &&
1150                (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1151
1152     /* begin the construction of relativePath */
1153     /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1154     /* by "..\\" in the begin */
1155     relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1156
1157     *relPath=0;
1158
1159     if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1160         for(j=sameIdx;(tabStr1[j] != NULL); j++)
1161             if (*tabStr1[j]!='\\')
1162                 strcatW(relPath,back);
1163
1164     /* add items of the second path (similar items with the first path are not included) to the relativePath */
1165     for(j=sameIdx;tabStr2[j]!=NULL;j++)
1166         strcatW(relPath,tabStr2[j]);
1167
1168     res=CreateFileMoniker(relPath,ppmkRelPath);
1169
1170     for(j=0; tabStr1[j]!=NULL;j++)
1171         CoTaskMemFree(tabStr1[j]);
1172     for(j=0; tabStr2[j]!=NULL;j++)
1173         CoTaskMemFree(tabStr2[j]);
1174     CoTaskMemFree(tabStr1);
1175     CoTaskMemFree(tabStr2);
1176     CoTaskMemFree(str1);
1177     CoTaskMemFree(str2);
1178     HeapFree(GetProcessHeap(),0,relPath);
1179
1180     if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1181         return MK_S_HIM;
1182
1183     return res;
1184 }
1185
1186 /******************************************************************************
1187  *        FileMoniker_GetDisplayName
1188  */
1189 static HRESULT WINAPI
1190 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1191                                IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1192 {
1193     FileMonikerImpl *This = impl_from_IMoniker(iface);
1194     int len=lstrlenW(This->filePathName);
1195
1196     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1197
1198     if (ppszDisplayName==NULL)
1199         return E_POINTER;
1200
1201     if (pmkToLeft!=NULL)
1202         return E_INVALIDARG;
1203
1204     *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1205     if (*ppszDisplayName==NULL)
1206         return E_OUTOFMEMORY;
1207
1208     strcpyW(*ppszDisplayName,This->filePathName);
1209
1210     TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1211     
1212     return S_OK;
1213 }
1214
1215 /******************************************************************************
1216  *        FileMoniker_ParseDisplayName
1217  */
1218 static HRESULT WINAPI
1219 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1220                      LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1221 {
1222     FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1223     return E_NOTIMPL;
1224 }
1225
1226 /******************************************************************************
1227  *        FileMoniker_IsSystemMoniker
1228  */
1229 static HRESULT WINAPI
1230 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1231 {
1232     TRACE("(%p,%p)\n",iface,pwdMksys);
1233
1234     if (!pwdMksys)
1235         return E_POINTER;
1236
1237     (*pwdMksys)=MKSYS_FILEMONIKER;
1238
1239     return S_OK;
1240 }
1241
1242 /*******************************************************************************
1243  *        FileMonikerIROTData_QueryInterface
1244  */
1245 static HRESULT WINAPI
1246 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1247 {
1248
1249     FileMonikerImpl *This = impl_from_IROTData(iface);
1250
1251     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1252
1253     return FileMonikerImpl_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
1254 }
1255
1256 /***********************************************************************
1257  *        FileMonikerIROTData_AddRef
1258  */
1259 static ULONG WINAPI
1260 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1261 {
1262     FileMonikerImpl *This = impl_from_IROTData(iface);
1263
1264     TRACE("(%p)\n",This);
1265
1266     return IMoniker_AddRef(&This->IMoniker_iface);
1267 }
1268
1269 /***********************************************************************
1270  *        FileMonikerIROTData_Release
1271  */
1272 static ULONG WINAPI
1273 FileMonikerROTDataImpl_Release(IROTData* iface)
1274 {
1275     FileMonikerImpl *This = impl_from_IROTData(iface);
1276
1277     TRACE("(%p)\n",This);
1278
1279     return FileMonikerImpl_Release(&This->IMoniker_iface);
1280 }
1281
1282 /******************************************************************************
1283  *        FileMonikerIROTData_GetComparisonData
1284  */
1285 static HRESULT WINAPI
1286 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1287                                           ULONG cbMax, ULONG* pcbData)
1288 {
1289     FileMonikerImpl *This = impl_from_IROTData(iface);
1290     int len = strlenW(This->filePathName)+1;
1291     int i;
1292     LPWSTR pszFileName;
1293
1294     TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1295
1296     *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1297     if (cbMax < *pcbData)
1298         return E_OUTOFMEMORY;
1299
1300     memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1301     pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1302     for (i = 0; i < len; i++)
1303         pszFileName[i] = toupperW(This->filePathName[i]);
1304
1305     return S_OK;
1306 }
1307
1308 /*
1309  * Virtual function table for the FileMonikerImpl class which include IPersist,
1310  * IPersistStream and IMoniker functions.
1311  */
1312 static const IMonikerVtbl VT_FileMonikerImpl =
1313 {
1314     FileMonikerImpl_QueryInterface,
1315     FileMonikerImpl_AddRef,
1316     FileMonikerImpl_Release,
1317     FileMonikerImpl_GetClassID,
1318     FileMonikerImpl_IsDirty,
1319     FileMonikerImpl_Load,
1320     FileMonikerImpl_Save,
1321     FileMonikerImpl_GetSizeMax,
1322     FileMonikerImpl_BindToObject,
1323     FileMonikerImpl_BindToStorage,
1324     FileMonikerImpl_Reduce,
1325     FileMonikerImpl_ComposeWith,
1326     FileMonikerImpl_Enum,
1327     FileMonikerImpl_IsEqual,
1328     FileMonikerImpl_Hash,
1329     FileMonikerImpl_IsRunning,
1330     FileMonikerImpl_GetTimeOfLastChange,
1331     FileMonikerImpl_Inverse,
1332     FileMonikerImpl_CommonPrefixWith,
1333     FileMonikerImpl_RelativePathTo,
1334     FileMonikerImpl_GetDisplayName,
1335     FileMonikerImpl_ParseDisplayName,
1336     FileMonikerImpl_IsSystemMoniker
1337 };
1338
1339 /* Virtual function table for the IROTData class. */
1340 static const IROTDataVtbl VT_ROTDataImpl =
1341 {
1342     FileMonikerROTDataImpl_QueryInterface,
1343     FileMonikerROTDataImpl_AddRef,
1344     FileMonikerROTDataImpl_Release,
1345     FileMonikerROTDataImpl_GetComparisonData
1346 };
1347
1348 /******************************************************************************
1349  *         FileMoniker_Construct (local function)
1350  */
1351 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1352 {
1353     int nb=0,i;
1354     int sizeStr=lstrlenW(lpszPathName);
1355     LPOLESTR *tabStr=0;
1356     static const WCHAR twoPoint[]={'.','.',0};
1357     static const WCHAR bkSlash[]={'\\',0};
1358     BYTE addBkSlash;
1359
1360     TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1361
1362     /* Initialize the virtual function table. */
1363     This->IMoniker_iface.lpVtbl = &VT_FileMonikerImpl;
1364     This->IROTData_iface.lpVtbl = &VT_ROTDataImpl;
1365     This->ref          = 0;
1366     This->pMarshal     = NULL;
1367
1368     This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1369
1370     if (This->filePathName==NULL)
1371         return E_OUTOFMEMORY;
1372
1373     strcpyW(This->filePathName,lpszPathName);
1374
1375     nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1376
1377     if (nb > 0 ){
1378
1379         addBkSlash=1;
1380         if (lstrcmpW(tabStr[0],twoPoint)!=0)
1381             addBkSlash=0;
1382         else
1383             for(i=0;i<nb;i++){
1384
1385                 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1386                     addBkSlash=0;
1387                     break;
1388                 }
1389                 else
1390
1391                     if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1392                         *tabStr[i]=0;
1393                         sizeStr--;
1394                         addBkSlash=0;
1395                         break;
1396                     }
1397             }
1398
1399         if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1400             addBkSlash=0;
1401
1402         This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1403
1404         *This->filePathName=0;
1405
1406         for(i=0;tabStr[i]!=NULL;i++)
1407             strcatW(This->filePathName,tabStr[i]);
1408
1409         if (addBkSlash)
1410             strcatW(This->filePathName,bkSlash);
1411     }
1412
1413     for(i=0; tabStr[i]!=NULL;i++)
1414         CoTaskMemFree(tabStr[i]);
1415     CoTaskMemFree(tabStr);
1416
1417     return S_OK;
1418 }
1419
1420 /******************************************************************************
1421  *        CreateFileMoniker (OLE32.@)
1422  ******************************************************************************/
1423 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, IMoniker **ppmk)
1424 {
1425     FileMonikerImpl* newFileMoniker;
1426     HRESULT  hr;
1427
1428     TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1429
1430     if (!ppmk)
1431         return E_POINTER;
1432
1433     if(!lpszPathName)
1434         return MK_E_SYNTAX;
1435
1436     *ppmk=NULL;
1437
1438     newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1439
1440     if (!newFileMoniker)
1441         return E_OUTOFMEMORY;
1442
1443     hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1444
1445     if (SUCCEEDED(hr))
1446         hr = IMoniker_QueryInterface(&newFileMoniker->IMoniker_iface,&IID_IMoniker,(void**)ppmk);
1447     else
1448         HeapFree(GetProcessHeap(),0,newFileMoniker);
1449
1450     return hr;
1451 }
1452
1453 /* find a character from a set in reverse without the string having to be null-terminated */
1454 static inline WCHAR *memrpbrkW(const WCHAR *ptr, size_t n, const WCHAR *accept)
1455 {
1456     const WCHAR *end, *ret = NULL;
1457     for (end = ptr + n; ptr < end; ptr++) if (strchrW(accept, *ptr)) ret = ptr;
1458     return (WCHAR *)ret;
1459 }
1460
1461 HRESULT FileMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1462                                           LPDWORD pchEaten, IMoniker **ppmk)
1463 {
1464     LPCWSTR end;
1465     static const WCHAR wszSeparators[] = {':','\\','/','!',0};
1466
1467     for (end = szDisplayName + strlenW(szDisplayName);
1468          end && (end != szDisplayName);
1469          end = memrpbrkW(szDisplayName, end - szDisplayName, wszSeparators))
1470     {
1471         HRESULT hr;
1472         IRunningObjectTable *rot;
1473         IMoniker *file_moniker;
1474         LPWSTR file_display_name;
1475         LPWSTR full_path_name;
1476         DWORD full_path_name_len;
1477         int len = end - szDisplayName;
1478
1479         file_display_name = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1480         if (!file_display_name) return E_OUTOFMEMORY;
1481         memcpy(file_display_name, szDisplayName, len * sizeof(WCHAR));
1482         file_display_name[len] = '\0';
1483
1484         hr = CreateFileMoniker(file_display_name, &file_moniker);
1485         if (FAILED(hr))
1486         {
1487             HeapFree(GetProcessHeap(), 0, file_display_name);
1488             return hr;
1489         }
1490
1491         hr = IBindCtx_GetRunningObjectTable(pbc, &rot);
1492         if (FAILED(hr))
1493         {
1494             HeapFree(GetProcessHeap(), 0, file_display_name);
1495             IMoniker_Release(file_moniker);
1496             return hr;
1497         }
1498
1499         hr = IRunningObjectTable_IsRunning(rot, file_moniker);
1500         IRunningObjectTable_Release(rot);
1501         if (FAILED(hr))
1502         {
1503             HeapFree(GetProcessHeap(), 0, file_display_name);
1504             IMoniker_Release(file_moniker);
1505             return hr;
1506         }
1507         if (hr == S_OK)
1508         {
1509             TRACE("found running file moniker for %s\n", debugstr_w(file_display_name));
1510             *pchEaten = len;
1511             *ppmk = file_moniker;
1512             HeapFree(GetProcessHeap(), 0, file_display_name);
1513             return S_OK;
1514         }
1515
1516         full_path_name_len = GetFullPathNameW(file_display_name, 0, NULL, NULL);
1517         if (!full_path_name_len)
1518         {
1519             HeapFree(GetProcessHeap(), 0, file_display_name);
1520             IMoniker_Release(file_moniker);
1521             return MK_E_SYNTAX;
1522         }
1523         full_path_name = HeapAlloc(GetProcessHeap(), 0, full_path_name_len * sizeof(WCHAR));
1524         if (!full_path_name)
1525         {
1526             HeapFree(GetProcessHeap(), 0, file_display_name);
1527             IMoniker_Release(file_moniker);
1528             return E_OUTOFMEMORY;
1529         }
1530         GetFullPathNameW(file_display_name, full_path_name_len, full_path_name, NULL);
1531
1532         if (GetFileAttributesW(full_path_name) == INVALID_FILE_ATTRIBUTES)
1533             TRACE("couldn't open file %s\n", debugstr_w(full_path_name));
1534         else
1535         {
1536             TRACE("got file moniker for %s\n", debugstr_w(szDisplayName));
1537             *pchEaten = len;
1538             *ppmk = file_moniker;
1539             HeapFree(GetProcessHeap(), 0, file_display_name);
1540             HeapFree(GetProcessHeap(), 0, full_path_name);
1541             return S_OK;
1542         }
1543         HeapFree(GetProcessHeap(), 0, file_display_name);
1544         HeapFree(GetProcessHeap(), 0, full_path_name);
1545         IMoniker_Release(file_moniker);
1546     }
1547
1548     return MK_E_CANTOPENFILE;
1549 }
1550
1551
1552 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1553                                                   REFIID riid, LPVOID *ppv)
1554 {
1555     *ppv = NULL;
1556     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1557     {
1558         *ppv = iface;
1559         IClassFactory_AddRef(iface);
1560         return S_OK;
1561     }
1562     return E_NOINTERFACE;
1563 }
1564
1565 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1566 {
1567     return 2; /* non-heap based object */
1568 }
1569
1570 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1571 {
1572     return 1; /* non-heap based object */
1573 }
1574
1575 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1576     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1577 {
1578     FileMonikerImpl* newFileMoniker;
1579     HRESULT  hr;
1580     static const WCHAR wszEmpty[] = { 0 };
1581
1582     TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1583
1584     *ppv = NULL;
1585
1586     if (pUnk)
1587         return CLASS_E_NOAGGREGATION;
1588
1589     newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1590     if (!newFileMoniker)
1591         return E_OUTOFMEMORY;
1592
1593     hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1594
1595     if (SUCCEEDED(hr))
1596         hr = IMoniker_QueryInterface(&newFileMoniker->IMoniker_iface, riid, ppv);
1597     if (FAILED(hr))
1598         HeapFree(GetProcessHeap(),0,newFileMoniker);
1599
1600     return hr;
1601 }
1602
1603 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1604 {
1605     FIXME("(%d), stub!\n",fLock);
1606     return S_OK;
1607 }
1608
1609 static const IClassFactoryVtbl FileMonikerCFVtbl =
1610 {
1611     FileMonikerCF_QueryInterface,
1612     FileMonikerCF_AddRef,
1613     FileMonikerCF_Release,
1614     FileMonikerCF_CreateInstance,
1615     FileMonikerCF_LockServer
1616 };
1617 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1618
1619 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1620 {
1621     return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);
1622 }