1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************************/
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
38 #include "compobj_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 const CLSID CLSID_FileMoniker = {
43 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
46 /* filemoniker data structure */
47 typedef struct FileMonikerImpl{
49 IMonikerVtbl* lpvtbl1; /* VTable relative to the IMoniker interface.*/
51 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
52 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
54 IROTDataVtbl* lpvtbl2; /* VTable relative to the IROTData interface.*/
56 ULONG ref; /* reference counter for this object */
58 LPOLESTR filePathName; /* path string identified by this filemoniker */
62 /********************************************************************************/
63 /* FileMoniker prototype functions : */
65 /* IUnknown prototype functions */
66 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
67 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
68 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
70 /* IPersist prototype functions */
71 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
73 /* IPersistStream prototype functions */
74 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
75 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
76 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
77 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
79 /* IMoniker prototype functions */
80 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
81 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
82 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
83 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
84 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
85 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
86 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
87 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
88 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
89 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
90 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
91 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
92 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
93 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
94 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
96 /********************************************************************************/
97 /* IROTData prototype functions */
99 /* IUnknown prototype functions */
100 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
101 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
102 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
104 /* IROTData prototype function */
105 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
107 /* Local function used by filemoniker implementation */
108 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
109 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
110 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
113 /********************************************************************************/
114 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
115 /* IPersistStream and IMoniker functions. */
116 static IMonikerVtbl VT_FileMonikerImpl =
118 FileMonikerImpl_QueryInterface,
119 FileMonikerImpl_AddRef,
120 FileMonikerImpl_Release,
121 FileMonikerImpl_GetClassID,
122 FileMonikerImpl_IsDirty,
123 FileMonikerImpl_Load,
124 FileMonikerImpl_Save,
125 FileMonikerImpl_GetSizeMax,
126 FileMonikerImpl_BindToObject,
127 FileMonikerImpl_BindToStorage,
128 FileMonikerImpl_Reduce,
129 FileMonikerImpl_ComposeWith,
130 FileMonikerImpl_Enum,
131 FileMonikerImpl_IsEqual,
132 FileMonikerImpl_Hash,
133 FileMonikerImpl_IsRunning,
134 FileMonikerImpl_GetTimeOfLastChange,
135 FileMonikerImpl_Inverse,
136 FileMonikerImpl_CommonPrefixWith,
137 FileMonikerImpl_RelativePathTo,
138 FileMonikerImpl_GetDisplayName,
139 FileMonikerImpl_ParseDisplayName,
140 FileMonikerImpl_IsSystemMoniker
143 /********************************************************************************/
144 /* Virtual function table for the IROTData class. */
145 static IROTDataVtbl VT_ROTDataImpl =
147 FileMonikerROTDataImpl_QueryInterface,
148 FileMonikerROTDataImpl_AddRef,
149 FileMonikerROTDataImpl_Release,
150 FileMonikerROTDataImpl_GetComparaisonData
153 /*******************************************************************************
154 * FileMoniker_QueryInterface
155 *******************************************************************************/
156 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
158 FileMonikerImpl *This = (FileMonikerImpl *)iface;
160 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
162 /* Perform a sanity check on the parameters.*/
163 if ( (This==0) || (ppvObject==0) )
166 /* Initialize the return parameter */
169 /* Compare the riid with the interface IDs implemented by this object.*/
170 if (IsEqualIID(&IID_IUnknown, riid) ||
171 IsEqualIID(&IID_IPersist, riid) ||
172 IsEqualIID(&IID_IPersistStream,riid) ||
173 IsEqualIID(&IID_IMoniker, riid)
177 else if (IsEqualIID(&IID_IROTData, riid))
178 *ppvObject = (IROTData*)&(This->lpvtbl2);
180 /* Check that we obtained an interface.*/
182 return E_NOINTERFACE;
184 /* Query Interface always increases the reference count by one when it is successful */
185 FileMonikerImpl_AddRef(iface);
190 /******************************************************************************
192 ******************************************************************************/
193 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
195 FileMonikerImpl *This = (FileMonikerImpl *)iface;
197 TRACE("(%p)\n",iface);
199 return InterlockedIncrement(&This->ref);
202 /******************************************************************************
203 * FileMoniker_Release
204 ******************************************************************************/
205 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
207 FileMonikerImpl *This = (FileMonikerImpl *)iface;
210 TRACE("(%p)\n",iface);
212 ref = InterlockedDecrement(&This->ref);
214 /* destroy the object if there's no more reference on it */
215 if (ref == 0) FileMonikerImpl_Destroy(This);
220 /******************************************************************************
221 * FileMoniker_GetClassID
222 ******************************************************************************/
223 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
224 CLSID *pClassID)/* Pointer to CLSID of object */
226 TRACE("(%p,%p),stub!\n",iface,pClassID);
231 *pClassID = CLSID_FileMoniker;
236 /******************************************************************************
237 * FileMoniker_IsDirty
238 ******************************************************************************/
239 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
241 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
242 method in the OLE-provided moniker interfaces always return S_FALSE because
243 their internal state never changes. */
245 TRACE("(%p)\n",iface);
250 /******************************************************************************
252 ******************************************************************************/
253 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
260 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
262 FileMonikerImpl *This = (FileMonikerImpl *)iface;
264 TRACE("(%p,%p)\n",iface,pStm);
266 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
268 /* first WORD is non significative */
269 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
270 if (bread!=sizeof(WORD) || wbuffer!=0)
273 /* read filePath string length (plus one) */
274 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
275 if (bread != sizeof(DWORD))
278 /* read filePath string */
279 filePathA=HeapAlloc(GetProcessHeap(),0,length);
280 res=IStream_Read(pStm,filePathA,length,&bread);
281 HeapFree(GetProcessHeap(),0,filePathA);
285 /* read the first constant */
286 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
287 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
293 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
294 if (bread!=sizeof(WORD) || wbuffer!=0)
301 doubleLenHex=doubleLenDec=2*length;
305 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
306 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
312 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
313 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
316 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
317 if (bread!=sizeof(WORD) || wbuffer!=0x3)
320 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
322 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
323 if (bread!=doubleLenHex) {
324 HeapFree(GetProcessHeap(), 0, filePathW);
328 HeapFree(GetProcessHeap(),0,This->filePathName);
330 This->filePathName=filePathW;
335 /******************************************************************************
337 ******************************************************************************/
338 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
339 IStream* pStm,/* pointer to the stream where the object is to be saved */
340 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
342 /* this function saves data of this object. In the beginning I thougth
343 * that I have just to write the filePath string on Stream. But, when I
344 * tested this function whith windows programs samples, I noticed that it
345 * was not the case. So I analysed data written by this function on
346 * Windows and what this did function exactly ! But I have no idea about
348 * I guessed data which must be written on stream is:
349 * 1) WORD constant:zero
350 * 2) length of the path string ("\0" included)
351 * 3) path string type A
352 * 4) DWORD constant : 0xDEADFFFF
353 * 5) ten WORD constant: zero
354 * 6) DWORD: double-length of the the path string type W ("\0" not
356 * 7) WORD constant: 0x3
357 * 8) filePath unicode string.
358 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
361 FileMonikerImpl *This = (FileMonikerImpl *)iface;
364 LPOLESTR filePathW=This->filePathName;
368 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
369 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
376 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
381 /* write a DWORD set to 0 : constant */
382 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
384 /* write length of filePath string ( "\0" included )*/
385 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
386 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
388 /* write filePath string type A */
389 filePathA=HeapAlloc(GetProcessHeap(),0,len);
390 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
391 res=IStream_Write(pStm,filePathA,len,NULL);
392 HeapFree(GetProcessHeap(),0,filePathA);
394 /* write a DWORD set to 0xDEADFFFF: constant */
395 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
398 /* write 10 times a DWORD set to 0 : constants */
400 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
405 doubleLenHex=doubleLenDec=2*len;
409 /* write double-length of the path string ( "\0" included )*/
410 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
415 /* write double-length (hexa representation) of the path string ( "\0" included ) */
416 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
418 /* write a WORD set to 0x3: constant */
419 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
421 /* write path unicode string */
422 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
427 /******************************************************************************
428 * FileMoniker_GetSizeMax
429 ******************************************************************************/
430 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
431 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
433 FileMonikerImpl *This = (FileMonikerImpl *)iface;
434 DWORD len=lstrlenW(This->filePathName);
437 TRACE("(%p,%p)\n",iface,pcbSize);
442 /* for more details see FileMonikerImpl_Save coments */
444 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
445 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
446 (len+1)+ /* filePath string */
447 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
448 10*sizeof(WORD)+ /* 10 zero WORD */
449 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
451 if (len==0 || len > 8)
454 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
455 sizeof(WORD)+ /* constant : 0x3 */
456 len*sizeof(WCHAR); /* unicde filePath string */
458 pcbSize->u.LowPart=sizeMAx;
459 pcbSize->u.HighPart=0;
464 /******************************************************************************
465 * FileMoniker_Construct (local function)
466 *******************************************************************************/
467 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
470 int sizeStr=lstrlenW(lpszPathName);
472 static const WCHAR twoPoint[]={'.','.',0};
473 static const WCHAR bkSlash[]={'\\',0};
476 TRACE("(%p,%p)\n",This,lpszPathName);
478 /* Initialize the virtual fgunction table. */
479 This->lpvtbl1 = &VT_FileMonikerImpl;
480 This->lpvtbl2 = &VT_ROTDataImpl;
483 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
485 if (This->filePathName==NULL)
486 return E_OUTOFMEMORY;
488 strcpyW(This->filePathName,lpszPathName);
490 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
495 if (lstrcmpW(tabStr[0],twoPoint)!=0)
500 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
506 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
514 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
517 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
519 *This->filePathName=0;
521 for(i=0;tabStr[i]!=NULL;i++)
522 strcatW(This->filePathName,tabStr[i]);
525 strcatW(This->filePathName,bkSlash);
528 for(i=0; tabStr[i]!=NULL;i++)
529 CoTaskMemFree(tabStr[i]);
530 CoTaskMemFree(tabStr);
535 /******************************************************************************
536 * FileMoniker_Destroy (local function)
537 *******************************************************************************/
538 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
540 TRACE("(%p)\n",This);
542 HeapFree(GetProcessHeap(),0,This->filePathName);
543 HeapFree(GetProcessHeap(),0,This);
548 /******************************************************************************
549 * FileMoniker_BindToObject
550 ******************************************************************************/
551 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
560 IRunningObjectTable *prot=0;
562 IClassFactory *pcf=0;
563 IClassActivator *pca=0;
565 FileMonikerImpl *This = (FileMonikerImpl *)iface;
569 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
573 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
576 /* if the requested class was loaded before ! we don't need to reload it */
577 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
580 /* first activation of this class */
581 res=GetClassFile(This->filePathName,&clsID);
584 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
587 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
591 IUnknown_AddRef(pObj);
599 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
601 if (res==E_NOINTERFACE){
603 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
605 if (res==E_NOINTERFACE)
606 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
610 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
612 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
617 IUnknown_AddRef(pObj);
624 /*res=GetClassFile(This->filePathName,&clsID);
628 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
633 IUnknown_AddRef(pObj);
640 /* get the requested interface from the loaded class */
641 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
643 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
645 IUnknown_Release(pObj);
649 IRunningObjectTable_Release(prot);
652 IPersistFile_Release(ppf);
655 IClassActivator_Release(pca);
658 IClassFactory_Release(pcf);
663 /******************************************************************************
664 * FileMoniker_BindToStorage
665 ******************************************************************************/
666 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
676 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
678 if (pmkToLeft==NULL){
680 if (IsEqualIID(&IID_IStorage, riid)){
682 /* get the file name */
683 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
685 /* verifie if the file contains a storage object */
686 res=StgIsStorageFile(filePath);
690 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
696 IStorage_AddRef(pstg);
701 CoTaskMemFree(filePath);
704 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
707 return E_NOINTERFACE;
711 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
718 /******************************************************************************
720 ******************************************************************************/
721 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
723 DWORD dwReduceHowFar,
724 IMoniker** ppmkToLeft,
725 IMoniker** ppmkReduced)
727 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
729 if (ppmkReduced==NULL)
732 FileMonikerImpl_AddRef(iface);
736 return MK_S_REDUCED_TO_SELF;
738 /******************************************************************************
739 * FileMoniker_ComposeWith
740 ******************************************************************************/
741 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
743 BOOL fOnlyIfNotGeneric,
744 IMoniker** ppmkComposite)
747 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
748 static const WCHAR twoPoint[]={'.','.',0};
749 static const WCHAR bkSlash[]={'\\',0};
751 int i=0,j=0,lastIdx1=0,lastIdx2=0;
754 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
756 if (ppmkComposite==NULL)
764 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
766 /* check if we have two filemonikers to compose or not */
767 if(mkSys==MKSYS_FILEMONIKER){
769 CreateBindCtx(0,&bind);
771 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
772 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
774 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
775 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
776 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
778 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
781 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
784 /* for etch "..\" in the left of str2 remove the right element from str1 */
785 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
790 /* the length of the composed path string is raised by the sum of the two paths lengths */
791 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
794 return E_OUTOFMEMORY;
796 /* new path is the concatenation of the rest of str1 and str2 */
797 for(*newStr=0,j=0;j<=lastIdx1;j++)
798 strcatW(newStr,strDec1[j]);
800 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
801 strcatW(newStr,bkSlash);
803 for(j=i;j<=lastIdx2;j++)
804 strcatW(newStr,strDec2[j]);
806 /* create a new moniker with the new string */
807 res=CreateFileMoniker(newStr,ppmkComposite);
809 /* free all strings space memory used by this function */
810 HeapFree(GetProcessHeap(),0,newStr);
812 for(i=0; strDec1[i]!=NULL;i++)
813 CoTaskMemFree(strDec1[i]);
814 for(i=0; strDec2[i]!=NULL;i++)
815 CoTaskMemFree(strDec2[i]);
816 CoTaskMemFree(strDec1);
817 CoTaskMemFree(strDec2);
824 else if(mkSys==MKSYS_ANTIMONIKER){
829 else if (fOnlyIfNotGeneric){
832 return MK_E_NEEDGENERIC;
836 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
839 /******************************************************************************
841 ******************************************************************************/
842 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
844 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
846 if (ppenumMoniker == NULL)
849 *ppenumMoniker = NULL;
854 /******************************************************************************
855 * FileMoniker_IsEqual
856 ******************************************************************************/
857 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
859 FileMonikerImpl *This = (FileMonikerImpl *)iface;
865 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
867 if (pmkOtherMoniker==NULL)
870 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
872 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
875 res = CreateBindCtx(0,&bind);
876 if (FAILED(res)) return res;
878 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
879 int result = lstrcmpiW(filePath, This->filePathName);
880 CoTaskMemFree(filePath);
881 if ( result == 0 ) return S_OK;
887 /******************************************************************************
889 ******************************************************************************/
890 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
892 FileMonikerImpl *This = (FileMonikerImpl *)iface;
894 int h = 0,i,skip,len;
901 val = This->filePathName;
905 for (i = len ; i > 0; i--) {
906 h = (h * 37) + val[off++];
909 /* only sample some characters */
911 for (i = len ; i > 0; i -= skip, off += skip) {
912 h = (h * 39) + val[off];
921 /******************************************************************************
922 * FileMoniker_IsRunning
923 ******************************************************************************/
924 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
927 IMoniker* pmkNewlyRunning)
929 IRunningObjectTable* rot;
932 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
934 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
940 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
945 res = IRunningObjectTable_IsRunning(rot,iface);
947 IRunningObjectTable_Release(rot);
952 /******************************************************************************
953 * FileMoniker_GetTimeOfLastChange
954 ******************************************************************************/
955 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
960 FileMonikerImpl *This = (FileMonikerImpl *)iface;
961 IRunningObjectTable* rot;
963 WIN32_FILE_ATTRIBUTE_DATA info;
965 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
973 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
978 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
980 if (FAILED(res)){ /* the moniker is not registered */
982 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
983 return MK_E_NOOBJECT;
985 *pFileTime=info.ftLastWriteTime;
991 /******************************************************************************
992 * FileMoniker_Inverse
993 ******************************************************************************/
994 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
997 TRACE("(%p,%p)\n",iface,ppmk);
999 return CreateAntiMoniker(ppmk);
1002 /******************************************************************************
1003 * FileMoniker_CommonPrefixWith
1004 ******************************************************************************/
1005 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
1008 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1011 ULONG nb1,nb2,i,sameIdx;
1012 BOOL machimeNameCase=FALSE;
1014 if (ppmkPrefix==NULL)
1018 return E_INVALIDARG;
1022 /* check if we have the same type of moniker */
1023 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1025 if(mkSys==MKSYS_FILEMONIKER){
1028 CreateBindCtx(0,&pbind);
1030 /* create a string based on common part of the two paths */
1032 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1033 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1035 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1036 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1038 if (nb1==0 || nb2==0)
1039 return MK_E_NOPREFIX;
1041 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1045 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1046 (stringTable2[sameIdx]!=NULL) &&
1047 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1049 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1051 machimeNameCase=TRUE;
1053 for(i=2;i<sameIdx;i++)
1055 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1056 machimeNameCase=FALSE;
1061 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1064 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1065 ret = MK_E_NOPREFIX;
1068 for(i=0;i<sameIdx;i++)
1069 strcatW(commonPath,stringTable1[i]);
1072 CoTaskMemFree(stringTable1[i]);
1074 CoTaskMemFree(stringTable1);
1077 CoTaskMemFree(stringTable2[i]);
1079 CoTaskMemFree(stringTable2);
1081 ret = CreateFileMoniker(commonPath,ppmkPrefix);
1083 HeapFree(GetProcessHeap(),0,commonPath);
1087 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1090 /******************************************************************************
1091 * DecomposePath (local function)
1092 ******************************************************************************/
1093 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1095 static const WCHAR bSlash[] = {'\\',0};
1096 WCHAR word[MAX_PATH];
1097 int i=0,j,tabIndex=0;
1098 LPOLESTR *strgtable ;
1100 int len=lstrlenW(str);
1102 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1104 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1106 if (strgtable==NULL)
1107 return E_OUTOFMEMORY;
1111 if(str[i]==bSlash[0]){
1113 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1115 if (strgtable[tabIndex]==NULL)
1116 return E_OUTOFMEMORY;
1118 strcpyW(strgtable[tabIndex++],bSlash);
1125 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1130 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1132 if (strgtable[tabIndex]==NULL)
1133 return E_OUTOFMEMORY;
1135 strcpyW(strgtable[tabIndex++],word);
1138 strgtable[tabIndex]=NULL;
1140 *stringTable=strgtable;
1145 /******************************************************************************
1146 * FileMoniker_RelativePathTo
1147 ******************************************************************************/
1148 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1152 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1153 DWORD len1=0,len2=0,sameIdx=0,j=0;
1154 static const WCHAR back[] ={'.','.','\\',0};
1156 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1158 if (ppmkRelPath==NULL)
1162 return E_INVALIDARG;
1164 res=CreateBindCtx(0,&bind);
1168 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1171 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1175 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1176 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1178 if (FAILED(len1) || FAILED(len2))
1179 return E_OUTOFMEMORY;
1181 /* count the number of similar items from the begin of the two paths */
1182 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1183 (tabStr2[sameIdx]!=NULL) &&
1184 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1186 /* begin the construction of relativePath */
1187 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1188 /* by "..\\" in the begin */
1189 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1193 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1194 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1195 if (*tabStr1[j]!='\\')
1196 strcatW(relPath,back);
1198 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1199 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1200 strcatW(relPath,tabStr2[j]);
1202 res=CreateFileMoniker(relPath,ppmkRelPath);
1204 for(j=0; tabStr1[j]!=NULL;j++)
1205 CoTaskMemFree(tabStr1[j]);
1206 for(j=0; tabStr2[j]!=NULL;j++)
1207 CoTaskMemFree(tabStr2[j]);
1208 CoTaskMemFree(tabStr1);
1209 CoTaskMemFree(tabStr2);
1210 CoTaskMemFree(str1);
1211 CoTaskMemFree(str2);
1212 HeapFree(GetProcessHeap(),0,relPath);
1214 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1220 /******************************************************************************
1221 * FileMoniker_GetDisplayName
1222 ******************************************************************************/
1223 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1225 IMoniker* pmkToLeft,
1226 LPOLESTR *ppszDisplayName)
1228 FileMonikerImpl *This = (FileMonikerImpl *)iface;
1230 int len=lstrlenW(This->filePathName);
1232 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1234 if (ppszDisplayName==NULL)
1237 if (pmkToLeft!=NULL)
1238 return E_INVALIDARG;
1240 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1241 if (*ppszDisplayName==NULL)
1242 return E_OUTOFMEMORY;
1244 strcpyW(*ppszDisplayName,This->filePathName);
1249 /******************************************************************************
1250 * FileMoniker_ParseDisplayName
1251 ******************************************************************************/
1252 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1254 IMoniker* pmkToLeft,
1255 LPOLESTR pszDisplayName,
1259 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1263 /******************************************************************************
1264 * FileMoniker_IsSystemMoniker
1265 ******************************************************************************/
1266 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1268 TRACE("(%p,%p)\n",iface,pwdMksys);
1273 (*pwdMksys)=MKSYS_FILEMONIKER;
1278 /*******************************************************************************
1279 * FileMonikerIROTData_QueryInterface
1280 *******************************************************************************/
1281 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1284 ICOM_THIS_From_IROTData(IMoniker, iface);
1286 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1288 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1291 /***********************************************************************
1292 * FileMonikerIROTData_AddRef
1294 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1296 ICOM_THIS_From_IROTData(IMoniker, iface);
1298 TRACE("(%p)\n",This);
1300 return FileMonikerImpl_AddRef(This);
1303 /***********************************************************************
1304 * FileMonikerIROTData_Release
1306 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1308 ICOM_THIS_From_IROTData(IMoniker, iface);
1310 TRACE("(%p)\n",This);
1312 return FileMonikerImpl_Release(This);
1315 /******************************************************************************
1316 * FileMonikerIROTData_GetComparaisonData
1317 ******************************************************************************/
1318 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1323 FIXME("(),stub!\n");
1327 /******************************************************************************
1328 * CreateFileMoniker (OLE32.@)
1329 ******************************************************************************/
1330 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1332 FileMonikerImpl* newFileMoniker = 0;
1333 HRESULT hr = E_FAIL;
1334 IID riid=IID_IMoniker;
1336 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1341 if(lpszPathName==NULL)
1346 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1348 if (newFileMoniker == 0)
1349 return E_OUTOFMEMORY;
1351 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1354 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1356 HeapFree(GetProcessHeap(),0,newFileMoniker);