1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
11 #include "wine/unicode.h"
12 #include "debugtools.h"
14 #include "wine/obj_storage.h"
15 #include "wine/obj_moniker.h"
16 #include "wine/obj_base.h"
18 #include "compobj_private.h"
20 DEFAULT_DEBUG_CHANNEL(ole);
22 /* filemoniker data structure */
23 typedef struct FileMonikerImpl{
25 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
27 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
28 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
30 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
32 ULONG ref; /* reference counter for this object */
34 LPOLESTR filePathName; /* path string identified by this filemoniker */
38 /********************************************************************************/
39 /* FileMoniker prototype functions : */
41 /* IUnknown prototype functions */
42 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
43 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
44 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
46 /* IPersist prototype functions */
47 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
49 /* IPersistStream prototype functions */
50 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
51 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
52 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
53 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
55 /* IMoniker prototype functions */
56 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
57 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
58 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
59 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
60 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
61 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
62 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
63 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
64 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
65 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
66 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
67 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
68 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
69 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
70 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
72 /********************************************************************************/
73 /* IROTData prototype functions */
75 /* IUnknown prototype functions */
76 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
77 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
78 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
80 /* IROTData prototype function */
81 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
83 /* Local function used by filemoniker implementation */
84 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
85 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
86 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** tabStr);
89 /********************************************************************************/
90 /* Virtual function table for the FileMonikerImpl class witch include Ipersist,*/
91 /* IPersistStream and IMoniker functions. */
92 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
94 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
95 FileMonikerImpl_QueryInterface,
96 FileMonikerImpl_AddRef,
97 FileMonikerImpl_Release,
98 FileMonikerImpl_GetClassID,
99 FileMonikerImpl_IsDirty,
100 FileMonikerImpl_Load,
101 FileMonikerImpl_Save,
102 FileMonikerImpl_GetSizeMax,
103 FileMonikerImpl_BindToObject,
104 FileMonikerImpl_BindToStorage,
105 FileMonikerImpl_Reduce,
106 FileMonikerImpl_ComposeWith,
107 FileMonikerImpl_Enum,
108 FileMonikerImpl_IsEqual,
109 FileMonikerImpl_Hash,
110 FileMonikerImpl_IsRunning,
111 FileMonikerImpl_GetTimeOfLastChange,
112 FileMonikerImpl_Inverse,
113 FileMonikerImpl_CommonPrefixWith,
114 FileMonikerImpl_RelativePathTo,
115 FileMonikerImpl_GetDisplayName,
116 FileMonikerImpl_ParseDisplayName,
117 FileMonikerImpl_IsSystemMoniker
120 /********************************************************************************/
121 /* Virtual function table for the IROTData class. */
122 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
124 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
125 FileMonikerROTDataImpl_QueryInterface,
126 FileMonikerROTDataImpl_AddRef,
127 FileMonikerROTDataImpl_Release,
128 FileMonikerROTDataImpl_GetComparaisonData
131 /*******************************************************************************
132 * FileMoniker_QueryInterface
133 *******************************************************************************/
134 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
136 ICOM_THIS(FileMonikerImpl,iface);
138 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
140 /* Perform a sanity check on the parameters.*/
141 if ( (This==0) || (ppvObject==0) )
144 /* Initialize the return parameter */
147 /* Compare the riid with the interface IDs implemented by this object.*/
148 if (IsEqualIID(&IID_IUnknown, riid) ||
149 IsEqualIID(&IID_IPersist, riid) ||
150 IsEqualIID(&IID_IPersistStream,riid) ||
151 IsEqualIID(&IID_IMoniker, riid)
155 else if (IsEqualIID(&IID_IROTData, riid))
156 *ppvObject = (IROTData*)&(This->lpvtbl2);
158 /* Check that we obtained an interface.*/
160 return E_NOINTERFACE;
162 /* Query Interface always increases the reference count by one when it is successful */
163 FileMonikerImpl_AddRef(iface);
168 /******************************************************************************
170 ******************************************************************************/
171 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
173 ICOM_THIS(FileMonikerImpl,iface);
175 TRACE("(%p)\n",iface);
177 return ++(This->ref);
180 /******************************************************************************
181 * FileMoniker_Release
182 ******************************************************************************/
183 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
185 ICOM_THIS(FileMonikerImpl,iface);
187 TRACE("(%p)\n",iface);
191 /* destroy the object if there's no more reference on it */
194 FileMonikerImpl_Destroy(This);
201 /******************************************************************************
202 * FileMoniker_GetClassID
203 ******************************************************************************/
204 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
205 CLSID *pClassID)/* Pointer to CLSID of object */
207 TRACE("(%p,%p),stub!\n",iface,pClassID);
212 *pClassID = CLSID_FileMoniker;
217 /******************************************************************************
218 * FileMoniker_IsDirty
219 ******************************************************************************/
220 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
222 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
223 method in the OLE-provided moniker interfaces always return S_FALSE because
224 their internal state never changes. */
226 TRACE("(%p)\n",iface);
231 /******************************************************************************
233 ******************************************************************************/
234 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
241 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
243 ICOM_THIS(FileMonikerImpl,iface);
245 TRACE("(%p,%p)\n",iface,pStm);
247 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
249 /* first WORD is non significative */
250 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
251 if (bread!=sizeof(WORD) || wbuffer!=0)
254 /* read filePath string length (plus one) */
255 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
256 if (bread != sizeof(DWORD))
259 /* read filePath string */
260 filePathA=HeapAlloc(GetProcessHeap(),0,length);
261 res=IStream_Read(pStm,filePathA,length,&bread);
265 /* read the first constant */
266 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
267 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
273 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
274 if (bread!=sizeof(WORD) || wbuffer!=0)
281 doubleLenHex=doubleLenDec=2*length;
285 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
286 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
292 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
293 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
296 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
297 if (bread!=sizeof(WORD) || wbuffer!=0x3)
300 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
302 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
303 if (bread!=doubleLenHex)
306 if (This->filePathName!=NULL)
307 HeapFree(GetProcessHeap(),0,This->filePathName);
309 This->filePathName=filePathW;
311 HeapFree(GetProcessHeap(),0,filePathA);
316 /******************************************************************************
318 ******************************************************************************/
319 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
320 IStream* pStm,/* poniter to the stream where the object is to be saved */
321 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
323 /* this function saves data of this object. In the begining I thougth that I have just to write
324 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
325 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
326 * what did this function do exactly ! but I have no idear a bout its logic !
327 * I guessed data who must be writen on stream wich is:
328 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
329 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
330 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
331 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
334 ICOM_THIS(FileMonikerImpl,iface);
337 LPOLESTR filePathW=This->filePathName;
341 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
342 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
349 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
354 /* write a DWORD seted to 0 : constant */
355 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
357 /* write length of filePath string ( "\0" included )*/
358 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
359 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
361 /* write filePath string type A */
362 filePathA=HeapAlloc(GetProcessHeap(),0,len);
363 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
364 res=IStream_Write(pStm,filePathA,len,NULL);
365 HeapFree(GetProcessHeap(),0,filePathA);
367 /* write a DWORD seted to 0xDEADFFFF: constant */
368 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
371 /* write 10 times a DWORD seted to 0 : constants */
373 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
378 doubleLenHex=doubleLenDec=2*len;
382 /* write double-length of the path string ( "\0" included )*/
383 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
388 /* write double-length (hexa representation) of the path string ( "\0" included ) */
389 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
391 /* write a WORD seted to 0x3: constant */
392 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
394 /* write path unicode string */
395 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
400 /******************************************************************************
401 * FileMoniker_GetSizeMax
402 ******************************************************************************/
403 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
404 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
406 ICOM_THIS(FileMonikerImpl,iface);
407 DWORD len=lstrlenW(This->filePathName);
410 TRACE("(%p,%p)\n",iface,pcbSize);
415 /* for more details see FileMonikerImpl_Save coments */
417 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
418 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
419 (len+1)+ /* filePath string */
420 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
421 10*sizeof(WORD)+ /* 10 zero WORD */
422 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
424 if (len==0 || len > 8)
427 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
428 sizeof(WORD)+ /* constant : 0x3 */
429 len*sizeof(WCHAR); /* unicde filePath string */
431 pcbSize->s.LowPart=sizeMAx;
432 pcbSize->s.HighPart=0;
437 /******************************************************************************
438 * FileMoniker_Construct (local function)
439 *******************************************************************************/
440 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
443 int sizeStr=lstrlenW(lpszPathName);
445 WCHAR twoPoint[]={'.','.',0};
446 WCHAR bkSlash[]={'\\',0};
449 TRACE("(%p,%p)\n",This,lpszPathName);
451 /* Initialize the virtual fgunction table. */
452 This->lpvtbl1 = &VT_FileMonikerImpl;
453 This->lpvtbl2 = &VT_ROTDataImpl;
456 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
458 if (This->filePathName==NULL)
459 return E_OUTOFMEMORY;
461 strcpyW(This->filePathName,lpszPathName);
463 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
468 if (lstrcmpW(tabStr[0],twoPoint)!=0)
473 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
479 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
487 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
490 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
492 *This->filePathName=0;
494 for(i=0;tabStr[i]!=NULL;i++)
495 strcatW(This->filePathName,tabStr[i]);
498 strcatW(This->filePathName,bkSlash);
501 for(i=0; tabStr[i]!=NULL;i++)
502 CoTaskMemFree(tabStr[i]);
503 CoTaskMemFree(tabStr);
508 /******************************************************************************
509 * FileMoniker_Destroy (local function)
510 *******************************************************************************/
511 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
513 TRACE("(%p)\n",This);
515 if (This->filePathName!=NULL)
516 HeapFree(GetProcessHeap(),0,This->filePathName);
518 HeapFree(GetProcessHeap(),0,This);
523 /******************************************************************************
524 * FileMoniker_BindToObject
525 ******************************************************************************/
526 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
535 IRunningObjectTable *prot=0;
537 IClassFactory *pcf=0;
538 IClassActivator *pca=0;
540 ICOM_THIS(FileMonikerImpl,iface);
544 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
548 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
551 /* if the requested class was loaded befor ! we dont need to reload it */
552 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
555 /* first activation of this class */
556 res=GetClassFile(This->filePathName,&clsID);
559 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
562 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
566 IUnknown_AddRef(pObj);
574 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
576 if (res==E_NOINTERFACE){
578 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
580 if (res==E_NOINTERFACE)
581 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
585 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
587 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
592 IUnknown_AddRef(pObj);
599 /*res=GetClassFile(This->filePathName,&clsID);
603 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
608 IUnknown_AddRef(pObj);
615 /* get the requested interface from the loaded class */
616 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
618 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
620 IUnknown_Release(pObj);
624 IRunningObjectTable_Release(prot);
627 IPersistFile_Release(ppf);
630 IClassActivator_Release(pca);
633 IClassFactory_Release(pcf);
638 /******************************************************************************
639 * FileMoniker_BindToStorage
640 ******************************************************************************/
641 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
651 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
653 if (pmkToLeft==NULL){
655 if (IsEqualIID(&IID_IStorage, riid)){
657 /* get the file name */
658 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
660 /* verifie if the file contains a storage object */
661 res=StgIsStorageFile(filePath);
665 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
671 IStorage_AddRef(pstg);
676 CoTaskMemFree(filePath);
679 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
684 return E_NOINTERFACE;
688 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
695 /******************************************************************************
697 ******************************************************************************/
698 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
700 DWORD dwReduceHowFar,
701 IMoniker** ppmkToLeft,
702 IMoniker** ppmkReduced)
704 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
706 if (ppmkReduced==NULL)
709 FileMonikerImpl_AddRef(iface);
713 return MK_S_REDUCED_TO_SELF;
715 /******************************************************************************
716 * FileMoniker_ComposeWith
717 ******************************************************************************/
718 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
720 BOOL fOnlyIfNotGeneric,
721 IMoniker** ppmkComposite)
724 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
725 WCHAR twoPoint[]={'.','.',0};
726 WCHAR bkSlash[]={'\\',0};
728 int i=0,j=0,lastIdx1=0,lastIdx2=0;
731 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
733 if (ppmkComposite==NULL)
741 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
743 /* check if we have two filemonikers to compose or not */
744 if(mkSys==MKSYS_FILEMONIKER){
746 CreateBindCtx(0,&bind);
748 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
749 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
751 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
752 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
753 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
755 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
758 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
761 /* for etch "..\" in the left of str2 remove the right element from str1 */
762 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
767 /* the length of the composed path string is raised by the sum of the two paths lengths */
768 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
771 return E_OUTOFMEMORY;
773 /* new path is the concatenation of the rest of str1 and str2 */
774 for(*newStr=0,j=0;j<=lastIdx1;j++)
775 strcatW(newStr,strDec1[j]);
777 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
778 strcatW(newStr,bkSlash);
780 for(j=i;j<=lastIdx2;j++)
781 strcatW(newStr,strDec2[j]);
783 /* create a new moniker with the new string */
784 res=CreateFileMoniker(newStr,ppmkComposite);
786 /* free all strings space memory used by this function */
787 HeapFree(GetProcessHeap(),0,newStr);
789 for(i=0; strDec1[i]!=NULL;i++)
790 CoTaskMemFree(strDec1[i]);
791 for(i=0; strDec2[i]!=NULL;i++)
792 CoTaskMemFree(strDec2[i]);
793 CoTaskMemFree(strDec1);
794 CoTaskMemFree(strDec2);
801 else if(mkSys==MKSYS_ANTIMONIKER){
806 else if (fOnlyIfNotGeneric){
809 return MK_E_NEEDGENERIC;
813 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
816 /******************************************************************************
818 ******************************************************************************/
819 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
821 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
823 if (ppenumMoniker == NULL)
826 *ppenumMoniker = NULL;
831 /******************************************************************************
832 * FileMoniker_IsEqual
833 ******************************************************************************/
834 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
836 ICOM_THIS(FileMonikerImpl,iface);
842 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
844 if (pmkOtherMoniker==NULL)
847 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
849 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
853 res=CreateBindCtx(0,&bind);
857 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
859 if (lstrcmpiW(filePath,
860 This->filePathName)!=0)
867 /******************************************************************************
869 ******************************************************************************/
870 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
872 ICOM_THIS(FileMonikerImpl,iface);
874 int h = 0,i,skip,len;
881 val = This->filePathName;
885 for (i = len ; i > 0; i--) {
886 h = (h * 37) + val[off++];
889 /* only sample some characters */
891 for (i = len ; i > 0; i -= skip, off += skip) {
892 h = (h * 39) + val[off];
901 /******************************************************************************
902 * FileMoniker_IsRunning
903 ******************************************************************************/
904 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
907 IMoniker* pmkNewlyRunning)
909 IRunningObjectTable* rot;
912 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
914 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
920 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
925 res = IRunningObjectTable_IsRunning(rot,iface);
927 IRunningObjectTable_Release(rot);
932 /******************************************************************************
933 * FileMoniker_GetTimeOfLastChange
934 ******************************************************************************/
935 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
940 ICOM_THIS(FileMonikerImpl,iface);
941 IRunningObjectTable* rot;
943 WIN32_FILE_ATTRIBUTE_DATA info;
945 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
953 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
958 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
960 if (FAILED(res)){ /* the moniker is not registred */
962 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
963 return MK_E_NOOBJECT;
965 *pFileTime=info.ftLastWriteTime;
971 /******************************************************************************
972 * FileMoniker_Inverse
973 ******************************************************************************/
974 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
977 TRACE("(%p,%p)\n",iface,ppmk);
979 return CreateAntiMoniker(ppmk);
982 /******************************************************************************
983 * FileMoniker_CommonPrefixWith
984 ******************************************************************************/
985 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
988 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
991 ULONG nb1,nb2,i,sameIdx;
992 BOOL machimeNameCase=FALSE;
994 if (ppmkPrefix==NULL)
1002 /* check if we have the same type of moniker */
1003 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1005 if(mkSys==MKSYS_FILEMONIKER){
1007 CreateBindCtx(0,&pbind);
1009 /* create a string based on common part of the two paths */
1011 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1012 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1014 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1015 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1017 if (nb1==0 || nb2==0)
1018 return MK_E_NOPREFIX;
1020 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1024 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1025 (stringTable2[sameIdx]!=NULL) &&
1026 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1028 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1030 machimeNameCase=TRUE;
1032 for(i=2;i<sameIdx;i++)
1034 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1035 machimeNameCase=FALSE;
1040 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1043 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1044 return MK_E_NOPREFIX;
1046 for(i=0;i<sameIdx;i++)
1047 strcatW(commonPath,stringTable1[i]);
1050 CoTaskMemFree(stringTable1[i]);
1052 CoTaskMemFree(stringTable1);
1055 CoTaskMemFree(stringTable2[i]);
1057 CoTaskMemFree(stringTable2);
1059 HeapFree(GetProcessHeap(),0,commonPath);
1061 return CreateFileMoniker(commonPath,ppmkPrefix);
1064 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1067 /******************************************************************************
1068 * DecomposePath (local function)
1069 ******************************************************************************/
1070 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** stringTable)
1072 WCHAR bSlash[] = {'\\',0};
1073 WCHAR word[MAX_PATH];
1074 int i=0,j,tabIndex=0;
1075 LPOLESTR *strgtable ;
1077 int len=lstrlenW(str);
1079 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1081 if (strgtable==NULL)
1082 return E_OUTOFMEMORY;
1086 if(str[i]==bSlash[0]){
1088 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1090 if (strgtable[tabIndex]==NULL)
1091 return E_OUTOFMEMORY;
1093 strcpyW(strgtable[tabIndex++],bSlash);
1100 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1105 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1107 if (strgtable[tabIndex]==NULL)
1108 return E_OUTOFMEMORY;
1110 strcpyW(strgtable[tabIndex++],word);
1113 strgtable[tabIndex]=NULL;
1115 *stringTable=strgtable;
1120 /******************************************************************************
1121 * FileMoniker_RelativePathTo
1122 ******************************************************************************/
1123 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1127 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1128 DWORD len1=0,len2=0,sameIdx=0,j=0;
1129 WCHAR back[] ={'.','.','\\',0};
1131 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1133 if (ppmkRelPath==NULL)
1137 return E_INVALIDARG;
1139 res=CreateBindCtx(0,&bind);
1143 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1146 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1150 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1151 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1153 if (FAILED(len1) || FAILED(len2))
1154 return E_OUTOFMEMORY;
1156 /* count the number of similar items from the begin of the two paths */
1157 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1158 (tabStr2[sameIdx]!=NULL) &&
1159 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1161 /* begin the construction of relativePath */
1162 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1163 /* by "..\\" in the begin */
1164 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1168 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1169 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1170 if (*tabStr1[j]!='\\')
1171 strcatW(relPath,back);
1173 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1174 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1175 strcatW(relPath,tabStr2[j]);
1177 res=CreateFileMoniker(relPath,ppmkRelPath);
1179 for(j=0; tabStr1[j]!=NULL;j++)
1180 CoTaskMemFree(tabStr1[j]);
1181 for(j=0; tabStr2[j]!=NULL;j++)
1182 CoTaskMemFree(tabStr2[j]);
1183 CoTaskMemFree(tabStr1);
1184 CoTaskMemFree(tabStr2);
1185 CoTaskMemFree(str1);
1186 CoTaskMemFree(str2);
1187 HeapFree(GetProcessHeap(),0,relPath);
1189 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1195 /******************************************************************************
1196 * FileMoniker_GetDisplayName
1197 ******************************************************************************/
1198 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1200 IMoniker* pmkToLeft,
1201 LPOLESTR *ppszDisplayName)
1203 ICOM_THIS(FileMonikerImpl,iface);
1205 int len=lstrlenW(This->filePathName);
1207 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1209 if (ppszDisplayName==NULL)
1212 if (pmkToLeft!=NULL)
1213 return E_INVALIDARG;
1215 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1216 if (*ppszDisplayName==NULL)
1217 return E_OUTOFMEMORY;
1219 strcpyW(*ppszDisplayName,This->filePathName);
1224 /******************************************************************************
1225 * FileMoniker_ParseDisplayName
1226 ******************************************************************************/
1227 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1229 IMoniker* pmkToLeft,
1230 LPOLESTR pszDisplayName,
1234 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1238 /******************************************************************************
1239 * FileMoniker_IsSystemMonker
1240 ******************************************************************************/
1241 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1243 TRACE("(%p,%p)\n",iface,pwdMksys);
1248 (*pwdMksys)=MKSYS_FILEMONIKER;
1253 /*******************************************************************************
1254 * FileMonikerIROTData_QueryInterface
1255 *******************************************************************************/
1256 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1259 ICOM_THIS_From_IROTData(IMoniker, iface);
1261 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1263 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1266 /***********************************************************************
1267 * FileMonikerIROTData_AddRef
1269 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1271 ICOM_THIS_From_IROTData(IMoniker, iface);
1273 TRACE("(%p)\n",This);
1275 return FileMonikerImpl_AddRef(This);
1278 /***********************************************************************
1279 * FileMonikerIROTData_Release
1281 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1283 ICOM_THIS_From_IROTData(IMoniker, iface);
1285 TRACE("(%p)\n",This);
1287 return FileMonikerImpl_Release(This);
1290 /******************************************************************************
1291 * FileMonikerIROTData_GetComparaisonData
1292 ******************************************************************************/
1293 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1298 FIXME("(),stub!\n");
1302 /******************************************************************************
1303 * CreateFileMoniker16
1304 ******************************************************************************/
1305 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1308 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1312 /******************************************************************************
1314 ******************************************************************************/
1315 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1317 FileMonikerImpl* newFileMoniker = 0;
1318 HRESULT hr = E_FAIL;
1319 IID riid=IID_IMoniker;
1321 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1326 if(lpszPathName==NULL)
1331 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1333 if (newFileMoniker == 0)
1334 return E_OUTOFMEMORY;
1336 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1339 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1341 HeapFree(GetProcessHeap(),0,newFileMoniker);