1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
10 #include <ole32/ole32.h>
12 #include <storage32.h>
16 /* filemoniker data structure */
17 typedef struct FileMonikerImpl{
19 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
21 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
22 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
24 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
26 ULONG ref; /* reference counter for this object */
28 LPOLESTR filePathName; /* path string identified by this filemoniker */
32 /********************************************************************************/
33 /* FileMoniker prototype functions : */
35 /* IUnknown prototype functions */
36 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
37 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
38 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
40 /* IPersist prototype functions */
41 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
43 /* IPersistStream prototype functions */
44 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
45 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
46 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
47 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
49 /* IMoniker prototype functions */
50 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
51 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
52 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
53 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
54 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
55 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
56 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
57 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
58 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
59 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
60 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
61 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
62 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
63 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
64 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
66 /********************************************************************************/
67 /* IROTData prototype functions */
69 /* IUnknown prototype functions */
70 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
71 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
72 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
74 /* IROTData prototype function */
75 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
77 /* Local function used by filemoniker implementation */
78 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
79 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
80 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** tabStr);
83 /********************************************************************************/
84 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
85 /* IPersistStream and IMoniker functions. */
86 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
88 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
89 FileMonikerImpl_QueryInterface,
90 FileMonikerImpl_AddRef,
91 FileMonikerImpl_Release,
92 FileMonikerImpl_GetClassID,
93 FileMonikerImpl_IsDirty,
96 FileMonikerImpl_GetSizeMax,
97 FileMonikerImpl_BindToObject,
98 FileMonikerImpl_BindToStorage,
99 FileMonikerImpl_Reduce,
100 FileMonikerImpl_ComposeWith,
101 FileMonikerImpl_Enum,
102 FileMonikerImpl_IsEqual,
103 FileMonikerImpl_Hash,
104 FileMonikerImpl_IsRunning,
105 FileMonikerImpl_GetTimeOfLastChange,
106 FileMonikerImpl_Inverse,
107 FileMonikerImpl_CommonPrefixWith,
108 FileMonikerImpl_RelativePathTo,
109 FileMonikerImpl_GetDisplayName,
110 FileMonikerImpl_ParseDisplayName,
111 FileMonikerImpl_IsSystemMoniker
114 /********************************************************************************/
115 /* Virtual function table for the IROTData class. */
116 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
118 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
119 FileMonikerROTDataImpl_QueryInterface,
120 FileMonikerROTDataImpl_AddRef,
121 FileMonikerROTDataImpl_Release,
122 FileMonikerROTDataImpl_GetComparaisonData
125 /*******************************************************************************
126 * FileMoniker_QueryInterface
127 *******************************************************************************/
128 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
130 ICOM_THIS(FileMonikerImpl,iface);
132 Print(MAX_TRACE, ("(%p,%p,%p)\n",This,riid,ppvObject));
134 /* Perform a sanity check on the parameters.*/
135 if ( (This==0) || (ppvObject==0) )
138 /* Initialize the return parameter */
141 /* Compare the riid with the interface IDs implemented by this object.*/
142 if (IsEqualIID(&IID_IUnknown, riid) ||
143 IsEqualIID(&IID_IPersist, riid) ||
144 IsEqualIID(&IID_IPersistStream,riid) ||
145 IsEqualIID(&IID_IMoniker, riid)
149 else if (IsEqualIID(&IID_IROTData, riid))
150 *ppvObject = (IROTData*)&(This->lpvtbl2);
152 /* Check that we obtained an interface.*/
154 return E_NOINTERFACE;
156 /* Query Interface always increases the reference count by one when it is successful */
157 FileMonikerImpl_AddRef(iface);
162 /******************************************************************************
164 ******************************************************************************/
165 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
167 ICOM_THIS(FileMonikerImpl,iface);
169 Print(MAX_TRACE, ("(%p)\n",iface));
171 return ++(This->ref);
174 /******************************************************************************
175 * FileMoniker_Release
176 ******************************************************************************/
177 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
179 ICOM_THIS(FileMonikerImpl,iface);
181 Print(MAX_TRACE, ("(%p)\n",iface));
185 /* destroy the object if there's no more reference on it */
188 FileMonikerImpl_Destroy(This);
195 /******************************************************************************
196 * FileMoniker_GetClassID
197 ******************************************************************************/
198 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
199 CLSID *pClassID)/* Pointer to CLSID of object */
201 Print(MAX_TRACE, ("(%p,%p),stub!\n",iface,pClassID));
206 *pClassID = CLSID_FileMoniker;
211 /******************************************************************************
212 * FileMoniker_IsDirty
213 ******************************************************************************/
214 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
216 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
217 method in the OLE-provided moniker interfaces always return S_FALSE because
218 their internal state never changes. */
220 Print(MAX_TRACE, ("(%p)\n",iface));
225 /******************************************************************************
227 ******************************************************************************/
228 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
235 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
237 ICOM_THIS(FileMonikerImpl,iface);
239 Print(MAX_TRACE, ("(%p,%p)\n",iface,pStm));
241 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
243 /* first WORD is non significative */
244 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
245 if (bread!=sizeof(WORD) || wbuffer!=0)
248 /* read filePath string length (plus one) */
249 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
250 if (bread != sizeof(DWORD))
253 /* read filePath string */
254 filePathA=HeapAlloc(GetProcessHeap(),0,length);
255 res=IStream_Read(pStm,filePathA,length,&bread);
259 /* read the first constant */
260 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
261 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
267 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
268 if (bread!=sizeof(WORD) || wbuffer!=0)
275 doubleLenHex=doubleLenDec=2*length;
279 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
280 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
286 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
287 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
290 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
291 if (bread!=sizeof(WORD) || wbuffer!=0x3)
294 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
296 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
297 if (bread!=doubleLenHex)
300 if (This->filePathName!=NULL)
301 HeapFree(GetProcessHeap(),0,This->filePathName);
303 This->filePathName=filePathW;
305 HeapFree(GetProcessHeap(),0,filePathA);
310 /******************************************************************************
312 ******************************************************************************/
313 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
314 IStream* pStm,/* pointer to the stream where the object is to be saved */
315 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
317 /* this function saves data of this object. In the begining I thougth that I have just to write
318 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
319 * I noted that it was not the case. So I analysed data written by this function on Windows system and
320 * what did this function do exactly ! but I have no idear a bout its logic !
321 * I guessed data who must be written on stream wich is:
322 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
323 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
324 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
325 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
328 ICOM_THIS(FileMonikerImpl,iface);
331 LPOLESTR filePathW=This->filePathName;
335 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
336 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
343 Print(MAX_TRACE, ("(%p,%p,%d)\n",iface,pStm,fClearDirty));
348 /* write a DWORD set to 0 : constant */
349 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
351 /* write length of filePath string ( "\0" included )*/
352 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
353 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
355 /* write filePath string type A */
356 filePathA=HeapAlloc(GetProcessHeap(),0,len);
357 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
358 res=IStream_Write(pStm,filePathA,len,NULL);
359 HeapFree(GetProcessHeap(),0,filePathA);
361 /* write a DWORD set to 0xDEADFFFF: constant */
362 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
365 /* write 10 times a DWORD set to 0 : constants */
367 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
372 doubleLenHex=doubleLenDec=2*len;
376 /* write double-length of the path string ( "\0" included )*/
377 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
382 /* write double-length (hexa representation) of the path string ( "\0" included ) */
383 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
385 /* write a WORD set to 0x3: constant */
386 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
388 /* write path unicode string */
389 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
394 /******************************************************************************
395 * FileMoniker_GetSizeMax
396 ******************************************************************************/
397 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
398 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
400 ICOM_THIS(FileMonikerImpl,iface);
401 DWORD len=lstrlenW(This->filePathName);
404 Print(MAX_TRACE, ("(%p,%p)\n",iface,pcbSize));
409 /* for more details see FileMonikerImpl_Save coments */
411 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
412 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
413 (len+1)+ /* filePath string */
414 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
415 10*sizeof(WORD)+ /* 10 zero WORD */
416 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
418 if (len==0 || len > 8)
421 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
422 sizeof(WORD)+ /* constant : 0x3 */
423 len*sizeof(WCHAR); /* unicde filePath string */
425 pcbSize->u.LowPart=sizeMAx;
426 pcbSize->u.HighPart=0;
431 /******************************************************************************
432 * FileMoniker_Construct (local function)
433 *******************************************************************************/
434 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
437 int sizeStr=lstrlenW(lpszPathName);
439 WCHAR twoPoint[]={'.','.',0};
440 WCHAR bkSlash[]={'\\',0};
443 Print(MAX_TRACE, ("(%p,%p)\n",This,lpszPathName));
445 /* Initialize the virtual fgunction table. */
446 This->lpvtbl1 = &VT_FileMonikerImpl;
447 This->lpvtbl2 = &VT_ROTDataImpl;
450 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
452 if (This->filePathName==NULL)
453 return E_OUTOFMEMORY;
455 lstrcpyW(This->filePathName,lpszPathName);
457 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
462 if (lstrcmpW(tabStr[0],twoPoint)!=0)
467 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
473 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
481 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
484 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
486 *This->filePathName=0;
488 for(i=0;tabStr[i]!=NULL;i++)
489 lstrcatW(This->filePathName,tabStr[i]);
492 lstrcatW(This->filePathName,bkSlash);
495 for(i=0; tabStr[i]!=NULL;i++)
496 CoTaskMemFree(tabStr[i]);
497 CoTaskMemFree(tabStr);
502 /******************************************************************************
503 * FileMoniker_Destroy (local function)
504 *******************************************************************************/
505 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
507 Print(MAX_TRACE, ("(%p)\n",This));
509 if (This->filePathName!=NULL)
510 HeapFree(GetProcessHeap(),0,This->filePathName);
512 HeapFree(GetProcessHeap(),0,This);
517 /******************************************************************************
518 * FileMoniker_BindToObject
519 ******************************************************************************/
520 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
529 IRunningObjectTable *prot=0;
531 IClassFactory *pcf=0;
532 IClassActivator *pca=0;
534 ICOM_THIS(FileMonikerImpl,iface);
538 Print(MAX_TRACE, ("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult));
542 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
545 /* if the requested class was loaded befor ! we dont need to reload it */
546 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
549 /* first activation of this class */
550 res=GetClassFile(This->filePathName,&clsID);
553 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
556 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
560 IUnknown_AddRef(pObj);
568 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
570 if (res==E_NOINTERFACE){
572 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
574 if (res==E_NOINTERFACE)
575 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
579 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
581 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
586 IUnknown_AddRef(pObj);
591 Print(MIN_TRACE, ("()\n"));
593 /*res=GetClassFile(This->filePathName,&clsID);
597 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
602 IUnknown_AddRef(pObj);
609 /* get the requested interface from the loaded class */
610 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
612 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
614 IUnknown_Release(pObj);
618 IRunningObjectTable_Release(prot);
621 IPersistFile_Release(ppf);
624 IClassActivator_Release(pca);
627 IClassFactory_Release(pcf);
632 /******************************************************************************
633 * FileMoniker_BindToStorage
634 ******************************************************************************/
635 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
645 Print(MAX_TRACE, ("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject));
647 if (pmkToLeft==NULL){
649 if (IsEqualIID(&IID_IStorage, riid)){
651 /* get the file name */
652 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
654 /* verifie if the file contains a storage object */
655 res=StgIsStorageFile(filePath);
659 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
665 IStorage_AddRef(pstg);
670 CoTaskMemFree(filePath);
673 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
678 return E_NOINTERFACE;
682 Print(MIN_TRACE, ("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject));
689 /******************************************************************************
691 ******************************************************************************/
692 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
694 DWORD dwReduceHowFar,
695 IMoniker** ppmkToLeft,
696 IMoniker** ppmkReduced)
698 Print(MAX_TRACE, ("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced));
700 if (ppmkReduced==NULL)
703 FileMonikerImpl_AddRef(iface);
707 return MK_S_REDUCED_TO_SELF;
709 /******************************************************************************
710 * FileMoniker_ComposeWith
711 ******************************************************************************/
712 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
714 BOOL fOnlyIfNotGeneric,
715 IMoniker** ppmkComposite)
718 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
719 WCHAR twoPoint[]={'.','.',0};
720 WCHAR bkSlash[]={'\\',0};
722 int i=0,j=0,lastIdx1=0,lastIdx2=0;
725 Print(MAX_TRACE, ("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite));
727 if (ppmkComposite==NULL)
735 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
737 /* check if we have two filemonikers to compose or not */
738 if(mkSys==MKSYS_FILEMONIKER){
740 CreateBindCtx(0,&bind);
742 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
743 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
745 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
746 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
747 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
749 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
752 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
755 /* for etch "..\" in the left of str2 remove the right element from str1 */
756 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
761 /* the length of the composed path string is raised by the sum of the two paths lengths */
762 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
765 return E_OUTOFMEMORY;
767 /* new path is the concatenation of the rest of str1 and str2 */
768 for(*newStr=0,j=0;j<=lastIdx1;j++)
769 lstrcatW(newStr,strDec1[j]);
771 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
772 lstrcatW(newStr,bkSlash);
774 for(j=i;j<=lastIdx2;j++)
775 lstrcatW(newStr,strDec2[j]);
777 /* create a new moniker with the new string */
778 res=CreateFileMoniker(newStr,ppmkComposite);
780 /* free all strings space memory used by this function */
781 HeapFree(GetProcessHeap(),0,newStr);
783 for(i=0; strDec1[i]!=NULL;i++)
784 CoTaskMemFree(strDec1[i]);
785 for(i=0; strDec2[i]!=NULL;i++)
786 CoTaskMemFree(strDec2[i]);
787 CoTaskMemFree(strDec1);
788 CoTaskMemFree(strDec2);
795 else if(mkSys==MKSYS_ANTIMONIKER){
800 else if (fOnlyIfNotGeneric){
803 return MK_E_NEEDGENERIC;
807 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
810 /******************************************************************************
812 ******************************************************************************/
813 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
815 Print(MAX_TRACE, ("(%p,%d,%p)\n",iface,fForward,ppenumMoniker));
817 if (ppenumMoniker == NULL)
820 *ppenumMoniker = NULL;
825 /******************************************************************************
826 * FileMoniker_IsEqual
827 ******************************************************************************/
828 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
830 ICOM_THIS(FileMonikerImpl,iface);
836 Print(MAX_TRACE, ("(%p,%p)\n",iface,pmkOtherMoniker));
838 if (pmkOtherMoniker==NULL)
841 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
843 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
847 res=CreateBindCtx(0,&bind);
851 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
853 if (lstrcmpiW(filePath,
854 This->filePathName)!=0)
861 /******************************************************************************
863 ******************************************************************************/
864 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
866 ICOM_THIS(FileMonikerImpl,iface);
868 int h = 0,i,skip,len;
875 val = This->filePathName;
879 for (i = len ; i > 0; i--) {
880 h = (h * 37) + val[off++];
883 /* only sample some characters */
885 for (i = len ; i > 0; i -= skip, off += skip) {
886 h = (h * 39) + val[off];
895 /******************************************************************************
896 * FileMoniker_IsRunning
897 ******************************************************************************/
898 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
901 IMoniker* pmkNewlyRunning)
903 IRunningObjectTable* rot;
906 Print(MAX_TRACE, ("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning));
908 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
914 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
919 res = IRunningObjectTable_IsRunning(rot,iface);
921 IRunningObjectTable_Release(rot);
926 /******************************************************************************
927 * FileMoniker_GetTimeOfLastChange
928 ******************************************************************************/
929 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
934 ICOM_THIS(FileMonikerImpl,iface);
935 IRunningObjectTable* rot;
937 WIN32_FILE_ATTRIBUTE_DATA info;
939 Print(MAX_TRACE, ("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime));
947 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
952 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
954 if (FAILED(res)){ /* the moniker is not registred */
956 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
957 return MK_E_NOOBJECT;
959 Print(MIN_TRACE, ("GetFileAttributesExW() nowhere to be found\n"));
961 *pFileTime=info.ftLastWriteTime;
967 /******************************************************************************
968 * FileMoniker_Inverse
969 ******************************************************************************/
970 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
973 Print(MAX_TRACE, ("(%p,%p)\n",iface,ppmk));
975 return CreateAntiMoniker(ppmk);
978 /******************************************************************************
979 * FileMoniker_CommonPrefixWith
980 ******************************************************************************/
981 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
984 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
987 ULONG nb1,nb2,i,sameIdx;
988 BOOL machimeNameCase=FALSE;
990 if (ppmkPrefix==NULL)
998 /* check if we have the same type of moniker */
999 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1001 if(mkSys==MKSYS_FILEMONIKER){
1003 CreateBindCtx(0,&pbind);
1005 /* create a string based on common part of the two paths */
1007 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1008 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1010 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1011 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1013 if (nb1==0 || nb2==0)
1014 return MK_E_NOPREFIX;
1016 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1020 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1021 (stringTable2[sameIdx]!=NULL) &&
1022 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1024 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1026 machimeNameCase=TRUE;
1028 for(i=2;i<sameIdx;i++)
1030 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1031 machimeNameCase=FALSE;
1036 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1039 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1040 return MK_E_NOPREFIX;
1042 for(i=0;i<sameIdx;i++)
1043 lstrcatW(commonPath,stringTable1[i]);
1046 CoTaskMemFree(stringTable1[i]);
1048 CoTaskMemFree(stringTable1);
1051 CoTaskMemFree(stringTable2[i]);
1053 CoTaskMemFree(stringTable2);
1055 HeapFree(GetProcessHeap(),0,commonPath);
1057 return CreateFileMoniker(commonPath,ppmkPrefix);
1060 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1063 /******************************************************************************
1064 * DecomposePath (local function)
1065 ******************************************************************************/
1066 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** stringTable)
1068 WCHAR bSlash[] = {'\\',0};
1069 WCHAR word[MAX_PATH];
1070 int i=0,j,tabIndex=0;
1071 LPOLESTR *strgtable ;
1073 int len=lstrlenW(str);
1075 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1077 if (strgtable==NULL)
1078 return E_OUTOFMEMORY;
1082 if(str[i]==bSlash[0]){
1084 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1086 if (strgtable[tabIndex]==NULL)
1087 return E_OUTOFMEMORY;
1089 lstrcpyW(strgtable[tabIndex++],bSlash);
1096 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1101 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1103 if (strgtable[tabIndex]==NULL)
1104 return E_OUTOFMEMORY;
1106 lstrcpyW(strgtable[tabIndex++],word);
1109 strgtable[tabIndex]=NULL;
1111 *stringTable=strgtable;
1116 /******************************************************************************
1117 * FileMoniker_RelativePathTo
1118 ******************************************************************************/
1119 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1123 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1124 DWORD len1=0,len2=0,sameIdx=0,j=0;
1125 WCHAR back[] ={'.','.','\\',0};
1127 Print(MAX_TRACE, ("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath));
1129 if (ppmkRelPath==NULL)
1133 return E_INVALIDARG;
1135 res=CreateBindCtx(0,&bind);
1139 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1142 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1146 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1147 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1149 if (FAILED(len1) || FAILED(len2))
1150 return E_OUTOFMEMORY;
1152 /* count the number of similar items from the begin of the two paths */
1153 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1154 (tabStr2[sameIdx]!=NULL) &&
1155 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1157 /* begin the construction of relativePath */
1158 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1159 /* by "..\\" in the begin */
1160 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1164 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1165 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1166 if (*tabStr1[j]!='\\')
1167 lstrcatW(relPath,back);
1169 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1170 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1171 lstrcatW(relPath,tabStr2[j]);
1173 res=CreateFileMoniker(relPath,ppmkRelPath);
1175 for(j=0; tabStr1[j]!=NULL;j++)
1176 CoTaskMemFree(tabStr1[j]);
1177 for(j=0; tabStr2[j]!=NULL;j++)
1178 CoTaskMemFree(tabStr2[j]);
1179 CoTaskMemFree(tabStr1);
1180 CoTaskMemFree(tabStr2);
1181 CoTaskMemFree(str1);
1182 CoTaskMemFree(str2);
1183 HeapFree(GetProcessHeap(),0,relPath);
1185 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1191 /******************************************************************************
1192 * FileMoniker_GetDisplayName
1193 ******************************************************************************/
1194 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1196 IMoniker* pmkToLeft,
1197 LPOLESTR *ppszDisplayName)
1199 ICOM_THIS(FileMonikerImpl,iface);
1201 int len=lstrlenW(This->filePathName);
1203 Print(MAX_TRACE, ("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName));
1205 if (ppszDisplayName==NULL)
1208 if (pmkToLeft!=NULL)
1209 return E_INVALIDARG;
1211 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1212 if (*ppszDisplayName==NULL)
1213 return E_OUTOFMEMORY;
1215 lstrcpyW(*ppszDisplayName,This->filePathName);
1220 /******************************************************************************
1221 * FileMoniker_ParseDisplayName
1222 ******************************************************************************/
1223 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1225 IMoniker* pmkToLeft,
1226 LPOLESTR pszDisplayName,
1230 Print(MIN_TRACE, ("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut));
1234 /******************************************************************************
1235 * FileMoniker_IsSystemMoniker
1236 ******************************************************************************/
1237 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1239 Print(MAX_TRACE, ("(%p,%p)\n",iface,pwdMksys));
1244 (*pwdMksys)=MKSYS_FILEMONIKER;
1249 /*******************************************************************************
1250 * FileMonikerIROTData_QueryInterface
1251 *******************************************************************************/
1252 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1255 ICOM_THIS_From_IROTData(IMoniker, iface);
1257 Print(MAX_TRACE, ("(%p,%p,%p)\n",This,riid,ppvObject));
1259 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1262 /***********************************************************************
1263 * FileMonikerIROTData_AddRef
1265 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1267 ICOM_THIS_From_IROTData(IMoniker, iface);
1269 Print(MAX_TRACE, ("(%p)\n",This));
1271 return FileMonikerImpl_AddRef(This);
1274 /***********************************************************************
1275 * FileMonikerIROTData_Release
1277 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1279 ICOM_THIS_From_IROTData(IMoniker, iface);
1281 Print(MAX_TRACE, ("(%p)\n",This));
1283 return FileMonikerImpl_Release(This);
1286 /******************************************************************************
1287 * FileMonikerIROTData_GetComparaisonData
1288 ******************************************************************************/
1289 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1298 /******************************************************************************
1299 * CreateFileMoniker16
1300 ******************************************************************************/
1301 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1308 /******************************************************************************
1310 ******************************************************************************/
1311 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1313 FileMonikerImpl* newFileMoniker = 0;
1314 HRESULT hr = E_FAIL;
1315 IID riid=IID_IMoniker;
1317 Print(MAX_TRACE, ("(%p,%p)\n",lpszPathName,ppmk));
1322 if(lpszPathName==NULL)
1327 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1329 if (newFileMoniker == 0)
1330 return E_OUTOFMEMORY;
1332 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1335 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1337 HeapFree(GetProcessHeap(),0,newFileMoniker);