2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 * Copyright 2007 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
39 #include "compobj_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* filemoniker data structure */
44 typedef struct FileMonikerImpl{
46 const IMonikerVtbl* lpvtbl1; /* VTable relative to the IMoniker interface.*/
48 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
49 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
51 const IROTDataVtbl* lpvtbl2; /* VTable relative to the IROTData interface.*/
53 LONG ref; /* reference counter for this object */
55 LPOLESTR filePathName; /* path string identified by this filemoniker */
57 IUnknown *pMarshal; /* custom marshaler */
60 static inline IMoniker *impl_from_IROTData( IROTData *iface )
62 return (IMoniker *)((char*)iface - FIELD_OFFSET(FileMonikerImpl, lpvtbl2));
65 /* Local function used by filemoniker implementation */
66 static HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
67 static HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
69 /*******************************************************************************
70 * FileMoniker_QueryInterface
73 FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
75 FileMonikerImpl *This = (FileMonikerImpl *)iface;
77 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
79 /* Perform a sanity check on the parameters.*/
80 if ( (This==0) || (ppvObject==0) )
83 /* Initialize the return parameter */
86 /* Compare the riid with the interface IDs implemented by this object.*/
87 if (IsEqualIID(&IID_IUnknown, riid) ||
88 IsEqualIID(&IID_IPersist, riid) ||
89 IsEqualIID(&IID_IPersistStream,riid) ||
90 IsEqualIID(&IID_IMoniker, riid)
94 else if (IsEqualIID(&IID_IROTData, riid))
95 *ppvObject = (IROTData*)&(This->lpvtbl2);
96 else if (IsEqualIID(&IID_IMarshal, riid))
100 hr = MonikerMarshal_Create(iface, &This->pMarshal);
103 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
106 /* Check that we obtained an interface.*/
108 return E_NOINTERFACE;
110 /* Query Interface always increases the reference count by one when it is successful */
111 IMoniker_AddRef(iface);
116 /******************************************************************************
120 FileMonikerImpl_AddRef(IMoniker* iface)
122 FileMonikerImpl *This = (FileMonikerImpl *)iface;
124 TRACE("(%p)\n",iface);
126 return InterlockedIncrement(&This->ref);
129 /******************************************************************************
130 * FileMoniker_Release
133 FileMonikerImpl_Release(IMoniker* iface)
135 FileMonikerImpl *This = (FileMonikerImpl *)iface;
138 TRACE("(%p)\n",iface);
140 ref = InterlockedDecrement(&This->ref);
142 /* destroy the object if there's no more reference on it */
143 if (ref == 0) FileMonikerImpl_Destroy(This);
148 /******************************************************************************
149 * FileMoniker_GetClassID
151 static HRESULT WINAPI
152 FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID)
154 TRACE("(%p,%p)\n",iface,pClassID);
159 *pClassID = CLSID_FileMoniker;
164 /******************************************************************************
165 * FileMoniker_IsDirty
167 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
168 * method in the OLE-provided moniker interfaces always return S_FALSE because
169 * their internal state never changes.
171 static HRESULT WINAPI
172 FileMonikerImpl_IsDirty(IMoniker* iface)
175 TRACE("(%p)\n",iface);
180 /******************************************************************************
183 * this function locates and reads from the stream the filePath string
184 * written by FileMonikerImpl_Save
186 static HRESULT WINAPI
187 FileMonikerImpl_Load(IMoniker* iface, IStream* pStm)
190 CHAR* filePathA = NULL;
191 WCHAR* filePathW = NULL;
194 DWORD dwbuffer, bytesA, bytesW, len;
197 FileMonikerImpl *This = (FileMonikerImpl *)iface;
199 TRACE("(%p,%p)\n",iface,pStm);
201 /* first WORD must be 0 */
202 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
203 if (bread!=sizeof(WORD) || wbuffer!=0)
205 WARN("Couldn't read 0 word\n");
209 /* read filePath string length (plus one) */
210 res=IStream_Read(pStm,&bytesA,sizeof(DWORD),&bread);
211 if (bread != sizeof(DWORD))
213 WARN("Couldn't read file string length\n");
217 /* read filePath string */
218 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
225 res=IStream_Read(pStm,filePathA,bytesA,&bread);
228 WARN("Couldn't read file path string\n");
232 /* read the first constant */
233 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
234 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
236 WARN("Couldn't read 0xDEADFFFF constant\n");
242 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
243 if (bread!=sizeof(DWORD) || dwbuffer!=0)
245 WARN("Couldn't read 0 padding\n");
250 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
251 if (bread!=sizeof(DWORD))
254 if (!dwbuffer) /* No W-string */
257 len=MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, bytesA, NULL, 0);
261 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
267 MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, -1, filePathW, len+1);
276 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
277 if (bread!=sizeof(DWORD) || dwbuffer!=bytesW)
280 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
281 if (bread!=sizeof(WORD) || wbuffer!=0x3)
284 len=bytesW/sizeof(WCHAR);
285 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
291 res=IStream_Read(pStm,filePathW,bytesW,&bread);
298 HeapFree(GetProcessHeap(),0,filePathA);
299 HeapFree(GetProcessHeap(),0,This->filePathName);
300 This->filePathName=filePathW;
305 HeapFree(GetProcessHeap(), 0, filePathA);
306 HeapFree(GetProcessHeap(), 0, filePathW);
313 /******************************************************************************
316 * This function saves data of this object. In the beginning I thought
317 * that I have just to write the filePath string on Stream. But, when I
318 * tested this function with windows program samples, I noticed that it
319 * was not the case. This implementation is based on XP SP2. Other versions
320 * of Windows have minor variations.
322 * Data which must be written on stream is:
323 * 1) WORD constant:zero
324 * 2) length of the path string ("\0" included)
325 * 3) path string type A
326 * 4) DWORD constant : 0xDEADFFFF
327 * 5) five DWORD constant: zero
328 * 6) If we're only writing the multibyte version,
329 * write a zero DWORD and finish.
331 * 7) DWORD: double-length of the path string type W ("\0" not
333 * 8) WORD constant: 0x3
334 * 9) filePath unicode string.
337 static HRESULT WINAPI
338 FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
340 FileMonikerImpl *This = (FileMonikerImpl *)iface;
343 LPOLESTR filePathW=This->filePathName;
345 DWORD bytesA, bytesW, len;
347 static const DWORD DEADFFFF = 0xDEADFFFF; /* Constants */
348 static const DWORD ZERO = 0;
349 static const WORD THREE = 0x3;
352 BOOL bUsedDefault, bWriteWide;
354 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
360 res=IStream_Write(pStm,&ZERO,sizeof(WORD),NULL);
361 if (!SUCCEEDED(res)) return res;
363 /* write length of filePath string ( 0 included )*/
364 bytesA = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
365 res=IStream_Write(pStm,&bytesA,sizeof(DWORD),NULL);
366 if (!SUCCEEDED(res)) return res;
368 /* write A string (with '\0') */
369 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
371 return E_OUTOFMEMORY;
372 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, bytesA, NULL, &bUsedDefault);
373 res=IStream_Write(pStm,filePathA,bytesA,NULL);
374 HeapFree(GetProcessHeap(),0,filePathA);
375 if (!SUCCEEDED(res)) return res;
377 /* write a DWORD 0xDEADFFFF */
378 res=IStream_Write(pStm,&DEADFFFF,sizeof(DWORD),NULL);
379 if (!SUCCEEDED(res)) return res;
381 /* write 5 zero DWORDs */
384 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
385 if (!SUCCEEDED(res)) return res;
388 /* Write the wide version if:
389 * + couldn't convert to CP_ACP,
390 * or + it's a directory,
391 * or + there's a character > 0xFF
393 len = lstrlenW(filePathW);
394 bWriteWide = (bUsedDefault || (len > 0 && filePathW[len-1]=='\\' ));
398 for(pch=filePathW;*pch;++pch)
410 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
414 /* write bytes needed for the filepathW (without 0) + 6 */
415 bytesW = len*sizeof(WCHAR) + 6;
416 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
417 if (!SUCCEEDED(res)) return res;
419 /* try again, without the extra 6 */
421 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
422 if (!SUCCEEDED(res)) return res;
425 res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
426 if (!SUCCEEDED(res)) return res;
428 /* write W string (no 0) */
429 res=IStream_Write(pStm,filePathW,bytesW,NULL);
434 /******************************************************************************
435 * FileMoniker_GetSizeMax
437 static HRESULT WINAPI
438 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
440 FileMonikerImpl *This = (FileMonikerImpl *)iface;
442 TRACE("(%p,%p)\n",iface,pcbSize);
447 /* We could calculate exactly (see ...::Save()) but instead
448 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
450 pcbSize->u.LowPart = 0x38 + 4 * lstrlenW(This->filePathName);
451 pcbSize->u.HighPart = 0;
456 /******************************************************************************
457 * FileMoniker_Destroy (local function)
458 *******************************************************************************/
459 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
461 TRACE("(%p)\n",This);
463 if (This->pMarshal) IUnknown_Release(This->pMarshal);
464 HeapFree(GetProcessHeap(),0,This->filePathName);
465 HeapFree(GetProcessHeap(),0,This);
470 /******************************************************************************
471 * FileMoniker_BindToObject
473 static HRESULT WINAPI
474 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
475 REFIID riid, VOID** ppvResult)
480 IRunningObjectTable *prot=0;
482 IClassFactory *pcf=0;
483 IClassActivator *pca=0;
485 FileMonikerImpl *This = (FileMonikerImpl *)iface;
489 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
493 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
496 /* if the requested class was loaded before ! we don't need to reload it */
497 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
500 /* first activation of this class */
501 res=GetClassFile(This->filePathName,&clsID);
504 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
507 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
511 IUnknown_AddRef(pObj);
519 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
521 if (res==E_NOINTERFACE){
523 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
525 if (res==E_NOINTERFACE)
526 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
530 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)&ppf);
532 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
537 IUnknown_AddRef(pObj);
544 /*res=GetClassFile(This->filePathName,&clsID);
548 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
553 IUnknown_AddRef(pObj);
560 /* get the requested interface from the loaded class */
561 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
563 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
565 IUnknown_Release(pObj);
569 IRunningObjectTable_Release(prot);
572 IPersistFile_Release(ppf);
575 IClassActivator_Release(pca);
578 IClassFactory_Release(pcf);
583 /******************************************************************************
584 * FileMoniker_BindToStorage
586 static HRESULT WINAPI
587 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
588 REFIID riid, VOID** ppvObject)
594 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
596 if (pmkToLeft==NULL){
598 if (IsEqualIID(&IID_IStorage, riid)){
600 /* get the file name */
601 IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
603 /* verifie if the file contains a storage object */
604 res=StgIsStorageFile(filePath);
608 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
614 IStorage_AddRef(pstg);
619 CoTaskMemFree(filePath);
622 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
625 return E_NOINTERFACE;
629 FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
636 /******************************************************************************
638 ******************************************************************************/
639 static HRESULT WINAPI
640 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
641 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
643 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
645 if (ppmkReduced==NULL)
648 IMoniker_AddRef(iface);
652 return MK_S_REDUCED_TO_SELF;
655 /******************************************************************************
656 * FileMoniker_ComposeWith
658 static HRESULT WINAPI
659 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
660 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
663 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
664 static const WCHAR twoPoint[]={'.','.',0};
665 static const WCHAR bkSlash[]={'\\',0};
667 int i=0,j=0,lastIdx1=0,lastIdx2=0;
670 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
672 if (ppmkComposite==NULL)
680 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
682 /* check if we have two filemonikers to compose or not */
683 if(mkSys==MKSYS_FILEMONIKER){
685 CreateBindCtx(0,&bind);
687 IMoniker_GetDisplayName(iface,bind,NULL,&str1);
688 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
690 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
691 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
692 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
694 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
697 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
700 /* for etch "..\" in the left of str2 remove the right element from str1 */
701 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
706 /* the length of the composed path string is raised by the sum of the two paths lengths */
707 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
710 return E_OUTOFMEMORY;
712 /* new path is the concatenation of the rest of str1 and str2 */
713 for(*newStr=0,j=0;j<=lastIdx1;j++)
714 strcatW(newStr,strDec1[j]);
716 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
717 strcatW(newStr,bkSlash);
719 for(j=i;j<=lastIdx2;j++)
720 strcatW(newStr,strDec2[j]);
722 /* create a new moniker with the new string */
723 res=CreateFileMoniker(newStr,ppmkComposite);
725 /* free all strings space memory used by this function */
726 HeapFree(GetProcessHeap(),0,newStr);
728 for(i=0; strDec1[i]!=NULL;i++)
729 CoTaskMemFree(strDec1[i]);
730 for(i=0; strDec2[i]!=NULL;i++)
731 CoTaskMemFree(strDec2[i]);
732 CoTaskMemFree(strDec1);
733 CoTaskMemFree(strDec2);
740 else if(mkSys==MKSYS_ANTIMONIKER){
745 else if (fOnlyIfNotGeneric){
748 return MK_E_NEEDGENERIC;
752 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
755 /******************************************************************************
758 static HRESULT WINAPI
759 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
761 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
763 if (ppenumMoniker == NULL)
766 *ppenumMoniker = NULL;
771 /******************************************************************************
772 * FileMoniker_IsEqual
774 static HRESULT WINAPI
775 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
777 FileMonikerImpl *This = (FileMonikerImpl *)iface;
783 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
785 if (pmkOtherMoniker==NULL)
788 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
790 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
793 res = CreateBindCtx(0,&bind);
794 if (FAILED(res)) return res;
796 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
797 int result = lstrcmpiW(filePath, This->filePathName);
798 CoTaskMemFree(filePath);
799 if ( result == 0 ) return S_OK;
805 /******************************************************************************
808 static HRESULT WINAPI
809 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
811 FileMonikerImpl *This = (FileMonikerImpl *)iface;
813 int h = 0,i,skip,len;
820 val = This->filePathName;
824 for (i = len ; i > 0; i--) {
825 h = (h * 37) + val[off++];
828 /* only sample some characters */
830 for (i = len ; i > 0; i -= skip, off += skip) {
831 h = (h * 39) + val[off];
840 /******************************************************************************
841 * FileMoniker_IsRunning
843 static HRESULT WINAPI
844 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
845 IMoniker* pmkNewlyRunning)
847 IRunningObjectTable* rot;
850 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
852 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
858 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
863 res = IRunningObjectTable_IsRunning(rot,iface);
865 IRunningObjectTable_Release(rot);
870 /******************************************************************************
871 * FileMoniker_GetTimeOfLastChange
872 ******************************************************************************/
873 static HRESULT WINAPI
874 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
875 IMoniker* pmkToLeft, FILETIME* pFileTime)
877 FileMonikerImpl *This = (FileMonikerImpl *)iface;
878 IRunningObjectTable* rot;
880 WIN32_FILE_ATTRIBUTE_DATA info;
882 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
890 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
895 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
897 if (FAILED(res)){ /* the moniker is not registered */
899 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
900 return MK_E_NOOBJECT;
902 *pFileTime=info.ftLastWriteTime;
908 /******************************************************************************
909 * FileMoniker_Inverse
911 static HRESULT WINAPI
912 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
914 TRACE("(%p,%p)\n",iface,ppmk);
916 return CreateAntiMoniker(ppmk);
919 /******************************************************************************
920 * FileMoniker_CommonPrefixWith
922 static HRESULT WINAPI
923 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
926 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
929 ULONG nb1,nb2,i,sameIdx;
930 BOOL machimeNameCase=FALSE;
932 if (ppmkPrefix==NULL)
940 /* check if we have the same type of moniker */
941 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
943 if(mkSys==MKSYS_FILEMONIKER){
946 CreateBindCtx(0,&pbind);
948 /* create a string based on common part of the two paths */
950 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
951 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
953 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
954 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
956 if (nb1==0 || nb2==0)
957 return MK_E_NOPREFIX;
959 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
963 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
964 (stringTable2[sameIdx]!=NULL) &&
965 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
967 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
969 machimeNameCase=TRUE;
971 for(i=2;i<sameIdx;i++)
973 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
974 machimeNameCase=FALSE;
979 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
982 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
986 for(i=0;i<sameIdx;i++)
987 strcatW(commonPath,stringTable1[i]);
990 CoTaskMemFree(stringTable1[i]);
992 CoTaskMemFree(stringTable1);
995 CoTaskMemFree(stringTable2[i]);
997 CoTaskMemFree(stringTable2);
999 ret = CreateFileMoniker(commonPath,ppmkPrefix);
1001 HeapFree(GetProcessHeap(),0,commonPath);
1005 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1008 /******************************************************************************
1009 * DecomposePath (local function)
1011 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1013 static const WCHAR bSlash[] = {'\\',0};
1015 int i=0,j,tabIndex=0, ret=0;
1016 LPOLESTR *strgtable ;
1018 int len=lstrlenW(str);
1020 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1022 strgtable = CoTaskMemAlloc(len*sizeof(WCHAR));
1024 if (strgtable==NULL)
1025 return E_OUTOFMEMORY;
1027 word = CoTaskMemAlloc((len + 1)*sizeof(WCHAR));
1031 ret = E_OUTOFMEMORY;
1037 if(str[i]==bSlash[0]){
1039 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1041 if (strgtable[tabIndex]==NULL)
1043 ret = E_OUTOFMEMORY;
1047 strcpyW(strgtable[tabIndex++],bSlash);
1054 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1059 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1061 if (strgtable[tabIndex]==NULL)
1063 ret = E_OUTOFMEMORY;
1067 strcpyW(strgtable[tabIndex++],word);
1070 strgtable[tabIndex]=NULL;
1072 *stringTable=strgtable;
1079 for (i = 0; i < tabIndex; i++)
1080 CoTaskMemFree(strgtable[i]);
1082 CoTaskMemFree(strgtable);
1086 CoTaskMemFree(word);
1091 /******************************************************************************
1092 * FileMoniker_RelativePathTo
1094 static HRESULT WINAPI
1095 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1099 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1100 DWORD len1=0,len2=0,sameIdx=0,j=0;
1101 static const WCHAR back[] ={'.','.','\\',0};
1103 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1105 if (ppmkRelPath==NULL)
1109 return E_INVALIDARG;
1111 res=CreateBindCtx(0,&bind);
1115 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1118 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1122 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1123 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1125 if (FAILED(len1) || FAILED(len2))
1126 return E_OUTOFMEMORY;
1128 /* count the number of similar items from the begin of the two paths */
1129 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1130 (tabStr2[sameIdx]!=NULL) &&
1131 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1133 /* begin the construction of relativePath */
1134 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1135 /* by "..\\" in the begin */
1136 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1140 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1141 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1142 if (*tabStr1[j]!='\\')
1143 strcatW(relPath,back);
1145 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1146 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1147 strcatW(relPath,tabStr2[j]);
1149 res=CreateFileMoniker(relPath,ppmkRelPath);
1151 for(j=0; tabStr1[j]!=NULL;j++)
1152 CoTaskMemFree(tabStr1[j]);
1153 for(j=0; tabStr2[j]!=NULL;j++)
1154 CoTaskMemFree(tabStr2[j]);
1155 CoTaskMemFree(tabStr1);
1156 CoTaskMemFree(tabStr2);
1157 CoTaskMemFree(str1);
1158 CoTaskMemFree(str2);
1159 HeapFree(GetProcessHeap(),0,relPath);
1161 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1167 /******************************************************************************
1168 * FileMoniker_GetDisplayName
1170 static HRESULT WINAPI
1171 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1172 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1174 FileMonikerImpl *This = (FileMonikerImpl *)iface;
1176 int len=lstrlenW(This->filePathName);
1178 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1180 if (ppszDisplayName==NULL)
1183 if (pmkToLeft!=NULL)
1184 return E_INVALIDARG;
1186 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1187 if (*ppszDisplayName==NULL)
1188 return E_OUTOFMEMORY;
1190 strcpyW(*ppszDisplayName,This->filePathName);
1192 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1197 /******************************************************************************
1198 * FileMoniker_ParseDisplayName
1200 static HRESULT WINAPI
1201 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1202 LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1204 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1208 /******************************************************************************
1209 * FileMoniker_IsSystemMoniker
1211 static HRESULT WINAPI
1212 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1214 TRACE("(%p,%p)\n",iface,pwdMksys);
1219 (*pwdMksys)=MKSYS_FILEMONIKER;
1224 /*******************************************************************************
1225 * FileMonikerIROTData_QueryInterface
1227 static HRESULT WINAPI
1228 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1231 IMoniker *This = impl_from_IROTData(iface);
1233 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1235 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1238 /***********************************************************************
1239 * FileMonikerIROTData_AddRef
1242 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1244 IMoniker *This = impl_from_IROTData(iface);
1246 TRACE("(%p)\n",This);
1248 return IMoniker_AddRef(This);
1251 /***********************************************************************
1252 * FileMonikerIROTData_Release
1255 FileMonikerROTDataImpl_Release(IROTData* iface)
1257 IMoniker *This = impl_from_IROTData(iface);
1259 TRACE("(%p)\n",This);
1261 return FileMonikerImpl_Release(This);
1264 /******************************************************************************
1265 * FileMonikerIROTData_GetComparaisonData
1267 static HRESULT WINAPI
1268 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1269 ULONG cbMax, ULONG* pcbData)
1271 IMoniker *This = impl_from_IROTData(iface);
1272 FileMonikerImpl *This1 = (FileMonikerImpl *)This;
1273 int len = (strlenW(This1->filePathName)+1);
1277 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1279 *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1280 if (cbMax < *pcbData)
1281 return E_OUTOFMEMORY;
1283 memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1284 pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1285 for (i = 0; i < len; i++)
1286 pszFileName[i] = toupperW(This1->filePathName[i]);
1292 * Virtual function table for the FileMonikerImpl class which include IPersist,
1293 * IPersistStream and IMoniker functions.
1295 static const IMonikerVtbl VT_FileMonikerImpl =
1297 FileMonikerImpl_QueryInterface,
1298 FileMonikerImpl_AddRef,
1299 FileMonikerImpl_Release,
1300 FileMonikerImpl_GetClassID,
1301 FileMonikerImpl_IsDirty,
1302 FileMonikerImpl_Load,
1303 FileMonikerImpl_Save,
1304 FileMonikerImpl_GetSizeMax,
1305 FileMonikerImpl_BindToObject,
1306 FileMonikerImpl_BindToStorage,
1307 FileMonikerImpl_Reduce,
1308 FileMonikerImpl_ComposeWith,
1309 FileMonikerImpl_Enum,
1310 FileMonikerImpl_IsEqual,
1311 FileMonikerImpl_Hash,
1312 FileMonikerImpl_IsRunning,
1313 FileMonikerImpl_GetTimeOfLastChange,
1314 FileMonikerImpl_Inverse,
1315 FileMonikerImpl_CommonPrefixWith,
1316 FileMonikerImpl_RelativePathTo,
1317 FileMonikerImpl_GetDisplayName,
1318 FileMonikerImpl_ParseDisplayName,
1319 FileMonikerImpl_IsSystemMoniker
1322 /* Virtual function table for the IROTData class. */
1323 static const IROTDataVtbl VT_ROTDataImpl =
1325 FileMonikerROTDataImpl_QueryInterface,
1326 FileMonikerROTDataImpl_AddRef,
1327 FileMonikerROTDataImpl_Release,
1328 FileMonikerROTDataImpl_GetComparisonData
1331 /******************************************************************************
1332 * FileMoniker_Construct (local function)
1334 static HRESULT WINAPI
1335 FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1338 int sizeStr=lstrlenW(lpszPathName);
1340 static const WCHAR twoPoint[]={'.','.',0};
1341 static const WCHAR bkSlash[]={'\\',0};
1344 TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1346 /* Initialize the virtual fgunction table. */
1347 This->lpvtbl1 = &VT_FileMonikerImpl;
1348 This->lpvtbl2 = &VT_ROTDataImpl;
1350 This->pMarshal = NULL;
1352 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1354 if (This->filePathName==NULL)
1355 return E_OUTOFMEMORY;
1357 strcpyW(This->filePathName,lpszPathName);
1359 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1364 if (lstrcmpW(tabStr[0],twoPoint)!=0)
1369 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1375 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1383 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1386 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1388 *This->filePathName=0;
1390 for(i=0;tabStr[i]!=NULL;i++)
1391 strcatW(This->filePathName,tabStr[i]);
1394 strcatW(This->filePathName,bkSlash);
1397 for(i=0; tabStr[i]!=NULL;i++)
1398 CoTaskMemFree(tabStr[i]);
1399 CoTaskMemFree(tabStr);
1404 /******************************************************************************
1405 * CreateFileMoniker (OLE32.@)
1406 ******************************************************************************/
1407 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1409 FileMonikerImpl* newFileMoniker;
1412 TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1422 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1424 if (!newFileMoniker)
1425 return E_OUTOFMEMORY;
1427 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1430 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&IID_IMoniker,(void**)ppmk);
1432 HeapFree(GetProcessHeap(),0,newFileMoniker);
1437 /* find a character from a set in reverse without the string having to be null-terminated */
1438 static inline WCHAR *memrpbrkW(const WCHAR *ptr, size_t n, const WCHAR *accept)
1440 const WCHAR *end, *ret = NULL;
1441 for (end = ptr + n; ptr < end; ptr++) if (strchrW(accept, *ptr)) ret = ptr;
1442 return (WCHAR *)ret;
1445 HRESULT FileMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1446 LPDWORD pchEaten, LPMONIKER *ppmk)
1449 static const WCHAR wszSeparators[] = {':','\\','/','!',0};
1451 for (end = szDisplayName + strlenW(szDisplayName);
1452 end && (end != szDisplayName);
1453 end = memrpbrkW(szDisplayName, end - szDisplayName, wszSeparators))
1456 IRunningObjectTable *rot;
1457 IMoniker *file_moniker;
1458 LPWSTR file_display_name;
1459 LPWSTR full_path_name;
1460 DWORD full_path_name_len;
1461 int len = end - szDisplayName;
1463 file_display_name = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1464 if (!file_display_name) return E_OUTOFMEMORY;
1465 memcpy(file_display_name, szDisplayName, len * sizeof(WCHAR));
1466 file_display_name[len] = '\0';
1468 hr = CreateFileMoniker(file_display_name, &file_moniker);
1471 HeapFree(GetProcessHeap(), 0, file_display_name);
1475 hr = IBindCtx_GetRunningObjectTable(pbc, &rot);
1478 HeapFree(GetProcessHeap(), 0, file_display_name);
1479 IMoniker_Release(file_moniker);
1483 hr = IRunningObjectTable_IsRunning(rot, file_moniker);
1484 IRunningObjectTable_Release(rot);
1487 HeapFree(GetProcessHeap(), 0, file_display_name);
1488 IMoniker_Release(file_moniker);
1493 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name));
1495 *ppmk = file_moniker;
1496 HeapFree(GetProcessHeap(), 0, file_display_name);
1500 full_path_name_len = GetFullPathNameW(file_display_name, 0, NULL, NULL);
1501 if (!full_path_name_len)
1503 HeapFree(GetProcessHeap(), 0, file_display_name);
1504 IMoniker_Release(file_moniker);
1507 full_path_name = HeapAlloc(GetProcessHeap(), 0, full_path_name_len * sizeof(WCHAR));
1508 if (!full_path_name)
1510 HeapFree(GetProcessHeap(), 0, file_display_name);
1511 IMoniker_Release(file_moniker);
1512 return E_OUTOFMEMORY;
1514 GetFullPathNameW(file_display_name, full_path_name_len, full_path_name, NULL);
1516 if (GetFileAttributesW(full_path_name) == INVALID_FILE_ATTRIBUTES)
1517 TRACE("couldn't open file %s\n", debugstr_w(full_path_name));
1520 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName));
1522 *ppmk = file_moniker;
1523 HeapFree(GetProcessHeap(), 0, file_display_name);
1524 HeapFree(GetProcessHeap(), 0, full_path_name);
1527 HeapFree(GetProcessHeap(), 0, file_display_name);
1528 HeapFree(GetProcessHeap(), 0, full_path_name);
1529 IMoniker_Release(file_moniker);
1532 return MK_E_CANTOPENFILE;
1536 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1537 REFIID riid, LPVOID *ppv)
1540 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1543 IUnknown_AddRef(iface);
1546 return E_NOINTERFACE;
1549 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1551 return 2; /* non-heap based object */
1554 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1556 return 1; /* non-heap based object */
1559 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1560 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1562 FileMonikerImpl* newFileMoniker;
1564 static const WCHAR wszEmpty[] = { 0 };
1566 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1571 return CLASS_E_NOAGGREGATION;
1573 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1574 if (!newFileMoniker)
1575 return E_OUTOFMEMORY;
1577 hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1580 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker, riid, ppv);
1582 HeapFree(GetProcessHeap(),0,newFileMoniker);
1587 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1589 FIXME("(%d), stub!\n",fLock);
1593 static const IClassFactoryVtbl FileMonikerCFVtbl =
1595 FileMonikerCF_QueryInterface,
1596 FileMonikerCF_AddRef,
1597 FileMonikerCF_Release,
1598 FileMonikerCF_CreateInstance,
1599 FileMonikerCF_LockServer
1601 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1603 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1605 return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);