1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
12 #include "wine/unicode.h"
13 #include "debugtools.h"
15 #include "wine/obj_storage.h"
16 #include "wine/obj_moniker.h"
17 #include "wine/obj_base.h"
19 #include "compobj_private.h"
21 DEFAULT_DEBUG_CHANNEL(ole);
23 /* filemoniker data structure */
24 typedef struct FileMonikerImpl{
26 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
28 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
29 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
31 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
33 ULONG ref; /* reference counter for this object */
35 LPOLESTR filePathName; /* path string identified by this filemoniker */
39 /********************************************************************************/
40 /* FileMoniker prototype functions : */
42 /* IUnknown prototype functions */
43 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
44 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
45 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
47 /* IPersist prototype functions */
48 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
50 /* IPersistStream prototype functions */
51 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
52 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
53 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
54 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
56 /* IMoniker prototype functions */
57 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
58 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
59 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
60 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
61 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
62 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
63 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
64 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
65 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
66 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
67 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
68 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
69 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
70 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
71 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
73 /********************************************************************************/
74 /* IROTData prototype functions */
76 /* IUnknown prototype functions */
77 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
78 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
79 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
81 /* IROTData prototype function */
82 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
84 /* Local function used by filemoniker implementation */
85 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
86 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
87 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
90 /********************************************************************************/
91 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
92 /* IPersistStream and IMoniker functions. */
93 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
95 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
96 FileMonikerImpl_QueryInterface,
97 FileMonikerImpl_AddRef,
98 FileMonikerImpl_Release,
99 FileMonikerImpl_GetClassID,
100 FileMonikerImpl_IsDirty,
101 FileMonikerImpl_Load,
102 FileMonikerImpl_Save,
103 FileMonikerImpl_GetSizeMax,
104 FileMonikerImpl_BindToObject,
105 FileMonikerImpl_BindToStorage,
106 FileMonikerImpl_Reduce,
107 FileMonikerImpl_ComposeWith,
108 FileMonikerImpl_Enum,
109 FileMonikerImpl_IsEqual,
110 FileMonikerImpl_Hash,
111 FileMonikerImpl_IsRunning,
112 FileMonikerImpl_GetTimeOfLastChange,
113 FileMonikerImpl_Inverse,
114 FileMonikerImpl_CommonPrefixWith,
115 FileMonikerImpl_RelativePathTo,
116 FileMonikerImpl_GetDisplayName,
117 FileMonikerImpl_ParseDisplayName,
118 FileMonikerImpl_IsSystemMoniker
121 /********************************************************************************/
122 /* Virtual function table for the IROTData class. */
123 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
125 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
126 FileMonikerROTDataImpl_QueryInterface,
127 FileMonikerROTDataImpl_AddRef,
128 FileMonikerROTDataImpl_Release,
129 FileMonikerROTDataImpl_GetComparaisonData
132 /*******************************************************************************
133 * FileMoniker_QueryInterface
134 *******************************************************************************/
135 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
137 ICOM_THIS(FileMonikerImpl,iface);
139 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
141 /* Perform a sanity check on the parameters.*/
142 if ( (This==0) || (ppvObject==0) )
145 /* Initialize the return parameter */
148 /* Compare the riid with the interface IDs implemented by this object.*/
149 if (IsEqualIID(&IID_IUnknown, riid) ||
150 IsEqualIID(&IID_IPersist, riid) ||
151 IsEqualIID(&IID_IPersistStream,riid) ||
152 IsEqualIID(&IID_IMoniker, riid)
156 else if (IsEqualIID(&IID_IROTData, riid))
157 *ppvObject = (IROTData*)&(This->lpvtbl2);
159 /* Check that we obtained an interface.*/
161 return E_NOINTERFACE;
163 /* Query Interface always increases the reference count by one when it is successful */
164 FileMonikerImpl_AddRef(iface);
169 /******************************************************************************
171 ******************************************************************************/
172 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
174 ICOM_THIS(FileMonikerImpl,iface);
176 TRACE("(%p)\n",iface);
178 return ++(This->ref);
181 /******************************************************************************
182 * FileMoniker_Release
183 ******************************************************************************/
184 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
186 ICOM_THIS(FileMonikerImpl,iface);
188 TRACE("(%p)\n",iface);
192 /* destroy the object if there's no more reference on it */
195 FileMonikerImpl_Destroy(This);
202 /******************************************************************************
203 * FileMoniker_GetClassID
204 ******************************************************************************/
205 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
206 CLSID *pClassID)/* Pointer to CLSID of object */
208 TRACE("(%p,%p),stub!\n",iface,pClassID);
213 *pClassID = CLSID_FileMoniker;
218 /******************************************************************************
219 * FileMoniker_IsDirty
220 ******************************************************************************/
221 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
223 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
224 method in the OLE-provided moniker interfaces always return S_FALSE because
225 their internal state never changes. */
227 TRACE("(%p)\n",iface);
232 /******************************************************************************
234 ******************************************************************************/
235 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
242 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
244 ICOM_THIS(FileMonikerImpl,iface);
246 TRACE("(%p,%p)\n",iface,pStm);
248 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
250 /* first WORD is non significative */
251 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
252 if (bread!=sizeof(WORD) || wbuffer!=0)
255 /* read filePath string length (plus one) */
256 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
257 if (bread != sizeof(DWORD))
260 /* read filePath string */
261 filePathA=HeapAlloc(GetProcessHeap(),0,length);
262 res=IStream_Read(pStm,filePathA,length,&bread);
266 /* read the first constant */
267 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
268 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
274 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
275 if (bread!=sizeof(WORD) || wbuffer!=0)
282 doubleLenHex=doubleLenDec=2*length;
286 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
287 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
293 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
294 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
297 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
298 if (bread!=sizeof(WORD) || wbuffer!=0x3)
301 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
303 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
304 if (bread!=doubleLenHex)
307 if (This->filePathName!=NULL)
308 HeapFree(GetProcessHeap(),0,This->filePathName);
310 This->filePathName=filePathW;
312 HeapFree(GetProcessHeap(),0,filePathA);
317 /******************************************************************************
319 ******************************************************************************/
320 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
321 IStream* pStm,/* pointer to the stream where the object is to be saved */
322 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
324 /* this function saves data of this object. In the beginning I thougth
325 * that I have just to write the filePath string on Stream. But, when I
326 * tested this function whith windows programs samples, I noticed that it
327 * was not the case. So I analysed data written by this function on
328 * Windows and what this did function exactly ! But I have no idea about
330 * I guessed data which must be written on stream is:
331 * 1) WORD constant:zero
332 * 2) length of the path string ("\0" included)
333 * 3) path string type A
334 * 4) DWORD constant : 0xDEADFFFF
335 * 5) ten WORD constant: zero
336 * 6) DWORD: double-length of the the path string type W ("\0" not
338 * 7) WORD constant: 0x3
339 * 8) filePath unicode string.
340 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
343 ICOM_THIS(FileMonikerImpl,iface);
346 LPOLESTR filePathW=This->filePathName;
350 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
351 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
358 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
363 /* write a DWORD set to 0 : constant */
364 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
366 /* write length of filePath string ( "\0" included )*/
367 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
368 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
370 /* write filePath string type A */
371 filePathA=HeapAlloc(GetProcessHeap(),0,len);
372 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
373 res=IStream_Write(pStm,filePathA,len,NULL);
374 HeapFree(GetProcessHeap(),0,filePathA);
376 /* write a DWORD set to 0xDEADFFFF: constant */
377 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
380 /* write 10 times a DWORD set to 0 : constants */
382 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
387 doubleLenHex=doubleLenDec=2*len;
391 /* write double-length of the path string ( "\0" included )*/
392 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
397 /* write double-length (hexa representation) of the path string ( "\0" included ) */
398 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
400 /* write a WORD set to 0x3: constant */
401 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
403 /* write path unicode string */
404 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
409 /******************************************************************************
410 * FileMoniker_GetSizeMax
411 ******************************************************************************/
412 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
413 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
415 ICOM_THIS(FileMonikerImpl,iface);
416 DWORD len=lstrlenW(This->filePathName);
419 TRACE("(%p,%p)\n",iface,pcbSize);
424 /* for more details see FileMonikerImpl_Save coments */
426 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
427 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
428 (len+1)+ /* filePath string */
429 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
430 10*sizeof(WORD)+ /* 10 zero WORD */
431 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
433 if (len==0 || len > 8)
436 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
437 sizeof(WORD)+ /* constant : 0x3 */
438 len*sizeof(WCHAR); /* unicde filePath string */
440 pcbSize->s.LowPart=sizeMAx;
441 pcbSize->s.HighPart=0;
446 /******************************************************************************
447 * FileMoniker_Construct (local function)
448 *******************************************************************************/
449 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
452 int sizeStr=lstrlenW(lpszPathName);
454 WCHAR twoPoint[]={'.','.',0};
455 WCHAR bkSlash[]={'\\',0};
458 TRACE("(%p,%p)\n",This,lpszPathName);
460 /* Initialize the virtual fgunction table. */
461 This->lpvtbl1 = &VT_FileMonikerImpl;
462 This->lpvtbl2 = &VT_ROTDataImpl;
465 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
467 if (This->filePathName==NULL)
468 return E_OUTOFMEMORY;
470 strcpyW(This->filePathName,lpszPathName);
472 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
477 if (lstrcmpW(tabStr[0],twoPoint)!=0)
482 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
488 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
496 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
499 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
501 *This->filePathName=0;
503 for(i=0;tabStr[i]!=NULL;i++)
504 strcatW(This->filePathName,tabStr[i]);
507 strcatW(This->filePathName,bkSlash);
510 for(i=0; tabStr[i]!=NULL;i++)
511 CoTaskMemFree(tabStr[i]);
512 CoTaskMemFree(tabStr);
517 /******************************************************************************
518 * FileMoniker_Destroy (local function)
519 *******************************************************************************/
520 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
522 TRACE("(%p)\n",This);
524 if (This->filePathName!=NULL)
525 HeapFree(GetProcessHeap(),0,This->filePathName);
527 HeapFree(GetProcessHeap(),0,This);
532 /******************************************************************************
533 * FileMoniker_BindToObject
534 ******************************************************************************/
535 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
544 IRunningObjectTable *prot=0;
546 IClassFactory *pcf=0;
547 IClassActivator *pca=0;
549 ICOM_THIS(FileMonikerImpl,iface);
553 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
557 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
560 /* if the requested class was loaded befor ! we dont need to reload it */
561 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
564 /* first activation of this class */
565 res=GetClassFile(This->filePathName,&clsID);
568 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
571 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
575 IUnknown_AddRef(pObj);
583 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
585 if (res==E_NOINTERFACE){
587 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
589 if (res==E_NOINTERFACE)
590 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
594 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
596 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
601 IUnknown_AddRef(pObj);
608 /*res=GetClassFile(This->filePathName,&clsID);
612 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
617 IUnknown_AddRef(pObj);
624 /* get the requested interface from the loaded class */
625 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
627 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
629 IUnknown_Release(pObj);
633 IRunningObjectTable_Release(prot);
636 IPersistFile_Release(ppf);
639 IClassActivator_Release(pca);
642 IClassFactory_Release(pcf);
647 /******************************************************************************
648 * FileMoniker_BindToStorage
649 ******************************************************************************/
650 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
660 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
662 if (pmkToLeft==NULL){
664 if (IsEqualIID(&IID_IStorage, riid)){
666 /* get the file name */
667 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
669 /* verifie if the file contains a storage object */
670 res=StgIsStorageFile(filePath);
674 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
680 IStorage_AddRef(pstg);
685 CoTaskMemFree(filePath);
688 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
693 return E_NOINTERFACE;
697 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
704 /******************************************************************************
706 ******************************************************************************/
707 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
709 DWORD dwReduceHowFar,
710 IMoniker** ppmkToLeft,
711 IMoniker** ppmkReduced)
713 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
715 if (ppmkReduced==NULL)
718 FileMonikerImpl_AddRef(iface);
722 return MK_S_REDUCED_TO_SELF;
724 /******************************************************************************
725 * FileMoniker_ComposeWith
726 ******************************************************************************/
727 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
729 BOOL fOnlyIfNotGeneric,
730 IMoniker** ppmkComposite)
733 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
734 WCHAR twoPoint[]={'.','.',0};
735 WCHAR bkSlash[]={'\\',0};
737 int i=0,j=0,lastIdx1=0,lastIdx2=0;
740 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
742 if (ppmkComposite==NULL)
750 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
752 /* check if we have two filemonikers to compose or not */
753 if(mkSys==MKSYS_FILEMONIKER){
755 CreateBindCtx(0,&bind);
757 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
758 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
760 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
761 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
762 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
764 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
767 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
770 /* for etch "..\" in the left of str2 remove the right element from str1 */
771 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
776 /* the length of the composed path string is raised by the sum of the two paths lengths */
777 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
780 return E_OUTOFMEMORY;
782 /* new path is the concatenation of the rest of str1 and str2 */
783 for(*newStr=0,j=0;j<=lastIdx1;j++)
784 strcatW(newStr,strDec1[j]);
786 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
787 strcatW(newStr,bkSlash);
789 for(j=i;j<=lastIdx2;j++)
790 strcatW(newStr,strDec2[j]);
792 /* create a new moniker with the new string */
793 res=CreateFileMoniker(newStr,ppmkComposite);
795 /* free all strings space memory used by this function */
796 HeapFree(GetProcessHeap(),0,newStr);
798 for(i=0; strDec1[i]!=NULL;i++)
799 CoTaskMemFree(strDec1[i]);
800 for(i=0; strDec2[i]!=NULL;i++)
801 CoTaskMemFree(strDec2[i]);
802 CoTaskMemFree(strDec1);
803 CoTaskMemFree(strDec2);
810 else if(mkSys==MKSYS_ANTIMONIKER){
815 else if (fOnlyIfNotGeneric){
818 return MK_E_NEEDGENERIC;
822 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
825 /******************************************************************************
827 ******************************************************************************/
828 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
830 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
832 if (ppenumMoniker == NULL)
835 *ppenumMoniker = NULL;
840 /******************************************************************************
841 * FileMoniker_IsEqual
842 ******************************************************************************/
843 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
845 ICOM_THIS(FileMonikerImpl,iface);
851 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
853 if (pmkOtherMoniker==NULL)
856 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
858 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
862 res=CreateBindCtx(0,&bind);
866 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
868 if (lstrcmpiW(filePath,
869 This->filePathName)!=0)
876 /******************************************************************************
878 ******************************************************************************/
879 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
881 ICOM_THIS(FileMonikerImpl,iface);
883 int h = 0,i,skip,len;
890 val = This->filePathName;
894 for (i = len ; i > 0; i--) {
895 h = (h * 37) + val[off++];
898 /* only sample some characters */
900 for (i = len ; i > 0; i -= skip, off += skip) {
901 h = (h * 39) + val[off];
910 /******************************************************************************
911 * FileMoniker_IsRunning
912 ******************************************************************************/
913 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
916 IMoniker* pmkNewlyRunning)
918 IRunningObjectTable* rot;
921 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
923 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
929 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
934 res = IRunningObjectTable_IsRunning(rot,iface);
936 IRunningObjectTable_Release(rot);
941 /******************************************************************************
942 * FileMoniker_GetTimeOfLastChange
943 ******************************************************************************/
944 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
949 ICOM_THIS(FileMonikerImpl,iface);
950 IRunningObjectTable* rot;
952 WIN32_FILE_ATTRIBUTE_DATA info;
954 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
962 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
967 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
969 if (FAILED(res)){ /* the moniker is not registred */
971 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
972 return MK_E_NOOBJECT;
974 *pFileTime=info.ftLastWriteTime;
980 /******************************************************************************
981 * FileMoniker_Inverse
982 ******************************************************************************/
983 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
986 TRACE("(%p,%p)\n",iface,ppmk);
988 return CreateAntiMoniker(ppmk);
991 /******************************************************************************
992 * FileMoniker_CommonPrefixWith
993 ******************************************************************************/
994 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
997 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1000 ULONG nb1,nb2,i,sameIdx;
1001 BOOL machimeNameCase=FALSE;
1003 if (ppmkPrefix==NULL)
1007 return E_INVALIDARG;
1011 /* check if we have the same type of moniker */
1012 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1014 if(mkSys==MKSYS_FILEMONIKER){
1016 CreateBindCtx(0,&pbind);
1018 /* create a string based on common part of the two paths */
1020 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1021 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1023 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1024 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1026 if (nb1==0 || nb2==0)
1027 return MK_E_NOPREFIX;
1029 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1033 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1034 (stringTable2[sameIdx]!=NULL) &&
1035 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1037 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1039 machimeNameCase=TRUE;
1041 for(i=2;i<sameIdx;i++)
1043 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1044 machimeNameCase=FALSE;
1049 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1052 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1053 return MK_E_NOPREFIX;
1055 for(i=0;i<sameIdx;i++)
1056 strcatW(commonPath,stringTable1[i]);
1059 CoTaskMemFree(stringTable1[i]);
1061 CoTaskMemFree(stringTable1);
1064 CoTaskMemFree(stringTable2[i]);
1066 CoTaskMemFree(stringTable2);
1068 HeapFree(GetProcessHeap(),0,commonPath);
1070 return CreateFileMoniker(commonPath,ppmkPrefix);
1073 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1076 /******************************************************************************
1077 * DecomposePath (local function)
1078 ******************************************************************************/
1079 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1081 WCHAR bSlash[] = {'\\',0};
1082 WCHAR word[MAX_PATH];
1083 int i=0,j,tabIndex=0;
1084 LPOLESTR *strgtable ;
1086 int len=lstrlenW(str);
1088 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1090 if (strgtable==NULL)
1091 return E_OUTOFMEMORY;
1095 if(str[i]==bSlash[0]){
1097 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1099 if (strgtable[tabIndex]==NULL)
1100 return E_OUTOFMEMORY;
1102 strcpyW(strgtable[tabIndex++],bSlash);
1109 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1114 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1116 if (strgtable[tabIndex]==NULL)
1117 return E_OUTOFMEMORY;
1119 strcpyW(strgtable[tabIndex++],word);
1122 strgtable[tabIndex]=NULL;
1124 *stringTable=strgtable;
1129 /******************************************************************************
1130 * FileMoniker_RelativePathTo
1131 ******************************************************************************/
1132 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1136 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1137 DWORD len1=0,len2=0,sameIdx=0,j=0;
1138 WCHAR back[] ={'.','.','\\',0};
1140 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1142 if (ppmkRelPath==NULL)
1146 return E_INVALIDARG;
1148 res=CreateBindCtx(0,&bind);
1152 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1155 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1159 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1160 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1162 if (FAILED(len1) || FAILED(len2))
1163 return E_OUTOFMEMORY;
1165 /* count the number of similar items from the begin of the two paths */
1166 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1167 (tabStr2[sameIdx]!=NULL) &&
1168 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1170 /* begin the construction of relativePath */
1171 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1172 /* by "..\\" in the begin */
1173 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1177 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1178 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1179 if (*tabStr1[j]!='\\')
1180 strcatW(relPath,back);
1182 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1183 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1184 strcatW(relPath,tabStr2[j]);
1186 res=CreateFileMoniker(relPath,ppmkRelPath);
1188 for(j=0; tabStr1[j]!=NULL;j++)
1189 CoTaskMemFree(tabStr1[j]);
1190 for(j=0; tabStr2[j]!=NULL;j++)
1191 CoTaskMemFree(tabStr2[j]);
1192 CoTaskMemFree(tabStr1);
1193 CoTaskMemFree(tabStr2);
1194 CoTaskMemFree(str1);
1195 CoTaskMemFree(str2);
1196 HeapFree(GetProcessHeap(),0,relPath);
1198 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1204 /******************************************************************************
1205 * FileMoniker_GetDisplayName
1206 ******************************************************************************/
1207 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1209 IMoniker* pmkToLeft,
1210 LPOLESTR *ppszDisplayName)
1212 ICOM_THIS(FileMonikerImpl,iface);
1214 int len=lstrlenW(This->filePathName);
1216 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1218 if (ppszDisplayName==NULL)
1221 if (pmkToLeft!=NULL)
1222 return E_INVALIDARG;
1224 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1225 if (*ppszDisplayName==NULL)
1226 return E_OUTOFMEMORY;
1228 strcpyW(*ppszDisplayName,This->filePathName);
1233 /******************************************************************************
1234 * FileMoniker_ParseDisplayName
1235 ******************************************************************************/
1236 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1238 IMoniker* pmkToLeft,
1239 LPOLESTR pszDisplayName,
1243 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1247 /******************************************************************************
1248 * FileMoniker_IsSystemMoniker
1249 ******************************************************************************/
1250 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1252 TRACE("(%p,%p)\n",iface,pwdMksys);
1257 (*pwdMksys)=MKSYS_FILEMONIKER;
1262 /*******************************************************************************
1263 * FileMonikerIROTData_QueryInterface
1264 *******************************************************************************/
1265 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1268 ICOM_THIS_From_IROTData(IMoniker, iface);
1270 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1272 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1275 /***********************************************************************
1276 * FileMonikerIROTData_AddRef
1278 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1280 ICOM_THIS_From_IROTData(IMoniker, iface);
1282 TRACE("(%p)\n",This);
1284 return FileMonikerImpl_AddRef(This);
1287 /***********************************************************************
1288 * FileMonikerIROTData_Release
1290 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1292 ICOM_THIS_From_IROTData(IMoniker, iface);
1294 TRACE("(%p)\n",This);
1296 return FileMonikerImpl_Release(This);
1299 /******************************************************************************
1300 * FileMonikerIROTData_GetComparaisonData
1301 ******************************************************************************/
1302 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1307 FIXME("(),stub!\n");
1311 /******************************************************************************
1312 * CreateFileMoniker (OLE2.28)
1313 ******************************************************************************/
1314 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1317 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1321 /******************************************************************************
1322 * CreateFileMoniker (OLE32.55)
1323 ******************************************************************************/
1324 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1326 FileMonikerImpl* newFileMoniker = 0;
1327 HRESULT hr = E_FAIL;
1328 IID riid=IID_IMoniker;
1330 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1335 if(lpszPathName==NULL)
1340 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1342 if (newFileMoniker == 0)
1343 return E_OUTOFMEMORY;
1345 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1348 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1350 HeapFree(GetProcessHeap(),0,newFileMoniker);