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"
29 #include "wine/obj_storage.h"
30 #include "wine/obj_moniker.h"
31 #include "wine/obj_base.h"
33 #include "compobj_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37 /* filemoniker data structure */
38 typedef struct FileMonikerImpl{
40 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
42 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
43 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
45 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
47 ULONG ref; /* reference counter for this object */
49 LPOLESTR filePathName; /* path string identified by this filemoniker */
53 /********************************************************************************/
54 /* FileMoniker prototype functions : */
56 /* IUnknown prototype functions */
57 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
58 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
59 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
61 /* IPersist prototype functions */
62 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
64 /* IPersistStream prototype functions */
65 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
66 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
67 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
68 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
70 /* IMoniker prototype functions */
71 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
72 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
73 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
74 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
75 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
76 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
77 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
78 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
79 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
80 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
81 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
82 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
83 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
84 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
85 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
87 /********************************************************************************/
88 /* IROTData prototype functions */
90 /* IUnknown prototype functions */
91 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
92 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
93 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
95 /* IROTData prototype function */
96 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
98 /* Local function used by filemoniker implementation */
99 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
100 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
101 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
104 /********************************************************************************/
105 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
106 /* IPersistStream and IMoniker functions. */
107 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
109 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
110 FileMonikerImpl_QueryInterface,
111 FileMonikerImpl_AddRef,
112 FileMonikerImpl_Release,
113 FileMonikerImpl_GetClassID,
114 FileMonikerImpl_IsDirty,
115 FileMonikerImpl_Load,
116 FileMonikerImpl_Save,
117 FileMonikerImpl_GetSizeMax,
118 FileMonikerImpl_BindToObject,
119 FileMonikerImpl_BindToStorage,
120 FileMonikerImpl_Reduce,
121 FileMonikerImpl_ComposeWith,
122 FileMonikerImpl_Enum,
123 FileMonikerImpl_IsEqual,
124 FileMonikerImpl_Hash,
125 FileMonikerImpl_IsRunning,
126 FileMonikerImpl_GetTimeOfLastChange,
127 FileMonikerImpl_Inverse,
128 FileMonikerImpl_CommonPrefixWith,
129 FileMonikerImpl_RelativePathTo,
130 FileMonikerImpl_GetDisplayName,
131 FileMonikerImpl_ParseDisplayName,
132 FileMonikerImpl_IsSystemMoniker
135 /********************************************************************************/
136 /* Virtual function table for the IROTData class. */
137 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
139 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
140 FileMonikerROTDataImpl_QueryInterface,
141 FileMonikerROTDataImpl_AddRef,
142 FileMonikerROTDataImpl_Release,
143 FileMonikerROTDataImpl_GetComparaisonData
146 /*******************************************************************************
147 * FileMoniker_QueryInterface
148 *******************************************************************************/
149 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
151 ICOM_THIS(FileMonikerImpl,iface);
153 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
155 /* Perform a sanity check on the parameters.*/
156 if ( (This==0) || (ppvObject==0) )
159 /* Initialize the return parameter */
162 /* Compare the riid with the interface IDs implemented by this object.*/
163 if (IsEqualIID(&IID_IUnknown, riid) ||
164 IsEqualIID(&IID_IPersist, riid) ||
165 IsEqualIID(&IID_IPersistStream,riid) ||
166 IsEqualIID(&IID_IMoniker, riid)
170 else if (IsEqualIID(&IID_IROTData, riid))
171 *ppvObject = (IROTData*)&(This->lpvtbl2);
173 /* Check that we obtained an interface.*/
175 return E_NOINTERFACE;
177 /* Query Interface always increases the reference count by one when it is successful */
178 FileMonikerImpl_AddRef(iface);
183 /******************************************************************************
185 ******************************************************************************/
186 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
188 ICOM_THIS(FileMonikerImpl,iface);
190 TRACE("(%p)\n",iface);
192 return ++(This->ref);
195 /******************************************************************************
196 * FileMoniker_Release
197 ******************************************************************************/
198 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
200 ICOM_THIS(FileMonikerImpl,iface);
202 TRACE("(%p)\n",iface);
206 /* destroy the object if there's no more reference on it */
209 FileMonikerImpl_Destroy(This);
216 /******************************************************************************
217 * FileMoniker_GetClassID
218 ******************************************************************************/
219 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
220 CLSID *pClassID)/* Pointer to CLSID of object */
222 TRACE("(%p,%p),stub!\n",iface,pClassID);
227 *pClassID = CLSID_FileMoniker;
232 /******************************************************************************
233 * FileMoniker_IsDirty
234 ******************************************************************************/
235 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
237 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
238 method in the OLE-provided moniker interfaces always return S_FALSE because
239 their internal state never changes. */
241 TRACE("(%p)\n",iface);
246 /******************************************************************************
248 ******************************************************************************/
249 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
256 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
258 ICOM_THIS(FileMonikerImpl,iface);
260 TRACE("(%p,%p)\n",iface,pStm);
262 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
264 /* first WORD is non significative */
265 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
266 if (bread!=sizeof(WORD) || wbuffer!=0)
269 /* read filePath string length (plus one) */
270 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
271 if (bread != sizeof(DWORD))
274 /* read filePath string */
275 filePathA=HeapAlloc(GetProcessHeap(),0,length);
276 res=IStream_Read(pStm,filePathA,length,&bread);
280 /* read the first constant */
281 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
282 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
288 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
289 if (bread!=sizeof(WORD) || wbuffer!=0)
296 doubleLenHex=doubleLenDec=2*length;
300 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
301 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
307 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
308 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
311 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
312 if (bread!=sizeof(WORD) || wbuffer!=0x3)
315 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
317 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
318 if (bread!=doubleLenHex)
321 if (This->filePathName!=NULL)
322 HeapFree(GetProcessHeap(),0,This->filePathName);
324 This->filePathName=filePathW;
326 HeapFree(GetProcessHeap(),0,filePathA);
331 /******************************************************************************
333 ******************************************************************************/
334 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
335 IStream* pStm,/* pointer to the stream where the object is to be saved */
336 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
338 /* this function saves data of this object. In the beginning I thougth
339 * that I have just to write the filePath string on Stream. But, when I
340 * tested this function whith windows programs samples, I noticed that it
341 * was not the case. So I analysed data written by this function on
342 * Windows and what this did function exactly ! But I have no idea about
344 * I guessed data which must be written on stream is:
345 * 1) WORD constant:zero
346 * 2) length of the path string ("\0" included)
347 * 3) path string type A
348 * 4) DWORD constant : 0xDEADFFFF
349 * 5) ten WORD constant: zero
350 * 6) DWORD: double-length of the the path string type W ("\0" not
352 * 7) WORD constant: 0x3
353 * 8) filePath unicode string.
354 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
357 ICOM_THIS(FileMonikerImpl,iface);
360 LPOLESTR filePathW=This->filePathName;
364 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
365 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
372 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
377 /* write a DWORD set to 0 : constant */
378 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
380 /* write length of filePath string ( "\0" included )*/
381 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
382 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
384 /* write filePath string type A */
385 filePathA=HeapAlloc(GetProcessHeap(),0,len);
386 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
387 res=IStream_Write(pStm,filePathA,len,NULL);
388 HeapFree(GetProcessHeap(),0,filePathA);
390 /* write a DWORD set to 0xDEADFFFF: constant */
391 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
394 /* write 10 times a DWORD set to 0 : constants */
396 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
401 doubleLenHex=doubleLenDec=2*len;
405 /* write double-length of the path string ( "\0" included )*/
406 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
411 /* write double-length (hexa representation) of the path string ( "\0" included ) */
412 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
414 /* write a WORD set to 0x3: constant */
415 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
417 /* write path unicode string */
418 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
423 /******************************************************************************
424 * FileMoniker_GetSizeMax
425 ******************************************************************************/
426 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
427 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
429 ICOM_THIS(FileMonikerImpl,iface);
430 DWORD len=lstrlenW(This->filePathName);
433 TRACE("(%p,%p)\n",iface,pcbSize);
438 /* for more details see FileMonikerImpl_Save coments */
440 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
441 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
442 (len+1)+ /* filePath string */
443 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
444 10*sizeof(WORD)+ /* 10 zero WORD */
445 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
447 if (len==0 || len > 8)
450 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
451 sizeof(WORD)+ /* constant : 0x3 */
452 len*sizeof(WCHAR); /* unicde filePath string */
454 pcbSize->s.LowPart=sizeMAx;
455 pcbSize->s.HighPart=0;
460 /******************************************************************************
461 * FileMoniker_Construct (local function)
462 *******************************************************************************/
463 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
466 int sizeStr=lstrlenW(lpszPathName);
468 WCHAR twoPoint[]={'.','.',0};
469 WCHAR bkSlash[]={'\\',0};
472 TRACE("(%p,%p)\n",This,lpszPathName);
474 /* Initialize the virtual fgunction table. */
475 This->lpvtbl1 = &VT_FileMonikerImpl;
476 This->lpvtbl2 = &VT_ROTDataImpl;
479 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
481 if (This->filePathName==NULL)
482 return E_OUTOFMEMORY;
484 strcpyW(This->filePathName,lpszPathName);
486 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
491 if (lstrcmpW(tabStr[0],twoPoint)!=0)
496 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
502 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
510 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
513 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
515 *This->filePathName=0;
517 for(i=0;tabStr[i]!=NULL;i++)
518 strcatW(This->filePathName,tabStr[i]);
521 strcatW(This->filePathName,bkSlash);
524 for(i=0; tabStr[i]!=NULL;i++)
525 CoTaskMemFree(tabStr[i]);
526 CoTaskMemFree(tabStr);
531 /******************************************************************************
532 * FileMoniker_Destroy (local function)
533 *******************************************************************************/
534 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
536 TRACE("(%p)\n",This);
538 if (This->filePathName!=NULL)
539 HeapFree(GetProcessHeap(),0,This->filePathName);
541 HeapFree(GetProcessHeap(),0,This);
546 /******************************************************************************
547 * FileMoniker_BindToObject
548 ******************************************************************************/
549 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
558 IRunningObjectTable *prot=0;
560 IClassFactory *pcf=0;
561 IClassActivator *pca=0;
563 ICOM_THIS(FileMonikerImpl,iface);
567 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
571 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
574 /* if the requested class was loaded befor ! we dont need to reload it */
575 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
578 /* first activation of this class */
579 res=GetClassFile(This->filePathName,&clsID);
582 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
585 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
589 IUnknown_AddRef(pObj);
597 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
599 if (res==E_NOINTERFACE){
601 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
603 if (res==E_NOINTERFACE)
604 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
608 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
610 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
615 IUnknown_AddRef(pObj);
622 /*res=GetClassFile(This->filePathName,&clsID);
626 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
631 IUnknown_AddRef(pObj);
638 /* get the requested interface from the loaded class */
639 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
641 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
643 IUnknown_Release(pObj);
647 IRunningObjectTable_Release(prot);
650 IPersistFile_Release(ppf);
653 IClassActivator_Release(pca);
656 IClassFactory_Release(pcf);
661 /******************************************************************************
662 * FileMoniker_BindToStorage
663 ******************************************************************************/
664 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
674 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
676 if (pmkToLeft==NULL){
678 if (IsEqualIID(&IID_IStorage, riid)){
680 /* get the file name */
681 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
683 /* verifie if the file contains a storage object */
684 res=StgIsStorageFile(filePath);
688 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
694 IStorage_AddRef(pstg);
699 CoTaskMemFree(filePath);
702 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
707 return E_NOINTERFACE;
711 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
718 /******************************************************************************
720 ******************************************************************************/
721 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
723 DWORD dwReduceHowFar,
724 IMoniker** ppmkToLeft,
725 IMoniker** ppmkReduced)
727 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
729 if (ppmkReduced==NULL)
732 FileMonikerImpl_AddRef(iface);
736 return MK_S_REDUCED_TO_SELF;
738 /******************************************************************************
739 * FileMoniker_ComposeWith
740 ******************************************************************************/
741 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
743 BOOL fOnlyIfNotGeneric,
744 IMoniker** ppmkComposite)
747 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
748 WCHAR twoPoint[]={'.','.',0};
749 WCHAR bkSlash[]={'\\',0};
751 int i=0,j=0,lastIdx1=0,lastIdx2=0;
754 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
756 if (ppmkComposite==NULL)
764 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
766 /* check if we have two filemonikers to compose or not */
767 if(mkSys==MKSYS_FILEMONIKER){
769 CreateBindCtx(0,&bind);
771 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
772 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
774 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
775 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
776 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
778 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
781 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
784 /* for etch "..\" in the left of str2 remove the right element from str1 */
785 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
790 /* the length of the composed path string is raised by the sum of the two paths lengths */
791 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
794 return E_OUTOFMEMORY;
796 /* new path is the concatenation of the rest of str1 and str2 */
797 for(*newStr=0,j=0;j<=lastIdx1;j++)
798 strcatW(newStr,strDec1[j]);
800 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
801 strcatW(newStr,bkSlash);
803 for(j=i;j<=lastIdx2;j++)
804 strcatW(newStr,strDec2[j]);
806 /* create a new moniker with the new string */
807 res=CreateFileMoniker(newStr,ppmkComposite);
809 /* free all strings space memory used by this function */
810 HeapFree(GetProcessHeap(),0,newStr);
812 for(i=0; strDec1[i]!=NULL;i++)
813 CoTaskMemFree(strDec1[i]);
814 for(i=0; strDec2[i]!=NULL;i++)
815 CoTaskMemFree(strDec2[i]);
816 CoTaskMemFree(strDec1);
817 CoTaskMemFree(strDec2);
824 else if(mkSys==MKSYS_ANTIMONIKER){
829 else if (fOnlyIfNotGeneric){
832 return MK_E_NEEDGENERIC;
836 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
839 /******************************************************************************
841 ******************************************************************************/
842 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
844 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
846 if (ppenumMoniker == NULL)
849 *ppenumMoniker = NULL;
854 /******************************************************************************
855 * FileMoniker_IsEqual
856 ******************************************************************************/
857 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
859 ICOM_THIS(FileMonikerImpl,iface);
865 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
867 if (pmkOtherMoniker==NULL)
870 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
872 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
875 res = CreateBindCtx(0,&bind);
876 if (FAILED(res)) return res;
878 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
879 int result = lstrcmpiW(filePath, This->filePathName);
880 CoTaskMemFree(filePath);
881 if ( result == 0 ) return S_OK;
887 /******************************************************************************
889 ******************************************************************************/
890 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
892 ICOM_THIS(FileMonikerImpl,iface);
894 int h = 0,i,skip,len;
901 val = This->filePathName;
905 for (i = len ; i > 0; i--) {
906 h = (h * 37) + val[off++];
909 /* only sample some characters */
911 for (i = len ; i > 0; i -= skip, off += skip) {
912 h = (h * 39) + val[off];
921 /******************************************************************************
922 * FileMoniker_IsRunning
923 ******************************************************************************/
924 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
927 IMoniker* pmkNewlyRunning)
929 IRunningObjectTable* rot;
932 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
934 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
940 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
945 res = IRunningObjectTable_IsRunning(rot,iface);
947 IRunningObjectTable_Release(rot);
952 /******************************************************************************
953 * FileMoniker_GetTimeOfLastChange
954 ******************************************************************************/
955 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
960 ICOM_THIS(FileMonikerImpl,iface);
961 IRunningObjectTable* rot;
963 WIN32_FILE_ATTRIBUTE_DATA info;
965 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
973 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
978 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
980 if (FAILED(res)){ /* the moniker is not registred */
982 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
983 return MK_E_NOOBJECT;
985 *pFileTime=info.ftLastWriteTime;
991 /******************************************************************************
992 * FileMoniker_Inverse
993 ******************************************************************************/
994 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
997 TRACE("(%p,%p)\n",iface,ppmk);
999 return CreateAntiMoniker(ppmk);
1002 /******************************************************************************
1003 * FileMoniker_CommonPrefixWith
1004 ******************************************************************************/
1005 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
1008 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1011 ULONG nb1,nb2,i,sameIdx;
1012 BOOL machimeNameCase=FALSE;
1014 if (ppmkPrefix==NULL)
1018 return E_INVALIDARG;
1022 /* check if we have the same type of moniker */
1023 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1025 if(mkSys==MKSYS_FILEMONIKER){
1027 CreateBindCtx(0,&pbind);
1029 /* create a string based on common part of the two paths */
1031 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1032 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1034 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1035 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1037 if (nb1==0 || nb2==0)
1038 return MK_E_NOPREFIX;
1040 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1044 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1045 (stringTable2[sameIdx]!=NULL) &&
1046 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1048 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1050 machimeNameCase=TRUE;
1052 for(i=2;i<sameIdx;i++)
1054 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1055 machimeNameCase=FALSE;
1060 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1063 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1064 return MK_E_NOPREFIX;
1066 for(i=0;i<sameIdx;i++)
1067 strcatW(commonPath,stringTable1[i]);
1070 CoTaskMemFree(stringTable1[i]);
1072 CoTaskMemFree(stringTable1);
1075 CoTaskMemFree(stringTable2[i]);
1077 CoTaskMemFree(stringTable2);
1079 HeapFree(GetProcessHeap(),0,commonPath);
1081 return CreateFileMoniker(commonPath,ppmkPrefix);
1084 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1087 /******************************************************************************
1088 * DecomposePath (local function)
1089 ******************************************************************************/
1090 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1092 WCHAR bSlash[] = {'\\',0};
1093 WCHAR word[MAX_PATH];
1094 int i=0,j,tabIndex=0;
1095 LPOLESTR *strgtable ;
1097 int len=lstrlenW(str);
1099 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1101 if (strgtable==NULL)
1102 return E_OUTOFMEMORY;
1106 if(str[i]==bSlash[0]){
1108 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1110 if (strgtable[tabIndex]==NULL)
1111 return E_OUTOFMEMORY;
1113 strcpyW(strgtable[tabIndex++],bSlash);
1120 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1125 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1127 if (strgtable[tabIndex]==NULL)
1128 return E_OUTOFMEMORY;
1130 strcpyW(strgtable[tabIndex++],word);
1133 strgtable[tabIndex]=NULL;
1135 *stringTable=strgtable;
1140 /******************************************************************************
1141 * FileMoniker_RelativePathTo
1142 ******************************************************************************/
1143 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1147 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1148 DWORD len1=0,len2=0,sameIdx=0,j=0;
1149 WCHAR back[] ={'.','.','\\',0};
1151 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1153 if (ppmkRelPath==NULL)
1157 return E_INVALIDARG;
1159 res=CreateBindCtx(0,&bind);
1163 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1166 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1170 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1171 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1173 if (FAILED(len1) || FAILED(len2))
1174 return E_OUTOFMEMORY;
1176 /* count the number of similar items from the begin of the two paths */
1177 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1178 (tabStr2[sameIdx]!=NULL) &&
1179 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1181 /* begin the construction of relativePath */
1182 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1183 /* by "..\\" in the begin */
1184 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1188 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1189 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1190 if (*tabStr1[j]!='\\')
1191 strcatW(relPath,back);
1193 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1194 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1195 strcatW(relPath,tabStr2[j]);
1197 res=CreateFileMoniker(relPath,ppmkRelPath);
1199 for(j=0; tabStr1[j]!=NULL;j++)
1200 CoTaskMemFree(tabStr1[j]);
1201 for(j=0; tabStr2[j]!=NULL;j++)
1202 CoTaskMemFree(tabStr2[j]);
1203 CoTaskMemFree(tabStr1);
1204 CoTaskMemFree(tabStr2);
1205 CoTaskMemFree(str1);
1206 CoTaskMemFree(str2);
1207 HeapFree(GetProcessHeap(),0,relPath);
1209 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1215 /******************************************************************************
1216 * FileMoniker_GetDisplayName
1217 ******************************************************************************/
1218 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1220 IMoniker* pmkToLeft,
1221 LPOLESTR *ppszDisplayName)
1223 ICOM_THIS(FileMonikerImpl,iface);
1225 int len=lstrlenW(This->filePathName);
1227 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1229 if (ppszDisplayName==NULL)
1232 if (pmkToLeft!=NULL)
1233 return E_INVALIDARG;
1235 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1236 if (*ppszDisplayName==NULL)
1237 return E_OUTOFMEMORY;
1239 strcpyW(*ppszDisplayName,This->filePathName);
1244 /******************************************************************************
1245 * FileMoniker_ParseDisplayName
1246 ******************************************************************************/
1247 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1249 IMoniker* pmkToLeft,
1250 LPOLESTR pszDisplayName,
1254 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1258 /******************************************************************************
1259 * FileMoniker_IsSystemMoniker
1260 ******************************************************************************/
1261 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1263 TRACE("(%p,%p)\n",iface,pwdMksys);
1268 (*pwdMksys)=MKSYS_FILEMONIKER;
1273 /*******************************************************************************
1274 * FileMonikerIROTData_QueryInterface
1275 *******************************************************************************/
1276 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1279 ICOM_THIS_From_IROTData(IMoniker, iface);
1281 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1283 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1286 /***********************************************************************
1287 * FileMonikerIROTData_AddRef
1289 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1291 ICOM_THIS_From_IROTData(IMoniker, iface);
1293 TRACE("(%p)\n",This);
1295 return FileMonikerImpl_AddRef(This);
1298 /***********************************************************************
1299 * FileMonikerIROTData_Release
1301 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1303 ICOM_THIS_From_IROTData(IMoniker, iface);
1305 TRACE("(%p)\n",This);
1307 return FileMonikerImpl_Release(This);
1310 /******************************************************************************
1311 * FileMonikerIROTData_GetComparaisonData
1312 ******************************************************************************/
1313 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1318 FIXME("(),stub!\n");
1322 /******************************************************************************
1323 * CreateFileMoniker (OLE2.28)
1324 ******************************************************************************/
1325 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1328 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1332 /******************************************************************************
1333 * CreateFileMoniker (OLE32.55)
1334 ******************************************************************************/
1335 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1337 FileMonikerImpl* newFileMoniker = 0;
1338 HRESULT hr = E_FAIL;
1339 IID riid=IID_IMoniker;
1341 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1346 if(lpszPathName==NULL)
1351 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1353 if (newFileMoniker == 0)
1354 return E_OUTOFMEMORY;
1356 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1359 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1361 HeapFree(GetProcessHeap(),0,newFileMoniker);