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.*/
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 = &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)
425 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
429 /* write bytes needed for the filepathW (without 0) + 6 */
430 bytesW = len*sizeof(WCHAR) + 6;
431 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
432 if (FAILED(res)) return res;
434 /* try again, without the extra 6 */
436 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
437 if (FAILED(res)) return res;
440 res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
441 if (FAILED(res)) return res;
443 /* write W string (no 0) */
444 res=IStream_Write(pStm,filePathW,bytesW,NULL);
449 /******************************************************************************
450 * FileMoniker_GetSizeMax
452 static HRESULT WINAPI
453 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
455 FileMonikerImpl *This = (FileMonikerImpl *)iface;
457 TRACE("(%p,%p)\n",iface,pcbSize);
462 /* We could calculate exactly (see ...::Save()) but instead
463 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
465 pcbSize->u.LowPart = 0x38 + 4 * lstrlenW(This->filePathName);
466 pcbSize->u.HighPart = 0;
471 /******************************************************************************
472 * FileMoniker_Destroy (local function)
473 *******************************************************************************/
474 HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* This)
476 TRACE("(%p)\n",This);
478 if (This->pMarshal) IUnknown_Release(This->pMarshal);
479 HeapFree(GetProcessHeap(),0,This->filePathName);
480 HeapFree(GetProcessHeap(),0,This);
485 /******************************************************************************
486 * FileMoniker_BindToObject
488 static HRESULT WINAPI
489 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
490 REFIID riid, VOID** ppvResult)
495 IRunningObjectTable *prot=0;
497 IClassFactory *pcf=0;
498 IClassActivator *pca=0;
500 FileMonikerImpl *This = (FileMonikerImpl *)iface;
504 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
508 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
511 /* if the requested class was loaded before ! we don't need to reload it */
512 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
515 /* first activation of this class */
516 res=GetClassFile(This->filePathName,&clsID);
519 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
522 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
526 IUnknown_AddRef(pObj);
534 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
536 if (res==E_NOINTERFACE){
538 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
540 if (res==E_NOINTERFACE)
541 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
545 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)&ppf);
547 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
552 IUnknown_AddRef(pObj);
559 /*res=GetClassFile(This->filePathName,&clsID);
563 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
568 IUnknown_AddRef(pObj);
575 /* get the requested interface from the loaded class */
576 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
578 IBindCtx_RegisterObjectBound(pbc,*ppvResult);
580 IUnknown_Release(pObj);
584 IRunningObjectTable_Release(prot);
587 IPersistFile_Release(ppf);
590 IClassActivator_Release(pca);
593 IClassFactory_Release(pcf);
598 /******************************************************************************
599 * FileMoniker_BindToStorage
601 static HRESULT WINAPI
602 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
603 REFIID riid, VOID** ppvObject)
609 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
611 if (pmkToLeft==NULL){
613 if (IsEqualIID(&IID_IStorage, riid)){
615 /* get the file name */
616 IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
618 /* verify if the file contains a storage object */
619 res=StgIsStorageFile(filePath);
623 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
629 IStorage_AddRef(pstg);
634 CoTaskMemFree(filePath);
637 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
640 return E_NOINTERFACE;
644 FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
651 /******************************************************************************
653 ******************************************************************************/
654 static HRESULT WINAPI
655 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
656 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
658 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
660 if (ppmkReduced==NULL)
663 IMoniker_AddRef(iface);
667 return MK_S_REDUCED_TO_SELF;
670 /******************************************************************************
671 * FileMoniker_ComposeWith
673 static HRESULT WINAPI
674 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
675 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
678 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
679 static const WCHAR twoPoint[]={'.','.',0};
680 static const WCHAR bkSlash[]={'\\',0};
682 int i=0,j=0,lastIdx1=0,lastIdx2=0;
685 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
687 if (ppmkComposite==NULL)
695 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
697 /* check if we have two FileMonikers to compose or not */
698 if(mkSys==MKSYS_FILEMONIKER){
700 CreateBindCtx(0,&bind);
702 IMoniker_GetDisplayName(iface,bind,NULL,&str1);
703 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
705 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
706 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
707 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
709 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
712 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
715 /* for etch "..\" in the left of str2 remove the right element from str1 */
716 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
721 /* the length of the composed path string is raised by the sum of the two paths lengths */
722 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
725 return E_OUTOFMEMORY;
727 /* new path is the concatenation of the rest of str1 and str2 */
728 for(*newStr=0,j=0;j<=lastIdx1;j++)
729 strcatW(newStr,strDec1[j]);
731 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
732 strcatW(newStr,bkSlash);
734 for(j=i;j<=lastIdx2;j++)
735 strcatW(newStr,strDec2[j]);
737 /* create a new moniker with the new string */
738 res=CreateFileMoniker(newStr,ppmkComposite);
740 /* free all strings space memory used by this function */
741 HeapFree(GetProcessHeap(),0,newStr);
743 for(i=0; strDec1[i]!=NULL;i++)
744 CoTaskMemFree(strDec1[i]);
745 for(i=0; strDec2[i]!=NULL;i++)
746 CoTaskMemFree(strDec2[i]);
747 CoTaskMemFree(strDec1);
748 CoTaskMemFree(strDec2);
755 else if(mkSys==MKSYS_ANTIMONIKER){
760 else if (fOnlyIfNotGeneric){
763 return MK_E_NEEDGENERIC;
767 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
770 /******************************************************************************
773 static HRESULT WINAPI
774 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
776 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
778 if (ppenumMoniker == NULL)
781 *ppenumMoniker = NULL;
786 /******************************************************************************
787 * FileMoniker_IsEqual
789 static HRESULT WINAPI
790 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
792 FileMonikerImpl *This = (FileMonikerImpl *)iface;
798 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
800 if (pmkOtherMoniker==NULL)
803 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
805 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
808 res = CreateBindCtx(0,&bind);
809 if (FAILED(res)) return res;
812 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
813 if (!lstrcmpiW(filePath, This->filePathName))
815 CoTaskMemFree(filePath);
818 IBindCtx_Release(bind);
822 /******************************************************************************
825 static HRESULT WINAPI
826 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
828 FileMonikerImpl *This = (FileMonikerImpl *)iface;
830 int h = 0,i,skip,len;
837 val = This->filePathName;
841 for (i = len ; i > 0; i--) {
842 h = (h * 37) + val[off++];
845 /* only sample some characters */
847 for (i = len ; i > 0; i -= skip, off += skip) {
848 h = (h * 39) + val[off];
857 /******************************************************************************
858 * FileMoniker_IsRunning
860 static HRESULT WINAPI
861 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
862 IMoniker* pmkNewlyRunning)
864 IRunningObjectTable* rot;
867 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
869 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
875 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
880 res = IRunningObjectTable_IsRunning(rot,iface);
882 IRunningObjectTable_Release(rot);
887 /******************************************************************************
888 * FileMoniker_GetTimeOfLastChange
889 ******************************************************************************/
890 static HRESULT WINAPI
891 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
892 IMoniker* pmkToLeft, FILETIME* pFileTime)
894 FileMonikerImpl *This = (FileMonikerImpl *)iface;
895 IRunningObjectTable* rot;
897 WIN32_FILE_ATTRIBUTE_DATA info;
899 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
907 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
912 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
914 if (FAILED(res)){ /* the moniker is not registered */
916 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
917 return MK_E_NOOBJECT;
919 *pFileTime=info.ftLastWriteTime;
925 /******************************************************************************
926 * FileMoniker_Inverse
928 static HRESULT WINAPI
929 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
931 TRACE("(%p,%p)\n",iface,ppmk);
933 return CreateAntiMoniker(ppmk);
936 /******************************************************************************
937 * FileMoniker_CommonPrefixWith
939 static HRESULT WINAPI
940 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
943 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
946 ULONG nb1,nb2,i,sameIdx;
947 BOOL machimeNameCase=FALSE;
949 if (ppmkPrefix==NULL)
957 /* check if we have the same type of moniker */
958 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
960 if(mkSys==MKSYS_FILEMONIKER){
963 CreateBindCtx(0,&pbind);
965 /* create a string based on common part of the two paths */
967 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
968 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
970 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
971 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
973 if (nb1==0 || nb2==0)
974 return MK_E_NOPREFIX;
976 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
980 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
981 (stringTable2[sameIdx]!=NULL) &&
982 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
984 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
986 machimeNameCase=TRUE;
988 for(i=2;i<sameIdx;i++)
990 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
991 machimeNameCase=FALSE;
996 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
999 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1000 ret = MK_E_NOPREFIX;
1003 for(i=0;i<sameIdx;i++)
1004 strcatW(commonPath,stringTable1[i]);
1007 CoTaskMemFree(stringTable1[i]);
1009 CoTaskMemFree(stringTable1);
1012 CoTaskMemFree(stringTable2[i]);
1014 CoTaskMemFree(stringTable2);
1016 ret = CreateFileMoniker(commonPath,ppmkPrefix);
1018 HeapFree(GetProcessHeap(),0,commonPath);
1022 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1025 /******************************************************************************
1026 * DecomposePath (local function)
1028 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1030 static const WCHAR bSlash[] = {'\\',0};
1032 int i=0,j,tabIndex=0, ret=0;
1033 LPOLESTR *strgtable ;
1035 int len=lstrlenW(str);
1037 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1039 strgtable = CoTaskMemAlloc(len*sizeof(WCHAR));
1041 if (strgtable==NULL)
1042 return E_OUTOFMEMORY;
1044 word = CoTaskMemAlloc((len + 1)*sizeof(WCHAR));
1048 ret = E_OUTOFMEMORY;
1054 if(str[i]==bSlash[0]){
1056 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1058 if (strgtable[tabIndex]==NULL)
1060 ret = E_OUTOFMEMORY;
1064 strcpyW(strgtable[tabIndex++],bSlash);
1071 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1076 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1078 if (strgtable[tabIndex]==NULL)
1080 ret = E_OUTOFMEMORY;
1084 strcpyW(strgtable[tabIndex++],word);
1087 strgtable[tabIndex]=NULL;
1089 *stringTable=strgtable;
1096 for (i = 0; i < tabIndex; i++)
1097 CoTaskMemFree(strgtable[i]);
1099 CoTaskMemFree(strgtable);
1103 CoTaskMemFree(word);
1108 /******************************************************************************
1109 * FileMoniker_RelativePathTo
1111 static HRESULT WINAPI
1112 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1116 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1117 DWORD len1=0,len2=0,sameIdx=0,j=0;
1118 static const WCHAR back[] ={'.','.','\\',0};
1120 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1122 if (ppmkRelPath==NULL)
1126 return E_INVALIDARG;
1128 res=CreateBindCtx(0,&bind);
1132 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1135 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1139 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1140 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1142 if (FAILED(len1) || FAILED(len2))
1143 return E_OUTOFMEMORY;
1145 /* count the number of similar items from the begin of the two paths */
1146 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1147 (tabStr2[sameIdx]!=NULL) &&
1148 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1150 /* begin the construction of relativePath */
1151 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1152 /* by "..\\" in the begin */
1153 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1157 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1158 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1159 if (*tabStr1[j]!='\\')
1160 strcatW(relPath,back);
1162 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1163 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1164 strcatW(relPath,tabStr2[j]);
1166 res=CreateFileMoniker(relPath,ppmkRelPath);
1168 for(j=0; tabStr1[j]!=NULL;j++)
1169 CoTaskMemFree(tabStr1[j]);
1170 for(j=0; tabStr2[j]!=NULL;j++)
1171 CoTaskMemFree(tabStr2[j]);
1172 CoTaskMemFree(tabStr1);
1173 CoTaskMemFree(tabStr2);
1174 CoTaskMemFree(str1);
1175 CoTaskMemFree(str2);
1176 HeapFree(GetProcessHeap(),0,relPath);
1178 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1184 /******************************************************************************
1185 * FileMoniker_GetDisplayName
1187 static HRESULT WINAPI
1188 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1189 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1191 FileMonikerImpl *This = (FileMonikerImpl *)iface;
1193 int len=lstrlenW(This->filePathName);
1195 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1197 if (ppszDisplayName==NULL)
1200 if (pmkToLeft!=NULL)
1201 return E_INVALIDARG;
1203 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1204 if (*ppszDisplayName==NULL)
1205 return E_OUTOFMEMORY;
1207 strcpyW(*ppszDisplayName,This->filePathName);
1209 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1214 /******************************************************************************
1215 * FileMoniker_ParseDisplayName
1217 static HRESULT WINAPI
1218 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1219 LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1221 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1225 /******************************************************************************
1226 * FileMoniker_IsSystemMoniker
1228 static HRESULT WINAPI
1229 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1231 TRACE("(%p,%p)\n",iface,pwdMksys);
1236 (*pwdMksys)=MKSYS_FILEMONIKER;
1241 /*******************************************************************************
1242 * FileMonikerIROTData_QueryInterface
1244 static HRESULT WINAPI
1245 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1248 IMoniker *This = impl_from_IROTData(iface);
1250 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1252 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1255 /***********************************************************************
1256 * FileMonikerIROTData_AddRef
1259 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1261 IMoniker *This = impl_from_IROTData(iface);
1263 TRACE("(%p)\n",This);
1265 return IMoniker_AddRef(This);
1268 /***********************************************************************
1269 * FileMonikerIROTData_Release
1272 FileMonikerROTDataImpl_Release(IROTData* iface)
1274 IMoniker *This = impl_from_IROTData(iface);
1276 TRACE("(%p)\n",This);
1278 return FileMonikerImpl_Release(This);
1281 /******************************************************************************
1282 * FileMonikerIROTData_GetComparisonData
1284 static HRESULT WINAPI
1285 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1286 ULONG cbMax, ULONG* pcbData)
1288 IMoniker *This = impl_from_IROTData(iface);
1289 FileMonikerImpl *This1 = (FileMonikerImpl *)This;
1290 int len = (strlenW(This1->filePathName)+1);
1294 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1296 *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1297 if (cbMax < *pcbData)
1298 return E_OUTOFMEMORY;
1300 memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1301 pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1302 for (i = 0; i < len; i++)
1303 pszFileName[i] = toupperW(This1->filePathName[i]);
1309 * Virtual function table for the FileMonikerImpl class which include IPersist,
1310 * IPersistStream and IMoniker functions.
1312 static const IMonikerVtbl VT_FileMonikerImpl =
1314 FileMonikerImpl_QueryInterface,
1315 FileMonikerImpl_AddRef,
1316 FileMonikerImpl_Release,
1317 FileMonikerImpl_GetClassID,
1318 FileMonikerImpl_IsDirty,
1319 FileMonikerImpl_Load,
1320 FileMonikerImpl_Save,
1321 FileMonikerImpl_GetSizeMax,
1322 FileMonikerImpl_BindToObject,
1323 FileMonikerImpl_BindToStorage,
1324 FileMonikerImpl_Reduce,
1325 FileMonikerImpl_ComposeWith,
1326 FileMonikerImpl_Enum,
1327 FileMonikerImpl_IsEqual,
1328 FileMonikerImpl_Hash,
1329 FileMonikerImpl_IsRunning,
1330 FileMonikerImpl_GetTimeOfLastChange,
1331 FileMonikerImpl_Inverse,
1332 FileMonikerImpl_CommonPrefixWith,
1333 FileMonikerImpl_RelativePathTo,
1334 FileMonikerImpl_GetDisplayName,
1335 FileMonikerImpl_ParseDisplayName,
1336 FileMonikerImpl_IsSystemMoniker
1339 /* Virtual function table for the IROTData class. */
1340 static const IROTDataVtbl VT_ROTDataImpl =
1342 FileMonikerROTDataImpl_QueryInterface,
1343 FileMonikerROTDataImpl_AddRef,
1344 FileMonikerROTDataImpl_Release,
1345 FileMonikerROTDataImpl_GetComparisonData
1348 /******************************************************************************
1349 * FileMoniker_Construct (local function)
1351 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1354 int sizeStr=lstrlenW(lpszPathName);
1356 static const WCHAR twoPoint[]={'.','.',0};
1357 static const WCHAR bkSlash[]={'\\',0};
1360 TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1362 /* Initialize the virtual function table. */
1363 This->lpvtbl1 = &VT_FileMonikerImpl;
1364 This->lpvtbl2 = &VT_ROTDataImpl;
1366 This->pMarshal = NULL;
1368 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1370 if (This->filePathName==NULL)
1371 return E_OUTOFMEMORY;
1373 strcpyW(This->filePathName,lpszPathName);
1375 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1380 if (lstrcmpW(tabStr[0],twoPoint)!=0)
1385 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1391 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1399 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1402 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1404 *This->filePathName=0;
1406 for(i=0;tabStr[i]!=NULL;i++)
1407 strcatW(This->filePathName,tabStr[i]);
1410 strcatW(This->filePathName,bkSlash);
1413 for(i=0; tabStr[i]!=NULL;i++)
1414 CoTaskMemFree(tabStr[i]);
1415 CoTaskMemFree(tabStr);
1420 /******************************************************************************
1421 * CreateFileMoniker (OLE32.@)
1422 ******************************************************************************/
1423 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1425 FileMonikerImpl* newFileMoniker;
1428 TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1438 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1440 if (!newFileMoniker)
1441 return E_OUTOFMEMORY;
1443 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1446 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&IID_IMoniker,(void**)ppmk);
1448 HeapFree(GetProcessHeap(),0,newFileMoniker);
1453 /* find a character from a set in reverse without the string having to be null-terminated */
1454 static inline WCHAR *memrpbrkW(const WCHAR *ptr, size_t n, const WCHAR *accept)
1456 const WCHAR *end, *ret = NULL;
1457 for (end = ptr + n; ptr < end; ptr++) if (strchrW(accept, *ptr)) ret = ptr;
1458 return (WCHAR *)ret;
1461 HRESULT FileMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1462 LPDWORD pchEaten, LPMONIKER *ppmk)
1465 static const WCHAR wszSeparators[] = {':','\\','/','!',0};
1467 for (end = szDisplayName + strlenW(szDisplayName);
1468 end && (end != szDisplayName);
1469 end = memrpbrkW(szDisplayName, end - szDisplayName, wszSeparators))
1472 IRunningObjectTable *rot;
1473 IMoniker *file_moniker;
1474 LPWSTR file_display_name;
1475 LPWSTR full_path_name;
1476 DWORD full_path_name_len;
1477 int len = end - szDisplayName;
1479 file_display_name = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1480 if (!file_display_name) return E_OUTOFMEMORY;
1481 memcpy(file_display_name, szDisplayName, len * sizeof(WCHAR));
1482 file_display_name[len] = '\0';
1484 hr = CreateFileMoniker(file_display_name, &file_moniker);
1487 HeapFree(GetProcessHeap(), 0, file_display_name);
1491 hr = IBindCtx_GetRunningObjectTable(pbc, &rot);
1494 HeapFree(GetProcessHeap(), 0, file_display_name);
1495 IMoniker_Release(file_moniker);
1499 hr = IRunningObjectTable_IsRunning(rot, file_moniker);
1500 IRunningObjectTable_Release(rot);
1503 HeapFree(GetProcessHeap(), 0, file_display_name);
1504 IMoniker_Release(file_moniker);
1509 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name));
1511 *ppmk = file_moniker;
1512 HeapFree(GetProcessHeap(), 0, file_display_name);
1516 full_path_name_len = GetFullPathNameW(file_display_name, 0, NULL, NULL);
1517 if (!full_path_name_len)
1519 HeapFree(GetProcessHeap(), 0, file_display_name);
1520 IMoniker_Release(file_moniker);
1523 full_path_name = HeapAlloc(GetProcessHeap(), 0, full_path_name_len * sizeof(WCHAR));
1524 if (!full_path_name)
1526 HeapFree(GetProcessHeap(), 0, file_display_name);
1527 IMoniker_Release(file_moniker);
1528 return E_OUTOFMEMORY;
1530 GetFullPathNameW(file_display_name, full_path_name_len, full_path_name, NULL);
1532 if (GetFileAttributesW(full_path_name) == INVALID_FILE_ATTRIBUTES)
1533 TRACE("couldn't open file %s\n", debugstr_w(full_path_name));
1536 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName));
1538 *ppmk = file_moniker;
1539 HeapFree(GetProcessHeap(), 0, file_display_name);
1540 HeapFree(GetProcessHeap(), 0, full_path_name);
1543 HeapFree(GetProcessHeap(), 0, file_display_name);
1544 HeapFree(GetProcessHeap(), 0, full_path_name);
1545 IMoniker_Release(file_moniker);
1548 return MK_E_CANTOPENFILE;
1552 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1553 REFIID riid, LPVOID *ppv)
1556 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1559 IUnknown_AddRef(iface);
1562 return E_NOINTERFACE;
1565 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1567 return 2; /* non-heap based object */
1570 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1572 return 1; /* non-heap based object */
1575 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1576 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1578 FileMonikerImpl* newFileMoniker;
1580 static const WCHAR wszEmpty[] = { 0 };
1582 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1587 return CLASS_E_NOAGGREGATION;
1589 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1590 if (!newFileMoniker)
1591 return E_OUTOFMEMORY;
1593 hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1596 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker, riid, ppv);
1598 HeapFree(GetProcessHeap(),0,newFileMoniker);
1603 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1605 FIXME("(%d), stub!\n",fLock);
1609 static const IClassFactoryVtbl FileMonikerCFVtbl =
1611 FileMonikerCF_QueryInterface,
1612 FileMonikerCF_AddRef,
1613 FileMonikerCF_Release,
1614 FileMonikerCF_CreateInstance,
1615 FileMonikerCF_LockServer
1617 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1619 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1621 return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);