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 ***************************************************************************************/
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
36 #include "compobj_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 const CLSID CLSID_FileMoniker = {
41 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
44 /* filemoniker data structure */
45 typedef struct FileMonikerImpl{
47 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
49 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
50 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
52 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
54 ULONG ref; /* reference counter for this object */
56 LPOLESTR filePathName; /* path string identified by this filemoniker */
60 /********************************************************************************/
61 /* FileMoniker prototype functions : */
63 /* IUnknown prototype functions */
64 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
65 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
66 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
68 /* IPersist prototype functions */
69 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
71 /* IPersistStream prototype functions */
72 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
73 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
74 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
75 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
77 /* IMoniker prototype functions */
78 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
79 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
80 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
81 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
82 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
83 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
84 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
85 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
86 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
87 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
88 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
89 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
90 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
91 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
92 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
94 /********************************************************************************/
95 /* IROTData prototype functions */
97 /* IUnknown prototype functions */
98 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
99 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
100 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
102 /* IROTData prototype function */
103 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
105 /* Local function used by filemoniker implementation */
106 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
107 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
108 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
111 /********************************************************************************/
112 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
113 /* IPersistStream and IMoniker functions. */
114 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
116 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
117 FileMonikerImpl_QueryInterface,
118 FileMonikerImpl_AddRef,
119 FileMonikerImpl_Release,
120 FileMonikerImpl_GetClassID,
121 FileMonikerImpl_IsDirty,
122 FileMonikerImpl_Load,
123 FileMonikerImpl_Save,
124 FileMonikerImpl_GetSizeMax,
125 FileMonikerImpl_BindToObject,
126 FileMonikerImpl_BindToStorage,
127 FileMonikerImpl_Reduce,
128 FileMonikerImpl_ComposeWith,
129 FileMonikerImpl_Enum,
130 FileMonikerImpl_IsEqual,
131 FileMonikerImpl_Hash,
132 FileMonikerImpl_IsRunning,
133 FileMonikerImpl_GetTimeOfLastChange,
134 FileMonikerImpl_Inverse,
135 FileMonikerImpl_CommonPrefixWith,
136 FileMonikerImpl_RelativePathTo,
137 FileMonikerImpl_GetDisplayName,
138 FileMonikerImpl_ParseDisplayName,
139 FileMonikerImpl_IsSystemMoniker
142 /********************************************************************************/
143 /* Virtual function table for the IROTData class. */
144 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
146 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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 ICOM_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 ICOM_THIS(FileMonikerImpl,iface);
197 TRACE("(%p)\n",iface);
199 return ++(This->ref);
202 /******************************************************************************
203 * FileMoniker_Release
204 ******************************************************************************/
205 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
207 ICOM_THIS(FileMonikerImpl,iface);
209 TRACE("(%p)\n",iface);
213 /* destroy the object if there's no more reference on it */
216 FileMonikerImpl_Destroy(This);
223 /******************************************************************************
224 * FileMoniker_GetClassID
225 ******************************************************************************/
226 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
227 CLSID *pClassID)/* Pointer to CLSID of object */
229 TRACE("(%p,%p),stub!\n",iface,pClassID);
234 *pClassID = CLSID_FileMoniker;
239 /******************************************************************************
240 * FileMoniker_IsDirty
241 ******************************************************************************/
242 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
244 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
245 method in the OLE-provided moniker interfaces always return S_FALSE because
246 their internal state never changes. */
248 TRACE("(%p)\n",iface);
253 /******************************************************************************
255 ******************************************************************************/
256 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
263 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
265 ICOM_THIS(FileMonikerImpl,iface);
267 TRACE("(%p,%p)\n",iface,pStm);
269 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
271 /* first WORD is non significative */
272 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
273 if (bread!=sizeof(WORD) || wbuffer!=0)
276 /* read filePath string length (plus one) */
277 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
278 if (bread != sizeof(DWORD))
281 /* read filePath string */
282 filePathA=HeapAlloc(GetProcessHeap(),0,length);
283 res=IStream_Read(pStm,filePathA,length,&bread);
287 /* read the first constant */
288 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
289 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
295 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
296 if (bread!=sizeof(WORD) || wbuffer!=0)
303 doubleLenHex=doubleLenDec=2*length;
307 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
308 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
314 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
315 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
318 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
319 if (bread!=sizeof(WORD) || wbuffer!=0x3)
322 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
324 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
325 if (bread!=doubleLenHex)
328 if (This->filePathName!=NULL)
329 HeapFree(GetProcessHeap(),0,This->filePathName);
331 This->filePathName=filePathW;
333 HeapFree(GetProcessHeap(),0,filePathA);
338 /******************************************************************************
340 ******************************************************************************/
341 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
342 IStream* pStm,/* pointer to the stream where the object is to be saved */
343 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
345 /* this function saves data of this object. In the beginning I thougth
346 * that I have just to write the filePath string on Stream. But, when I
347 * tested this function whith windows programs samples, I noticed that it
348 * was not the case. So I analysed data written by this function on
349 * Windows and what this did function exactly ! But I have no idea about
351 * I guessed data which must be written on stream is:
352 * 1) WORD constant:zero
353 * 2) length of the path string ("\0" included)
354 * 3) path string type A
355 * 4) DWORD constant : 0xDEADFFFF
356 * 5) ten WORD constant: zero
357 * 6) DWORD: double-length of the the path string type W ("\0" not
359 * 7) WORD constant: 0x3
360 * 8) filePath unicode string.
361 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
364 ICOM_THIS(FileMonikerImpl,iface);
367 LPOLESTR filePathW=This->filePathName;
371 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
372 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
379 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
384 /* write a DWORD set to 0 : constant */
385 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
387 /* write length of filePath string ( "\0" included )*/
388 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
389 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
391 /* write filePath string type A */
392 filePathA=HeapAlloc(GetProcessHeap(),0,len);
393 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
394 res=IStream_Write(pStm,filePathA,len,NULL);
395 HeapFree(GetProcessHeap(),0,filePathA);
397 /* write a DWORD set to 0xDEADFFFF: constant */
398 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
401 /* write 10 times a DWORD set to 0 : constants */
403 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
408 doubleLenHex=doubleLenDec=2*len;
412 /* write double-length of the path string ( "\0" included )*/
413 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
418 /* write double-length (hexa representation) of the path string ( "\0" included ) */
419 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
421 /* write a WORD set to 0x3: constant */
422 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
424 /* write path unicode string */
425 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
430 /******************************************************************************
431 * FileMoniker_GetSizeMax
432 ******************************************************************************/
433 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
434 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
436 ICOM_THIS(FileMonikerImpl,iface);
437 DWORD len=lstrlenW(This->filePathName);
440 TRACE("(%p,%p)\n",iface,pcbSize);
445 /* for more details see FileMonikerImpl_Save coments */
447 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
448 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
449 (len+1)+ /* filePath string */
450 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
451 10*sizeof(WORD)+ /* 10 zero WORD */
452 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
454 if (len==0 || len > 8)
457 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
458 sizeof(WORD)+ /* constant : 0x3 */
459 len*sizeof(WCHAR); /* unicde filePath string */
461 pcbSize->s.LowPart=sizeMAx;
462 pcbSize->s.HighPart=0;
467 /******************************************************************************
468 * FileMoniker_Construct (local function)
469 *******************************************************************************/
470 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
473 int sizeStr=lstrlenW(lpszPathName);
475 WCHAR twoPoint[]={'.','.',0};
476 WCHAR bkSlash[]={'\\',0};
479 TRACE("(%p,%p)\n",This,lpszPathName);
481 /* Initialize the virtual fgunction table. */
482 This->lpvtbl1 = &VT_FileMonikerImpl;
483 This->lpvtbl2 = &VT_ROTDataImpl;
486 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
488 if (This->filePathName==NULL)
489 return E_OUTOFMEMORY;
491 strcpyW(This->filePathName,lpszPathName);
493 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
498 if (lstrcmpW(tabStr[0],twoPoint)!=0)
503 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
509 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
517 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
520 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
522 *This->filePathName=0;
524 for(i=0;tabStr[i]!=NULL;i++)
525 strcatW(This->filePathName,tabStr[i]);
528 strcatW(This->filePathName,bkSlash);
531 for(i=0; tabStr[i]!=NULL;i++)
532 CoTaskMemFree(tabStr[i]);
533 CoTaskMemFree(tabStr);
538 /******************************************************************************
539 * FileMoniker_Destroy (local function)
540 *******************************************************************************/
541 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
543 TRACE("(%p)\n",This);
545 if (This->filePathName!=NULL)
546 HeapFree(GetProcessHeap(),0,This->filePathName);
548 HeapFree(GetProcessHeap(),0,This);
553 /******************************************************************************
554 * FileMoniker_BindToObject
555 ******************************************************************************/
556 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
565 IRunningObjectTable *prot=0;
567 IClassFactory *pcf=0;
568 IClassActivator *pca=0;
570 ICOM_THIS(FileMonikerImpl,iface);
574 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
578 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
581 /* if the requested class was loaded befor ! we dont need to reload it */
582 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
585 /* first activation of this class */
586 res=GetClassFile(This->filePathName,&clsID);
589 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
592 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
596 IUnknown_AddRef(pObj);
604 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
606 if (res==E_NOINTERFACE){
608 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
610 if (res==E_NOINTERFACE)
611 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
615 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
617 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
622 IUnknown_AddRef(pObj);
629 /*res=GetClassFile(This->filePathName,&clsID);
633 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
638 IUnknown_AddRef(pObj);
645 /* get the requested interface from the loaded class */
646 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
648 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
650 IUnknown_Release(pObj);
654 IRunningObjectTable_Release(prot);
657 IPersistFile_Release(ppf);
660 IClassActivator_Release(pca);
663 IClassFactory_Release(pcf);
668 /******************************************************************************
669 * FileMoniker_BindToStorage
670 ******************************************************************************/
671 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
681 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
683 if (pmkToLeft==NULL){
685 if (IsEqualIID(&IID_IStorage, riid)){
687 /* get the file name */
688 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
690 /* verifie if the file contains a storage object */
691 res=StgIsStorageFile(filePath);
695 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
701 IStorage_AddRef(pstg);
706 CoTaskMemFree(filePath);
709 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
714 return E_NOINTERFACE;
718 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
725 /******************************************************************************
727 ******************************************************************************/
728 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
730 DWORD dwReduceHowFar,
731 IMoniker** ppmkToLeft,
732 IMoniker** ppmkReduced)
734 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
736 if (ppmkReduced==NULL)
739 FileMonikerImpl_AddRef(iface);
743 return MK_S_REDUCED_TO_SELF;
745 /******************************************************************************
746 * FileMoniker_ComposeWith
747 ******************************************************************************/
748 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
750 BOOL fOnlyIfNotGeneric,
751 IMoniker** ppmkComposite)
754 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
755 WCHAR twoPoint[]={'.','.',0};
756 WCHAR bkSlash[]={'\\',0};
758 int i=0,j=0,lastIdx1=0,lastIdx2=0;
761 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
763 if (ppmkComposite==NULL)
771 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
773 /* check if we have two filemonikers to compose or not */
774 if(mkSys==MKSYS_FILEMONIKER){
776 CreateBindCtx(0,&bind);
778 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
779 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
781 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
782 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
783 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
785 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
788 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
791 /* for etch "..\" in the left of str2 remove the right element from str1 */
792 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
797 /* the length of the composed path string is raised by the sum of the two paths lengths */
798 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
801 return E_OUTOFMEMORY;
803 /* new path is the concatenation of the rest of str1 and str2 */
804 for(*newStr=0,j=0;j<=lastIdx1;j++)
805 strcatW(newStr,strDec1[j]);
807 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
808 strcatW(newStr,bkSlash);
810 for(j=i;j<=lastIdx2;j++)
811 strcatW(newStr,strDec2[j]);
813 /* create a new moniker with the new string */
814 res=CreateFileMoniker(newStr,ppmkComposite);
816 /* free all strings space memory used by this function */
817 HeapFree(GetProcessHeap(),0,newStr);
819 for(i=0; strDec1[i]!=NULL;i++)
820 CoTaskMemFree(strDec1[i]);
821 for(i=0; strDec2[i]!=NULL;i++)
822 CoTaskMemFree(strDec2[i]);
823 CoTaskMemFree(strDec1);
824 CoTaskMemFree(strDec2);
831 else if(mkSys==MKSYS_ANTIMONIKER){
836 else if (fOnlyIfNotGeneric){
839 return MK_E_NEEDGENERIC;
843 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
846 /******************************************************************************
848 ******************************************************************************/
849 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
851 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
853 if (ppenumMoniker == NULL)
856 *ppenumMoniker = NULL;
861 /******************************************************************************
862 * FileMoniker_IsEqual
863 ******************************************************************************/
864 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
866 ICOM_THIS(FileMonikerImpl,iface);
872 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
874 if (pmkOtherMoniker==NULL)
877 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
879 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
882 res = CreateBindCtx(0,&bind);
883 if (FAILED(res)) return res;
885 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
886 int result = lstrcmpiW(filePath, This->filePathName);
887 CoTaskMemFree(filePath);
888 if ( result == 0 ) return S_OK;
894 /******************************************************************************
896 ******************************************************************************/
897 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
899 ICOM_THIS(FileMonikerImpl,iface);
901 int h = 0,i,skip,len;
908 val = This->filePathName;
912 for (i = len ; i > 0; i--) {
913 h = (h * 37) + val[off++];
916 /* only sample some characters */
918 for (i = len ; i > 0; i -= skip, off += skip) {
919 h = (h * 39) + val[off];
928 /******************************************************************************
929 * FileMoniker_IsRunning
930 ******************************************************************************/
931 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
934 IMoniker* pmkNewlyRunning)
936 IRunningObjectTable* rot;
939 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
941 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
947 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
952 res = IRunningObjectTable_IsRunning(rot,iface);
954 IRunningObjectTable_Release(rot);
959 /******************************************************************************
960 * FileMoniker_GetTimeOfLastChange
961 ******************************************************************************/
962 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
967 ICOM_THIS(FileMonikerImpl,iface);
968 IRunningObjectTable* rot;
970 WIN32_FILE_ATTRIBUTE_DATA info;
972 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
980 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
985 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
987 if (FAILED(res)){ /* the moniker is not registred */
989 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
990 return MK_E_NOOBJECT;
992 *pFileTime=info.ftLastWriteTime;
998 /******************************************************************************
999 * FileMoniker_Inverse
1000 ******************************************************************************/
1001 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
1004 TRACE("(%p,%p)\n",iface,ppmk);
1006 return CreateAntiMoniker(ppmk);
1009 /******************************************************************************
1010 * FileMoniker_CommonPrefixWith
1011 ******************************************************************************/
1012 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
1015 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1018 ULONG nb1,nb2,i,sameIdx;
1019 BOOL machimeNameCase=FALSE;
1021 if (ppmkPrefix==NULL)
1025 return E_INVALIDARG;
1029 /* check if we have the same type of moniker */
1030 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1032 if(mkSys==MKSYS_FILEMONIKER){
1034 CreateBindCtx(0,&pbind);
1036 /* create a string based on common part of the two paths */
1038 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1039 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1041 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1042 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1044 if (nb1==0 || nb2==0)
1045 return MK_E_NOPREFIX;
1047 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1051 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1052 (stringTable2[sameIdx]!=NULL) &&
1053 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1055 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1057 machimeNameCase=TRUE;
1059 for(i=2;i<sameIdx;i++)
1061 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1062 machimeNameCase=FALSE;
1067 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1070 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1071 return MK_E_NOPREFIX;
1073 for(i=0;i<sameIdx;i++)
1074 strcatW(commonPath,stringTable1[i]);
1077 CoTaskMemFree(stringTable1[i]);
1079 CoTaskMemFree(stringTable1);
1082 CoTaskMemFree(stringTable2[i]);
1084 CoTaskMemFree(stringTable2);
1086 HeapFree(GetProcessHeap(),0,commonPath);
1088 return CreateFileMoniker(commonPath,ppmkPrefix);
1091 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1094 /******************************************************************************
1095 * DecomposePath (local function)
1096 ******************************************************************************/
1097 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1099 WCHAR bSlash[] = {'\\',0};
1100 WCHAR word[MAX_PATH];
1101 int i=0,j,tabIndex=0;
1102 LPOLESTR *strgtable ;
1104 int len=lstrlenW(str);
1106 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1108 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1110 if (strgtable==NULL)
1111 return E_OUTOFMEMORY;
1115 if(str[i]==bSlash[0]){
1117 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1119 if (strgtable[tabIndex]==NULL)
1120 return E_OUTOFMEMORY;
1122 strcpyW(strgtable[tabIndex++],bSlash);
1129 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1134 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1136 if (strgtable[tabIndex]==NULL)
1137 return E_OUTOFMEMORY;
1139 strcpyW(strgtable[tabIndex++],word);
1142 strgtable[tabIndex]=NULL;
1144 *stringTable=strgtable;
1149 /******************************************************************************
1150 * FileMoniker_RelativePathTo
1151 ******************************************************************************/
1152 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1156 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1157 DWORD len1=0,len2=0,sameIdx=0,j=0;
1158 WCHAR back[] ={'.','.','\\',0};
1160 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1162 if (ppmkRelPath==NULL)
1166 return E_INVALIDARG;
1168 res=CreateBindCtx(0,&bind);
1172 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1175 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1179 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1180 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1182 if (FAILED(len1) || FAILED(len2))
1183 return E_OUTOFMEMORY;
1185 /* count the number of similar items from the begin of the two paths */
1186 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1187 (tabStr2[sameIdx]!=NULL) &&
1188 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1190 /* begin the construction of relativePath */
1191 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1192 /* by "..\\" in the begin */
1193 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1197 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1198 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1199 if (*tabStr1[j]!='\\')
1200 strcatW(relPath,back);
1202 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1203 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1204 strcatW(relPath,tabStr2[j]);
1206 res=CreateFileMoniker(relPath,ppmkRelPath);
1208 for(j=0; tabStr1[j]!=NULL;j++)
1209 CoTaskMemFree(tabStr1[j]);
1210 for(j=0; tabStr2[j]!=NULL;j++)
1211 CoTaskMemFree(tabStr2[j]);
1212 CoTaskMemFree(tabStr1);
1213 CoTaskMemFree(tabStr2);
1214 CoTaskMemFree(str1);
1215 CoTaskMemFree(str2);
1216 HeapFree(GetProcessHeap(),0,relPath);
1218 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1224 /******************************************************************************
1225 * FileMoniker_GetDisplayName
1226 ******************************************************************************/
1227 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1229 IMoniker* pmkToLeft,
1230 LPOLESTR *ppszDisplayName)
1232 ICOM_THIS(FileMonikerImpl,iface);
1234 int len=lstrlenW(This->filePathName);
1236 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1238 if (ppszDisplayName==NULL)
1241 if (pmkToLeft!=NULL)
1242 return E_INVALIDARG;
1244 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1245 if (*ppszDisplayName==NULL)
1246 return E_OUTOFMEMORY;
1248 strcpyW(*ppszDisplayName,This->filePathName);
1253 /******************************************************************************
1254 * FileMoniker_ParseDisplayName
1255 ******************************************************************************/
1256 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1258 IMoniker* pmkToLeft,
1259 LPOLESTR pszDisplayName,
1263 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1267 /******************************************************************************
1268 * FileMoniker_IsSystemMoniker
1269 ******************************************************************************/
1270 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1272 TRACE("(%p,%p)\n",iface,pwdMksys);
1277 (*pwdMksys)=MKSYS_FILEMONIKER;
1282 /*******************************************************************************
1283 * FileMonikerIROTData_QueryInterface
1284 *******************************************************************************/
1285 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1288 ICOM_THIS_From_IROTData(IMoniker, iface);
1290 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1292 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1295 /***********************************************************************
1296 * FileMonikerIROTData_AddRef
1298 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1300 ICOM_THIS_From_IROTData(IMoniker, iface);
1302 TRACE("(%p)\n",This);
1304 return FileMonikerImpl_AddRef(This);
1307 /***********************************************************************
1308 * FileMonikerIROTData_Release
1310 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1312 ICOM_THIS_From_IROTData(IMoniker, iface);
1314 TRACE("(%p)\n",This);
1316 return FileMonikerImpl_Release(This);
1319 /******************************************************************************
1320 * FileMonikerIROTData_GetComparaisonData
1321 ******************************************************************************/
1322 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1327 FIXME("(),stub!\n");
1331 /******************************************************************************
1332 * CreateFileMoniker (OLE2.28)
1333 ******************************************************************************/
1334 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1337 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1341 /******************************************************************************
1342 * CreateFileMoniker (OLE32.@)
1343 ******************************************************************************/
1344 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1346 FileMonikerImpl* newFileMoniker = 0;
1347 HRESULT hr = E_FAIL;
1348 IID riid=IID_IMoniker;
1350 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1355 if(lpszPathName==NULL)
1360 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1362 if (newFileMoniker == 0)
1363 return E_OUTOFMEMORY;
1365 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1368 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1370 HeapFree(GetProcessHeap(),0,newFileMoniker);