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 FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
67 static HRESULT 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.*/
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 = &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);
202 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
203 if (bread!=sizeof(WORD))
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 unknown value */
233 IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
234 if (bread != sizeof(WORD))
236 WARN("Couldn't read unknown value\n");
240 /* read the DEAD constant */
241 IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
242 if (bread != sizeof(WORD))
244 WARN("Couldn't read DEAD constant\n");
250 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
251 if (bread!=sizeof(DWORD))
253 WARN("Couldn't read 0 padding\n");
258 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
259 if (bread!=sizeof(DWORD))
262 if (!dwbuffer) /* No W-string */
265 len=MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, bytesA, NULL, 0);
269 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
275 MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, -1, filePathW, len+1);
284 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
285 if (bread!=sizeof(DWORD) || dwbuffer!=bytesW)
288 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
289 if (bread!=sizeof(WORD) || wbuffer!=0x3)
292 len=bytesW/sizeof(WCHAR);
293 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
299 res=IStream_Read(pStm,filePathW,bytesW,&bread);
306 HeapFree(GetProcessHeap(),0,filePathA);
307 HeapFree(GetProcessHeap(),0,This->filePathName);
308 This->filePathName=filePathW;
313 HeapFree(GetProcessHeap(), 0, filePathA);
314 HeapFree(GetProcessHeap(), 0, filePathW);
321 /******************************************************************************
324 * This function saves data of this object. In the beginning I thought
325 * that I have just to write the filePath string on Stream. But, when I
326 * tested this function with windows program samples, I noticed that it
327 * was not the case. This implementation is based on XP SP2. Other versions
328 * of Windows have minor variations.
330 * Data which must be written on stream is:
331 * 1) WORD constant: zero (not validated by Windows)
332 * 2) length of the path string ("\0" included)
333 * 3) path string type A
334 * 4) Unknown WORD value: Frequently 0xFFFF, but not always. If set very large,
335 * Windows returns E_OUTOFMEMORY
336 * 5) WORD Constant: 0xDEAD (not validated by Windows)
337 * 6) five DWORD constant: zero (not validated by Windows)
338 * 7) If we're only writing the multibyte version,
339 * write a zero DWORD and finish.
341 * 8) DWORD: double-length of the path string type W ("\0" not
343 * 9) WORD constant: 0x3
344 * 10) filePath unicode string.
347 static HRESULT WINAPI
348 FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
350 FileMonikerImpl *This = (FileMonikerImpl *)iface;
353 LPOLESTR filePathW=This->filePathName;
355 DWORD bytesA, bytesW, len;
357 static const WORD FFFF = 0xFFFF; /* Constants */
358 static const WORD DEAD = 0xDEAD;
359 static const DWORD ZERO = 0;
360 static const WORD THREE = 0x3;
363 BOOL bUsedDefault, bWriteWide;
365 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
371 res=IStream_Write(pStm,&ZERO,sizeof(WORD),NULL);
372 if (FAILED(res)) return res;
374 /* write length of filePath string ( 0 included )*/
375 bytesA = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
376 res=IStream_Write(pStm,&bytesA,sizeof(DWORD),NULL);
377 if (FAILED(res)) return res;
379 /* write A string (with '\0') */
380 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
382 return E_OUTOFMEMORY;
383 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, bytesA, NULL, &bUsedDefault);
384 res=IStream_Write(pStm,filePathA,bytesA,NULL);
385 HeapFree(GetProcessHeap(),0,filePathA);
386 if (FAILED(res)) return res;
388 /* write a WORD 0xFFFF */
389 res=IStream_Write(pStm,&FFFF,sizeof(WORD),NULL);
390 if (FAILED(res)) return res;
392 /* write a WORD 0xDEAD */
393 res=IStream_Write(pStm,&DEAD,sizeof(WORD),NULL);
394 if (FAILED(res)) return res;
396 /* write 5 zero DWORDs */
399 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
400 if (FAILED(res)) return res;
403 /* Write the wide version if:
404 * + couldn't convert to CP_ACP,
405 * or + it's a directory,
406 * or + there's a character > 0xFF
408 len = lstrlenW(filePathW);
409 bWriteWide = (bUsedDefault || (len > 0 && filePathW[len-1]=='\\' ));
413 for(pch=filePathW;*pch;++pch)
424 return IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
426 /* write bytes needed for the filepathW (without 0) + 6 */
427 bytesW = len*sizeof(WCHAR) + 6;
428 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
429 if (FAILED(res)) return res;
431 /* try again, without the extra 6 */
433 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
434 if (FAILED(res)) return res;
437 res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
438 if (FAILED(res)) return res;
440 /* write W string (no 0) */
441 return IStream_Write(pStm,filePathW,bytesW,NULL);
444 /******************************************************************************
445 * FileMoniker_GetSizeMax
447 static HRESULT WINAPI
448 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
450 FileMonikerImpl *This = (FileMonikerImpl *)iface;
452 TRACE("(%p,%p)\n",iface,pcbSize);
457 /* We could calculate exactly (see ...::Save()) but instead
458 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
460 pcbSize->u.LowPart = 0x38 + 4 * lstrlenW(This->filePathName);
461 pcbSize->u.HighPart = 0;
466 /******************************************************************************
467 * FileMoniker_Destroy (local function)
468 *******************************************************************************/
469 HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* This)
471 TRACE("(%p)\n",This);
473 if (This->pMarshal) IUnknown_Release(This->pMarshal);
474 HeapFree(GetProcessHeap(),0,This->filePathName);
475 HeapFree(GetProcessHeap(),0,This);
480 /******************************************************************************
481 * FileMoniker_BindToObject
483 static HRESULT WINAPI
484 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
485 REFIID riid, VOID** ppvResult)
490 IRunningObjectTable *prot=0;
492 IClassFactory *pcf=0;
493 IClassActivator *pca=0;
495 FileMonikerImpl *This = (FileMonikerImpl *)iface;
499 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
503 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
506 /* if the requested class was loaded before ! we don't need to reload it */
507 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
510 /* first activation of this class */
511 res=GetClassFile(This->filePathName,&clsID);
514 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
517 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
521 IUnknown_AddRef(pObj);
529 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
531 if (res==E_NOINTERFACE){
533 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
535 if (res==E_NOINTERFACE)
536 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
540 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)&ppf);
542 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
547 IUnknown_AddRef(pObj);
554 /*res=GetClassFile(This->filePathName,&clsID);
558 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
563 IUnknown_AddRef(pObj);
570 /* get the requested interface from the loaded class */
571 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
573 IBindCtx_RegisterObjectBound(pbc,*ppvResult);
575 IUnknown_Release(pObj);
579 IRunningObjectTable_Release(prot);
582 IPersistFile_Release(ppf);
585 IClassActivator_Release(pca);
588 IClassFactory_Release(pcf);
593 /******************************************************************************
594 * FileMoniker_BindToStorage
596 static HRESULT WINAPI
597 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
598 REFIID riid, VOID** ppvObject)
604 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
606 if (pmkToLeft==NULL){
608 if (IsEqualIID(&IID_IStorage, riid)){
610 /* get the file name */
611 IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
613 /* verify if the file contains a storage object */
614 res=StgIsStorageFile(filePath);
618 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
624 IStorage_AddRef(pstg);
629 CoTaskMemFree(filePath);
632 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
635 return E_NOINTERFACE;
639 FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
646 /******************************************************************************
648 ******************************************************************************/
649 static HRESULT WINAPI
650 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
651 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
653 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
655 if (ppmkReduced==NULL)
658 IMoniker_AddRef(iface);
662 return MK_S_REDUCED_TO_SELF;
665 /******************************************************************************
666 * FileMoniker_ComposeWith
668 static HRESULT WINAPI
669 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
670 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
673 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
674 static const WCHAR twoPoint[]={'.','.',0};
675 static const WCHAR bkSlash[]={'\\',0};
677 int i=0,j=0,lastIdx1=0,lastIdx2=0;
680 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
682 if (ppmkComposite==NULL)
690 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
692 /* check if we have two FileMonikers to compose or not */
693 if(mkSys==MKSYS_FILEMONIKER){
695 CreateBindCtx(0,&bind);
697 IMoniker_GetDisplayName(iface,bind,NULL,&str1);
698 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
700 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
701 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
702 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
704 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
707 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
710 /* for etch "..\" in the left of str2 remove the right element from str1 */
711 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
716 /* the length of the composed path string is raised by the sum of the two paths lengths */
717 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
720 return E_OUTOFMEMORY;
722 /* new path is the concatenation of the rest of str1 and str2 */
723 for(*newStr=0,j=0;j<=lastIdx1;j++)
724 strcatW(newStr,strDec1[j]);
726 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
727 strcatW(newStr,bkSlash);
729 for(j=i;j<=lastIdx2;j++)
730 strcatW(newStr,strDec2[j]);
732 /* create a new moniker with the new string */
733 res=CreateFileMoniker(newStr,ppmkComposite);
735 /* free all strings space memory used by this function */
736 HeapFree(GetProcessHeap(),0,newStr);
738 for(i=0; strDec1[i]!=NULL;i++)
739 CoTaskMemFree(strDec1[i]);
740 for(i=0; strDec2[i]!=NULL;i++)
741 CoTaskMemFree(strDec2[i]);
742 CoTaskMemFree(strDec1);
743 CoTaskMemFree(strDec2);
750 else if(mkSys==MKSYS_ANTIMONIKER){
755 else if (fOnlyIfNotGeneric){
758 return MK_E_NEEDGENERIC;
762 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
765 /******************************************************************************
768 static HRESULT WINAPI
769 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
771 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
773 if (ppenumMoniker == NULL)
776 *ppenumMoniker = NULL;
781 /******************************************************************************
782 * FileMoniker_IsEqual
784 static HRESULT WINAPI
785 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
787 FileMonikerImpl *This = (FileMonikerImpl *)iface;
793 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
795 if (pmkOtherMoniker==NULL)
798 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
800 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
803 res = CreateBindCtx(0,&bind);
804 if (FAILED(res)) return res;
807 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
808 if (!lstrcmpiW(filePath, This->filePathName))
810 CoTaskMemFree(filePath);
813 IBindCtx_Release(bind);
817 /******************************************************************************
820 static HRESULT WINAPI
821 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
823 FileMonikerImpl *This = (FileMonikerImpl *)iface;
825 int h = 0,i,skip,len;
832 val = This->filePathName;
836 for (i = len ; i > 0; i--) {
837 h = (h * 37) + val[off++];
840 /* only sample some characters */
842 for (i = len ; i > 0; i -= skip, off += skip) {
843 h = (h * 39) + val[off];
852 /******************************************************************************
853 * FileMoniker_IsRunning
855 static HRESULT WINAPI
856 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
857 IMoniker* pmkNewlyRunning)
859 IRunningObjectTable* rot;
862 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
864 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
870 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
875 res = IRunningObjectTable_IsRunning(rot,iface);
877 IRunningObjectTable_Release(rot);
882 /******************************************************************************
883 * FileMoniker_GetTimeOfLastChange
884 ******************************************************************************/
885 static HRESULT WINAPI
886 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
887 IMoniker* pmkToLeft, FILETIME* pFileTime)
889 FileMonikerImpl *This = (FileMonikerImpl *)iface;
890 IRunningObjectTable* rot;
892 WIN32_FILE_ATTRIBUTE_DATA info;
894 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
902 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
907 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
909 if (FAILED(res)){ /* the moniker is not registered */
911 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
912 return MK_E_NOOBJECT;
914 *pFileTime=info.ftLastWriteTime;
920 /******************************************************************************
921 * FileMoniker_Inverse
923 static HRESULT WINAPI
924 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
926 TRACE("(%p,%p)\n",iface,ppmk);
928 return CreateAntiMoniker(ppmk);
931 /******************************************************************************
932 * FileMoniker_CommonPrefixWith
934 static HRESULT WINAPI
935 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
938 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
941 ULONG nb1,nb2,i,sameIdx;
942 BOOL machimeNameCase=FALSE;
944 if (ppmkPrefix==NULL)
952 /* check if we have the same type of moniker */
953 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
955 if(mkSys==MKSYS_FILEMONIKER){
958 ret = CreateBindCtx(0,&pbind);
962 /* create a string based on common part of the two paths */
964 ret = IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
967 ret = IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
971 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
974 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
978 if (nb1==0 || nb2==0)
979 return MK_E_NOPREFIX;
981 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
983 return E_OUTOFMEMORY;
987 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
988 (stringTable2[sameIdx]!=NULL) &&
989 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
991 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
993 machimeNameCase=TRUE;
995 for(i=2;i<sameIdx;i++)
997 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
998 machimeNameCase=FALSE;
1003 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1006 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1007 ret = MK_E_NOPREFIX;
1010 for(i=0;i<sameIdx;i++)
1011 strcatW(commonPath,stringTable1[i]);
1014 CoTaskMemFree(stringTable1[i]);
1016 CoTaskMemFree(stringTable1);
1019 CoTaskMemFree(stringTable2[i]);
1021 CoTaskMemFree(stringTable2);
1023 ret = CreateFileMoniker(commonPath,ppmkPrefix);
1025 HeapFree(GetProcessHeap(),0,commonPath);
1029 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1032 /******************************************************************************
1033 * DecomposePath (local function)
1035 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1037 static const WCHAR bSlash[] = {'\\',0};
1039 int i=0,j,tabIndex=0, ret=0;
1040 LPOLESTR *strgtable ;
1042 int len=lstrlenW(str);
1044 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1046 strgtable = CoTaskMemAlloc((len + 1)*sizeof(*strgtable));
1048 if (strgtable==NULL)
1049 return E_OUTOFMEMORY;
1051 word = CoTaskMemAlloc((len + 1)*sizeof(WCHAR));
1055 ret = E_OUTOFMEMORY;
1061 if(str[i]==bSlash[0]){
1063 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1065 if (strgtable[tabIndex]==NULL)
1067 ret = E_OUTOFMEMORY;
1071 strcpyW(strgtable[tabIndex++],bSlash);
1078 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1083 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1085 if (strgtable[tabIndex]==NULL)
1087 ret = E_OUTOFMEMORY;
1091 strcpyW(strgtable[tabIndex++],word);
1094 strgtable[tabIndex]=NULL;
1096 *stringTable=strgtable;
1103 for (i = 0; i < tabIndex; i++)
1104 CoTaskMemFree(strgtable[i]);
1106 CoTaskMemFree(strgtable);
1110 CoTaskMemFree(word);
1115 /******************************************************************************
1116 * FileMoniker_RelativePathTo
1118 static HRESULT WINAPI
1119 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1123 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1124 DWORD len1=0,len2=0,sameIdx=0,j=0;
1125 static const WCHAR back[] ={'.','.','\\',0};
1127 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1129 if (ppmkRelPath==NULL)
1133 return E_INVALIDARG;
1135 res=CreateBindCtx(0,&bind);
1139 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1142 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1146 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1147 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1149 if (FAILED(len1) || FAILED(len2))
1150 return E_OUTOFMEMORY;
1152 /* count the number of similar items from the begin of the two paths */
1153 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1154 (tabStr2[sameIdx]!=NULL) &&
1155 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1157 /* begin the construction of relativePath */
1158 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1159 /* by "..\\" in the begin */
1160 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1164 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1165 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1166 if (*tabStr1[j]!='\\')
1167 strcatW(relPath,back);
1169 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1170 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1171 strcatW(relPath,tabStr2[j]);
1173 res=CreateFileMoniker(relPath,ppmkRelPath);
1175 for(j=0; tabStr1[j]!=NULL;j++)
1176 CoTaskMemFree(tabStr1[j]);
1177 for(j=0; tabStr2[j]!=NULL;j++)
1178 CoTaskMemFree(tabStr2[j]);
1179 CoTaskMemFree(tabStr1);
1180 CoTaskMemFree(tabStr2);
1181 CoTaskMemFree(str1);
1182 CoTaskMemFree(str2);
1183 HeapFree(GetProcessHeap(),0,relPath);
1185 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1191 /******************************************************************************
1192 * FileMoniker_GetDisplayName
1194 static HRESULT WINAPI
1195 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1196 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1198 FileMonikerImpl *This = (FileMonikerImpl *)iface;
1200 int len=lstrlenW(This->filePathName);
1202 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1204 if (ppszDisplayName==NULL)
1207 if (pmkToLeft!=NULL)
1208 return E_INVALIDARG;
1210 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1211 if (*ppszDisplayName==NULL)
1212 return E_OUTOFMEMORY;
1214 strcpyW(*ppszDisplayName,This->filePathName);
1216 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1221 /******************************************************************************
1222 * FileMoniker_ParseDisplayName
1224 static HRESULT WINAPI
1225 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1226 LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1228 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1232 /******************************************************************************
1233 * FileMoniker_IsSystemMoniker
1235 static HRESULT WINAPI
1236 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1238 TRACE("(%p,%p)\n",iface,pwdMksys);
1243 (*pwdMksys)=MKSYS_FILEMONIKER;
1248 /*******************************************************************************
1249 * FileMonikerIROTData_QueryInterface
1251 static HRESULT WINAPI
1252 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1255 IMoniker *This = impl_from_IROTData(iface);
1257 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1259 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1262 /***********************************************************************
1263 * FileMonikerIROTData_AddRef
1266 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1268 IMoniker *This = impl_from_IROTData(iface);
1270 TRACE("(%p)\n",This);
1272 return IMoniker_AddRef(This);
1275 /***********************************************************************
1276 * FileMonikerIROTData_Release
1279 FileMonikerROTDataImpl_Release(IROTData* iface)
1281 IMoniker *This = impl_from_IROTData(iface);
1283 TRACE("(%p)\n",This);
1285 return FileMonikerImpl_Release(This);
1288 /******************************************************************************
1289 * FileMonikerIROTData_GetComparisonData
1291 static HRESULT WINAPI
1292 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1293 ULONG cbMax, ULONG* pcbData)
1295 IMoniker *This = impl_from_IROTData(iface);
1296 FileMonikerImpl *This1 = (FileMonikerImpl *)This;
1297 int len = (strlenW(This1->filePathName)+1);
1301 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1303 *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1304 if (cbMax < *pcbData)
1305 return E_OUTOFMEMORY;
1307 memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1308 pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1309 for (i = 0; i < len; i++)
1310 pszFileName[i] = toupperW(This1->filePathName[i]);
1316 * Virtual function table for the FileMonikerImpl class which include IPersist,
1317 * IPersistStream and IMoniker functions.
1319 static const IMonikerVtbl VT_FileMonikerImpl =
1321 FileMonikerImpl_QueryInterface,
1322 FileMonikerImpl_AddRef,
1323 FileMonikerImpl_Release,
1324 FileMonikerImpl_GetClassID,
1325 FileMonikerImpl_IsDirty,
1326 FileMonikerImpl_Load,
1327 FileMonikerImpl_Save,
1328 FileMonikerImpl_GetSizeMax,
1329 FileMonikerImpl_BindToObject,
1330 FileMonikerImpl_BindToStorage,
1331 FileMonikerImpl_Reduce,
1332 FileMonikerImpl_ComposeWith,
1333 FileMonikerImpl_Enum,
1334 FileMonikerImpl_IsEqual,
1335 FileMonikerImpl_Hash,
1336 FileMonikerImpl_IsRunning,
1337 FileMonikerImpl_GetTimeOfLastChange,
1338 FileMonikerImpl_Inverse,
1339 FileMonikerImpl_CommonPrefixWith,
1340 FileMonikerImpl_RelativePathTo,
1341 FileMonikerImpl_GetDisplayName,
1342 FileMonikerImpl_ParseDisplayName,
1343 FileMonikerImpl_IsSystemMoniker
1346 /* Virtual function table for the IROTData class. */
1347 static const IROTDataVtbl VT_ROTDataImpl =
1349 FileMonikerROTDataImpl_QueryInterface,
1350 FileMonikerROTDataImpl_AddRef,
1351 FileMonikerROTDataImpl_Release,
1352 FileMonikerROTDataImpl_GetComparisonData
1355 /******************************************************************************
1356 * FileMoniker_Construct (local function)
1358 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1361 int sizeStr=lstrlenW(lpszPathName);
1363 static const WCHAR twoPoint[]={'.','.',0};
1364 static const WCHAR bkSlash[]={'\\',0};
1367 TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1369 /* Initialize the virtual function table. */
1370 This->lpvtbl1 = &VT_FileMonikerImpl;
1371 This->lpvtbl2 = &VT_ROTDataImpl;
1373 This->pMarshal = NULL;
1375 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1377 if (This->filePathName==NULL)
1378 return E_OUTOFMEMORY;
1380 strcpyW(This->filePathName,lpszPathName);
1382 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1387 if (lstrcmpW(tabStr[0],twoPoint)!=0)
1392 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1398 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1406 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1409 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1411 *This->filePathName=0;
1413 for(i=0;tabStr[i]!=NULL;i++)
1414 strcatW(This->filePathName,tabStr[i]);
1417 strcatW(This->filePathName,bkSlash);
1420 for(i=0; tabStr[i]!=NULL;i++)
1421 CoTaskMemFree(tabStr[i]);
1422 CoTaskMemFree(tabStr);
1427 /******************************************************************************
1428 * CreateFileMoniker (OLE32.@)
1429 ******************************************************************************/
1430 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1432 FileMonikerImpl* newFileMoniker;
1435 TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1445 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1447 if (!newFileMoniker)
1448 return E_OUTOFMEMORY;
1450 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1453 hr = IMoniker_QueryInterface((IMoniker*)newFileMoniker,&IID_IMoniker,(void**)ppmk);
1455 HeapFree(GetProcessHeap(),0,newFileMoniker);
1460 /* find a character from a set in reverse without the string having to be null-terminated */
1461 static inline WCHAR *memrpbrkW(const WCHAR *ptr, size_t n, const WCHAR *accept)
1463 const WCHAR *end, *ret = NULL;
1464 for (end = ptr + n; ptr < end; ptr++) if (strchrW(accept, *ptr)) ret = ptr;
1465 return (WCHAR *)ret;
1468 HRESULT FileMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1469 LPDWORD pchEaten, LPMONIKER *ppmk)
1472 static const WCHAR wszSeparators[] = {':','\\','/','!',0};
1474 for (end = szDisplayName + strlenW(szDisplayName);
1475 end && (end != szDisplayName);
1476 end = memrpbrkW(szDisplayName, end - szDisplayName, wszSeparators))
1479 IRunningObjectTable *rot;
1480 IMoniker *file_moniker;
1481 LPWSTR file_display_name;
1482 LPWSTR full_path_name;
1483 DWORD full_path_name_len;
1484 int len = end - szDisplayName;
1486 file_display_name = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1487 if (!file_display_name) return E_OUTOFMEMORY;
1488 memcpy(file_display_name, szDisplayName, len * sizeof(WCHAR));
1489 file_display_name[len] = '\0';
1491 hr = CreateFileMoniker(file_display_name, &file_moniker);
1494 HeapFree(GetProcessHeap(), 0, file_display_name);
1498 hr = IBindCtx_GetRunningObjectTable(pbc, &rot);
1501 HeapFree(GetProcessHeap(), 0, file_display_name);
1502 IMoniker_Release(file_moniker);
1506 hr = IRunningObjectTable_IsRunning(rot, file_moniker);
1507 IRunningObjectTable_Release(rot);
1510 HeapFree(GetProcessHeap(), 0, file_display_name);
1511 IMoniker_Release(file_moniker);
1516 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name));
1518 *ppmk = file_moniker;
1519 HeapFree(GetProcessHeap(), 0, file_display_name);
1523 full_path_name_len = GetFullPathNameW(file_display_name, 0, NULL, NULL);
1524 if (!full_path_name_len)
1526 HeapFree(GetProcessHeap(), 0, file_display_name);
1527 IMoniker_Release(file_moniker);
1530 full_path_name = HeapAlloc(GetProcessHeap(), 0, full_path_name_len * sizeof(WCHAR));
1531 if (!full_path_name)
1533 HeapFree(GetProcessHeap(), 0, file_display_name);
1534 IMoniker_Release(file_moniker);
1535 return E_OUTOFMEMORY;
1537 GetFullPathNameW(file_display_name, full_path_name_len, full_path_name, NULL);
1539 if (GetFileAttributesW(full_path_name) == INVALID_FILE_ATTRIBUTES)
1540 TRACE("couldn't open file %s\n", debugstr_w(full_path_name));
1543 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName));
1545 *ppmk = file_moniker;
1546 HeapFree(GetProcessHeap(), 0, file_display_name);
1547 HeapFree(GetProcessHeap(), 0, full_path_name);
1550 HeapFree(GetProcessHeap(), 0, file_display_name);
1551 HeapFree(GetProcessHeap(), 0, full_path_name);
1552 IMoniker_Release(file_moniker);
1555 return MK_E_CANTOPENFILE;
1559 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1560 REFIID riid, LPVOID *ppv)
1563 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1566 IUnknown_AddRef(iface);
1569 return E_NOINTERFACE;
1572 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1574 return 2; /* non-heap based object */
1577 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1579 return 1; /* non-heap based object */
1582 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1583 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1585 FileMonikerImpl* newFileMoniker;
1587 static const WCHAR wszEmpty[] = { 0 };
1589 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1594 return CLASS_E_NOAGGREGATION;
1596 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1597 if (!newFileMoniker)
1598 return E_OUTOFMEMORY;
1600 hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1603 hr = IMoniker_QueryInterface((IMoniker*)newFileMoniker, riid, ppv);
1605 HeapFree(GetProcessHeap(),0,newFileMoniker);
1610 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1612 FIXME("(%d), stub!\n",fLock);
1616 static const IClassFactoryVtbl FileMonikerCFVtbl =
1618 FileMonikerCF_QueryInterface,
1619 FileMonikerCF_AddRef,
1620 FileMonikerCF_Release,
1621 FileMonikerCF_CreateInstance,
1622 FileMonikerCF_LockServer
1624 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1626 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1628 return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);