1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
9 #include "debugtools.h"
15 #include "wine/obj_storage.h"
16 #include "wine/obj_base.h"
18 DEFAULT_DEBUG_CHANNEL(ole)
20 /* filemoniker data structure */
21 typedef struct FileMonikerImpl{
23 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
25 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
26 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
28 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
30 ULONG ref; /* reference counter for this object */
32 LPOLESTR filePathName; /* path string identified by this filemoniker */
36 /********************************************************************************/
37 /* FileMoniker prototype functions : */
39 /* IUnknown prototype functions */
40 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
41 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
42 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
44 /* IPersist prototype functions */
45 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
47 /* IPersistStream prototype functions */
48 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
49 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
50 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
51 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
53 /* IMoniker prototype functions */
54 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
55 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
56 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
57 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
58 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
59 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
60 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
61 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
62 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
63 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
64 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
65 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
66 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
67 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
68 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
70 /********************************************************************************/
71 /* IROTData prototype functions */
73 /* IUnknown prototype functions */
74 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
75 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
76 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
78 /* IROTData prototype function */
79 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
81 /* Local function used by filemoniker implementation */
82 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
83 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
84 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** tabStr);
87 /********************************************************************************/
88 /* Virtual function table for the FileMonikerImpl class witch include Ipersist,*/
89 /* IPersistStream and IMoniker functions. */
90 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
92 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
93 FileMonikerImpl_QueryInterface,
94 FileMonikerImpl_AddRef,
95 FileMonikerImpl_Release,
96 FileMonikerImpl_GetClassID,
97 FileMonikerImpl_IsDirty,
100 FileMonikerImpl_GetSizeMax,
101 FileMonikerImpl_BindToObject,
102 FileMonikerImpl_BindToStorage,
103 FileMonikerImpl_Reduce,
104 FileMonikerImpl_ComposeWith,
105 FileMonikerImpl_Enum,
106 FileMonikerImpl_IsEqual,
107 FileMonikerImpl_Hash,
108 FileMonikerImpl_IsRunning,
109 FileMonikerImpl_GetTimeOfLastChange,
110 FileMonikerImpl_Inverse,
111 FileMonikerImpl_CommonPrefixWith,
112 FileMonikerImpl_RelativePathTo,
113 FileMonikerImpl_GetDisplayName,
114 FileMonikerImpl_ParseDisplayName,
115 FileMonikerImpl_IsSystemMoniker
118 /********************************************************************************/
119 /* Virtual function table for the IROTData class. */
120 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
122 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
123 FileMonikerROTDataImpl_QueryInterface,
124 FileMonikerROTDataImpl_AddRef,
125 FileMonikerROTDataImpl_Release,
126 FileMonikerROTDataImpl_GetComparaisonData
129 /*******************************************************************************
130 * FileMoniker_QueryInterface
131 *******************************************************************************/
132 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
134 ICOM_THIS(FileMonikerImpl,iface);
136 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
138 /* Perform a sanity check on the parameters.*/
139 if ( (This==0) || (ppvObject==0) )
142 /* Initialize the return parameter */
145 /* Compare the riid with the interface IDs implemented by this object.*/
146 if (IsEqualIID(&IID_IUnknown, riid) ||
147 IsEqualIID(&IID_IPersist, riid) ||
148 IsEqualIID(&IID_IPersistStream,riid) ||
149 IsEqualIID(&IID_IMoniker, riid)
153 else if (IsEqualIID(&IID_IROTData, riid))
154 *ppvObject = (IROTData*)&(This->lpvtbl2);
156 /* Check that we obtained an interface.*/
158 return E_NOINTERFACE;
160 /* Query Interface always increases the reference count by one when it is successful */
161 FileMonikerImpl_AddRef(iface);
166 /******************************************************************************
168 ******************************************************************************/
169 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
171 ICOM_THIS(FileMonikerImpl,iface);
173 TRACE("(%p)\n",iface);
175 return ++(This->ref);
178 /******************************************************************************
179 * FileMoniker_Release
180 ******************************************************************************/
181 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
183 ICOM_THIS(FileMonikerImpl,iface);
185 TRACE("(%p)\n",iface);
189 /* destroy the object if there's no more reference on it */
192 FileMonikerImpl_Destroy(This);
199 /******************************************************************************
200 * FileMoniker_GetClassID
201 ******************************************************************************/
202 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
203 CLSID *pClassID)/* Pointer to CLSID of object */
205 TRACE("(%p,%p),stub!\n",iface,pClassID);
210 *pClassID = CLSID_FileMoniker;
215 /******************************************************************************
216 * FileMoniker_IsDirty
217 ******************************************************************************/
218 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
220 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
221 method in the OLE-provided moniker interfaces always return S_FALSE because
222 their internal state never changes. */
224 TRACE("(%p)\n",iface);
229 /******************************************************************************
231 ******************************************************************************/
232 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
239 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
241 ICOM_THIS(FileMonikerImpl,iface);
243 TRACE("(%p,%p)\n",iface,pStm);
245 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
247 /* first WORD is non significative */
248 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
249 if (bread!=sizeof(WORD) || wbuffer!=0)
252 /* read filePath string length (plus one) */
253 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
254 if (bread != sizeof(DWORD))
257 /* read filePath string */
258 filePathA=HeapAlloc(GetProcessHeap(),0,length);
259 res=IStream_Read(pStm,filePathA,length,&bread);
263 /* read the first constant */
264 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
265 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
271 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
272 if (bread!=sizeof(WORD) || wbuffer!=0)
279 doubleLenHex=doubleLenDec=2*length;
283 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
284 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
290 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
291 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
294 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
295 if (bread!=sizeof(WORD) || wbuffer!=0x3)
298 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
300 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
301 if (bread!=doubleLenHex)
304 if (This->filePathName!=NULL)
305 HeapFree(GetProcessHeap(),0,This->filePathName);
307 This->filePathName=filePathW;
309 HeapFree(GetProcessHeap(),0,filePathA);
314 /******************************************************************************
316 ******************************************************************************/
317 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
318 IStream* pStm,/* poniter to the stream where the object is to be saved */
319 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
321 /* this function saves data of this object. In the begining I thougth that I have just to write
322 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
323 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
324 * what did this function do exactly ! but I have no idear a bout its logic !
325 * I guessed data who must be writen on stream wich is:
326 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
327 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
328 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
329 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
332 ICOM_THIS(FileMonikerImpl,iface);
335 LPOLESTR filePathW=This->filePathName;
337 DWORD len=1+lstrlenW(filePathW);
339 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
340 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
347 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
352 /* write a DWORD seted to 0 : constant */
353 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
355 /* write length of filePath string ( "\0" included )*/
356 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
358 /* write filePath string type A */
359 filePathA=HeapAlloc(GetProcessHeap(),0,len);
360 lstrcpyWtoA(filePathA,filePathW);
361 res=IStream_Write(pStm,filePathA,len,NULL);
362 HeapFree(GetProcessHeap(),0,filePathA);
364 /* write a DWORD seted to 0xDEADFFFF: constant */
365 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
368 /* write 10 times a DWORD seted to 0 : constants */
370 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
375 doubleLenHex=doubleLenDec=2*len;
379 /* write double-length of the path string ( "\0" included )*/
380 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
385 /* write double-length (hexa representation) of the path string ( "\0" included ) */
386 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
388 /* write a WORD seted to 0x3: constant */
389 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
391 /* write path unicode string */
392 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
397 /******************************************************************************
398 * FileMoniker_GetSizeMax
399 ******************************************************************************/
400 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
401 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
403 ICOM_THIS(FileMonikerImpl,iface);
404 DWORD len=lstrlenW(This->filePathName);
407 TRACE("(%p,%p)\n",iface,pcbSize);
412 /* for more details see FileMonikerImpl_Save coments */
414 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
415 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
416 (len+1)+ /* filePath string */
417 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
418 10*sizeof(WORD)+ /* 10 zero WORD */
419 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
421 if (len==0 || len > 8)
424 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
425 sizeof(WORD)+ /* constant : 0x3 */
426 len*sizeof(WCHAR); /* unicde filePath string */
428 pcbSize->LowPart=sizeMAx;
434 /******************************************************************************
435 * FileMoniker_Construct (local function)
436 *******************************************************************************/
437 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
440 int sizeStr=lstrlenW(lpszPathName);
442 WCHAR twoPoint[]={'.','.',0};
443 WCHAR bkSlash[]={'\\',0};
446 TRACE("(%p,%p)\n",This,lpszPathName);
448 /* Initialize the virtual fgunction table. */
449 This->lpvtbl1 = &VT_FileMonikerImpl;
450 This->lpvtbl2 = &VT_ROTDataImpl;
453 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
455 if (This->filePathName==NULL)
456 return E_OUTOFMEMORY;
458 lstrcpyW(This->filePathName,lpszPathName);
460 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
465 if (lstrcmpW(tabStr[0],twoPoint)!=0)
470 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
476 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
484 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
487 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
489 *This->filePathName=0;
491 for(i=0;tabStr[i]!=NULL;i++)
492 lstrcatW(This->filePathName,tabStr[i]);
495 lstrcatW(This->filePathName,bkSlash);
498 for(i=0; tabStr[i]!=NULL;i++)
499 CoTaskMemFree(tabStr[i]);
500 CoTaskMemFree(tabStr);
505 /******************************************************************************
506 * FileMoniker_Destroy (local function)
507 *******************************************************************************/
508 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
510 TRACE("(%p)\n",This);
512 if (This->filePathName!=NULL)
513 HeapFree(GetProcessHeap(),0,This->filePathName);
515 HeapFree(GetProcessHeap(),0,This);
520 /******************************************************************************
521 * FileMoniker_BindToObject
522 ******************************************************************************/
523 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
532 IRunningObjectTable *prot=0;
534 IClassFactory *pcf=0;
535 IClassActivator *pca=0;
537 ICOM_THIS(FileMonikerImpl,iface);
541 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
545 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
548 /* if the requested class was loaded befor ! we dont need to reload it */
549 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
552 /* first activation of this class */
553 res=GetClassFile(This->filePathName,&clsID);
556 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
559 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
563 IUnknown_AddRef(pObj);
571 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
573 if (res==E_NOINTERFACE){
575 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
577 if (res==E_NOINTERFACE)
578 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
582 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
584 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
589 IUnknown_AddRef(pObj);
596 /*res=GetClassFile(This->filePathName,&clsID);
600 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
605 IUnknown_AddRef(pObj);
612 /* get the requested interface from the loaded class */
613 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
615 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
617 IUnknown_Release(pObj);
621 IRunningObjectTable_Release(prot);
624 IPersistFile_Release(ppf);
627 IClassActivator_Release(pca);
630 IClassFactory_Release(pcf);
635 /******************************************************************************
636 * FileMoniker_BindToStorage
637 ******************************************************************************/
638 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
648 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
650 if (pmkToLeft==NULL){
652 if (IsEqualIID(&IID_IStorage, riid)){
654 /* get the file name */
655 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
657 /* verifie if the file contains a storage object */
658 res=StgIsStorageFile(filePath);
662 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
668 IStorage_AddRef(pstg);
673 CoTaskMemFree(filePath);
676 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
681 return E_NOINTERFACE;
685 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
692 /******************************************************************************
694 ******************************************************************************/
695 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
697 DWORD dwReduceHowFar,
698 IMoniker** ppmkToLeft,
699 IMoniker** ppmkReduced)
701 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
703 if (ppmkReduced==NULL)
706 FileMonikerImpl_AddRef(iface);
710 return MK_S_REDUCED_TO_SELF;
712 /******************************************************************************
713 * FileMoniker_ComposeWith
714 ******************************************************************************/
715 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
717 BOOL fOnlyIfNotGeneric,
718 IMoniker** ppmkComposite)
721 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
722 WCHAR twoPoint[]={'.','.',0};
723 WCHAR bkSlash[]={'\\',0};
725 int i=0,j=0,lastIdx1=0,lastIdx2=0;
728 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
730 if (ppmkComposite==NULL)
738 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
740 /* check if we have two filemonikers to compose or not */
741 if(mkSys==MKSYS_FILEMONIKER){
743 CreateBindCtx(0,&bind);
745 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
746 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
748 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
749 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
750 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
752 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
755 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
758 /* for etch "..\" in the left of str2 remove the right element from str1 */
759 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
764 /* the length of the composed path string is raised by the sum of the two paths lengths */
765 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
768 return E_OUTOFMEMORY;
770 /* new path is the concatenation of the rest of str1 and str2 */
771 for(*newStr=0,j=0;j<=lastIdx1;j++)
772 lstrcatW(newStr,strDec1[j]);
774 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
775 lstrcatW(newStr,bkSlash);
777 for(j=i;j<=lastIdx2;j++)
778 lstrcatW(newStr,strDec2[j]);
780 /* create a new moniker with the new string */
781 res=CreateFileMoniker(newStr,ppmkComposite);
783 /* free all strings space memory used by this function */
784 HeapFree(GetProcessHeap(),0,newStr);
786 for(i=0; strDec1[i]!=NULL;i++)
787 CoTaskMemFree(strDec1[i]);
788 for(i=0; strDec2[i]!=NULL;i++)
789 CoTaskMemFree(strDec2[i]);
790 CoTaskMemFree(strDec1);
791 CoTaskMemFree(strDec2);
798 else if(mkSys==MKSYS_ANTIMONIKER){
803 else if (fOnlyIfNotGeneric){
806 return MK_E_NEEDGENERIC;
810 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
813 /******************************************************************************
815 ******************************************************************************/
816 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
818 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
820 if (ppenumMoniker == NULL)
823 *ppenumMoniker = NULL;
828 /******************************************************************************
829 * FileMoniker_IsEqual
830 ******************************************************************************/
831 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
833 ICOM_THIS(FileMonikerImpl,iface);
839 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
841 if (pmkOtherMoniker==NULL)
844 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
846 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
850 res=CreateBindCtx(0,&bind);
854 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
856 if (lstrcmpiW(filePath,
857 This->filePathName)!=0)
864 /******************************************************************************
866 ******************************************************************************/
867 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
869 ICOM_THIS(FileMonikerImpl,iface);
871 int h = 0,i,skip,len;
878 val = This->filePathName;
882 for (i = len ; i > 0; i--) {
883 h = (h * 37) + val[off++];
886 /* only sample some characters */
888 for (i = len ; i > 0; i -= skip, off += skip) {
889 h = (h * 39) + val[off];
898 /******************************************************************************
899 * FileMoniker_IsRunning
900 ******************************************************************************/
901 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
904 IMoniker* pmkNewlyRunning)
906 IRunningObjectTable* rot;
909 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
911 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
917 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
922 res = IRunningObjectTable_IsRunning(rot,iface);
924 IRunningObjectTable_Release(rot);
929 /******************************************************************************
930 * FileMoniker_GetTimeOfLastChange
931 ******************************************************************************/
932 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
937 ICOM_THIS(FileMonikerImpl,iface);
938 IRunningObjectTable* rot;
940 WIN32_FILE_ATTRIBUTE_DATA info;
942 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
950 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
955 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
957 if (FAILED(res)){ /* the moniker is not registred */
959 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
960 return MK_E_NOOBJECT;
962 *pFileTime=info.ftLastWriteTime;
968 /******************************************************************************
969 * FileMoniker_Inverse
970 ******************************************************************************/
971 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
974 TRACE("(%p,%p)\n",iface,ppmk);
976 return CreateAntiMoniker(ppmk);
979 /******************************************************************************
980 * FileMoniker_CommonPrefixWith
981 ******************************************************************************/
982 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
985 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
988 ULONG nb1,nb2,i,sameIdx;
989 BOOL machimeNameCase=FALSE;
991 if (ppmkPrefix==NULL)
999 /* check if we have the same type of moniker */
1000 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1002 if(mkSys==MKSYS_FILEMONIKER){
1004 CreateBindCtx(0,&pbind);
1006 /* create a string based on common part of the two paths */
1008 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1009 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1011 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1012 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1014 if (nb1==0 || nb2==0)
1015 return MK_E_NOPREFIX;
1017 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1021 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1022 (stringTable2[sameIdx]!=NULL) &&
1023 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1025 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1027 machimeNameCase=TRUE;
1029 for(i=2;i<sameIdx;i++)
1031 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1032 machimeNameCase=FALSE;
1037 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1040 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1041 return MK_E_NOPREFIX;
1043 for(i=0;i<sameIdx;i++)
1044 lstrcatW(commonPath,stringTable1[i]);
1047 CoTaskMemFree(stringTable1[i]);
1049 CoTaskMemFree(stringTable1);
1052 CoTaskMemFree(stringTable2[i]);
1054 CoTaskMemFree(stringTable2);
1056 HeapFree(GetProcessHeap(),0,commonPath);
1058 return CreateFileMoniker(commonPath,ppmkPrefix);
1061 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1064 /******************************************************************************
1065 * DecomposePath (local function)
1066 ******************************************************************************/
1067 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** stringTable)
1069 WCHAR bSlash[] = {'\\',0};
1071 int i=0,j,tabIndex=0;
1072 LPOLESTR *strgtable ;
1074 int len=lstrlenW(str);
1076 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1078 if (strgtable==NULL)
1079 return E_OUTOFMEMORY;
1083 if(str[i]==bSlash[0]){
1085 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1087 if (strgtable[tabIndex]==NULL)
1088 return E_OUTOFMEMORY;
1090 lstrcpyW(strgtable[tabIndex++],bSlash);
1097 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1102 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1104 if (strgtable[tabIndex]==NULL)
1105 return E_OUTOFMEMORY;
1107 lstrcpyW(strgtable[tabIndex++],word);
1110 strgtable[tabIndex]=NULL;
1112 *stringTable=strgtable;
1117 /******************************************************************************
1118 * FileMoniker_RelativePathTo
1119 ******************************************************************************/
1120 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1124 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1125 DWORD len1=0,len2=0,sameIdx=0,j=0;
1126 WCHAR back[] ={'.','.','\\',0};
1128 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1130 if (ppmkRelPath==NULL)
1134 return E_INVALIDARG;
1136 res=CreateBindCtx(0,&bind);
1140 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1143 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1147 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1148 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1150 if (FAILED(len1) || FAILED(len2))
1151 return E_OUTOFMEMORY;
1153 /* count the number of similar items from the begin of the two paths */
1154 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1155 (tabStr2[sameIdx]!=NULL) &&
1156 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1158 /* begin the construction of relativePath */
1159 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1160 /* by "..\\" in the begin */
1161 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1165 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1166 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1167 if (*tabStr1[j]!='\\')
1168 lstrcatW(relPath,back);
1170 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1171 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1172 lstrcatW(relPath,tabStr2[j]);
1174 res=CreateFileMoniker(relPath,ppmkRelPath);
1176 for(j=0; tabStr1[j]!=NULL;j++)
1177 CoTaskMemFree(tabStr1[j]);
1178 for(j=0; tabStr2[j]!=NULL;j++)
1179 CoTaskMemFree(tabStr2[j]);
1180 CoTaskMemFree(tabStr1);
1181 CoTaskMemFree(tabStr2);
1182 CoTaskMemFree(str1);
1183 CoTaskMemFree(str2);
1184 HeapFree(GetProcessHeap(),0,relPath);
1186 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1192 /******************************************************************************
1193 * FileMoniker_GetDisplayName
1194 ******************************************************************************/
1195 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1197 IMoniker* pmkToLeft,
1198 LPOLESTR *ppszDisplayName)
1200 ICOM_THIS(FileMonikerImpl,iface);
1202 int len=lstrlenW(This->filePathName);
1204 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1206 if (ppszDisplayName==NULL)
1209 if (pmkToLeft!=NULL)
1210 return E_INVALIDARG;
1212 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1213 if (*ppszDisplayName==NULL)
1214 return E_OUTOFMEMORY;
1216 lstrcpyW(*ppszDisplayName,This->filePathName);
1221 /******************************************************************************
1222 * FileMoniker_ParseDisplayName
1223 ******************************************************************************/
1224 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1226 IMoniker* pmkToLeft,
1227 LPOLESTR pszDisplayName,
1231 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1235 /******************************************************************************
1236 * FileMoniker_IsSystemMonker
1237 ******************************************************************************/
1238 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1240 TRACE("(%p,%p)\n",iface,pwdMksys);
1245 (*pwdMksys)=MKSYS_FILEMONIKER;
1250 /*******************************************************************************
1251 * FileMonikerIROTData_QueryInterface
1252 *******************************************************************************/
1253 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1256 ICOM_THIS_From_IROTData(IMoniker, iface);
1258 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1260 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1263 /***********************************************************************
1264 * FileMonikerIROTData_AddRef
1266 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1268 ICOM_THIS_From_IROTData(IMoniker, iface);
1270 TRACE("(%p)\n",This);
1272 return FileMonikerImpl_AddRef(This);
1275 /***********************************************************************
1276 * FileMonikerIROTData_Release
1278 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1280 ICOM_THIS_From_IROTData(IMoniker, iface);
1282 TRACE("(%p)\n",This);
1284 return FileMonikerImpl_Release(This);
1287 /******************************************************************************
1288 * FileMonikerIROTData_GetComparaisonData
1289 ******************************************************************************/
1290 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1295 FIXME("(),stub!\n");
1299 /******************************************************************************
1300 * CreateFileMoniker16
1301 ******************************************************************************/
1302 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1305 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1309 /******************************************************************************
1311 ******************************************************************************/
1312 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1314 FileMonikerImpl* newFileMoniker = 0;
1315 HRESULT hr = E_FAIL;
1316 IID riid=IID_IMoniker;
1318 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1323 if(lpszPathName==NULL)
1328 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1330 if (newFileMoniker == 0)
1331 return E_OUTOFMEMORY;
1333 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1336 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1338 HeapFree(GetProcessHeap(),0,newFileMoniker);