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"
30 #include "compobj_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ole);
34 /* filemoniker data structure */
35 typedef struct FileMonikerImpl{
37 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
39 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
40 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
42 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
44 ULONG ref; /* reference counter for this object */
46 LPOLESTR filePathName; /* path string identified by this filemoniker */
50 /********************************************************************************/
51 /* FileMoniker prototype functions : */
53 /* IUnknown prototype functions */
54 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
55 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
56 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
58 /* IPersist prototype functions */
59 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
61 /* IPersistStream prototype functions */
62 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
63 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
64 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
65 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
67 /* IMoniker prototype functions */
68 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
69 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
70 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
71 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
72 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
73 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
74 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
75 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
76 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
77 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
78 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
79 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
80 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
81 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
82 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
84 /********************************************************************************/
85 /* IROTData prototype functions */
87 /* IUnknown prototype functions */
88 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
89 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
90 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
92 /* IROTData prototype function */
93 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
95 /* Local function used by filemoniker implementation */
96 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
97 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
98 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** tabStr);
101 /********************************************************************************/
102 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
103 /* IPersistStream and IMoniker functions. */
104 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
106 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
107 FileMonikerImpl_QueryInterface,
108 FileMonikerImpl_AddRef,
109 FileMonikerImpl_Release,
110 FileMonikerImpl_GetClassID,
111 FileMonikerImpl_IsDirty,
112 FileMonikerImpl_Load,
113 FileMonikerImpl_Save,
114 FileMonikerImpl_GetSizeMax,
115 FileMonikerImpl_BindToObject,
116 FileMonikerImpl_BindToStorage,
117 FileMonikerImpl_Reduce,
118 FileMonikerImpl_ComposeWith,
119 FileMonikerImpl_Enum,
120 FileMonikerImpl_IsEqual,
121 FileMonikerImpl_Hash,
122 FileMonikerImpl_IsRunning,
123 FileMonikerImpl_GetTimeOfLastChange,
124 FileMonikerImpl_Inverse,
125 FileMonikerImpl_CommonPrefixWith,
126 FileMonikerImpl_RelativePathTo,
127 FileMonikerImpl_GetDisplayName,
128 FileMonikerImpl_ParseDisplayName,
129 FileMonikerImpl_IsSystemMoniker
132 /********************************************************************************/
133 /* Virtual function table for the IROTData class. */
134 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
136 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
137 FileMonikerROTDataImpl_QueryInterface,
138 FileMonikerROTDataImpl_AddRef,
139 FileMonikerROTDataImpl_Release,
140 FileMonikerROTDataImpl_GetComparaisonData
143 /*******************************************************************************
144 * FileMoniker_QueryInterface
145 *******************************************************************************/
146 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
148 ICOM_THIS(FileMonikerImpl,iface);
150 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
152 /* Perform a sanity check on the parameters.*/
153 if ( (This==0) || (ppvObject==0) )
156 /* Initialize the return parameter */
159 /* Compare the riid with the interface IDs implemented by this object.*/
160 if (IsEqualIID(&IID_IUnknown, riid) ||
161 IsEqualIID(&IID_IPersist, riid) ||
162 IsEqualIID(&IID_IPersistStream,riid) ||
163 IsEqualIID(&IID_IMoniker, riid)
167 else if (IsEqualIID(&IID_IROTData, riid))
168 *ppvObject = (IROTData*)&(This->lpvtbl2);
170 /* Check that we obtained an interface.*/
172 return E_NOINTERFACE;
174 /* Query Interface always increases the reference count by one when it is successful */
175 FileMonikerImpl_AddRef(iface);
180 /******************************************************************************
182 ******************************************************************************/
183 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
185 ICOM_THIS(FileMonikerImpl,iface);
187 TRACE("(%p)\n",iface);
189 return ++(This->ref);
192 /******************************************************************************
193 * FileMoniker_Release
194 ******************************************************************************/
195 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
197 ICOM_THIS(FileMonikerImpl,iface);
199 TRACE("(%p)\n",iface);
203 /* destroy the object if there's no more reference on it */
206 FileMonikerImpl_Destroy(This);
213 /******************************************************************************
214 * FileMoniker_GetClassID
215 ******************************************************************************/
216 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
217 CLSID *pClassID)/* Pointer to CLSID of object */
219 TRACE("(%p,%p),stub!\n",iface,pClassID);
224 *pClassID = CLSID_FileMoniker;
229 /******************************************************************************
230 * FileMoniker_IsDirty
231 ******************************************************************************/
232 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
234 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
235 method in the OLE-provided moniker interfaces always return S_FALSE because
236 their internal state never changes. */
238 TRACE("(%p)\n",iface);
243 /******************************************************************************
245 ******************************************************************************/
246 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
253 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
255 ICOM_THIS(FileMonikerImpl,iface);
257 TRACE("(%p,%p)\n",iface,pStm);
259 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
261 /* first WORD is non significative */
262 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
263 if (bread!=sizeof(WORD) || wbuffer!=0)
266 /* read filePath string length (plus one) */
267 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
268 if (bread != sizeof(DWORD))
271 /* read filePath string */
272 filePathA=HeapAlloc(GetProcessHeap(),0,length);
273 res=IStream_Read(pStm,filePathA,length,&bread);
277 /* read the first constant */
278 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
279 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
285 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
286 if (bread!=sizeof(WORD) || wbuffer!=0)
293 doubleLenHex=doubleLenDec=2*length;
297 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
298 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
304 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
305 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
308 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
309 if (bread!=sizeof(WORD) || wbuffer!=0x3)
312 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
314 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
315 if (bread!=doubleLenHex)
318 if (This->filePathName!=NULL)
319 HeapFree(GetProcessHeap(),0,This->filePathName);
321 This->filePathName=filePathW;
323 HeapFree(GetProcessHeap(),0,filePathA);
328 /******************************************************************************
330 ******************************************************************************/
331 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
332 IStream* pStm,/* pointer to the stream where the object is to be saved */
333 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
335 /* this function saves data of this object. In the beginning I thougth
336 * that I have just to write the filePath string on Stream. But, when I
337 * tested this function whith windows programs samples, I noticed that it
338 * was not the case. So I analysed data written by this function on
339 * Windows and what this did function exactly ! But I have no idea about
341 * I guessed data which must be written on stream is:
342 * 1) WORD constant:zero
343 * 2) length of the path string ("\0" included)
344 * 3) path string type A
345 * 4) DWORD constant : 0xDEADFFFF
346 * 5) ten WORD constant: zero
347 * 6) DWORD: double-length of the the path string type W ("\0" not
349 * 7) WORD constant: 0x3
350 * 8) filePath unicode string.
351 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
354 ICOM_THIS(FileMonikerImpl,iface);
357 LPOLESTR filePathW=This->filePathName;
361 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
362 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
369 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
374 /* write a DWORD set to 0 : constant */
375 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
377 /* write length of filePath string ( "\0" included )*/
378 len = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
379 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
381 /* write filePath string type A */
382 filePathA=HeapAlloc(GetProcessHeap(),0,len);
383 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, len, NULL, NULL );
384 res=IStream_Write(pStm,filePathA,len,NULL);
385 HeapFree(GetProcessHeap(),0,filePathA);
387 /* write a DWORD set to 0xDEADFFFF: constant */
388 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
391 /* write 10 times a DWORD set to 0 : constants */
393 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
398 doubleLenHex=doubleLenDec=2*len;
402 /* write double-length of the path string ( "\0" included )*/
403 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
408 /* write double-length (hexa representation) of the path string ( "\0" included ) */
409 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
411 /* write a WORD set to 0x3: constant */
412 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
414 /* write path unicode string */
415 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
420 /******************************************************************************
421 * FileMoniker_GetSizeMax
422 ******************************************************************************/
423 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
424 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
426 ICOM_THIS(FileMonikerImpl,iface);
427 DWORD len=lstrlenW(This->filePathName);
430 TRACE("(%p,%p)\n",iface,pcbSize);
435 /* for more details see FileMonikerImpl_Save coments */
437 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
438 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
439 (len+1)+ /* filePath string */
440 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
441 10*sizeof(WORD)+ /* 10 zero WORD */
442 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
444 if (len==0 || len > 8)
447 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
448 sizeof(WORD)+ /* constant : 0x3 */
449 len*sizeof(WCHAR); /* unicde filePath string */
451 pcbSize->s.LowPart=sizeMAx;
452 pcbSize->s.HighPart=0;
457 /******************************************************************************
458 * FileMoniker_Construct (local function)
459 *******************************************************************************/
460 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
463 int sizeStr=lstrlenW(lpszPathName);
465 WCHAR twoPoint[]={'.','.',0};
466 WCHAR bkSlash[]={'\\',0};
469 TRACE("(%p,%p)\n",This,lpszPathName);
471 /* Initialize the virtual fgunction table. */
472 This->lpvtbl1 = &VT_FileMonikerImpl;
473 This->lpvtbl2 = &VT_ROTDataImpl;
476 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
478 if (This->filePathName==NULL)
479 return E_OUTOFMEMORY;
481 strcpyW(This->filePathName,lpszPathName);
483 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
488 if (lstrcmpW(tabStr[0],twoPoint)!=0)
493 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
499 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
507 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
510 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
512 *This->filePathName=0;
514 for(i=0;tabStr[i]!=NULL;i++)
515 strcatW(This->filePathName,tabStr[i]);
518 strcatW(This->filePathName,bkSlash);
521 for(i=0; tabStr[i]!=NULL;i++)
522 CoTaskMemFree(tabStr[i]);
523 CoTaskMemFree(tabStr);
528 /******************************************************************************
529 * FileMoniker_Destroy (local function)
530 *******************************************************************************/
531 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
533 TRACE("(%p)\n",This);
535 if (This->filePathName!=NULL)
536 HeapFree(GetProcessHeap(),0,This->filePathName);
538 HeapFree(GetProcessHeap(),0,This);
543 /******************************************************************************
544 * FileMoniker_BindToObject
545 ******************************************************************************/
546 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
555 IRunningObjectTable *prot=0;
557 IClassFactory *pcf=0;
558 IClassActivator *pca=0;
560 ICOM_THIS(FileMonikerImpl,iface);
564 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
568 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
571 /* if the requested class was loaded befor ! we dont need to reload it */
572 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
575 /* first activation of this class */
576 res=GetClassFile(This->filePathName,&clsID);
579 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
582 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
586 IUnknown_AddRef(pObj);
594 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
596 if (res==E_NOINTERFACE){
598 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
600 if (res==E_NOINTERFACE)
601 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
605 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
607 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
612 IUnknown_AddRef(pObj);
619 /*res=GetClassFile(This->filePathName,&clsID);
623 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
628 IUnknown_AddRef(pObj);
635 /* get the requested interface from the loaded class */
636 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
638 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
640 IUnknown_Release(pObj);
644 IRunningObjectTable_Release(prot);
647 IPersistFile_Release(ppf);
650 IClassActivator_Release(pca);
653 IClassFactory_Release(pcf);
658 /******************************************************************************
659 * FileMoniker_BindToStorage
660 ******************************************************************************/
661 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
671 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
673 if (pmkToLeft==NULL){
675 if (IsEqualIID(&IID_IStorage, riid)){
677 /* get the file name */
678 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
680 /* verifie if the file contains a storage object */
681 res=StgIsStorageFile(filePath);
685 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
691 IStorage_AddRef(pstg);
696 CoTaskMemFree(filePath);
699 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
704 return E_NOINTERFACE;
708 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
715 /******************************************************************************
717 ******************************************************************************/
718 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
720 DWORD dwReduceHowFar,
721 IMoniker** ppmkToLeft,
722 IMoniker** ppmkReduced)
724 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
726 if (ppmkReduced==NULL)
729 FileMonikerImpl_AddRef(iface);
733 return MK_S_REDUCED_TO_SELF;
735 /******************************************************************************
736 * FileMoniker_ComposeWith
737 ******************************************************************************/
738 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
740 BOOL fOnlyIfNotGeneric,
741 IMoniker** ppmkComposite)
744 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
745 WCHAR twoPoint[]={'.','.',0};
746 WCHAR bkSlash[]={'\\',0};
748 int i=0,j=0,lastIdx1=0,lastIdx2=0;
751 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
753 if (ppmkComposite==NULL)
761 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
763 /* check if we have two filemonikers to compose or not */
764 if(mkSys==MKSYS_FILEMONIKER){
766 CreateBindCtx(0,&bind);
768 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
769 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
771 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
772 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
773 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
775 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
778 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
781 /* for etch "..\" in the left of str2 remove the right element from str1 */
782 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
787 /* the length of the composed path string is raised by the sum of the two paths lengths */
788 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
791 return E_OUTOFMEMORY;
793 /* new path is the concatenation of the rest of str1 and str2 */
794 for(*newStr=0,j=0;j<=lastIdx1;j++)
795 strcatW(newStr,strDec1[j]);
797 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
798 strcatW(newStr,bkSlash);
800 for(j=i;j<=lastIdx2;j++)
801 strcatW(newStr,strDec2[j]);
803 /* create a new moniker with the new string */
804 res=CreateFileMoniker(newStr,ppmkComposite);
806 /* free all strings space memory used by this function */
807 HeapFree(GetProcessHeap(),0,newStr);
809 for(i=0; strDec1[i]!=NULL;i++)
810 CoTaskMemFree(strDec1[i]);
811 for(i=0; strDec2[i]!=NULL;i++)
812 CoTaskMemFree(strDec2[i]);
813 CoTaskMemFree(strDec1);
814 CoTaskMemFree(strDec2);
821 else if(mkSys==MKSYS_ANTIMONIKER){
826 else if (fOnlyIfNotGeneric){
829 return MK_E_NEEDGENERIC;
833 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
836 /******************************************************************************
838 ******************************************************************************/
839 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
841 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
843 if (ppenumMoniker == NULL)
846 *ppenumMoniker = NULL;
851 /******************************************************************************
852 * FileMoniker_IsEqual
853 ******************************************************************************/
854 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
856 ICOM_THIS(FileMonikerImpl,iface);
862 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
864 if (pmkOtherMoniker==NULL)
867 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
869 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
872 res = CreateBindCtx(0,&bind);
873 if (FAILED(res)) return res;
875 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
876 int result = lstrcmpiW(filePath, This->filePathName);
877 CoTaskMemFree(filePath);
878 if ( result == 0 ) return S_OK;
884 /******************************************************************************
886 ******************************************************************************/
887 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
889 ICOM_THIS(FileMonikerImpl,iface);
891 int h = 0,i,skip,len;
898 val = This->filePathName;
902 for (i = len ; i > 0; i--) {
903 h = (h * 37) + val[off++];
906 /* only sample some characters */
908 for (i = len ; i > 0; i -= skip, off += skip) {
909 h = (h * 39) + val[off];
918 /******************************************************************************
919 * FileMoniker_IsRunning
920 ******************************************************************************/
921 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
924 IMoniker* pmkNewlyRunning)
926 IRunningObjectTable* rot;
929 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
931 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
937 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
942 res = IRunningObjectTable_IsRunning(rot,iface);
944 IRunningObjectTable_Release(rot);
949 /******************************************************************************
950 * FileMoniker_GetTimeOfLastChange
951 ******************************************************************************/
952 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
957 ICOM_THIS(FileMonikerImpl,iface);
958 IRunningObjectTable* rot;
960 WIN32_FILE_ATTRIBUTE_DATA info;
962 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
970 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
975 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
977 if (FAILED(res)){ /* the moniker is not registred */
979 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
980 return MK_E_NOOBJECT;
982 *pFileTime=info.ftLastWriteTime;
988 /******************************************************************************
989 * FileMoniker_Inverse
990 ******************************************************************************/
991 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
994 TRACE("(%p,%p)\n",iface,ppmk);
996 return CreateAntiMoniker(ppmk);
999 /******************************************************************************
1000 * FileMoniker_CommonPrefixWith
1001 ******************************************************************************/
1002 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
1005 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
1008 ULONG nb1,nb2,i,sameIdx;
1009 BOOL machimeNameCase=FALSE;
1011 if (ppmkPrefix==NULL)
1015 return E_INVALIDARG;
1019 /* check if we have the same type of moniker */
1020 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1022 if(mkSys==MKSYS_FILEMONIKER){
1024 CreateBindCtx(0,&pbind);
1026 /* create a string based on common part of the two paths */
1028 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1029 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1031 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1032 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1034 if (nb1==0 || nb2==0)
1035 return MK_E_NOPREFIX;
1037 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1041 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1042 (stringTable2[sameIdx]!=NULL) &&
1043 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1045 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1047 machimeNameCase=TRUE;
1049 for(i=2;i<sameIdx;i++)
1051 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1052 machimeNameCase=FALSE;
1057 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1060 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1061 return MK_E_NOPREFIX;
1063 for(i=0;i<sameIdx;i++)
1064 strcatW(commonPath,stringTable1[i]);
1067 CoTaskMemFree(stringTable1[i]);
1069 CoTaskMemFree(stringTable1);
1072 CoTaskMemFree(stringTable2[i]);
1074 CoTaskMemFree(stringTable2);
1076 HeapFree(GetProcessHeap(),0,commonPath);
1078 return CreateFileMoniker(commonPath,ppmkPrefix);
1081 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1084 /******************************************************************************
1085 * DecomposePath (local function)
1086 ******************************************************************************/
1087 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1089 WCHAR bSlash[] = {'\\',0};
1090 WCHAR word[MAX_PATH];
1091 int i=0,j,tabIndex=0;
1092 LPOLESTR *strgtable ;
1094 int len=lstrlenW(str);
1096 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1098 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1100 if (strgtable==NULL)
1101 return E_OUTOFMEMORY;
1105 if(str[i]==bSlash[0]){
1107 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1109 if (strgtable[tabIndex]==NULL)
1110 return E_OUTOFMEMORY;
1112 strcpyW(strgtable[tabIndex++],bSlash);
1119 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1124 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1126 if (strgtable[tabIndex]==NULL)
1127 return E_OUTOFMEMORY;
1129 strcpyW(strgtable[tabIndex++],word);
1132 strgtable[tabIndex]=NULL;
1134 *stringTable=strgtable;
1139 /******************************************************************************
1140 * FileMoniker_RelativePathTo
1141 ******************************************************************************/
1142 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1146 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1147 DWORD len1=0,len2=0,sameIdx=0,j=0;
1148 WCHAR back[] ={'.','.','\\',0};
1150 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1152 if (ppmkRelPath==NULL)
1156 return E_INVALIDARG;
1158 res=CreateBindCtx(0,&bind);
1162 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1165 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1169 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1170 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1172 if (FAILED(len1) || FAILED(len2))
1173 return E_OUTOFMEMORY;
1175 /* count the number of similar items from the begin of the two paths */
1176 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1177 (tabStr2[sameIdx]!=NULL) &&
1178 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1180 /* begin the construction of relativePath */
1181 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1182 /* by "..\\" in the begin */
1183 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1187 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1188 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1189 if (*tabStr1[j]!='\\')
1190 strcatW(relPath,back);
1192 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1193 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1194 strcatW(relPath,tabStr2[j]);
1196 res=CreateFileMoniker(relPath,ppmkRelPath);
1198 for(j=0; tabStr1[j]!=NULL;j++)
1199 CoTaskMemFree(tabStr1[j]);
1200 for(j=0; tabStr2[j]!=NULL;j++)
1201 CoTaskMemFree(tabStr2[j]);
1202 CoTaskMemFree(tabStr1);
1203 CoTaskMemFree(tabStr2);
1204 CoTaskMemFree(str1);
1205 CoTaskMemFree(str2);
1206 HeapFree(GetProcessHeap(),0,relPath);
1208 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1214 /******************************************************************************
1215 * FileMoniker_GetDisplayName
1216 ******************************************************************************/
1217 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1219 IMoniker* pmkToLeft,
1220 LPOLESTR *ppszDisplayName)
1222 ICOM_THIS(FileMonikerImpl,iface);
1224 int len=lstrlenW(This->filePathName);
1226 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1228 if (ppszDisplayName==NULL)
1231 if (pmkToLeft!=NULL)
1232 return E_INVALIDARG;
1234 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1235 if (*ppszDisplayName==NULL)
1236 return E_OUTOFMEMORY;
1238 strcpyW(*ppszDisplayName,This->filePathName);
1243 /******************************************************************************
1244 * FileMoniker_ParseDisplayName
1245 ******************************************************************************/
1246 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1248 IMoniker* pmkToLeft,
1249 LPOLESTR pszDisplayName,
1253 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1257 /******************************************************************************
1258 * FileMoniker_IsSystemMoniker
1259 ******************************************************************************/
1260 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1262 TRACE("(%p,%p)\n",iface,pwdMksys);
1267 (*pwdMksys)=MKSYS_FILEMONIKER;
1272 /*******************************************************************************
1273 * FileMonikerIROTData_QueryInterface
1274 *******************************************************************************/
1275 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1278 ICOM_THIS_From_IROTData(IMoniker, iface);
1280 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1282 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1285 /***********************************************************************
1286 * FileMonikerIROTData_AddRef
1288 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1290 ICOM_THIS_From_IROTData(IMoniker, iface);
1292 TRACE("(%p)\n",This);
1294 return FileMonikerImpl_AddRef(This);
1297 /***********************************************************************
1298 * FileMonikerIROTData_Release
1300 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1302 ICOM_THIS_From_IROTData(IMoniker, iface);
1304 TRACE("(%p)\n",This);
1306 return FileMonikerImpl_Release(This);
1309 /******************************************************************************
1310 * FileMonikerIROTData_GetComparaisonData
1311 ******************************************************************************/
1312 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1317 FIXME("(),stub!\n");
1321 /******************************************************************************
1322 * CreateFileMoniker (OLE2.28)
1323 ******************************************************************************/
1324 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1327 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1331 /******************************************************************************
1332 * CreateFileMoniker (OLE32.55)
1333 ******************************************************************************/
1334 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1336 FileMonikerImpl* newFileMoniker = 0;
1337 HRESULT hr = E_FAIL;
1338 IID riid=IID_IMoniker;
1340 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1345 if(lpszPathName==NULL)
1350 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1352 if (newFileMoniker == 0)
1353 return E_OUTOFMEMORY;
1355 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1358 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1360 HeapFree(GetProcessHeap(),0,newFileMoniker);