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 /* Local function used by filemoniker implementation */
64 static HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
65 static HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
67 /*******************************************************************************
68 * FileMoniker_QueryInterface
71 FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
73 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
75 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
77 /* Perform a sanity check on the parameters.*/
78 if ( (This
==0) || (ppvObject
==0) )
81 /* Initialize the return parameter */
84 /* Compare the riid with the interface IDs implemented by this object.*/
85 if (IsEqualIID(&IID_IUnknown
, riid
) ||
86 IsEqualIID(&IID_IPersist
, riid
) ||
87 IsEqualIID(&IID_IPersistStream
,riid
) ||
88 IsEqualIID(&IID_IMoniker
, riid
)
92 else if (IsEqualIID(&IID_IROTData
, riid
))
93 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
94 else if (IsEqualIID(&IID_IMarshal
, riid
))
98 hr
= MonikerMarshal_Create(iface
, &This
->pMarshal
);
101 return IUnknown_QueryInterface(This
->pMarshal
, riid
, ppvObject
);
104 /* Check that we obtained an interface.*/
106 return E_NOINTERFACE
;
108 /* Query Interface always increases the reference count by one when it is successful */
109 IMoniker_AddRef(iface
);
114 /******************************************************************************
118 FileMonikerImpl_AddRef(IMoniker
* iface
)
120 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
122 TRACE("(%p)\n",iface
);
124 return InterlockedIncrement(&This
->ref
);
127 /******************************************************************************
128 * FileMoniker_Release
131 FileMonikerImpl_Release(IMoniker
* iface
)
133 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
136 TRACE("(%p)\n",iface
);
138 ref
= InterlockedDecrement(&This
->ref
);
140 /* destroy the object if there's no more reference on it */
141 if (ref
== 0) FileMonikerImpl_Destroy(This
);
146 /******************************************************************************
147 * FileMoniker_GetClassID
149 static HRESULT WINAPI
150 FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
)
152 TRACE("(%p,%p)\n",iface
,pClassID
);
157 *pClassID
= CLSID_FileMoniker
;
162 /******************************************************************************
163 * FileMoniker_IsDirty
165 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
166 * method in the OLE-provided moniker interfaces always return S_FALSE because
167 * their internal state never changes.
169 static HRESULT WINAPI
170 FileMonikerImpl_IsDirty(IMoniker
* iface
)
173 TRACE("(%p)\n",iface
);
178 /******************************************************************************
181 * this function locates and reads from the stream the filePath string
182 * written by FileMonikerImpl_Save
184 static HRESULT WINAPI
185 FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
)
188 CHAR
* filePathA
= NULL
;
189 WCHAR
* filePathW
= NULL
;
192 DWORD dwbuffer
, bytesA
, bytesW
, len
;
195 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
197 TRACE("(%p,%p)\n",iface
,pStm
);
199 /* first WORD must be 0 */
200 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
201 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
203 WARN("Couldn't read 0 word\n");
207 /* read filePath string length (plus one) */
208 res
=IStream_Read(pStm
,&bytesA
,sizeof(DWORD
),&bread
);
209 if (bread
!= sizeof(DWORD
))
211 WARN("Couldn't read file string length\n");
215 /* read filePath string */
216 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
223 res
=IStream_Read(pStm
,filePathA
,bytesA
,&bread
);
226 WARN("Couldn't read file path string\n");
230 /* read the first constant */
231 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
232 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
234 WARN("Couldn't read 0xDEADFFFF constant\n");
240 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
241 if (bread
!=sizeof(DWORD
) || dwbuffer
!=0)
243 WARN("Couldn't read 0 padding\n");
248 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
249 if (bread
!=sizeof(DWORD
))
252 if (!dwbuffer
) /* No W-string */
255 len
=MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, bytesA
, NULL
, 0);
259 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
265 MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, -1, filePathW
, len
+1);
274 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
275 if (bread
!=sizeof(DWORD
) || dwbuffer
!=bytesW
)
278 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
279 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
282 len
=bytesW
/sizeof(WCHAR
);
283 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
289 res
=IStream_Read(pStm
,filePathW
,bytesW
,&bread
);
296 HeapFree(GetProcessHeap(),0,filePathA
);
297 HeapFree(GetProcessHeap(),0,This
->filePathName
);
298 This
->filePathName
=filePathW
;
303 HeapFree(GetProcessHeap(), 0, filePathA
);
304 HeapFree(GetProcessHeap(), 0, filePathW
);
311 /******************************************************************************
314 * This function saves data of this object. In the beginning I thought
315 * that I have just to write the filePath string on Stream. But, when I
316 * tested this function with windows programs samples, I noticed that it
317 * was not the case. This implementation is based on XP SP2. Other versions
318 * of Windows have minor variations.
320 * Data which must be written on stream is:
321 * 1) WORD constant:zero
322 * 2) length of the path string ("\0" included)
323 * 3) path string type A
324 * 4) DWORD constant : 0xDEADFFFF
325 * 5) five DWORD constant: zero
326 * 6) If we're only writing the multibyte version,
327 * write a zero DWORD and finish.
329 * 7) DWORD: double-length of the the path string type W ("\0" not
331 * 8) WORD constant: 0x3
332 * 9) filePath unicode string.
335 static HRESULT WINAPI
336 FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
)
338 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
341 LPOLESTR filePathW
=This
->filePathName
;
343 DWORD bytesA
, bytesW
, len
;
345 static const DWORD DEADFFFF
= 0xDEADFFFF; /* Constants */
346 static const DWORD ZERO
= 0;
347 static const WORD THREE
= 0x3;
350 BOOL bUsedDefault
, bWriteWide
;
352 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
358 res
=IStream_Write(pStm
,&ZERO
,sizeof(WORD
),NULL
);
359 if (!SUCCEEDED(res
)) return res
;
361 /* write length of filePath string ( 0 included )*/
362 bytesA
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
363 res
=IStream_Write(pStm
,&bytesA
,sizeof(DWORD
),NULL
);
364 if (!SUCCEEDED(res
)) return res
;
366 /* write A string (with '\0') */
367 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
369 return E_OUTOFMEMORY
;
370 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, bytesA
, NULL
, &bUsedDefault
);
371 res
=IStream_Write(pStm
,filePathA
,bytesA
,NULL
);
372 HeapFree(GetProcessHeap(),0,filePathA
);
373 if (!SUCCEEDED(res
)) return res
;
375 /* write a DWORD 0xDEADFFFF */
376 res
=IStream_Write(pStm
,&DEADFFFF
,sizeof(DWORD
),NULL
);
377 if (!SUCCEEDED(res
)) return res
;
379 /* write 5 zero DWORDs */
382 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
383 if (!SUCCEEDED(res
)) return res
;
386 /* Write the wide version if:
387 * + couldn't convert to CP_ACP,
388 * or + it's a directory,
389 * or + there's a character > 0xFF
391 len
= lstrlenW(filePathW
);
392 bWriteWide
= (bUsedDefault
|| (len
> 0 && filePathW
[len
-1]=='\\' ));
396 for(pch
=filePathW
;*pch
;++pch
)
408 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
412 /* write bytes needed for the filepathW (without 0) + 6 */
413 bytesW
= len
*sizeof(WCHAR
) + 6;
414 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
415 if (!SUCCEEDED(res
)) return res
;
417 /* try again, without the extra 6 */
419 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
420 if (!SUCCEEDED(res
)) return res
;
423 res
=IStream_Write(pStm
,&THREE
,sizeof(WORD
),NULL
);
424 if (!SUCCEEDED(res
)) return res
;
426 /* write W string (no 0) */
427 res
=IStream_Write(pStm
,filePathW
,bytesW
,NULL
);
432 /******************************************************************************
433 * FileMoniker_GetSizeMax
435 static HRESULT WINAPI
436 FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
)
438 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
440 TRACE("(%p,%p)\n",iface
,pcbSize
);
445 /* We could calculate exactly (see ...::Save()) but instead
446 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
448 pcbSize
->u
.LowPart
= 0x38 + 4 * lstrlenW(This
->filePathName
);
449 pcbSize
->u
.HighPart
= 0;
454 /******************************************************************************
455 * FileMoniker_Destroy (local function)
456 *******************************************************************************/
457 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
459 TRACE("(%p)\n",This
);
461 if (This
->pMarshal
) IUnknown_Release(This
->pMarshal
);
462 HeapFree(GetProcessHeap(),0,This
->filePathName
);
463 HeapFree(GetProcessHeap(),0,This
);
468 /******************************************************************************
469 * FileMoniker_BindToObject
471 static HRESULT WINAPI
472 FileMonikerImpl_BindToObject(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
473 REFIID riid
, VOID
** ppvResult
)
478 IRunningObjectTable
*prot
=0;
480 IClassFactory
*pcf
=0;
481 IClassActivator
*pca
=0;
483 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
487 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvResult
);
491 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
494 /* if the requested class was loaded before ! we don't need to reload it */
495 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
498 /* first activation of this class */
499 res
=GetClassFile(This
->filePathName
,&clsID
);
502 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
505 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
509 IUnknown_AddRef(pObj
);
517 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
519 if (res
==E_NOINTERFACE
){
521 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
523 if (res
==E_NOINTERFACE
)
524 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
528 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
530 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
535 IUnknown_AddRef(pObj
);
542 /*res=GetClassFile(This->filePathName,&clsID);
546 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
551 IUnknown_AddRef(pObj);
558 /* get the requested interface from the loaded class */
559 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
561 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
563 IUnknown_Release(pObj
);
567 IRunningObjectTable_Release(prot
);
570 IPersistFile_Release(ppf
);
573 IClassActivator_Release(pca
);
576 IClassFactory_Release(pcf
);
581 /******************************************************************************
582 * FileMoniker_BindToStorage
584 static HRESULT WINAPI
585 FileMonikerImpl_BindToStorage(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
586 REFIID riid
, VOID
** ppvObject
)
592 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
594 if (pmkToLeft
==NULL
){
596 if (IsEqualIID(&IID_IStorage
, riid
)){
598 /* get the file name */
599 IMoniker_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
601 /* verifie if the file contains a storage object */
602 res
=StgIsStorageFile(filePath
);
606 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
612 IStorage_AddRef(pstg
);
617 CoTaskMemFree(filePath
);
620 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
623 return E_NOINTERFACE
;
627 FIXME("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
634 /******************************************************************************
636 ******************************************************************************/
637 static HRESULT WINAPI
638 FileMonikerImpl_Reduce(IMoniker
* iface
, IBindCtx
* pbc
, DWORD dwReduceHowFar
,
639 IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
)
641 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
643 if (ppmkReduced
==NULL
)
646 IMoniker_AddRef(iface
);
650 return MK_S_REDUCED_TO_SELF
;
653 /******************************************************************************
654 * FileMoniker_ComposeWith
656 static HRESULT WINAPI
657 FileMonikerImpl_ComposeWith(IMoniker
* iface
, IMoniker
* pmkRight
,
658 BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
)
661 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
662 static const WCHAR twoPoint
[]={'.','.',0};
663 static const WCHAR bkSlash
[]={'\\',0};
665 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
668 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
670 if (ppmkComposite
==NULL
)
678 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
680 /* check if we have two filemonikers to compose or not */
681 if(mkSys
==MKSYS_FILEMONIKER
){
683 CreateBindCtx(0,&bind
);
685 IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
686 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
688 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
689 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
690 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
692 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
695 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
698 /* for etch "..\" in the left of str2 remove the right element from str1 */
699 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
704 /* the length of the composed path string is raised by the sum of the two paths lengths */
705 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
708 return E_OUTOFMEMORY
;
710 /* new path is the concatenation of the rest of str1 and str2 */
711 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
712 strcatW(newStr
,strDec1
[j
]);
714 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
715 strcatW(newStr
,bkSlash
);
717 for(j
=i
;j
<=lastIdx2
;j
++)
718 strcatW(newStr
,strDec2
[j
]);
720 /* create a new moniker with the new string */
721 res
=CreateFileMoniker(newStr
,ppmkComposite
);
723 /* free all strings space memory used by this function */
724 HeapFree(GetProcessHeap(),0,newStr
);
726 for(i
=0; strDec1
[i
]!=NULL
;i
++)
727 CoTaskMemFree(strDec1
[i
]);
728 for(i
=0; strDec2
[i
]!=NULL
;i
++)
729 CoTaskMemFree(strDec2
[i
]);
730 CoTaskMemFree(strDec1
);
731 CoTaskMemFree(strDec2
);
738 else if(mkSys
==MKSYS_ANTIMONIKER
){
743 else if (fOnlyIfNotGeneric
){
746 return MK_E_NEEDGENERIC
;
750 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
753 /******************************************************************************
756 static HRESULT WINAPI
757 FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
759 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
761 if (ppenumMoniker
== NULL
)
764 *ppenumMoniker
= NULL
;
769 /******************************************************************************
770 * FileMoniker_IsEqual
772 static HRESULT WINAPI
773 FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
775 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
781 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
783 if (pmkOtherMoniker
==NULL
)
786 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
788 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
791 res
= CreateBindCtx(0,&bind
);
792 if (FAILED(res
)) return res
;
794 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
795 int result
= lstrcmpiW(filePath
, This
->filePathName
);
796 CoTaskMemFree(filePath
);
797 if ( result
== 0 ) return S_OK
;
803 /******************************************************************************
806 static HRESULT WINAPI
807 FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
809 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
811 int h
= 0,i
,skip
,len
;
818 val
= This
->filePathName
;
822 for (i
= len
; i
> 0; i
--) {
823 h
= (h
* 37) + val
[off
++];
826 /* only sample some characters */
828 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
829 h
= (h
* 39) + val
[off
];
838 /******************************************************************************
839 * FileMoniker_IsRunning
841 static HRESULT WINAPI
842 FileMonikerImpl_IsRunning(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
843 IMoniker
* pmkNewlyRunning
)
845 IRunningObjectTable
* rot
;
848 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
850 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
856 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
861 res
= IRunningObjectTable_IsRunning(rot
,iface
);
863 IRunningObjectTable_Release(rot
);
868 /******************************************************************************
869 * FileMoniker_GetTimeOfLastChange
870 ******************************************************************************/
871 static HRESULT WINAPI
872 FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
,
873 IMoniker
* pmkToLeft
, FILETIME
* pFileTime
)
875 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
876 IRunningObjectTable
* rot
;
878 WIN32_FILE_ATTRIBUTE_DATA info
;
880 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
888 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
893 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
895 if (FAILED(res
)){ /* the moniker is not registered */
897 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
898 return MK_E_NOOBJECT
;
900 *pFileTime
=info
.ftLastWriteTime
;
906 /******************************************************************************
907 * FileMoniker_Inverse
909 static HRESULT WINAPI
910 FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
912 TRACE("(%p,%p)\n",iface
,ppmk
);
914 return CreateAntiMoniker(ppmk
);
917 /******************************************************************************
918 * FileMoniker_CommonPrefixWith
920 static HRESULT WINAPI
921 FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
924 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
927 ULONG nb1
,nb2
,i
,sameIdx
;
928 BOOL machimeNameCase
=FALSE
;
930 if (ppmkPrefix
==NULL
)
938 /* check if we have the same type of moniker */
939 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
941 if(mkSys
==MKSYS_FILEMONIKER
){
944 CreateBindCtx(0,&pbind
);
946 /* create a string based on common part of the two paths */
948 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
949 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
951 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
952 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
954 if (nb1
==0 || nb2
==0)
955 return MK_E_NOPREFIX
;
957 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
961 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
962 (stringTable2
[sameIdx
]!=NULL
) &&
963 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
965 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
967 machimeNameCase
=TRUE
;
969 for(i
=2;i
<sameIdx
;i
++)
971 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
972 machimeNameCase
=FALSE
;
977 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
980 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
984 for(i
=0;i
<sameIdx
;i
++)
985 strcatW(commonPath
,stringTable1
[i
]);
988 CoTaskMemFree(stringTable1
[i
]);
990 CoTaskMemFree(stringTable1
);
993 CoTaskMemFree(stringTable2
[i
]);
995 CoTaskMemFree(stringTable2
);
997 ret
= CreateFileMoniker(commonPath
,ppmkPrefix
);
999 HeapFree(GetProcessHeap(),0,commonPath
);
1003 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1006 /******************************************************************************
1007 * DecomposePath (local function)
1009 int FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
1011 static const WCHAR bSlash
[] = {'\\',0};
1012 WCHAR word
[MAX_PATH
];
1013 int i
=0,j
,tabIndex
=0;
1014 LPOLESTR
*strgtable
;
1016 int len
=lstrlenW(str
);
1018 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
1020 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1022 if (strgtable
==NULL
)
1023 return E_OUTOFMEMORY
;
1027 if(str
[i
]==bSlash
[0]){
1029 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1031 if (strgtable
[tabIndex
]==NULL
)
1032 return E_OUTOFMEMORY
;
1034 strcpyW(strgtable
[tabIndex
++],bSlash
);
1041 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1046 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1048 if (strgtable
[tabIndex
]==NULL
)
1049 return E_OUTOFMEMORY
;
1051 strcpyW(strgtable
[tabIndex
++],word
);
1054 strgtable
[tabIndex
]=NULL
;
1056 *stringTable
=strgtable
;
1061 /******************************************************************************
1062 * FileMoniker_RelativePathTo
1064 static HRESULT WINAPI
1065 FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1069 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1070 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1071 static const WCHAR back
[] ={'.','.','\\',0};
1073 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1075 if (ppmkRelPath
==NULL
)
1079 return E_INVALIDARG
;
1081 res
=CreateBindCtx(0,&bind
);
1085 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1088 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1092 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1093 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1095 if (FAILED(len1
) || FAILED(len2
))
1096 return E_OUTOFMEMORY
;
1098 /* count the number of similar items from the begin of the two paths */
1099 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1100 (tabStr2
[sameIdx
]!=NULL
) &&
1101 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1103 /* begin the construction of relativePath */
1104 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1105 /* by "..\\" in the begin */
1106 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1110 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1111 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1112 if (*tabStr1
[j
]!='\\')
1113 strcatW(relPath
,back
);
1115 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1116 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1117 strcatW(relPath
,tabStr2
[j
]);
1119 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1121 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1122 CoTaskMemFree(tabStr1
[j
]);
1123 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1124 CoTaskMemFree(tabStr2
[j
]);
1125 CoTaskMemFree(tabStr1
);
1126 CoTaskMemFree(tabStr2
);
1127 CoTaskMemFree(str1
);
1128 CoTaskMemFree(str2
);
1129 HeapFree(GetProcessHeap(),0,relPath
);
1131 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1137 /******************************************************************************
1138 * FileMoniker_GetDisplayName
1140 static HRESULT WINAPI
1141 FileMonikerImpl_GetDisplayName(IMoniker
* iface
, IBindCtx
* pbc
,
1142 IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
)
1144 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
1146 int len
=lstrlenW(This
->filePathName
);
1148 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1150 if (ppszDisplayName
==NULL
)
1153 if (pmkToLeft
!=NULL
)
1154 return E_INVALIDARG
;
1156 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1157 if (*ppszDisplayName
==NULL
)
1158 return E_OUTOFMEMORY
;
1160 strcpyW(*ppszDisplayName
,This
->filePathName
);
1162 TRACE("-- %s\n", debugstr_w(*ppszDisplayName
));
1167 /******************************************************************************
1168 * FileMoniker_ParseDisplayName
1170 static HRESULT WINAPI
1171 FileMonikerImpl_ParseDisplayName(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
1172 LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
)
1174 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1178 /******************************************************************************
1179 * FileMoniker_IsSystemMoniker
1181 static HRESULT WINAPI
1182 FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1184 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1189 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1194 /*******************************************************************************
1195 * FileMonikerIROTData_QueryInterface
1197 static HRESULT WINAPI
1198 FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1201 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1203 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1205 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1208 /***********************************************************************
1209 * FileMonikerIROTData_AddRef
1212 FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1214 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1216 TRACE("(%p)\n",This
);
1218 return IMoniker_AddRef(This
);
1221 /***********************************************************************
1222 * FileMonikerIROTData_Release
1225 FileMonikerROTDataImpl_Release(IROTData
* iface
)
1227 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1229 TRACE("(%p)\n",This
);
1231 return FileMonikerImpl_Release(This
);
1234 /******************************************************************************
1235 * FileMonikerIROTData_GetComparaisonData
1237 static HRESULT WINAPI
1238 FileMonikerROTDataImpl_GetComparisonData(IROTData
* iface
, BYTE
* pbData
,
1239 ULONG cbMax
, ULONG
* pcbData
)
1241 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1242 FileMonikerImpl
*This1
= (FileMonikerImpl
*)This
;
1243 int len
= (strlenW(This1
->filePathName
)+1);
1247 TRACE("(%p, %lu, %p)\n", pbData
, cbMax
, pcbData
);
1249 *pcbData
= sizeof(CLSID
) + len
* sizeof(WCHAR
);
1250 if (cbMax
< *pcbData
)
1251 return E_OUTOFMEMORY
;
1253 memcpy(pbData
, &CLSID_FileMoniker
, sizeof(CLSID
));
1254 pszFileName
= (LPWSTR
)(pbData
+sizeof(CLSID
));
1255 for (i
= 0; i
< len
; i
++)
1256 pszFileName
[i
] = toupperW(This1
->filePathName
[i
]);
1262 * Virtual function table for the FileMonikerImpl class which include IPersist,
1263 * IPersistStream and IMoniker functions.
1265 static const IMonikerVtbl VT_FileMonikerImpl
=
1267 FileMonikerImpl_QueryInterface
,
1268 FileMonikerImpl_AddRef
,
1269 FileMonikerImpl_Release
,
1270 FileMonikerImpl_GetClassID
,
1271 FileMonikerImpl_IsDirty
,
1272 FileMonikerImpl_Load
,
1273 FileMonikerImpl_Save
,
1274 FileMonikerImpl_GetSizeMax
,
1275 FileMonikerImpl_BindToObject
,
1276 FileMonikerImpl_BindToStorage
,
1277 FileMonikerImpl_Reduce
,
1278 FileMonikerImpl_ComposeWith
,
1279 FileMonikerImpl_Enum
,
1280 FileMonikerImpl_IsEqual
,
1281 FileMonikerImpl_Hash
,
1282 FileMonikerImpl_IsRunning
,
1283 FileMonikerImpl_GetTimeOfLastChange
,
1284 FileMonikerImpl_Inverse
,
1285 FileMonikerImpl_CommonPrefixWith
,
1286 FileMonikerImpl_RelativePathTo
,
1287 FileMonikerImpl_GetDisplayName
,
1288 FileMonikerImpl_ParseDisplayName
,
1289 FileMonikerImpl_IsSystemMoniker
1292 /* Virtual function table for the IROTData class. */
1293 static const IROTDataVtbl VT_ROTDataImpl
=
1295 FileMonikerROTDataImpl_QueryInterface
,
1296 FileMonikerROTDataImpl_AddRef
,
1297 FileMonikerROTDataImpl_Release
,
1298 FileMonikerROTDataImpl_GetComparisonData
1301 /******************************************************************************
1302 * FileMoniker_Construct (local function)
1304 static HRESULT WINAPI
1305 FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
1308 int sizeStr
=lstrlenW(lpszPathName
);
1310 static const WCHAR twoPoint
[]={'.','.',0};
1311 static const WCHAR bkSlash
[]={'\\',0};
1314 TRACE("(%p,%s)\n",This
,debugstr_w(lpszPathName
));
1316 /* Initialize the virtual fgunction table. */
1317 This
->lpvtbl1
= &VT_FileMonikerImpl
;
1318 This
->lpvtbl2
= &VT_ROTDataImpl
;
1320 This
->pMarshal
= NULL
;
1322 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
1324 if (This
->filePathName
==NULL
)
1325 return E_OUTOFMEMORY
;
1327 strcpyW(This
->filePathName
,lpszPathName
);
1329 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
1334 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
1339 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
1345 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
1353 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
1356 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
1358 *This
->filePathName
=0;
1360 for(i
=0;tabStr
[i
]!=NULL
;i
++)
1361 strcatW(This
->filePathName
,tabStr
[i
]);
1364 strcatW(This
->filePathName
,bkSlash
);
1367 for(i
=0; tabStr
[i
]!=NULL
;i
++)
1368 CoTaskMemFree(tabStr
[i
]);
1369 CoTaskMemFree(tabStr
);
1374 /******************************************************************************
1375 * CreateFileMoniker (OLE32.@)
1376 ******************************************************************************/
1377 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1379 FileMonikerImpl
* newFileMoniker
;
1382 TRACE("(%s,%p)\n",debugstr_w(lpszPathName
),ppmk
);
1392 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1394 if (!newFileMoniker
)
1395 return E_OUTOFMEMORY
;
1397 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1400 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&IID_IMoniker
,(void**)ppmk
);
1402 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1407 static HRESULT WINAPI
FileMonikerCF_QueryInterface(LPCLASSFACTORY iface
,
1408 REFIID riid
, LPVOID
*ppv
)
1411 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1414 IUnknown_AddRef(iface
);
1417 return E_NOINTERFACE
;
1420 static ULONG WINAPI
FileMonikerCF_AddRef(LPCLASSFACTORY iface
)
1422 return 2; /* non-heap based object */
1425 static ULONG WINAPI
FileMonikerCF_Release(LPCLASSFACTORY iface
)
1427 return 1; /* non-heap based object */
1430 static HRESULT WINAPI
FileMonikerCF_CreateInstance(LPCLASSFACTORY iface
,
1431 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1433 FileMonikerImpl
* newFileMoniker
;
1435 static const WCHAR wszEmpty
[] = { 0 };
1437 TRACE("(%p, %s, %p)\n", pUnk
, debugstr_guid(riid
), ppv
);
1442 return CLASS_E_NOAGGREGATION
;
1444 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1445 if (!newFileMoniker
)
1446 return E_OUTOFMEMORY
;
1448 hr
= FileMonikerImpl_Construct(newFileMoniker
, wszEmpty
);
1451 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
, riid
, ppv
);
1453 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1458 static HRESULT WINAPI
FileMonikerCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1460 FIXME("(%d), stub!\n",fLock
);
1464 static const IClassFactoryVtbl FileMonikerCFVtbl
=
1466 FileMonikerCF_QueryInterface
,
1467 FileMonikerCF_AddRef
,
1468 FileMonikerCF_Release
,
1469 FileMonikerCF_CreateInstance
,
1470 FileMonikerCF_LockServer
1472 static const IClassFactoryVtbl
*FileMonikerCF
= &FileMonikerCFVtbl
;
1474 HRESULT
FileMonikerCF_Create(REFIID riid
, LPVOID
*ppv
)
1476 return IClassFactory_QueryInterface((IClassFactory
*)&FileMonikerCF
, riid
, ppv
);