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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
38 #include "compobj_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 const CLSID CLSID_FileMoniker = {
43 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
46 /* filemoniker data structure */
47 typedef struct FileMonikerImpl{
49 const IMonikerVtbl* lpvtbl1; /* VTable relative to the IMoniker interface.*/
51 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
52 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
54 const IROTDataVtbl* lpvtbl2; /* VTable relative to the IROTData interface.*/
56 LONG ref; /* reference counter for this object */
58 LPOLESTR filePathName; /* path string identified by this filemoniker */
60 IUnknown *pMarshal; /* custom marshaler */
63 static inline IMoniker *impl_from_IROTData( IROTData *iface )
65 return (IMoniker *)((char*)iface - FIELD_OFFSET(FileMonikerImpl, lpvtbl2));
68 /* Local function used by filemoniker implementation */
69 static HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
70 static HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
72 /*******************************************************************************
73 * FileMoniker_QueryInterface
76 FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
78 FileMonikerImpl *This = (FileMonikerImpl *)iface;
80 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
82 /* Perform a sanity check on the parameters.*/
83 if ( (This==0) || (ppvObject==0) )
86 /* Initialize the return parameter */
89 /* Compare the riid with the interface IDs implemented by this object.*/
90 if (IsEqualIID(&IID_IUnknown, riid) ||
91 IsEqualIID(&IID_IPersist, riid) ||
92 IsEqualIID(&IID_IPersistStream,riid) ||
93 IsEqualIID(&IID_IMoniker, riid)
97 else if (IsEqualIID(&IID_IROTData, riid))
98 *ppvObject = (IROTData*)&(This->lpvtbl2);
99 else if (IsEqualIID(&IID_IMarshal, riid))
103 hr = MonikerMarshal_Create(iface, &This->pMarshal);
106 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
109 /* Check that we obtained an interface.*/
111 return E_NOINTERFACE;
113 /* Query Interface always increases the reference count by one when it is successful */
114 IMoniker_AddRef(iface);
119 /******************************************************************************
123 FileMonikerImpl_AddRef(IMoniker* iface)
125 FileMonikerImpl *This = (FileMonikerImpl *)iface;
127 TRACE("(%p)\n",iface);
129 return InterlockedIncrement(&This->ref);
132 /******************************************************************************
133 * FileMoniker_Release
136 FileMonikerImpl_Release(IMoniker* iface)
138 FileMonikerImpl *This = (FileMonikerImpl *)iface;
141 TRACE("(%p)\n",iface);
143 ref = InterlockedDecrement(&This->ref);
145 /* destroy the object if there's no more reference on it */
146 if (ref == 0) FileMonikerImpl_Destroy(This);
151 /******************************************************************************
152 * FileMoniker_GetClassID
154 static HRESULT WINAPI
155 FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID)
157 TRACE("(%p,%p)\n",iface,pClassID);
162 *pClassID = CLSID_FileMoniker;
167 /******************************************************************************
168 * FileMoniker_IsDirty
170 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
171 * method in the OLE-provided moniker interfaces always return S_FALSE because
172 * their internal state never changes.
174 static HRESULT WINAPI
175 FileMonikerImpl_IsDirty(IMoniker* iface)
178 TRACE("(%p)\n",iface);
183 /******************************************************************************
186 * this function locates and reads from the stream the filePath string
187 * written by FileMonikerImpl_Save
189 static HRESULT WINAPI
190 FileMonikerImpl_Load(IMoniker* iface, IStream* pStm)
193 CHAR* filePathA = NULL;
194 WCHAR* filePathW = NULL;
197 DWORD dwbuffer, bytesA, bytesW, len;
200 FileMonikerImpl *This = (FileMonikerImpl *)iface;
202 TRACE("(%p,%p)\n",iface,pStm);
204 /* first WORD must be 0 */
205 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
206 if (bread!=sizeof(WORD) || wbuffer!=0)
208 WARN("Couldn't read 0 word\n");
212 /* read filePath string length (plus one) */
213 res=IStream_Read(pStm,&bytesA,sizeof(DWORD),&bread);
214 if (bread != sizeof(DWORD))
216 WARN("Couldn't read file string length\n");
220 /* read filePath string */
221 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
228 res=IStream_Read(pStm,filePathA,bytesA,&bread);
231 WARN("Couldn't read file path string\n");
235 /* read the first constant */
236 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
237 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
239 WARN("Couldn't read 0xDEADFFFF constant\n");
245 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
246 if (bread!=sizeof(DWORD) || dwbuffer!=0)
248 WARN("Couldn't read 0 padding\n");
253 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
254 if (bread!=sizeof(DWORD))
257 if (!dwbuffer) /* No W-string */
260 len=MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, bytesA, NULL, 0);
264 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
270 MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, -1, filePathW, len+1);
279 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
280 if (bread!=sizeof(DWORD) || dwbuffer!=bytesW)
283 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
284 if (bread!=sizeof(WORD) || wbuffer!=0x3)
287 len=bytesW/sizeof(WCHAR);
288 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
294 res=IStream_Read(pStm,filePathW,bytesW,&bread);
301 HeapFree(GetProcessHeap(),0,filePathA);
302 HeapFree(GetProcessHeap(),0,This->filePathName);
303 This->filePathName=filePathW;
308 HeapFree(GetProcessHeap(), 0, filePathA);
309 HeapFree(GetProcessHeap(), 0, filePathW);
316 /******************************************************************************
319 * This function saves data of this object. In the beginning I thought
320 * that I have just to write the filePath string on Stream. But, when I
321 * tested this function with windows programs samples, I noticed that it
322 * was not the case. This implementation is based on XP SP2. Other versions
323 * of Windows have minor variations.
325 * Data which must be written on stream is:
326 * 1) WORD constant:zero
327 * 2) length of the path string ("\0" included)
328 * 3) path string type A
329 * 4) DWORD constant : 0xDEADFFFF
330 * 5) five DWORD constant: zero
331 * 6) If we're only writing the multibyte version,
332 * write a zero DWORD and finish.
334 * 7) DWORD: double-length of the the path string type W ("\0" not
336 * 8) WORD constant: 0x3
337 * 9) filePath unicode string.
340 static HRESULT WINAPI
341 FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
343 FileMonikerImpl *This = (FileMonikerImpl *)iface;
346 LPOLESTR filePathW=This->filePathName;
348 DWORD bytesA, bytesW, len;
350 static const DWORD DEADFFFF = 0xDEADFFFF; /* Constants */
351 static const DWORD ZERO = 0;
352 static const WORD THREE = 0x3;
355 BOOL bUsedDefault, bWriteWide;
357 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
363 res=IStream_Write(pStm,&ZERO,sizeof(WORD),NULL);
364 if (!SUCCEEDED(res)) return res;
366 /* write length of filePath string ( 0 included )*/
367 bytesA = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
368 res=IStream_Write(pStm,&bytesA,sizeof(DWORD),NULL);
369 if (!SUCCEEDED(res)) return res;
371 /* write A string (with '\0') */
372 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
374 return E_OUTOFMEMORY;
375 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, bytesA, NULL, &bUsedDefault);
376 res=IStream_Write(pStm,filePathA,bytesA,NULL);
377 HeapFree(GetProcessHeap(),0,filePathA);
378 if (!SUCCEEDED(res)) return res;
380 /* write a DWORD 0xDEADFFFF */
381 res=IStream_Write(pStm,&DEADFFFF,sizeof(DWORD),NULL);
382 if (!SUCCEEDED(res)) return res;
384 /* write 5 zero DWORDs */
387 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
388 if (!SUCCEEDED(res)) return res;
391 /* Write the wide version if:
392 * + couldn't convert to CP_ACP,
393 * or + it's a directory,
394 * or + there's a character > 0xFF
396 len = lstrlenW(filePathW);
397 bWriteWide = (bUsedDefault || (len > 0 && filePathW[len-1]=='\\' ));
401 for(pch=filePathW;*pch;++pch)
413 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
417 /* write bytes needed for the filepathW (without 0) + 6 */
418 bytesW = len*sizeof(WCHAR) + 6;
419 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
420 if (!SUCCEEDED(res)) return res;
422 /* try again, without the extra 6 */
424 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
425 if (!SUCCEEDED(res)) return res;
428 res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
429 if (!SUCCEEDED(res)) return res;
431 /* write W string (no 0) */
432 res=IStream_Write(pStm,filePathW,bytesW,NULL);
437 /******************************************************************************
438 * FileMoniker_GetSizeMax
440 static HRESULT WINAPI
441 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
443 FileMonikerImpl *This = (FileMonikerImpl *)iface;
445 TRACE("(%p,%p)\n",iface,pcbSize);
450 /* We could calculate exactly (see ...::Save()) but instead
451 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
453 pcbSize->u.LowPart = 0x38 + 4 * lstrlenW(This->filePathName);
454 pcbSize->u.HighPart = 0;
459 /******************************************************************************
460 * FileMoniker_Destroy (local function)
461 *******************************************************************************/
462 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
464 TRACE("(%p)\n",This);
466 if (This->pMarshal) IUnknown_Release(This->pMarshal);
467 HeapFree(GetProcessHeap(),0,This->filePathName);
468 HeapFree(GetProcessHeap(),0,This);
473 /******************************************************************************
474 * FileMoniker_BindToObject
476 static HRESULT WINAPI
477 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
478 REFIID riid, VOID** ppvResult)
483 IRunningObjectTable *prot=0;
485 IClassFactory *pcf=0;
486 IClassActivator *pca=0;
488 FileMonikerImpl *This = (FileMonikerImpl *)iface;
492 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
496 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
499 /* if the requested class was loaded before ! we don't need to reload it */
500 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
503 /* first activation of this class */
504 res=GetClassFile(This->filePathName,&clsID);
507 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
510 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
514 IUnknown_AddRef(pObj);
522 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
524 if (res==E_NOINTERFACE){
526 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
528 if (res==E_NOINTERFACE)
529 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
533 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
535 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
540 IUnknown_AddRef(pObj);
547 /*res=GetClassFile(This->filePathName,&clsID);
551 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
556 IUnknown_AddRef(pObj);
563 /* get the requested interface from the loaded class */
564 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
566 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
568 IUnknown_Release(pObj);
572 IRunningObjectTable_Release(prot);
575 IPersistFile_Release(ppf);
578 IClassActivator_Release(pca);
581 IClassFactory_Release(pcf);
586 /******************************************************************************
587 * FileMoniker_BindToStorage
589 static HRESULT WINAPI
590 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
591 REFIID riid, VOID** ppvObject)
597 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
599 if (pmkToLeft==NULL){
601 if (IsEqualIID(&IID_IStorage, riid)){
603 /* get the file name */
604 IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
606 /* verifie if the file contains a storage object */
607 res=StgIsStorageFile(filePath);
611 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
617 IStorage_AddRef(pstg);
622 CoTaskMemFree(filePath);
625 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
628 return E_NOINTERFACE;
632 FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
639 /******************************************************************************
641 ******************************************************************************/
642 static HRESULT WINAPI
643 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
644 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
646 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
648 if (ppmkReduced==NULL)
651 IMoniker_AddRef(iface);
655 return MK_S_REDUCED_TO_SELF;
658 /******************************************************************************
659 * FileMoniker_ComposeWith
661 static HRESULT WINAPI
662 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
663 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
666 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
667 static const WCHAR twoPoint[]={'.','.',0};
668 static const WCHAR bkSlash[]={'\\',0};
670 int i=0,j=0,lastIdx1=0,lastIdx2=0;
673 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
675 if (ppmkComposite==NULL)
683 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
685 /* check if we have two filemonikers to compose or not */
686 if(mkSys==MKSYS_FILEMONIKER){
688 CreateBindCtx(0,&bind);
690 IMoniker_GetDisplayName(iface,bind,NULL,&str1);
691 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
693 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
694 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
695 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
697 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
700 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
703 /* for etch "..\" in the left of str2 remove the right element from str1 */
704 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
709 /* the length of the composed path string is raised by the sum of the two paths lengths */
710 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
713 return E_OUTOFMEMORY;
715 /* new path is the concatenation of the rest of str1 and str2 */
716 for(*newStr=0,j=0;j<=lastIdx1;j++)
717 strcatW(newStr,strDec1[j]);
719 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
720 strcatW(newStr,bkSlash);
722 for(j=i;j<=lastIdx2;j++)
723 strcatW(newStr,strDec2[j]);
725 /* create a new moniker with the new string */
726 res=CreateFileMoniker(newStr,ppmkComposite);
728 /* free all strings space memory used by this function */
729 HeapFree(GetProcessHeap(),0,newStr);
731 for(i=0; strDec1[i]!=NULL;i++)
732 CoTaskMemFree(strDec1[i]);
733 for(i=0; strDec2[i]!=NULL;i++)
734 CoTaskMemFree(strDec2[i]);
735 CoTaskMemFree(strDec1);
736 CoTaskMemFree(strDec2);
743 else if(mkSys==MKSYS_ANTIMONIKER){
748 else if (fOnlyIfNotGeneric){
751 return MK_E_NEEDGENERIC;
755 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
758 /******************************************************************************
761 static HRESULT WINAPI
762 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
764 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
766 if (ppenumMoniker == NULL)
769 *ppenumMoniker = NULL;
774 /******************************************************************************
775 * FileMoniker_IsEqual
777 static HRESULT WINAPI
778 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
780 FileMonikerImpl *This = (FileMonikerImpl *)iface;
786 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
788 if (pmkOtherMoniker==NULL)
791 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
793 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
796 res = CreateBindCtx(0,&bind);
797 if (FAILED(res)) return res;
799 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
800 int result = lstrcmpiW(filePath, This->filePathName);
801 CoTaskMemFree(filePath);
802 if ( result == 0 ) return S_OK;
808 /******************************************************************************
811 static HRESULT WINAPI
812 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
814 FileMonikerImpl *This = (FileMonikerImpl *)iface;
816 int h = 0,i,skip,len;
823 val = This->filePathName;
827 for (i = len ; i > 0; i--) {
828 h = (h * 37) + val[off++];
831 /* only sample some characters */
833 for (i = len ; i > 0; i -= skip, off += skip) {
834 h = (h * 39) + val[off];
843 /******************************************************************************
844 * FileMoniker_IsRunning
846 static HRESULT WINAPI
847 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
848 IMoniker* pmkNewlyRunning)
850 IRunningObjectTable* rot;
853 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
855 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
861 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
866 res = IRunningObjectTable_IsRunning(rot,iface);
868 IRunningObjectTable_Release(rot);
873 /******************************************************************************
874 * FileMoniker_GetTimeOfLastChange
875 ******************************************************************************/
876 static HRESULT WINAPI
877 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
878 IMoniker* pmkToLeft, FILETIME* pFileTime)
880 FileMonikerImpl *This = (FileMonikerImpl *)iface;
881 IRunningObjectTable* rot;
883 WIN32_FILE_ATTRIBUTE_DATA info;
885 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
893 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
898 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
900 if (FAILED(res)){ /* the moniker is not registered */
902 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
903 return MK_E_NOOBJECT;
905 *pFileTime=info.ftLastWriteTime;
911 /******************************************************************************
912 * FileMoniker_Inverse
914 static HRESULT WINAPI
915 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
917 TRACE("(%p,%p)\n",iface,ppmk);
919 return CreateAntiMoniker(ppmk);
922 /******************************************************************************
923 * FileMoniker_CommonPrefixWith
925 static HRESULT WINAPI
926 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
929 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
932 ULONG nb1,nb2,i,sameIdx;
933 BOOL machimeNameCase=FALSE;
935 if (ppmkPrefix==NULL)
943 /* check if we have the same type of moniker */
944 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
946 if(mkSys==MKSYS_FILEMONIKER){
949 CreateBindCtx(0,&pbind);
951 /* create a string based on common part of the two paths */
953 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
954 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
956 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
957 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
959 if (nb1==0 || nb2==0)
960 return MK_E_NOPREFIX;
962 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
966 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
967 (stringTable2[sameIdx]!=NULL) &&
968 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
970 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
972 machimeNameCase=TRUE;
974 for(i=2;i<sameIdx;i++)
976 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
977 machimeNameCase=FALSE;
982 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
985 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
989 for(i=0;i<sameIdx;i++)
990 strcatW(commonPath,stringTable1[i]);
993 CoTaskMemFree(stringTable1[i]);
995 CoTaskMemFree(stringTable1);
998 CoTaskMemFree(stringTable2[i]);
1000 CoTaskMemFree(stringTable2);
1002 ret = CreateFileMoniker(commonPath,ppmkPrefix);
1004 HeapFree(GetProcessHeap(),0,commonPath);
1008 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1011 /******************************************************************************
1012 * DecomposePath (local function)
1014 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1016 static const WCHAR bSlash[] = {'\\',0};
1017 WCHAR word[MAX_PATH];
1018 int i=0,j,tabIndex=0;
1019 LPOLESTR *strgtable ;
1021 int len=lstrlenW(str);
1023 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1025 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1027 if (strgtable==NULL)
1028 return E_OUTOFMEMORY;
1032 if(str[i]==bSlash[0]){
1034 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1036 if (strgtable[tabIndex]==NULL)
1037 return E_OUTOFMEMORY;
1039 strcpyW(strgtable[tabIndex++],bSlash);
1046 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1051 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1053 if (strgtable[tabIndex]==NULL)
1054 return E_OUTOFMEMORY;
1056 strcpyW(strgtable[tabIndex++],word);
1059 strgtable[tabIndex]=NULL;
1061 *stringTable=strgtable;
1066 /******************************************************************************
1067 * FileMoniker_RelativePathTo
1069 static HRESULT WINAPI
1070 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1074 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1075 DWORD len1=0,len2=0,sameIdx=0,j=0;
1076 static const WCHAR back[] ={'.','.','\\',0};
1078 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1080 if (ppmkRelPath==NULL)
1084 return E_INVALIDARG;
1086 res=CreateBindCtx(0,&bind);
1090 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1093 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1097 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1098 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1100 if (FAILED(len1) || FAILED(len2))
1101 return E_OUTOFMEMORY;
1103 /* count the number of similar items from the begin of the two paths */
1104 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1105 (tabStr2[sameIdx]!=NULL) &&
1106 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1108 /* begin the construction of relativePath */
1109 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1110 /* by "..\\" in the begin */
1111 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1115 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1116 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1117 if (*tabStr1[j]!='\\')
1118 strcatW(relPath,back);
1120 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1121 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1122 strcatW(relPath,tabStr2[j]);
1124 res=CreateFileMoniker(relPath,ppmkRelPath);
1126 for(j=0; tabStr1[j]!=NULL;j++)
1127 CoTaskMemFree(tabStr1[j]);
1128 for(j=0; tabStr2[j]!=NULL;j++)
1129 CoTaskMemFree(tabStr2[j]);
1130 CoTaskMemFree(tabStr1);
1131 CoTaskMemFree(tabStr2);
1132 CoTaskMemFree(str1);
1133 CoTaskMemFree(str2);
1134 HeapFree(GetProcessHeap(),0,relPath);
1136 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1142 /******************************************************************************
1143 * FileMoniker_GetDisplayName
1145 static HRESULT WINAPI
1146 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1147 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1149 FileMonikerImpl *This = (FileMonikerImpl *)iface;
1151 int len=lstrlenW(This->filePathName);
1153 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1155 if (ppszDisplayName==NULL)
1158 if (pmkToLeft!=NULL)
1159 return E_INVALIDARG;
1161 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1162 if (*ppszDisplayName==NULL)
1163 return E_OUTOFMEMORY;
1165 strcpyW(*ppszDisplayName,This->filePathName);
1167 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1172 /******************************************************************************
1173 * FileMoniker_ParseDisplayName
1175 static HRESULT WINAPI
1176 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1177 LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1179 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1183 /******************************************************************************
1184 * FileMoniker_IsSystemMoniker
1186 static HRESULT WINAPI
1187 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1189 TRACE("(%p,%p)\n",iface,pwdMksys);
1194 (*pwdMksys)=MKSYS_FILEMONIKER;
1199 /*******************************************************************************
1200 * FileMonikerIROTData_QueryInterface
1202 static HRESULT WINAPI
1203 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1206 IMoniker *This = impl_from_IROTData(iface);
1208 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1210 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1213 /***********************************************************************
1214 * FileMonikerIROTData_AddRef
1217 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1219 IMoniker *This = impl_from_IROTData(iface);
1221 TRACE("(%p)\n",This);
1223 return IMoniker_AddRef(This);
1226 /***********************************************************************
1227 * FileMonikerIROTData_Release
1230 FileMonikerROTDataImpl_Release(IROTData* iface)
1232 IMoniker *This = impl_from_IROTData(iface);
1234 TRACE("(%p)\n",This);
1236 return FileMonikerImpl_Release(This);
1239 /******************************************************************************
1240 * FileMonikerIROTData_GetComparaisonData
1242 static HRESULT WINAPI
1243 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1244 ULONG cbMax, ULONG* pcbData)
1246 IMoniker *This = impl_from_IROTData(iface);
1247 FileMonikerImpl *This1 = (FileMonikerImpl *)This;
1248 int len = (strlenW(This1->filePathName)+1);
1252 TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
1254 *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1255 if (cbMax < *pcbData)
1256 return E_OUTOFMEMORY;
1258 memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1259 pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1260 for (i = 0; i < len; i++)
1261 pszFileName[i] = toupperW(This1->filePathName[i]);
1267 * Virtual function table for the FileMonikerImpl class which include IPersist,
1268 * IPersistStream and IMoniker functions.
1270 static const IMonikerVtbl VT_FileMonikerImpl =
1272 FileMonikerImpl_QueryInterface,
1273 FileMonikerImpl_AddRef,
1274 FileMonikerImpl_Release,
1275 FileMonikerImpl_GetClassID,
1276 FileMonikerImpl_IsDirty,
1277 FileMonikerImpl_Load,
1278 FileMonikerImpl_Save,
1279 FileMonikerImpl_GetSizeMax,
1280 FileMonikerImpl_BindToObject,
1281 FileMonikerImpl_BindToStorage,
1282 FileMonikerImpl_Reduce,
1283 FileMonikerImpl_ComposeWith,
1284 FileMonikerImpl_Enum,
1285 FileMonikerImpl_IsEqual,
1286 FileMonikerImpl_Hash,
1287 FileMonikerImpl_IsRunning,
1288 FileMonikerImpl_GetTimeOfLastChange,
1289 FileMonikerImpl_Inverse,
1290 FileMonikerImpl_CommonPrefixWith,
1291 FileMonikerImpl_RelativePathTo,
1292 FileMonikerImpl_GetDisplayName,
1293 FileMonikerImpl_ParseDisplayName,
1294 FileMonikerImpl_IsSystemMoniker
1297 /* Virtual function table for the IROTData class. */
1298 static const IROTDataVtbl VT_ROTDataImpl =
1300 FileMonikerROTDataImpl_QueryInterface,
1301 FileMonikerROTDataImpl_AddRef,
1302 FileMonikerROTDataImpl_Release,
1303 FileMonikerROTDataImpl_GetComparisonData
1306 /******************************************************************************
1307 * FileMoniker_Construct (local function)
1309 static HRESULT WINAPI
1310 FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1313 int sizeStr=lstrlenW(lpszPathName);
1315 static const WCHAR twoPoint[]={'.','.',0};
1316 static const WCHAR bkSlash[]={'\\',0};
1319 TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1321 /* Initialize the virtual fgunction table. */
1322 This->lpvtbl1 = &VT_FileMonikerImpl;
1323 This->lpvtbl2 = &VT_ROTDataImpl;
1325 This->pMarshal = NULL;
1327 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1329 if (This->filePathName==NULL)
1330 return E_OUTOFMEMORY;
1332 strcpyW(This->filePathName,lpszPathName);
1334 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1339 if (lstrcmpW(tabStr[0],twoPoint)!=0)
1344 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1350 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1358 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1361 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1363 *This->filePathName=0;
1365 for(i=0;tabStr[i]!=NULL;i++)
1366 strcatW(This->filePathName,tabStr[i]);
1369 strcatW(This->filePathName,bkSlash);
1372 for(i=0; tabStr[i]!=NULL;i++)
1373 CoTaskMemFree(tabStr[i]);
1374 CoTaskMemFree(tabStr);
1379 /******************************************************************************
1380 * CreateFileMoniker (OLE32.@)
1381 ******************************************************************************/
1382 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1384 FileMonikerImpl* newFileMoniker;
1387 TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1397 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1399 if (!newFileMoniker)
1400 return E_OUTOFMEMORY;
1402 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1405 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&IID_IMoniker,(void**)ppmk);
1407 HeapFree(GetProcessHeap(),0,newFileMoniker);
1412 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1413 REFIID riid, LPVOID *ppv)
1416 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1419 IUnknown_AddRef(iface);
1422 return E_NOINTERFACE;
1425 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1427 return 2; /* non-heap based object */
1430 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1432 return 1; /* non-heap based object */
1435 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1436 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1438 FileMonikerImpl* newFileMoniker;
1440 static const WCHAR wszEmpty[] = { 0 };
1442 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1447 return CLASS_E_NOAGGREGATION;
1449 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1450 if (!newFileMoniker)
1451 return E_OUTOFMEMORY;
1453 hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1456 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker, riid, ppv);
1458 HeapFree(GetProcessHeap(),0,newFileMoniker);
1463 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1465 FIXME("(%d), stub!\n",fLock);
1469 static const IClassFactoryVtbl FileMonikerCFVtbl =
1471 FileMonikerCF_QueryInterface,
1472 FileMonikerCF_AddRef,
1473 FileMonikerCF_Release,
1474 FileMonikerCF_CreateInstance,
1475 FileMonikerCF_LockServer
1477 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1479 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1481 return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);