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(LPOLESTR 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 begining I thougth that I have just to write
325 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
326 * I noted that it was not the case. So I analysed data written by this function on Windows system and
327 * what did this function do exactly ! but I have no idear a bout its logic !
328 * I guessed data who must be written on stream wich is:
329 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
330 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
331 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
332 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
335 ICOM_THIS(FileMonikerImpl,iface);
338 LPOLESTR filePathW=This->filePathName;
342 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
343 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
350 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
355 /* write a DWORD set to 0 : constant */
356 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
358 /* write length of filePath string ( "\0" included )*/
359 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
360 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
362 /* write filePath string type A */
363 filePathA=HeapAlloc(GetProcessHeap(),0,len);
364 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
365 res=IStream_Write(pStm,filePathA,len,NULL);
366 HeapFree(GetProcessHeap(),0,filePathA);
368 /* write a DWORD set to 0xDEADFFFF: constant */
369 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
372 /* write 10 times a DWORD set to 0 : constants */
374 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
379 doubleLenHex=doubleLenDec=2*len;
383 /* write double-length of the path string ( "\0" included )*/
384 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
389 /* write double-length (hexa representation) of the path string ( "\0" included ) */
390 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
392 /* write a WORD set to 0x3: constant */
393 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
395 /* write path unicode string */
396 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
401 /******************************************************************************
402 * FileMoniker_GetSizeMax
403 ******************************************************************************/
404 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
405 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
407 ICOM_THIS(FileMonikerImpl,iface);
408 DWORD len=lstrlenW(This->filePathName);
411 TRACE("(%p,%p)\n",iface,pcbSize);
416 /* for more details see FileMonikerImpl_Save coments */
418 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
419 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
420 (len+1)+ /* filePath string */
421 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
422 10*sizeof(WORD)+ /* 10 zero WORD */
423 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
425 if (len==0 || len > 8)
428 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
429 sizeof(WORD)+ /* constant : 0x3 */
430 len*sizeof(WCHAR); /* unicde filePath string */
432 pcbSize->s.LowPart=sizeMAx;
433 pcbSize->s.HighPart=0;
438 /******************************************************************************
439 * FileMoniker_Construct (local function)
440 *******************************************************************************/
441 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
444 int sizeStr=lstrlenW(lpszPathName);
446 WCHAR twoPoint[]={'.','.',0};
447 WCHAR bkSlash[]={'\\',0};
450 TRACE("(%p,%p)\n",This,lpszPathName);
452 /* Initialize the virtual fgunction table. */
453 This->lpvtbl1 = &VT_FileMonikerImpl;
454 This->lpvtbl2 = &VT_ROTDataImpl;
457 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
459 if (This->filePathName==NULL)
460 return E_OUTOFMEMORY;
462 strcpyW(This->filePathName,lpszPathName);
464 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
469 if (lstrcmpW(tabStr[0],twoPoint)!=0)
474 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
480 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
488 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
491 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
493 *This->filePathName=0;
495 for(i=0;tabStr[i]!=NULL;i++)
496 strcatW(This->filePathName,tabStr[i]);
499 strcatW(This->filePathName,bkSlash);
502 for(i=0; tabStr[i]!=NULL;i++)
503 CoTaskMemFree(tabStr[i]);
504 CoTaskMemFree(tabStr);
509 /******************************************************************************
510 * FileMoniker_Destroy (local function)
511 *******************************************************************************/
512 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
514 TRACE("(%p)\n",This);
516 if (This->filePathName!=NULL)
517 HeapFree(GetProcessHeap(),0,This->filePathName);
519 HeapFree(GetProcessHeap(),0,This);
524 /******************************************************************************
525 * FileMoniker_BindToObject
526 ******************************************************************************/
527 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
536 IRunningObjectTable *prot=0;
538 IClassFactory *pcf=0;
539 IClassActivator *pca=0;
541 ICOM_THIS(FileMonikerImpl,iface);
545 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
549 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
552 /* if the requested class was loaded befor ! we dont need to reload it */
553 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
556 /* first activation of this class */
557 res=GetClassFile(This->filePathName,&clsID);
560 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
563 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
567 IUnknown_AddRef(pObj);
575 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
577 if (res==E_NOINTERFACE){
579 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
581 if (res==E_NOINTERFACE)
582 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
586 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
588 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
593 IUnknown_AddRef(pObj);
600 /*res=GetClassFile(This->filePathName,&clsID);
604 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
609 IUnknown_AddRef(pObj);
616 /* get the requested interface from the loaded class */
617 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
619 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
621 IUnknown_Release(pObj);
625 IRunningObjectTable_Release(prot);
628 IPersistFile_Release(ppf);
631 IClassActivator_Release(pca);
634 IClassFactory_Release(pcf);
639 /******************************************************************************
640 * FileMoniker_BindToStorage
641 ******************************************************************************/
642 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
652 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
654 if (pmkToLeft==NULL){
656 if (IsEqualIID(&IID_IStorage, riid)){
658 /* get the file name */
659 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
661 /* verifie if the file contains a storage object */
662 res=StgIsStorageFile(filePath);
666 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
672 IStorage_AddRef(pstg);
677 CoTaskMemFree(filePath);
680 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
685 return E_NOINTERFACE;
689 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
696 /******************************************************************************
698 ******************************************************************************/
699 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
701 DWORD dwReduceHowFar,
702 IMoniker** ppmkToLeft,
703 IMoniker** ppmkReduced)
705 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
707 if (ppmkReduced==NULL)
710 FileMonikerImpl_AddRef(iface);
714 return MK_S_REDUCED_TO_SELF;
716 /******************************************************************************
717 * FileMoniker_ComposeWith
718 ******************************************************************************/
719 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
721 BOOL fOnlyIfNotGeneric,
722 IMoniker** ppmkComposite)
725 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
726 WCHAR twoPoint[]={'.','.',0};
727 WCHAR bkSlash[]={'\\',0};
729 int i=0,j=0,lastIdx1=0,lastIdx2=0;
732 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
734 if (ppmkComposite==NULL)
742 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
744 /* check if we have two filemonikers to compose or not */
745 if(mkSys==MKSYS_FILEMONIKER){
747 CreateBindCtx(0,&bind);
749 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
750 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
752 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
753 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
754 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
756 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
759 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
762 /* for etch "..\" in the left of str2 remove the right element from str1 */
763 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
768 /* the length of the composed path string is raised by the sum of the two paths lengths */
769 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
772 return E_OUTOFMEMORY;
774 /* new path is the concatenation of the rest of str1 and str2 */
775 for(*newStr=0,j=0;j<=lastIdx1;j++)
776 strcatW(newStr,strDec1[j]);
778 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
779 strcatW(newStr,bkSlash);
781 for(j=i;j<=lastIdx2;j++)
782 strcatW(newStr,strDec2[j]);
784 /* create a new moniker with the new string */
785 res=CreateFileMoniker(newStr,ppmkComposite);
787 /* free all strings space memory used by this function */
788 HeapFree(GetProcessHeap(),0,newStr);
790 for(i=0; strDec1[i]!=NULL;i++)
791 CoTaskMemFree(strDec1[i]);
792 for(i=0; strDec2[i]!=NULL;i++)
793 CoTaskMemFree(strDec2[i]);
794 CoTaskMemFree(strDec1);
795 CoTaskMemFree(strDec2);
802 else if(mkSys==MKSYS_ANTIMONIKER){
807 else if (fOnlyIfNotGeneric){
810 return MK_E_NEEDGENERIC;
814 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
817 /******************************************************************************
819 ******************************************************************************/
820 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
822 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
824 if (ppenumMoniker == NULL)
827 *ppenumMoniker = NULL;
832 /******************************************************************************
833 * FileMoniker_IsEqual
834 ******************************************************************************/
835 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
837 ICOM_THIS(FileMonikerImpl,iface);
843 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
845 if (pmkOtherMoniker==NULL)
848 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
850 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
854 res=CreateBindCtx(0,&bind);
858 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
860 if (lstrcmpiW(filePath,
861 This->filePathName)!=0)
868 /******************************************************************************
870 ******************************************************************************/
871 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
873 ICOM_THIS(FileMonikerImpl,iface);
875 int h = 0,i,skip,len;
882 val = This->filePathName;
886 for (i = len ; i > 0; i--) {
887 h = (h * 37) + val[off++];
890 /* only sample some characters */
892 for (i = len ; i > 0; i -= skip, off += skip) {
893 h = (h * 39) + val[off];
902 /******************************************************************************
903 * FileMoniker_IsRunning
904 ******************************************************************************/
905 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
908 IMoniker* pmkNewlyRunning)
910 IRunningObjectTable* rot;
913 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
915 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
921 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
926 res = IRunningObjectTable_IsRunning(rot,iface);
928 IRunningObjectTable_Release(rot);
933 /******************************************************************************
934 * FileMoniker_GetTimeOfLastChange
935 ******************************************************************************/
936 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
941 ICOM_THIS(FileMonikerImpl,iface);
942 IRunningObjectTable* rot;
944 WIN32_FILE_ATTRIBUTE_DATA info;
946 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
954 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
959 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
961 if (FAILED(res)){ /* the moniker is not registred */
963 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
964 return MK_E_NOOBJECT;
966 *pFileTime=info.ftLastWriteTime;
972 /******************************************************************************
973 * FileMoniker_Inverse
974 ******************************************************************************/
975 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
978 TRACE("(%p,%p)\n",iface,ppmk);
980 return CreateAntiMoniker(ppmk);
983 /******************************************************************************
984 * FileMoniker_CommonPrefixWith
985 ******************************************************************************/
986 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
989 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
992 ULONG nb1,nb2,i,sameIdx;
993 BOOL machimeNameCase=FALSE;
995 if (ppmkPrefix==NULL)
1003 /* check if we have the same type of moniker */
1004 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1006 if(mkSys==MKSYS_FILEMONIKER){
1008 CreateBindCtx(0,&pbind);
1010 /* create a string based on common part of the two paths */
1012 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1013 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1015 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1016 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1018 if (nb1==0 || nb2==0)
1019 return MK_E_NOPREFIX;
1021 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1025 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1026 (stringTable2[sameIdx]!=NULL) &&
1027 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1029 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1031 machimeNameCase=TRUE;
1033 for(i=2;i<sameIdx;i++)
1035 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1036 machimeNameCase=FALSE;
1041 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1044 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1045 return MK_E_NOPREFIX;
1047 for(i=0;i<sameIdx;i++)
1048 strcatW(commonPath,stringTable1[i]);
1051 CoTaskMemFree(stringTable1[i]);
1053 CoTaskMemFree(stringTable1);
1056 CoTaskMemFree(stringTable2[i]);
1058 CoTaskMemFree(stringTable2);
1060 HeapFree(GetProcessHeap(),0,commonPath);
1062 return CreateFileMoniker(commonPath,ppmkPrefix);
1065 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1068 /******************************************************************************
1069 * DecomposePath (local function)
1070 ******************************************************************************/
1071 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** stringTable)
1073 WCHAR bSlash[] = {'\\',0};
1074 WCHAR word[MAX_PATH];
1075 int i=0,j,tabIndex=0;
1076 LPOLESTR *strgtable ;
1078 int len=lstrlenW(str);
1080 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1082 if (strgtable==NULL)
1083 return E_OUTOFMEMORY;
1087 if(str[i]==bSlash[0]){
1089 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1091 if (strgtable[tabIndex]==NULL)
1092 return E_OUTOFMEMORY;
1094 strcpyW(strgtable[tabIndex++],bSlash);
1101 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1106 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1108 if (strgtable[tabIndex]==NULL)
1109 return E_OUTOFMEMORY;
1111 strcpyW(strgtable[tabIndex++],word);
1114 strgtable[tabIndex]=NULL;
1116 *stringTable=strgtable;
1121 /******************************************************************************
1122 * FileMoniker_RelativePathTo
1123 ******************************************************************************/
1124 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1128 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1129 DWORD len1=0,len2=0,sameIdx=0,j=0;
1130 WCHAR back[] ={'.','.','\\',0};
1132 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1134 if (ppmkRelPath==NULL)
1138 return E_INVALIDARG;
1140 res=CreateBindCtx(0,&bind);
1144 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1147 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1151 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1152 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1154 if (FAILED(len1) || FAILED(len2))
1155 return E_OUTOFMEMORY;
1157 /* count the number of similar items from the begin of the two paths */
1158 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1159 (tabStr2[sameIdx]!=NULL) &&
1160 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1162 /* begin the construction of relativePath */
1163 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1164 /* by "..\\" in the begin */
1165 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1169 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1170 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1171 if (*tabStr1[j]!='\\')
1172 strcatW(relPath,back);
1174 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1175 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1176 strcatW(relPath,tabStr2[j]);
1178 res=CreateFileMoniker(relPath,ppmkRelPath);
1180 for(j=0; tabStr1[j]!=NULL;j++)
1181 CoTaskMemFree(tabStr1[j]);
1182 for(j=0; tabStr2[j]!=NULL;j++)
1183 CoTaskMemFree(tabStr2[j]);
1184 CoTaskMemFree(tabStr1);
1185 CoTaskMemFree(tabStr2);
1186 CoTaskMemFree(str1);
1187 CoTaskMemFree(str2);
1188 HeapFree(GetProcessHeap(),0,relPath);
1190 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1196 /******************************************************************************
1197 * FileMoniker_GetDisplayName
1198 ******************************************************************************/
1199 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1201 IMoniker* pmkToLeft,
1202 LPOLESTR *ppszDisplayName)
1204 ICOM_THIS(FileMonikerImpl,iface);
1206 int len=lstrlenW(This->filePathName);
1208 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1210 if (ppszDisplayName==NULL)
1213 if (pmkToLeft!=NULL)
1214 return E_INVALIDARG;
1216 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1217 if (*ppszDisplayName==NULL)
1218 return E_OUTOFMEMORY;
1220 strcpyW(*ppszDisplayName,This->filePathName);
1225 /******************************************************************************
1226 * FileMoniker_ParseDisplayName
1227 ******************************************************************************/
1228 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1230 IMoniker* pmkToLeft,
1231 LPOLESTR pszDisplayName,
1235 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1239 /******************************************************************************
1240 * FileMoniker_IsSystemMoniker
1241 ******************************************************************************/
1242 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1244 TRACE("(%p,%p)\n",iface,pwdMksys);
1249 (*pwdMksys)=MKSYS_FILEMONIKER;
1254 /*******************************************************************************
1255 * FileMonikerIROTData_QueryInterface
1256 *******************************************************************************/
1257 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1260 ICOM_THIS_From_IROTData(IMoniker, iface);
1262 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1264 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1267 /***********************************************************************
1268 * FileMonikerIROTData_AddRef
1270 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1272 ICOM_THIS_From_IROTData(IMoniker, iface);
1274 TRACE("(%p)\n",This);
1276 return FileMonikerImpl_AddRef(This);
1279 /***********************************************************************
1280 * FileMonikerIROTData_Release
1282 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1284 ICOM_THIS_From_IROTData(IMoniker, iface);
1286 TRACE("(%p)\n",This);
1288 return FileMonikerImpl_Release(This);
1291 /******************************************************************************
1292 * FileMonikerIROTData_GetComparaisonData
1293 ******************************************************************************/
1294 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1299 FIXME("(),stub!\n");
1303 /******************************************************************************
1304 * CreateFileMoniker16
1305 ******************************************************************************/
1306 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1309 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1313 /******************************************************************************
1315 ******************************************************************************/
1316 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1318 FileMonikerImpl* newFileMoniker = 0;
1319 HRESULT hr = E_FAIL;
1320 IID riid=IID_IMoniker;
1322 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1327 if(lpszPathName==NULL)
1332 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1334 if (newFileMoniker == 0)
1335 return E_OUTOFMEMORY;
1337 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1340 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1342 HeapFree(GetProcessHeap(),0,newFileMoniker);