1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************************/
26 #include "wine/unicode.h"
27 #include "wine/debug.h"
31 #include "compobj_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ole);
35 const CLSID CLSID_FileMoniker = {
36 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
39 /* filemoniker data structure */
40 typedef struct FileMonikerImpl{
42 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
44 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
45 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
47 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
49 ULONG ref; /* reference counter for this object */
51 LPOLESTR filePathName; /* path string identified by this filemoniker */
55 /********************************************************************************/
56 /* FileMoniker prototype functions : */
58 /* IUnknown prototype functions */
59 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
60 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
61 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
63 /* IPersist prototype functions */
64 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
66 /* IPersistStream prototype functions */
67 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
68 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
69 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
70 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
72 /* IMoniker prototype functions */
73 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
74 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
75 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
76 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
77 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
78 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
79 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
80 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
81 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
82 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
83 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
84 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
85 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
86 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
87 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
89 /********************************************************************************/
90 /* IROTData prototype functions */
92 /* IUnknown prototype functions */
93 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
94 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
95 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
97 /* IROTData prototype function */
98 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
100 /* Local function used by filemoniker implementation */
101 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
102 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
103 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
106 /********************************************************************************/
107 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
108 /* IPersistStream and IMoniker functions. */
109 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
111 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
112 FileMonikerImpl_QueryInterface,
113 FileMonikerImpl_AddRef,
114 FileMonikerImpl_Release,
115 FileMonikerImpl_GetClassID,
116 FileMonikerImpl_IsDirty,
117 FileMonikerImpl_Load,
118 FileMonikerImpl_Save,
119 FileMonikerImpl_GetSizeMax,
120 FileMonikerImpl_BindToObject,
121 FileMonikerImpl_BindToStorage,
122 FileMonikerImpl_Reduce,
123 FileMonikerImpl_ComposeWith,
124 FileMonikerImpl_Enum,
125 FileMonikerImpl_IsEqual,
126 FileMonikerImpl_Hash,
127 FileMonikerImpl_IsRunning,
128 FileMonikerImpl_GetTimeOfLastChange,
129 FileMonikerImpl_Inverse,
130 FileMonikerImpl_CommonPrefixWith,
131 FileMonikerImpl_RelativePathTo,
132 FileMonikerImpl_GetDisplayName,
133 FileMonikerImpl_ParseDisplayName,
134 FileMonikerImpl_IsSystemMoniker
137 /********************************************************************************/
138 /* Virtual function table for the IROTData class. */
139 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
141 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
142 FileMonikerROTDataImpl_QueryInterface,
143 FileMonikerROTDataImpl_AddRef,
144 FileMonikerROTDataImpl_Release,
145 FileMonikerROTDataImpl_GetComparaisonData
148 /*******************************************************************************
149 * FileMoniker_QueryInterface
150 *******************************************************************************/
151 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
153 ICOM_THIS(FileMonikerImpl,iface);
155 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
157 /* Perform a sanity check on the parameters.*/
158 if ( (This==0) || (ppvObject==0) )
161 /* Initialize the return parameter */
164 /* Compare the riid with the interface IDs implemented by this object.*/
165 if (IsEqualIID(&IID_IUnknown, riid) ||
166 IsEqualIID(&IID_IPersist, riid) ||
167 IsEqualIID(&IID_IPersistStream,riid) ||
168 IsEqualIID(&IID_IMoniker, riid)
172 else if (IsEqualIID(&IID_IROTData, riid))
173 *ppvObject = (IROTData*)&(This->lpvtbl2);
175 /* Check that we obtained an interface.*/
177 return E_NOINTERFACE;
179 /* Query Interface always increases the reference count by one when it is successful */
180 FileMonikerImpl_AddRef(iface);
185 /******************************************************************************
187 ******************************************************************************/
188 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
190 ICOM_THIS(FileMonikerImpl,iface);
192 TRACE("(%p)\n",iface);
194 return ++(This->ref);
197 /******************************************************************************
198 * FileMoniker_Release
199 ******************************************************************************/
200 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
202 ICOM_THIS(FileMonikerImpl,iface);
204 TRACE("(%p)\n",iface);
208 /* destroy the object if there's no more reference on it */
211 FileMonikerImpl_Destroy(This);
218 /******************************************************************************
219 * FileMoniker_GetClassID
220 ******************************************************************************/
221 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
222 CLSID *pClassID)/* Pointer to CLSID of object */
224 TRACE("(%p,%p),stub!\n",iface,pClassID);
229 *pClassID = CLSID_FileMoniker;
234 /******************************************************************************
235 * FileMoniker_IsDirty
236 ******************************************************************************/
237 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
239 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
240 method in the OLE-provided moniker interfaces always return S_FALSE because
241 their internal state never changes. */
243 TRACE("(%p)\n",iface);
248 /******************************************************************************
250 ******************************************************************************/
251 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
258 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
260 ICOM_THIS(FileMonikerImpl,iface);
262 TRACE("(%p,%p)\n",iface,pStm);
264 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
266 /* first WORD is non significative */
267 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
268 if (bread!=sizeof(WORD) || wbuffer!=0)
271 /* read filePath string length (plus one) */
272 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
273 if (bread != sizeof(DWORD))
276 /* read filePath string */
277 filePathA=HeapAlloc(GetProcessHeap(),0,length);
278 res=IStream_Read(pStm,filePathA,length,&bread);
282 /* read the first constant */
283 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
284 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
290 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
291 if (bread!=sizeof(WORD) || wbuffer!=0)
298 doubleLenHex=doubleLenDec=2*length;
302 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
303 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
309 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
310 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
313 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
314 if (bread!=sizeof(WORD) || wbuffer!=0x3)
317 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
319 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
320 if (bread!=doubleLenHex)
323 if (This->filePathName!=NULL)
324 HeapFree(GetProcessHeap(),0,This->filePathName);
326 This->filePathName=filePathW;
328 HeapFree(GetProcessHeap(),0,filePathA);
333 /******************************************************************************
335 ******************************************************************************/
336 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
337 IStream* pStm,/* pointer to the stream where the object is to be saved */
338 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
340 /* this function saves data of this object. In the beginning I thougth
341 * that I have just to write the filePath string on Stream. But, when I
342 * tested this function whith windows programs samples, I noticed that it
343 * was not the case. So I analysed data written by this function on
344 * Windows and what this did function exactly ! But I have no idea about
346 * I guessed data which must be written on stream is:
347 * 1) WORD constant:zero
348 * 2) length of the path string ("\0" included)
349 * 3) path string type A
350 * 4) DWORD constant : 0xDEADFFFF
351 * 5) ten WORD constant: zero
352 * 6) DWORD: double-length of the the path string type W ("\0" not
354 * 7) WORD constant: 0x3
355 * 8) filePath unicode string.
356 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
359 ICOM_THIS(FileMonikerImpl,iface);
362 LPOLESTR filePathW=This->filePathName;
366 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
367 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
374 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
379 /* write a DWORD set to 0 : constant */
380 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
382 /* write length of filePath string ( "\0" included )*/
383 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
384 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
386 /* write filePath string type A */
387 filePathA=HeapAlloc(GetProcessHeap(),0,len);
388 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
389 res=IStream_Write(pStm,filePathA,len,NULL);
390 HeapFree(GetProcessHeap(),0,filePathA);
392 /* write a DWORD set to 0xDEADFFFF: constant */
393 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
396 /* write 10 times a DWORD set to 0 : constants */
398 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
403 doubleLenHex=doubleLenDec=2*len;
407 /* write double-length of the path string ( "\0" included )*/
408 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
413 /* write double-length (hexa representation) of the path string ( "\0" included ) */
414 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
416 /* write a WORD set to 0x3: constant */
417 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
419 /* write path unicode string */
420 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
425 /******************************************************************************
426 * FileMoniker_GetSizeMax
427 ******************************************************************************/
428 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
429 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
431 ICOM_THIS(FileMonikerImpl,iface);
432 DWORD len=lstrlenW(This->filePathName);
435 TRACE("(%p,%p)\n",iface,pcbSize);
440 /* for more details see FileMonikerImpl_Save coments */
442 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
443 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
444 (len+1)+ /* filePath string */
445 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
446 10*sizeof(WORD)+ /* 10 zero WORD */
447 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
449 if (len==0 || len > 8)
452 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
453 sizeof(WORD)+ /* constant : 0x3 */
454 len*sizeof(WCHAR); /* unicde filePath string */
456 pcbSize->s.LowPart=sizeMAx;
457 pcbSize->s.HighPart=0;
462 /******************************************************************************
463 * FileMoniker_Construct (local function)
464 *******************************************************************************/
465 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
468 int sizeStr=lstrlenW(lpszPathName);
470 WCHAR twoPoint[]={'.','.',0};
471 WCHAR bkSlash[]={'\\',0};
474 TRACE("(%p,%p)\n",This,lpszPathName);
476 /* Initialize the virtual fgunction table. */
477 This->lpvtbl1 = &VT_FileMonikerImpl;
478 This->lpvtbl2 = &VT_ROTDataImpl;
481 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
483 if (This->filePathName==NULL)
484 return E_OUTOFMEMORY;
486 strcpyW(This->filePathName,lpszPathName);
488 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
493 if (lstrcmpW(tabStr[0],twoPoint)!=0)
498 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
504 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
512 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
515 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
517 *This->filePathName=0;
519 for(i=0;tabStr[i]!=NULL;i++)
520 strcatW(This->filePathName,tabStr[i]);
523 strcatW(This->filePathName,bkSlash);
526 for(i=0; tabStr[i]!=NULL;i++)
527 CoTaskMemFree(tabStr[i]);
528 CoTaskMemFree(tabStr);
533 /******************************************************************************
534 * FileMoniker_Destroy (local function)
535 *******************************************************************************/
536 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
538 TRACE("(%p)\n",This);
540 if (This->filePathName!=NULL)
541 HeapFree(GetProcessHeap(),0,This->filePathName);
543 HeapFree(GetProcessHeap(),0,This);
548 /******************************************************************************
549 * FileMoniker_BindToObject
550 ******************************************************************************/
551 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
560 IRunningObjectTable *prot=0;
562 IClassFactory *pcf=0;
563 IClassActivator *pca=0;
565 ICOM_THIS(FileMonikerImpl,iface);
569 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
573 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
576 /* if the requested class was loaded befor ! we dont need to reload it */
577 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
580 /* first activation of this class */
581 res=GetClassFile(This->filePathName,&clsID);
584 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
587 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
591 IUnknown_AddRef(pObj);
599 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
601 if (res==E_NOINTERFACE){
603 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
605 if (res==E_NOINTERFACE)
606 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
610 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
612 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
617 IUnknown_AddRef(pObj);
624 /*res=GetClassFile(This->filePathName,&clsID);
628 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
633 IUnknown_AddRef(pObj);
640 /* get the requested interface from the loaded class */
641 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
643 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
645 IUnknown_Release(pObj);
649 IRunningObjectTable_Release(prot);
652 IPersistFile_Release(ppf);
655 IClassActivator_Release(pca);
658 IClassFactory_Release(pcf);
663 /******************************************************************************
664 * FileMoniker_BindToStorage
665 ******************************************************************************/
666 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
676 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
678 if (pmkToLeft==NULL){
680 if (IsEqualIID(&IID_IStorage, riid)){
682 /* get the file name */
683 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
685 /* verifie if the file contains a storage object */
686 res=StgIsStorageFile(filePath);
690 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
696 IStorage_AddRef(pstg);
701 CoTaskMemFree(filePath);
704 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
709 return E_NOINTERFACE;
713 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
720 /******************************************************************************
722 ******************************************************************************/
723 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
725 DWORD dwReduceHowFar,
726 IMoniker** ppmkToLeft,
727 IMoniker** ppmkReduced)
729 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
731 if (ppmkReduced==NULL)
734 FileMonikerImpl_AddRef(iface);
738 return MK_S_REDUCED_TO_SELF;
740 /******************************************************************************
741 * FileMoniker_ComposeWith
742 ******************************************************************************/
743 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
745 BOOL fOnlyIfNotGeneric,
746 IMoniker** ppmkComposite)
749 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
750 WCHAR twoPoint[]={'.','.',0};
751 WCHAR bkSlash[]={'\\',0};
753 int i=0,j=0,lastIdx1=0,lastIdx2=0;
756 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
758 if (ppmkComposite==NULL)
766 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
768 /* check if we have two filemonikers to compose or not */
769 if(mkSys==MKSYS_FILEMONIKER){
771 CreateBindCtx(0,&bind);
773 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
774 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
776 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
777 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
778 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
780 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
783 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
786 /* for etch "..\" in the left of str2 remove the right element from str1 */
787 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
792 /* the length of the composed path string is raised by the sum of the two paths lengths */
793 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
796 return E_OUTOFMEMORY;
798 /* new path is the concatenation of the rest of str1 and str2 */
799 for(*newStr=0,j=0;j<=lastIdx1;j++)
800 strcatW(newStr,strDec1[j]);
802 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
803 strcatW(newStr,bkSlash);
805 for(j=i;j<=lastIdx2;j++)
806 strcatW(newStr,strDec2[j]);
808 /* create a new moniker with the new string */
809 res=CreateFileMoniker(newStr,ppmkComposite);
811 /* free all strings space memory used by this function */
812 HeapFree(GetProcessHeap(),0,newStr);
814 for(i=0; strDec1[i]!=NULL;i++)
815 CoTaskMemFree(strDec1[i]);
816 for(i=0; strDec2[i]!=NULL;i++)
817 CoTaskMemFree(strDec2[i]);
818 CoTaskMemFree(strDec1);
819 CoTaskMemFree(strDec2);
826 else if(mkSys==MKSYS_ANTIMONIKER){
831 else if (fOnlyIfNotGeneric){
834 return MK_E_NEEDGENERIC;
838 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
841 /******************************************************************************
843 ******************************************************************************/
844 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
846 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
848 if (ppenumMoniker == NULL)
851 *ppenumMoniker = NULL;
856 /******************************************************************************
857 * FileMoniker_IsEqual
858 ******************************************************************************/
859 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
861 ICOM_THIS(FileMonikerImpl,iface);
867 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
869 if (pmkOtherMoniker==NULL)
872 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
874 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
877 res = CreateBindCtx(0,&bind);
878 if (FAILED(res)) return res;
880 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
881 int result = lstrcmpiW(filePath, This->filePathName);
882 CoTaskMemFree(filePath);
883 if ( result == 0 ) return S_OK;
889 /******************************************************************************
891 ******************************************************************************/
892 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
894 ICOM_THIS(FileMonikerImpl,iface);
896 int h = 0,i,skip,len;
903 val = This->filePathName;
907 for (i = len ; i > 0; i--) {
908 h = (h * 37) + val[off++];
911 /* only sample some characters */
913 for (i = len ; i > 0; i -= skip, off += skip) {
914 h = (h * 39) + val[off];
923 /******************************************************************************
924 * FileMoniker_IsRunning
925 ******************************************************************************/
926 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
929 IMoniker* pmkNewlyRunning)
931 IRunningObjectTable* rot;
934 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
936 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
942 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
947 res = IRunningObjectTable_IsRunning(rot,iface);
949 IRunningObjectTable_Release(rot);
954 /******************************************************************************
955 * FileMoniker_GetTimeOfLastChange
956 ******************************************************************************/
957 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
962 ICOM_THIS(FileMonikerImpl,iface);
963 IRunningObjectTable* rot;
965 WIN32_FILE_ATTRIBUTE_DATA info;
967 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
975 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
980 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
982 if (FAILED(res)){ /* the moniker is not registred */
984 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
985 return MK_E_NOOBJECT;
987 *pFileTime=info.ftLastWriteTime;
993 /******************************************************************************
994 * FileMoniker_Inverse
995 ******************************************************************************/
996 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
999 TRACE("(%p,%p)\n",iface,ppmk);
1001 return CreateAntiMoniker(ppmk);
1004 /******************************************************************************
1005 * FileMoniker_CommonPrefixWith
1006 ******************************************************************************/
1007 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
1010 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1013 ULONG nb1,nb2,i,sameIdx;
1014 BOOL machimeNameCase=FALSE;
1016 if (ppmkPrefix==NULL)
1020 return E_INVALIDARG;
1024 /* check if we have the same type of moniker */
1025 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1027 if(mkSys==MKSYS_FILEMONIKER){
1029 CreateBindCtx(0,&pbind);
1031 /* create a string based on common part of the two paths */
1033 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1034 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1036 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1037 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1039 if (nb1==0 || nb2==0)
1040 return MK_E_NOPREFIX;
1042 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1046 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1047 (stringTable2[sameIdx]!=NULL) &&
1048 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1050 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1052 machimeNameCase=TRUE;
1054 for(i=2;i<sameIdx;i++)
1056 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1057 machimeNameCase=FALSE;
1062 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1065 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1066 return MK_E_NOPREFIX;
1068 for(i=0;i<sameIdx;i++)
1069 strcatW(commonPath,stringTable1[i]);
1072 CoTaskMemFree(stringTable1[i]);
1074 CoTaskMemFree(stringTable1);
1077 CoTaskMemFree(stringTable2[i]);
1079 CoTaskMemFree(stringTable2);
1081 HeapFree(GetProcessHeap(),0,commonPath);
1083 return CreateFileMoniker(commonPath,ppmkPrefix);
1086 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1089 /******************************************************************************
1090 * DecomposePath (local function)
1091 ******************************************************************************/
1092 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1094 WCHAR bSlash[] = {'\\',0};
1095 WCHAR word[MAX_PATH];
1096 int i=0,j,tabIndex=0;
1097 LPOLESTR *strgtable ;
1099 int len=lstrlenW(str);
1101 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1103 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1105 if (strgtable==NULL)
1106 return E_OUTOFMEMORY;
1110 if(str[i]==bSlash[0]){
1112 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1114 if (strgtable[tabIndex]==NULL)
1115 return E_OUTOFMEMORY;
1117 strcpyW(strgtable[tabIndex++],bSlash);
1124 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1129 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1131 if (strgtable[tabIndex]==NULL)
1132 return E_OUTOFMEMORY;
1134 strcpyW(strgtable[tabIndex++],word);
1137 strgtable[tabIndex]=NULL;
1139 *stringTable=strgtable;
1144 /******************************************************************************
1145 * FileMoniker_RelativePathTo
1146 ******************************************************************************/
1147 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1151 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1152 DWORD len1=0,len2=0,sameIdx=0,j=0;
1153 WCHAR back[] ={'.','.','\\',0};
1155 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1157 if (ppmkRelPath==NULL)
1161 return E_INVALIDARG;
1163 res=CreateBindCtx(0,&bind);
1167 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1170 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1174 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1175 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1177 if (FAILED(len1) || FAILED(len2))
1178 return E_OUTOFMEMORY;
1180 /* count the number of similar items from the begin of the two paths */
1181 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1182 (tabStr2[sameIdx]!=NULL) &&
1183 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1185 /* begin the construction of relativePath */
1186 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1187 /* by "..\\" in the begin */
1188 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1192 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1193 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1194 if (*tabStr1[j]!='\\')
1195 strcatW(relPath,back);
1197 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1198 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1199 strcatW(relPath,tabStr2[j]);
1201 res=CreateFileMoniker(relPath,ppmkRelPath);
1203 for(j=0; tabStr1[j]!=NULL;j++)
1204 CoTaskMemFree(tabStr1[j]);
1205 for(j=0; tabStr2[j]!=NULL;j++)
1206 CoTaskMemFree(tabStr2[j]);
1207 CoTaskMemFree(tabStr1);
1208 CoTaskMemFree(tabStr2);
1209 CoTaskMemFree(str1);
1210 CoTaskMemFree(str2);
1211 HeapFree(GetProcessHeap(),0,relPath);
1213 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1219 /******************************************************************************
1220 * FileMoniker_GetDisplayName
1221 ******************************************************************************/
1222 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1224 IMoniker* pmkToLeft,
1225 LPOLESTR *ppszDisplayName)
1227 ICOM_THIS(FileMonikerImpl,iface);
1229 int len=lstrlenW(This->filePathName);
1231 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1233 if (ppszDisplayName==NULL)
1236 if (pmkToLeft!=NULL)
1237 return E_INVALIDARG;
1239 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1240 if (*ppszDisplayName==NULL)
1241 return E_OUTOFMEMORY;
1243 strcpyW(*ppszDisplayName,This->filePathName);
1248 /******************************************************************************
1249 * FileMoniker_ParseDisplayName
1250 ******************************************************************************/
1251 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1253 IMoniker* pmkToLeft,
1254 LPOLESTR pszDisplayName,
1258 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1262 /******************************************************************************
1263 * FileMoniker_IsSystemMoniker
1264 ******************************************************************************/
1265 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1267 TRACE("(%p,%p)\n",iface,pwdMksys);
1272 (*pwdMksys)=MKSYS_FILEMONIKER;
1277 /*******************************************************************************
1278 * FileMonikerIROTData_QueryInterface
1279 *******************************************************************************/
1280 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1283 ICOM_THIS_From_IROTData(IMoniker, iface);
1285 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1287 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1290 /***********************************************************************
1291 * FileMonikerIROTData_AddRef
1293 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1295 ICOM_THIS_From_IROTData(IMoniker, iface);
1297 TRACE("(%p)\n",This);
1299 return FileMonikerImpl_AddRef(This);
1302 /***********************************************************************
1303 * FileMonikerIROTData_Release
1305 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1307 ICOM_THIS_From_IROTData(IMoniker, iface);
1309 TRACE("(%p)\n",This);
1311 return FileMonikerImpl_Release(This);
1314 /******************************************************************************
1315 * FileMonikerIROTData_GetComparaisonData
1316 ******************************************************************************/
1317 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1322 FIXME("(),stub!\n");
1326 /******************************************************************************
1327 * CreateFileMoniker (OLE2.28)
1328 ******************************************************************************/
1329 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1332 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1336 /******************************************************************************
1337 * CreateFileMoniker (OLE32.55)
1338 ******************************************************************************/
1339 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1341 FileMonikerImpl* newFileMoniker = 0;
1342 HRESULT hr = E_FAIL;
1343 IID riid=IID_IMoniker;
1345 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1350 if(lpszPathName==NULL)
1355 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1357 if (newFileMoniker == 0)
1358 return E_OUTOFMEMORY;
1360 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1363 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1365 HeapFree(GetProcessHeap(),0,newFileMoniker);