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
{
45 IMoniker IMoniker_iface
;
46 IROTData IROTData_iface
;
48 LPOLESTR filePathName
; /* path string identified by this filemoniker */
49 IUnknown
*pMarshal
; /* custom marshaler */
52 static inline FileMonikerImpl
*impl_from_IMoniker(IMoniker
*iface
)
54 return CONTAINING_RECORD(iface
, FileMonikerImpl
, IMoniker_iface
);
57 static inline FileMonikerImpl
*impl_from_IROTData(IROTData
*iface
)
59 return CONTAINING_RECORD(iface
, FileMonikerImpl
, IROTData_iface
);
62 /* Local function used by filemoniker implementation */
63 static HRESULT
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
64 static HRESULT
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
66 /*******************************************************************************
67 * FileMoniker_QueryInterface
70 FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
72 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
74 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
76 /* Perform a sanity check on the parameters.*/
80 /* Initialize the return parameter */
83 /* Compare the riid with the interface IDs implemented by this object.*/
84 if (IsEqualIID(&IID_IUnknown
, riid
) ||
85 IsEqualIID(&IID_IPersist
, riid
) ||
86 IsEqualIID(&IID_IPersistStream
,riid
) ||
87 IsEqualIID(&IID_IMoniker
, riid
)
91 else if (IsEqualIID(&IID_IROTData
, riid
))
92 *ppvObject
= &This
->IROTData_iface
;
93 else if (IsEqualIID(&IID_IMarshal
, riid
))
97 hr
= MonikerMarshal_Create(iface
, &This
->pMarshal
);
100 return IUnknown_QueryInterface(This
->pMarshal
, riid
, ppvObject
);
103 /* Check that we obtained an interface.*/
105 return E_NOINTERFACE
;
107 /* Query Interface always increases the reference count by one when it is successful */
108 IMoniker_AddRef(iface
);
113 /******************************************************************************
117 FileMonikerImpl_AddRef(IMoniker
* iface
)
119 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
121 TRACE("(%p)\n",iface
);
123 return InterlockedIncrement(&This
->ref
);
126 /******************************************************************************
127 * FileMoniker_Release
130 FileMonikerImpl_Release(IMoniker
* iface
)
132 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
135 TRACE("(%p)\n",iface
);
137 ref
= InterlockedDecrement(&This
->ref
);
139 /* destroy the object if there's no more reference on it */
140 if (ref
== 0) FileMonikerImpl_Destroy(This
);
145 /******************************************************************************
146 * FileMoniker_GetClassID
148 static HRESULT WINAPI
149 FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
)
151 TRACE("(%p,%p)\n",iface
,pClassID
);
156 *pClassID
= CLSID_FileMoniker
;
161 /******************************************************************************
162 * FileMoniker_IsDirty
164 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
165 * method in the OLE-provided moniker interfaces always return S_FALSE because
166 * their internal state never changes.
168 static HRESULT WINAPI
169 FileMonikerImpl_IsDirty(IMoniker
* iface
)
172 TRACE("(%p)\n",iface
);
177 /******************************************************************************
180 * this function locates and reads from the stream the filePath string
181 * written by FileMonikerImpl_Save
183 static HRESULT WINAPI
184 FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
)
186 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
188 CHAR
* filePathA
= NULL
;
189 WCHAR
* filePathW
= NULL
;
192 DWORD dwbuffer
, bytesA
, bytesW
, len
;
196 TRACE("(%p,%p)\n",iface
,pStm
);
199 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
200 if (bread
!=sizeof(WORD
))
202 WARN("Couldn't read 0 word\n");
206 /* read filePath string length (plus one) */
207 res
=IStream_Read(pStm
,&bytesA
,sizeof(DWORD
),&bread
);
208 if (bread
!= sizeof(DWORD
))
210 WARN("Couldn't read file string length\n");
214 /* read filePath string */
215 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
222 res
=IStream_Read(pStm
,filePathA
,bytesA
,&bread
);
225 WARN("Couldn't read file path string\n");
229 /* read the unknown value */
230 IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
231 if (bread
!= sizeof(WORD
))
233 WARN("Couldn't read unknown value\n");
237 /* read the DEAD constant */
238 IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
239 if (bread
!= sizeof(WORD
))
241 WARN("Couldn't read DEAD constant\n");
247 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
248 if (bread
!=sizeof(DWORD
))
250 WARN("Couldn't read 0 padding\n");
255 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
256 if (bread
!=sizeof(DWORD
))
259 if (!dwbuffer
) /* No W-string */
262 len
=MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, bytesA
, NULL
, 0);
266 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
272 MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, -1, filePathW
, len
+1);
281 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
282 if (bread
!=sizeof(DWORD
) || dwbuffer
!=bytesW
)
285 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
286 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
289 len
=bytesW
/sizeof(WCHAR
);
290 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
296 res
=IStream_Read(pStm
,filePathW
,bytesW
,&bread
);
303 HeapFree(GetProcessHeap(),0,filePathA
);
304 HeapFree(GetProcessHeap(),0,This
->filePathName
);
305 This
->filePathName
=filePathW
;
310 HeapFree(GetProcessHeap(), 0, filePathA
);
311 HeapFree(GetProcessHeap(), 0, filePathW
);
318 /******************************************************************************
321 * This function saves data of this object. In the beginning I thought
322 * that I have just to write the filePath string on Stream. But, when I
323 * tested this function with windows program samples, I noticed that it
324 * was not the case. This implementation is based on XP SP2. Other versions
325 * of Windows have minor variations.
327 * Data which must be written on stream is:
328 * 1) WORD constant: zero (not validated by Windows)
329 * 2) length of the path string ("\0" included)
330 * 3) path string type A
331 * 4) Unknown WORD value: Frequently 0xFFFF, but not always. If set very large,
332 * Windows returns E_OUTOFMEMORY
333 * 5) WORD Constant: 0xDEAD (not validated by Windows)
334 * 6) five DWORD constant: zero (not validated by Windows)
335 * 7) If we're only writing the multibyte version,
336 * write a zero DWORD and finish.
338 * 8) DWORD: double-length of the path string type W ("\0" not
340 * 9) WORD constant: 0x3
341 * 10) filePath unicode string.
344 static HRESULT WINAPI
345 FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
)
347 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
349 LPOLESTR filePathW
=This
->filePathName
;
351 DWORD bytesA
, bytesW
, len
;
353 static const WORD FFFF
= 0xFFFF; /* Constants */
354 static const WORD DEAD
= 0xDEAD;
355 static const DWORD ZERO
= 0;
356 static const WORD THREE
= 0x3;
359 BOOL bUsedDefault
, bWriteWide
;
361 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
367 res
=IStream_Write(pStm
,&ZERO
,sizeof(WORD
),NULL
);
368 if (FAILED(res
)) return res
;
370 /* write length of filePath string ( 0 included )*/
371 bytesA
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
372 res
=IStream_Write(pStm
,&bytesA
,sizeof(DWORD
),NULL
);
373 if (FAILED(res
)) return res
;
375 /* write A string (with '\0') */
376 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
378 return E_OUTOFMEMORY
;
379 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, bytesA
, NULL
, &bUsedDefault
);
380 res
=IStream_Write(pStm
,filePathA
,bytesA
,NULL
);
381 HeapFree(GetProcessHeap(),0,filePathA
);
382 if (FAILED(res
)) return res
;
384 /* write a WORD 0xFFFF */
385 res
=IStream_Write(pStm
,&FFFF
,sizeof(WORD
),NULL
);
386 if (FAILED(res
)) return res
;
388 /* write a WORD 0xDEAD */
389 res
=IStream_Write(pStm
,&DEAD
,sizeof(WORD
),NULL
);
390 if (FAILED(res
)) return res
;
392 /* write 5 zero DWORDs */
395 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
396 if (FAILED(res
)) return res
;
399 /* Write the wide version if:
400 * + couldn't convert to CP_ACP,
401 * or + it's a directory,
402 * or + there's a character > 0xFF
404 len
= lstrlenW(filePathW
);
405 bWriteWide
= (bUsedDefault
|| (len
> 0 && filePathW
[len
-1]=='\\' ));
409 for(pch
=filePathW
;*pch
;++pch
)
420 return IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
422 /* write bytes needed for the filepathW (without 0) + 6 */
423 bytesW
= len
*sizeof(WCHAR
) + 6;
424 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
425 if (FAILED(res
)) return res
;
427 /* try again, without the extra 6 */
429 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
430 if (FAILED(res
)) return res
;
433 res
=IStream_Write(pStm
,&THREE
,sizeof(WORD
),NULL
);
434 if (FAILED(res
)) return res
;
436 /* write W string (no 0) */
437 return IStream_Write(pStm
,filePathW
,bytesW
,NULL
);
440 /******************************************************************************
441 * FileMoniker_GetSizeMax
443 static HRESULT WINAPI
444 FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
)
446 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
448 TRACE("(%p,%p)\n",iface
,pcbSize
);
453 /* We could calculate exactly (see ...::Save()) but instead
454 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
456 pcbSize
->u
.LowPart
= 0x38 + 4 * lstrlenW(This
->filePathName
);
457 pcbSize
->u
.HighPart
= 0;
462 /******************************************************************************
463 * FileMoniker_Destroy (local function)
464 *******************************************************************************/
465 HRESULT
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
467 TRACE("(%p)\n",This
);
469 if (This
->pMarshal
) IUnknown_Release(This
->pMarshal
);
470 HeapFree(GetProcessHeap(),0,This
->filePathName
);
471 HeapFree(GetProcessHeap(),0,This
);
476 /******************************************************************************
477 * FileMoniker_BindToObject
479 static HRESULT WINAPI
480 FileMonikerImpl_BindToObject(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
481 REFIID riid
, VOID
** ppvResult
)
483 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
487 IRunningObjectTable
*prot
=0;
489 IClassFactory
*pcf
=0;
490 IClassActivator
*pca
=0;
494 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvResult
);
498 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
501 /* if the requested class was loaded before ! we don't need to reload it */
502 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
505 /* first activation of this class */
506 res
=GetClassFile(This
->filePathName
,&clsID
);
509 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
512 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
516 IUnknown_AddRef(pObj
);
524 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
526 if (res
==E_NOINTERFACE
){
528 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
530 if (res
==E_NOINTERFACE
)
531 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
535 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)&ppf
);
537 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
542 IUnknown_AddRef(pObj
);
549 /*res=GetClassFile(This->filePathName,&clsID);
553 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
558 IUnknown_AddRef(pObj);
565 /* get the requested interface from the loaded class */
566 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
568 IBindCtx_RegisterObjectBound(pbc
,*ppvResult
);
570 IUnknown_Release(pObj
);
574 IRunningObjectTable_Release(prot
);
577 IPersistFile_Release(ppf
);
580 IClassActivator_Release(pca
);
583 IClassFactory_Release(pcf
);
588 /******************************************************************************
589 * FileMoniker_BindToStorage
591 static HRESULT WINAPI
592 FileMonikerImpl_BindToStorage(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
593 REFIID riid
, VOID
** ppvObject
)
599 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
601 if (pmkToLeft
==NULL
){
603 if (IsEqualIID(&IID_IStorage
, riid
)){
605 /* get the file name */
606 IMoniker_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
608 /* verify if the file contains a storage object */
609 res
=StgIsStorageFile(filePath
);
613 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
619 IStorage_AddRef(pstg
);
624 CoTaskMemFree(filePath
);
627 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
630 return E_NOINTERFACE
;
634 FIXME("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
641 /******************************************************************************
643 ******************************************************************************/
644 static HRESULT WINAPI
645 FileMonikerImpl_Reduce(IMoniker
* iface
, IBindCtx
* pbc
, DWORD dwReduceHowFar
,
646 IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
)
648 TRACE("(%p,%p,%d,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
650 if (ppmkReduced
==NULL
)
653 IMoniker_AddRef(iface
);
657 return MK_S_REDUCED_TO_SELF
;
660 static void free_stringtable(LPOLESTR
*stringTable
)
664 for (i
=0; stringTable
[i
]!=NULL
; i
++)
665 CoTaskMemFree(stringTable
[i
]);
666 CoTaskMemFree(stringTable
);
669 /******************************************************************************
670 * FileMoniker_ComposeWith
672 static HRESULT WINAPI
673 FileMonikerImpl_ComposeWith(IMoniker
* iface
, IMoniker
* pmkRight
,
674 BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
)
677 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
678 static const WCHAR twoPoint
[]={'.','.',0};
679 static const WCHAR bkSlash
[]={'\\',0};
681 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
684 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
686 if (ppmkComposite
==NULL
)
694 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
696 /* check if we have two FileMonikers to compose or not */
697 if(mkSys
==MKSYS_FILEMONIKER
){
699 CreateBindCtx(0,&bind
);
701 IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
702 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
704 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
705 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
706 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
708 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
711 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
714 /* for etch "..\" in the left of str2 remove the right element from str1 */
715 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
720 /* the length of the composed path string is raised by the sum of the two paths lengths */
721 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
725 /* new path is the concatenation of the rest of str1 and str2 */
726 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
727 strcatW(newStr
,strDec1
[j
]);
729 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
730 strcatW(newStr
,bkSlash
);
732 for(j
=i
;j
<=lastIdx2
;j
++)
733 strcatW(newStr
,strDec2
[j
]);
735 /* create a new moniker with the new string */
736 res
=CreateFileMoniker(newStr
,ppmkComposite
);
738 /* free all strings space memory used by this function */
739 HeapFree(GetProcessHeap(),0,newStr
);
741 else res
= E_OUTOFMEMORY
;
743 free_stringtable(strDec1
);
744 free_stringtable(strDec2
);
751 else if(mkSys
==MKSYS_ANTIMONIKER
){
756 else if (fOnlyIfNotGeneric
){
759 return MK_E_NEEDGENERIC
;
763 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
766 /******************************************************************************
769 static HRESULT WINAPI
770 FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
772 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
774 if (ppenumMoniker
== NULL
)
777 *ppenumMoniker
= NULL
;
782 /******************************************************************************
783 * FileMoniker_IsEqual
785 static HRESULT WINAPI
786 FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
788 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
794 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
796 if (pmkOtherMoniker
==NULL
)
799 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
801 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
804 res
= CreateBindCtx(0,&bind
);
805 if (FAILED(res
)) return res
;
808 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
809 if (!lstrcmpiW(filePath
, This
->filePathName
))
811 CoTaskMemFree(filePath
);
814 IBindCtx_Release(bind
);
818 /******************************************************************************
821 static HRESULT WINAPI
822 FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
824 FileMonikerImpl
*This
= impl_from_IMoniker(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
= impl_from_IMoniker(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
);
977 free_stringtable(stringTable1
);
981 if (nb1
==0 || nb2
==0)
983 free_stringtable(stringTable1
);
984 free_stringtable(stringTable2
);
985 return MK_E_NOPREFIX
;
988 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
990 return E_OUTOFMEMORY
;
994 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
995 (stringTable2
[sameIdx
]!=NULL
) &&
996 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
998 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1000 machimeNameCase
=TRUE
;
1002 for(i
=2;i
<sameIdx
;i
++)
1004 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1005 machimeNameCase
=FALSE
;
1010 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1013 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1014 ret
= MK_E_NOPREFIX
;
1017 for(i
=0;i
<sameIdx
;i
++)
1018 strcatW(commonPath
,stringTable1
[i
]);
1020 free_stringtable(stringTable1
);
1021 free_stringtable(stringTable2
);
1022 ret
= CreateFileMoniker(commonPath
,ppmkPrefix
);
1024 HeapFree(GetProcessHeap(),0,commonPath
);
1028 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1031 /******************************************************************************
1032 * DecomposePath (local function)
1034 int FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
1036 static const WCHAR bSlash
[] = {'\\',0};
1038 int i
=0,j
,tabIndex
=0, ret
=0;
1039 LPOLESTR
*strgtable
;
1041 int len
=lstrlenW(str
);
1043 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
1045 strgtable
= CoTaskMemAlloc((len
+ 1)*sizeof(*strgtable
));
1047 if (strgtable
==NULL
)
1048 return E_OUTOFMEMORY
;
1050 word
= CoTaskMemAlloc((len
+ 1)*sizeof(WCHAR
));
1054 ret
= E_OUTOFMEMORY
;
1060 if(str
[i
]==bSlash
[0]){
1062 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1064 if (strgtable
[tabIndex
]==NULL
)
1066 ret
= E_OUTOFMEMORY
;
1070 strcpyW(strgtable
[tabIndex
++],bSlash
);
1077 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1082 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1084 if (strgtable
[tabIndex
]==NULL
)
1086 ret
= E_OUTOFMEMORY
;
1090 strcpyW(strgtable
[tabIndex
++],word
);
1093 strgtable
[tabIndex
]=NULL
;
1095 *stringTable
=strgtable
;
1102 for (i
= 0; i
< tabIndex
; i
++)
1103 CoTaskMemFree(strgtable
[i
]);
1105 CoTaskMemFree(strgtable
);
1109 CoTaskMemFree(word
);
1114 /******************************************************************************
1115 * FileMoniker_RelativePathTo
1117 static HRESULT WINAPI
1118 FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1122 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1123 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1124 static const WCHAR back
[] ={'.','.','\\',0};
1126 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1128 if (ppmkRelPath
==NULL
)
1132 return E_INVALIDARG
;
1134 res
=CreateBindCtx(0,&bind
);
1138 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1141 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1145 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1147 return E_OUTOFMEMORY
;
1148 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1152 free_stringtable(tabStr1
);
1153 return E_OUTOFMEMORY
;
1156 /* count the number of similar items from the begin of the two paths */
1157 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1158 (tabStr2
[sameIdx
]!=NULL
) &&
1159 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1161 /* begin the construction of relativePath */
1162 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1163 /* by "..\\" in the begin */
1164 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1168 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1169 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1170 if (*tabStr1
[j
]!='\\')
1171 strcatW(relPath
,back
);
1173 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1174 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1175 strcatW(relPath
,tabStr2
[j
]);
1177 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1179 free_stringtable(tabStr1
);
1180 free_stringtable(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
= impl_from_IMoniker(iface
);
1199 int len
=lstrlenW(This
->filePathName
);
1201 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1203 if (ppszDisplayName
==NULL
)
1206 if (pmkToLeft
!=NULL
)
1207 return E_INVALIDARG
;
1209 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1210 if (*ppszDisplayName
==NULL
)
1211 return E_OUTOFMEMORY
;
1213 strcpyW(*ppszDisplayName
,This
->filePathName
);
1215 TRACE("-- %s\n", debugstr_w(*ppszDisplayName
));
1220 /******************************************************************************
1221 * FileMoniker_ParseDisplayName
1223 static HRESULT WINAPI
1224 FileMonikerImpl_ParseDisplayName(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
1225 LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
)
1227 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1231 /******************************************************************************
1232 * FileMoniker_IsSystemMoniker
1234 static HRESULT WINAPI
1235 FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1237 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1242 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1247 /*******************************************************************************
1248 * FileMonikerIROTData_QueryInterface
1250 static HRESULT WINAPI
1251 FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1254 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1256 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1258 return FileMonikerImpl_QueryInterface(&This
->IMoniker_iface
, riid
, ppvObject
);
1261 /***********************************************************************
1262 * FileMonikerIROTData_AddRef
1265 FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1267 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1269 TRACE("(%p)\n",This
);
1271 return IMoniker_AddRef(&This
->IMoniker_iface
);
1274 /***********************************************************************
1275 * FileMonikerIROTData_Release
1278 FileMonikerROTDataImpl_Release(IROTData
* iface
)
1280 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1282 TRACE("(%p)\n",This
);
1284 return FileMonikerImpl_Release(&This
->IMoniker_iface
);
1287 /******************************************************************************
1288 * FileMonikerIROTData_GetComparisonData
1290 static HRESULT WINAPI
1291 FileMonikerROTDataImpl_GetComparisonData(IROTData
* iface
, BYTE
* pbData
,
1292 ULONG cbMax
, ULONG
* pcbData
)
1294 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1295 int len
= strlenW(This
->filePathName
)+1;
1299 TRACE("(%p, %u, %p)\n", pbData
, cbMax
, pcbData
);
1301 *pcbData
= sizeof(CLSID
) + len
* sizeof(WCHAR
);
1302 if (cbMax
< *pcbData
)
1303 return E_OUTOFMEMORY
;
1305 memcpy(pbData
, &CLSID_FileMoniker
, sizeof(CLSID
));
1306 pszFileName
= (LPWSTR
)(pbData
+sizeof(CLSID
));
1307 for (i
= 0; i
< len
; i
++)
1308 pszFileName
[i
] = toupperW(This
->filePathName
[i
]);
1314 * Virtual function table for the FileMonikerImpl class which include IPersist,
1315 * IPersistStream and IMoniker functions.
1317 static const IMonikerVtbl VT_FileMonikerImpl
=
1319 FileMonikerImpl_QueryInterface
,
1320 FileMonikerImpl_AddRef
,
1321 FileMonikerImpl_Release
,
1322 FileMonikerImpl_GetClassID
,
1323 FileMonikerImpl_IsDirty
,
1324 FileMonikerImpl_Load
,
1325 FileMonikerImpl_Save
,
1326 FileMonikerImpl_GetSizeMax
,
1327 FileMonikerImpl_BindToObject
,
1328 FileMonikerImpl_BindToStorage
,
1329 FileMonikerImpl_Reduce
,
1330 FileMonikerImpl_ComposeWith
,
1331 FileMonikerImpl_Enum
,
1332 FileMonikerImpl_IsEqual
,
1333 FileMonikerImpl_Hash
,
1334 FileMonikerImpl_IsRunning
,
1335 FileMonikerImpl_GetTimeOfLastChange
,
1336 FileMonikerImpl_Inverse
,
1337 FileMonikerImpl_CommonPrefixWith
,
1338 FileMonikerImpl_RelativePathTo
,
1339 FileMonikerImpl_GetDisplayName
,
1340 FileMonikerImpl_ParseDisplayName
,
1341 FileMonikerImpl_IsSystemMoniker
1344 /* Virtual function table for the IROTData class. */
1345 static const IROTDataVtbl VT_ROTDataImpl
=
1347 FileMonikerROTDataImpl_QueryInterface
,
1348 FileMonikerROTDataImpl_AddRef
,
1349 FileMonikerROTDataImpl_Release
,
1350 FileMonikerROTDataImpl_GetComparisonData
1353 /******************************************************************************
1354 * FileMoniker_Construct (local function)
1356 static HRESULT
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
1359 int sizeStr
=lstrlenW(lpszPathName
);
1361 static const WCHAR twoPoint
[]={'.','.',0};
1362 static const WCHAR bkSlash
[]={'\\',0};
1365 TRACE("(%p,%s)\n",This
,debugstr_w(lpszPathName
));
1367 /* Initialize the virtual function table. */
1368 This
->IMoniker_iface
.lpVtbl
= &VT_FileMonikerImpl
;
1369 This
->IROTData_iface
.lpVtbl
= &VT_ROTDataImpl
;
1371 This
->pMarshal
= NULL
;
1373 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
1375 if (This
->filePathName
==NULL
)
1376 return E_OUTOFMEMORY
;
1378 strcpyW(This
->filePathName
,lpszPathName
);
1380 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
1385 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
1390 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
1396 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
1404 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
1407 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
1409 *This
->filePathName
=0;
1411 for(i
=0;tabStr
[i
]!=NULL
;i
++)
1412 strcatW(This
->filePathName
,tabStr
[i
]);
1415 strcatW(This
->filePathName
,bkSlash
);
1418 free_stringtable(tabStr
);
1423 /******************************************************************************
1424 * CreateFileMoniker (OLE32.@)
1425 ******************************************************************************/
1426 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, IMoniker
**ppmk
)
1428 FileMonikerImpl
* newFileMoniker
;
1431 TRACE("(%s,%p)\n",debugstr_w(lpszPathName
),ppmk
);
1441 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1443 if (!newFileMoniker
)
1444 return E_OUTOFMEMORY
;
1446 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1449 hr
= IMoniker_QueryInterface(&newFileMoniker
->IMoniker_iface
,&IID_IMoniker
,(void**)ppmk
);
1451 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1456 /* find a character from a set in reverse without the string having to be null-terminated */
1457 static inline WCHAR
*memrpbrkW(const WCHAR
*ptr
, size_t n
, const WCHAR
*accept
)
1459 const WCHAR
*end
, *ret
= NULL
;
1460 for (end
= ptr
+ n
; ptr
< end
; ptr
++) if (strchrW(accept
, *ptr
)) ret
= ptr
;
1461 return (WCHAR
*)ret
;
1464 HRESULT
FileMoniker_CreateFromDisplayName(LPBC pbc
, LPCOLESTR szDisplayName
,
1465 LPDWORD pchEaten
, IMoniker
**ppmk
)
1468 static const WCHAR wszSeparators
[] = {':','\\','/','!',0};
1470 for (end
= szDisplayName
+ strlenW(szDisplayName
);
1471 end
&& (end
!= szDisplayName
);
1472 end
= memrpbrkW(szDisplayName
, end
- szDisplayName
, wszSeparators
))
1475 IRunningObjectTable
*rot
;
1476 IMoniker
*file_moniker
;
1477 LPWSTR file_display_name
;
1478 LPWSTR full_path_name
;
1479 DWORD full_path_name_len
;
1480 int len
= end
- szDisplayName
;
1482 file_display_name
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1483 if (!file_display_name
) return E_OUTOFMEMORY
;
1484 memcpy(file_display_name
, szDisplayName
, len
* sizeof(WCHAR
));
1485 file_display_name
[len
] = '\0';
1487 hr
= CreateFileMoniker(file_display_name
, &file_moniker
);
1490 HeapFree(GetProcessHeap(), 0, file_display_name
);
1494 hr
= IBindCtx_GetRunningObjectTable(pbc
, &rot
);
1497 HeapFree(GetProcessHeap(), 0, file_display_name
);
1498 IMoniker_Release(file_moniker
);
1502 hr
= IRunningObjectTable_IsRunning(rot
, file_moniker
);
1503 IRunningObjectTable_Release(rot
);
1506 HeapFree(GetProcessHeap(), 0, file_display_name
);
1507 IMoniker_Release(file_moniker
);
1512 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name
));
1514 *ppmk
= file_moniker
;
1515 HeapFree(GetProcessHeap(), 0, file_display_name
);
1519 full_path_name_len
= GetFullPathNameW(file_display_name
, 0, NULL
, NULL
);
1520 if (!full_path_name_len
)
1522 HeapFree(GetProcessHeap(), 0, file_display_name
);
1523 IMoniker_Release(file_moniker
);
1526 full_path_name
= HeapAlloc(GetProcessHeap(), 0, full_path_name_len
* sizeof(WCHAR
));
1527 if (!full_path_name
)
1529 HeapFree(GetProcessHeap(), 0, file_display_name
);
1530 IMoniker_Release(file_moniker
);
1531 return E_OUTOFMEMORY
;
1533 GetFullPathNameW(file_display_name
, full_path_name_len
, full_path_name
, NULL
);
1535 if (GetFileAttributesW(full_path_name
) == INVALID_FILE_ATTRIBUTES
)
1536 TRACE("couldn't open file %s\n", debugstr_w(full_path_name
));
1539 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName
));
1541 *ppmk
= file_moniker
;
1542 HeapFree(GetProcessHeap(), 0, file_display_name
);
1543 HeapFree(GetProcessHeap(), 0, full_path_name
);
1546 HeapFree(GetProcessHeap(), 0, file_display_name
);
1547 HeapFree(GetProcessHeap(), 0, full_path_name
);
1548 IMoniker_Release(file_moniker
);
1551 return MK_E_CANTOPENFILE
;
1555 static HRESULT WINAPI
FileMonikerCF_QueryInterface(LPCLASSFACTORY iface
,
1556 REFIID riid
, LPVOID
*ppv
)
1559 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1562 IClassFactory_AddRef(iface
);
1565 return E_NOINTERFACE
;
1568 static ULONG WINAPI
FileMonikerCF_AddRef(LPCLASSFACTORY iface
)
1570 return 2; /* non-heap based object */
1573 static ULONG WINAPI
FileMonikerCF_Release(LPCLASSFACTORY iface
)
1575 return 1; /* non-heap based object */
1578 static HRESULT WINAPI
FileMonikerCF_CreateInstance(LPCLASSFACTORY iface
,
1579 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1581 FileMonikerImpl
* newFileMoniker
;
1583 static const WCHAR wszEmpty
[] = { 0 };
1585 TRACE("(%p, %s, %p)\n", pUnk
, debugstr_guid(riid
), ppv
);
1590 return CLASS_E_NOAGGREGATION
;
1592 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1593 if (!newFileMoniker
)
1594 return E_OUTOFMEMORY
;
1596 hr
= FileMonikerImpl_Construct(newFileMoniker
, wszEmpty
);
1599 hr
= IMoniker_QueryInterface(&newFileMoniker
->IMoniker_iface
, riid
, ppv
);
1601 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1606 static HRESULT WINAPI
FileMonikerCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1608 FIXME("(%d), stub!\n",fLock
);
1612 static const IClassFactoryVtbl FileMonikerCFVtbl
=
1614 FileMonikerCF_QueryInterface
,
1615 FileMonikerCF_AddRef
,
1616 FileMonikerCF_Release
,
1617 FileMonikerCF_CreateInstance
,
1618 FileMonikerCF_LockServer
1620 static const IClassFactoryVtbl
*FileMonikerCF
= &FileMonikerCFVtbl
;
1622 HRESULT
FileMonikerCF_Create(REFIID riid
, LPVOID
*ppv
)
1624 return IClassFactory_QueryInterface((IClassFactory
*)&FileMonikerCF
, riid
, ppv
);