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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 /* filemoniker data structure */
43 typedef struct FileMonikerImpl
{
45 const IMonikerVtbl
* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
47 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
48 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
50 const IROTDataVtbl
* lpvtbl2
; /* VTable relative to the IROTData interface.*/
52 LONG ref
; /* reference counter for this object */
54 LPOLESTR filePathName
; /* path string identified by this filemoniker */
56 IUnknown
*pMarshal
; /* custom marshaler */
59 static inline IMoniker
*impl_from_IROTData( IROTData
*iface
)
61 return (IMoniker
*)((char*)iface
- FIELD_OFFSET(FileMonikerImpl
, lpvtbl2
));
64 /* Local function used by filemoniker implementation */
65 static HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
66 static HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
68 /*******************************************************************************
69 * FileMoniker_QueryInterface
72 FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
74 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
76 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
78 /* Perform a sanity check on the parameters.*/
79 if ( (This
==0) || (ppvObject
==0) )
82 /* Initialize the return parameter */
85 /* Compare the riid with the interface IDs implemented by this object.*/
86 if (IsEqualIID(&IID_IUnknown
, riid
) ||
87 IsEqualIID(&IID_IPersist
, riid
) ||
88 IsEqualIID(&IID_IPersistStream
,riid
) ||
89 IsEqualIID(&IID_IMoniker
, riid
)
93 else if (IsEqualIID(&IID_IROTData
, riid
))
94 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
95 else if (IsEqualIID(&IID_IMarshal
, riid
))
99 hr
= MonikerMarshal_Create(iface
, &This
->pMarshal
);
102 return IUnknown_QueryInterface(This
->pMarshal
, riid
, ppvObject
);
105 /* Check that we obtained an interface.*/
107 return E_NOINTERFACE
;
109 /* Query Interface always increases the reference count by one when it is successful */
110 IMoniker_AddRef(iface
);
115 /******************************************************************************
119 FileMonikerImpl_AddRef(IMoniker
* iface
)
121 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
123 TRACE("(%p)\n",iface
);
125 return InterlockedIncrement(&This
->ref
);
128 /******************************************************************************
129 * FileMoniker_Release
132 FileMonikerImpl_Release(IMoniker
* iface
)
134 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
137 TRACE("(%p)\n",iface
);
139 ref
= InterlockedDecrement(&This
->ref
);
141 /* destroy the object if there's no more reference on it */
142 if (ref
== 0) FileMonikerImpl_Destroy(This
);
147 /******************************************************************************
148 * FileMoniker_GetClassID
150 static HRESULT WINAPI
151 FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
)
153 TRACE("(%p,%p)\n",iface
,pClassID
);
158 *pClassID
= CLSID_FileMoniker
;
163 /******************************************************************************
164 * FileMoniker_IsDirty
166 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
167 * method in the OLE-provided moniker interfaces always return S_FALSE because
168 * their internal state never changes.
170 static HRESULT WINAPI
171 FileMonikerImpl_IsDirty(IMoniker
* iface
)
174 TRACE("(%p)\n",iface
);
179 /******************************************************************************
182 * this function locates and reads from the stream the filePath string
183 * written by FileMonikerImpl_Save
185 static HRESULT WINAPI
186 FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
)
189 CHAR
* filePathA
= NULL
;
190 WCHAR
* filePathW
= NULL
;
193 DWORD dwbuffer
, bytesA
, bytesW
, len
;
196 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
198 TRACE("(%p,%p)\n",iface
,pStm
);
200 /* first WORD must be 0 */
201 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
202 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
204 WARN("Couldn't read 0 word\n");
208 /* read filePath string length (plus one) */
209 res
=IStream_Read(pStm
,&bytesA
,sizeof(DWORD
),&bread
);
210 if (bread
!= sizeof(DWORD
))
212 WARN("Couldn't read file string length\n");
216 /* read filePath string */
217 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
224 res
=IStream_Read(pStm
,filePathA
,bytesA
,&bread
);
227 WARN("Couldn't read file path string\n");
231 /* read the first constant */
232 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
233 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
235 WARN("Couldn't read 0xDEADFFFF constant\n");
241 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
242 if (bread
!=sizeof(DWORD
) || dwbuffer
!=0)
244 WARN("Couldn't read 0 padding\n");
249 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
250 if (bread
!=sizeof(DWORD
))
253 if (!dwbuffer
) /* No W-string */
256 len
=MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, bytesA
, NULL
, 0);
260 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
266 MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, -1, filePathW
, len
+1);
275 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
276 if (bread
!=sizeof(DWORD
) || dwbuffer
!=bytesW
)
279 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
280 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
283 len
=bytesW
/sizeof(WCHAR
);
284 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
290 res
=IStream_Read(pStm
,filePathW
,bytesW
,&bread
);
297 HeapFree(GetProcessHeap(),0,filePathA
);
298 HeapFree(GetProcessHeap(),0,This
->filePathName
);
299 This
->filePathName
=filePathW
;
304 HeapFree(GetProcessHeap(), 0, filePathA
);
305 HeapFree(GetProcessHeap(), 0, filePathW
);
312 /******************************************************************************
315 * This function saves data of this object. In the beginning I thought
316 * that I have just to write the filePath string on Stream. But, when I
317 * tested this function with windows programs samples, I noticed that it
318 * was not the case. This implementation is based on XP SP2. Other versions
319 * of Windows have minor variations.
321 * Data which must be written on stream is:
322 * 1) WORD constant:zero
323 * 2) length of the path string ("\0" included)
324 * 3) path string type A
325 * 4) DWORD constant : 0xDEADFFFF
326 * 5) five DWORD constant: zero
327 * 6) If we're only writing the multibyte version,
328 * write a zero DWORD and finish.
330 * 7) DWORD: double-length of the the path string type W ("\0" not
332 * 8) WORD constant: 0x3
333 * 9) filePath unicode string.
336 static HRESULT WINAPI
337 FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
)
339 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
342 LPOLESTR filePathW
=This
->filePathName
;
344 DWORD bytesA
, bytesW
, len
;
346 static const DWORD DEADFFFF
= 0xDEADFFFF; /* Constants */
347 static const DWORD ZERO
= 0;
348 static const WORD THREE
= 0x3;
351 BOOL bUsedDefault
, bWriteWide
;
353 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
359 res
=IStream_Write(pStm
,&ZERO
,sizeof(WORD
),NULL
);
360 if (!SUCCEEDED(res
)) return res
;
362 /* write length of filePath string ( 0 included )*/
363 bytesA
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
364 res
=IStream_Write(pStm
,&bytesA
,sizeof(DWORD
),NULL
);
365 if (!SUCCEEDED(res
)) return res
;
367 /* write A string (with '\0') */
368 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
370 return E_OUTOFMEMORY
;
371 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, bytesA
, NULL
, &bUsedDefault
);
372 res
=IStream_Write(pStm
,filePathA
,bytesA
,NULL
);
373 HeapFree(GetProcessHeap(),0,filePathA
);
374 if (!SUCCEEDED(res
)) return res
;
376 /* write a DWORD 0xDEADFFFF */
377 res
=IStream_Write(pStm
,&DEADFFFF
,sizeof(DWORD
),NULL
);
378 if (!SUCCEEDED(res
)) return res
;
380 /* write 5 zero DWORDs */
383 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
384 if (!SUCCEEDED(res
)) return res
;
387 /* Write the wide version if:
388 * + couldn't convert to CP_ACP,
389 * or + it's a directory,
390 * or + there's a character > 0xFF
392 len
= lstrlenW(filePathW
);
393 bWriteWide
= (bUsedDefault
|| (len
> 0 && filePathW
[len
-1]=='\\' ));
397 for(pch
=filePathW
;*pch
;++pch
)
409 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
413 /* write bytes needed for the filepathW (without 0) + 6 */
414 bytesW
= len
*sizeof(WCHAR
) + 6;
415 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
416 if (!SUCCEEDED(res
)) return res
;
418 /* try again, without the extra 6 */
420 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
421 if (!SUCCEEDED(res
)) return res
;
424 res
=IStream_Write(pStm
,&THREE
,sizeof(WORD
),NULL
);
425 if (!SUCCEEDED(res
)) return res
;
427 /* write W string (no 0) */
428 res
=IStream_Write(pStm
,filePathW
,bytesW
,NULL
);
433 /******************************************************************************
434 * FileMoniker_GetSizeMax
436 static HRESULT WINAPI
437 FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
)
439 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
441 TRACE("(%p,%p)\n",iface
,pcbSize
);
446 /* We could calculate exactly (see ...::Save()) but instead
447 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
449 pcbSize
->u
.LowPart
= 0x38 + 4 * lstrlenW(This
->filePathName
);
450 pcbSize
->u
.HighPart
= 0;
455 /******************************************************************************
456 * FileMoniker_Destroy (local function)
457 *******************************************************************************/
458 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
460 TRACE("(%p)\n",This
);
462 if (This
->pMarshal
) IUnknown_Release(This
->pMarshal
);
463 HeapFree(GetProcessHeap(),0,This
->filePathName
);
464 HeapFree(GetProcessHeap(),0,This
);
469 /******************************************************************************
470 * FileMoniker_BindToObject
472 static HRESULT WINAPI
473 FileMonikerImpl_BindToObject(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
474 REFIID riid
, VOID
** ppvResult
)
479 IRunningObjectTable
*prot
=0;
481 IClassFactory
*pcf
=0;
482 IClassActivator
*pca
=0;
484 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
488 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvResult
);
492 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
495 /* if the requested class was loaded before ! we don't need to reload it */
496 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
499 /* first activation of this class */
500 res
=GetClassFile(This
->filePathName
,&clsID
);
503 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
506 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
510 IUnknown_AddRef(pObj
);
518 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
520 if (res
==E_NOINTERFACE
){
522 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
524 if (res
==E_NOINTERFACE
)
525 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
529 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)&ppf
);
531 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
536 IUnknown_AddRef(pObj
);
543 /*res=GetClassFile(This->filePathName,&clsID);
547 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
552 IUnknown_AddRef(pObj);
559 /* get the requested interface from the loaded class */
560 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
562 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
564 IUnknown_Release(pObj
);
568 IRunningObjectTable_Release(prot
);
571 IPersistFile_Release(ppf
);
574 IClassActivator_Release(pca
);
577 IClassFactory_Release(pcf
);
582 /******************************************************************************
583 * FileMoniker_BindToStorage
585 static HRESULT WINAPI
586 FileMonikerImpl_BindToStorage(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
587 REFIID riid
, VOID
** ppvObject
)
593 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
595 if (pmkToLeft
==NULL
){
597 if (IsEqualIID(&IID_IStorage
, riid
)){
599 /* get the file name */
600 IMoniker_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
602 /* verifie if the file contains a storage object */
603 res
=StgIsStorageFile(filePath
);
607 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
613 IStorage_AddRef(pstg
);
618 CoTaskMemFree(filePath
);
621 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
624 return E_NOINTERFACE
;
628 FIXME("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
635 /******************************************************************************
637 ******************************************************************************/
638 static HRESULT WINAPI
639 FileMonikerImpl_Reduce(IMoniker
* iface
, IBindCtx
* pbc
, DWORD dwReduceHowFar
,
640 IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
)
642 TRACE("(%p,%p,%d,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
644 if (ppmkReduced
==NULL
)
647 IMoniker_AddRef(iface
);
651 return MK_S_REDUCED_TO_SELF
;
654 /******************************************************************************
655 * FileMoniker_ComposeWith
657 static HRESULT WINAPI
658 FileMonikerImpl_ComposeWith(IMoniker
* iface
, IMoniker
* pmkRight
,
659 BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
)
662 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
663 static const WCHAR twoPoint
[]={'.','.',0};
664 static const WCHAR bkSlash
[]={'\\',0};
666 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
669 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
671 if (ppmkComposite
==NULL
)
679 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
681 /* check if we have two filemonikers to compose or not */
682 if(mkSys
==MKSYS_FILEMONIKER
){
684 CreateBindCtx(0,&bind
);
686 IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
687 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
689 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
690 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
691 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
693 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
696 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
699 /* for etch "..\" in the left of str2 remove the right element from str1 */
700 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
705 /* the length of the composed path string is raised by the sum of the two paths lengths */
706 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
709 return E_OUTOFMEMORY
;
711 /* new path is the concatenation of the rest of str1 and str2 */
712 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
713 strcatW(newStr
,strDec1
[j
]);
715 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
716 strcatW(newStr
,bkSlash
);
718 for(j
=i
;j
<=lastIdx2
;j
++)
719 strcatW(newStr
,strDec2
[j
]);
721 /* create a new moniker with the new string */
722 res
=CreateFileMoniker(newStr
,ppmkComposite
);
724 /* free all strings space memory used by this function */
725 HeapFree(GetProcessHeap(),0,newStr
);
727 for(i
=0; strDec1
[i
]!=NULL
;i
++)
728 CoTaskMemFree(strDec1
[i
]);
729 for(i
=0; strDec2
[i
]!=NULL
;i
++)
730 CoTaskMemFree(strDec2
[i
]);
731 CoTaskMemFree(strDec1
);
732 CoTaskMemFree(strDec2
);
739 else if(mkSys
==MKSYS_ANTIMONIKER
){
744 else if (fOnlyIfNotGeneric
){
747 return MK_E_NEEDGENERIC
;
751 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
754 /******************************************************************************
757 static HRESULT WINAPI
758 FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
760 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
762 if (ppenumMoniker
== NULL
)
765 *ppenumMoniker
= NULL
;
770 /******************************************************************************
771 * FileMoniker_IsEqual
773 static HRESULT WINAPI
774 FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
776 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
782 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
784 if (pmkOtherMoniker
==NULL
)
787 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
789 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
792 res
= CreateBindCtx(0,&bind
);
793 if (FAILED(res
)) return res
;
795 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
796 int result
= lstrcmpiW(filePath
, This
->filePathName
);
797 CoTaskMemFree(filePath
);
798 if ( result
== 0 ) return S_OK
;
804 /******************************************************************************
807 static HRESULT WINAPI
808 FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
810 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
812 int h
= 0,i
,skip
,len
;
819 val
= This
->filePathName
;
823 for (i
= len
; i
> 0; i
--) {
824 h
= (h
* 37) + val
[off
++];
827 /* only sample some characters */
829 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
830 h
= (h
* 39) + val
[off
];
839 /******************************************************************************
840 * FileMoniker_IsRunning
842 static HRESULT WINAPI
843 FileMonikerImpl_IsRunning(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
844 IMoniker
* pmkNewlyRunning
)
846 IRunningObjectTable
* rot
;
849 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
851 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
857 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
862 res
= IRunningObjectTable_IsRunning(rot
,iface
);
864 IRunningObjectTable_Release(rot
);
869 /******************************************************************************
870 * FileMoniker_GetTimeOfLastChange
871 ******************************************************************************/
872 static HRESULT WINAPI
873 FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
,
874 IMoniker
* pmkToLeft
, FILETIME
* pFileTime
)
876 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
877 IRunningObjectTable
* rot
;
879 WIN32_FILE_ATTRIBUTE_DATA info
;
881 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
889 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
894 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
896 if (FAILED(res
)){ /* the moniker is not registered */
898 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
899 return MK_E_NOOBJECT
;
901 *pFileTime
=info
.ftLastWriteTime
;
907 /******************************************************************************
908 * FileMoniker_Inverse
910 static HRESULT WINAPI
911 FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
913 TRACE("(%p,%p)\n",iface
,ppmk
);
915 return CreateAntiMoniker(ppmk
);
918 /******************************************************************************
919 * FileMoniker_CommonPrefixWith
921 static HRESULT WINAPI
922 FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
925 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
928 ULONG nb1
,nb2
,i
,sameIdx
;
929 BOOL machimeNameCase
=FALSE
;
931 if (ppmkPrefix
==NULL
)
939 /* check if we have the same type of moniker */
940 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
942 if(mkSys
==MKSYS_FILEMONIKER
){
945 CreateBindCtx(0,&pbind
);
947 /* create a string based on common part of the two paths */
949 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
950 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
952 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
953 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
955 if (nb1
==0 || nb2
==0)
956 return MK_E_NOPREFIX
;
958 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
962 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
963 (stringTable2
[sameIdx
]!=NULL
) &&
964 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
966 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
968 machimeNameCase
=TRUE
;
970 for(i
=2;i
<sameIdx
;i
++)
972 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
973 machimeNameCase
=FALSE
;
978 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
981 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
985 for(i
=0;i
<sameIdx
;i
++)
986 strcatW(commonPath
,stringTable1
[i
]);
989 CoTaskMemFree(stringTable1
[i
]);
991 CoTaskMemFree(stringTable1
);
994 CoTaskMemFree(stringTable2
[i
]);
996 CoTaskMemFree(stringTable2
);
998 ret
= CreateFileMoniker(commonPath
,ppmkPrefix
);
1000 HeapFree(GetProcessHeap(),0,commonPath
);
1004 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1007 /******************************************************************************
1008 * DecomposePath (local function)
1010 int FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
1012 static const WCHAR bSlash
[] = {'\\',0};
1013 WCHAR word
[MAX_PATH
];
1014 int i
=0,j
,tabIndex
=0;
1015 LPOLESTR
*strgtable
;
1017 int len
=lstrlenW(str
);
1019 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
1021 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1023 if (strgtable
==NULL
)
1024 return E_OUTOFMEMORY
;
1028 if(str
[i
]==bSlash
[0]){
1030 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1032 if (strgtable
[tabIndex
]==NULL
)
1033 return E_OUTOFMEMORY
;
1035 strcpyW(strgtable
[tabIndex
++],bSlash
);
1042 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1047 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1049 if (strgtable
[tabIndex
]==NULL
)
1050 return E_OUTOFMEMORY
;
1052 strcpyW(strgtable
[tabIndex
++],word
);
1055 strgtable
[tabIndex
]=NULL
;
1057 *stringTable
=strgtable
;
1062 /******************************************************************************
1063 * FileMoniker_RelativePathTo
1065 static HRESULT WINAPI
1066 FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1070 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1071 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1072 static const WCHAR back
[] ={'.','.','\\',0};
1074 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1076 if (ppmkRelPath
==NULL
)
1080 return E_INVALIDARG
;
1082 res
=CreateBindCtx(0,&bind
);
1086 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1089 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1093 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1094 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1096 if (FAILED(len1
) || FAILED(len2
))
1097 return E_OUTOFMEMORY
;
1099 /* count the number of similar items from the begin of the two paths */
1100 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1101 (tabStr2
[sameIdx
]!=NULL
) &&
1102 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1104 /* begin the construction of relativePath */
1105 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1106 /* by "..\\" in the begin */
1107 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1111 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1112 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1113 if (*tabStr1
[j
]!='\\')
1114 strcatW(relPath
,back
);
1116 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1117 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1118 strcatW(relPath
,tabStr2
[j
]);
1120 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1122 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1123 CoTaskMemFree(tabStr1
[j
]);
1124 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1125 CoTaskMemFree(tabStr2
[j
]);
1126 CoTaskMemFree(tabStr1
);
1127 CoTaskMemFree(tabStr2
);
1128 CoTaskMemFree(str1
);
1129 CoTaskMemFree(str2
);
1130 HeapFree(GetProcessHeap(),0,relPath
);
1132 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1138 /******************************************************************************
1139 * FileMoniker_GetDisplayName
1141 static HRESULT WINAPI
1142 FileMonikerImpl_GetDisplayName(IMoniker
* iface
, IBindCtx
* pbc
,
1143 IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
)
1145 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
1147 int len
=lstrlenW(This
->filePathName
);
1149 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1151 if (ppszDisplayName
==NULL
)
1154 if (pmkToLeft
!=NULL
)
1155 return E_INVALIDARG
;
1157 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1158 if (*ppszDisplayName
==NULL
)
1159 return E_OUTOFMEMORY
;
1161 strcpyW(*ppszDisplayName
,This
->filePathName
);
1163 TRACE("-- %s\n", debugstr_w(*ppszDisplayName
));
1168 /******************************************************************************
1169 * FileMoniker_ParseDisplayName
1171 static HRESULT WINAPI
1172 FileMonikerImpl_ParseDisplayName(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
1173 LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
)
1175 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1179 /******************************************************************************
1180 * FileMoniker_IsSystemMoniker
1182 static HRESULT WINAPI
1183 FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1185 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1190 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1195 /*******************************************************************************
1196 * FileMonikerIROTData_QueryInterface
1198 static HRESULT WINAPI
1199 FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1202 IMoniker
*This
= impl_from_IROTData(iface
);
1204 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1206 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1209 /***********************************************************************
1210 * FileMonikerIROTData_AddRef
1213 FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1215 IMoniker
*This
= impl_from_IROTData(iface
);
1217 TRACE("(%p)\n",This
);
1219 return IMoniker_AddRef(This
);
1222 /***********************************************************************
1223 * FileMonikerIROTData_Release
1226 FileMonikerROTDataImpl_Release(IROTData
* iface
)
1228 IMoniker
*This
= impl_from_IROTData(iface
);
1230 TRACE("(%p)\n",This
);
1232 return FileMonikerImpl_Release(This
);
1235 /******************************************************************************
1236 * FileMonikerIROTData_GetComparaisonData
1238 static HRESULT WINAPI
1239 FileMonikerROTDataImpl_GetComparisonData(IROTData
* iface
, BYTE
* pbData
,
1240 ULONG cbMax
, ULONG
* pcbData
)
1242 IMoniker
*This
= impl_from_IROTData(iface
);
1243 FileMonikerImpl
*This1
= (FileMonikerImpl
*)This
;
1244 int len
= (strlenW(This1
->filePathName
)+1);
1248 TRACE("(%p, %u, %p)\n", pbData
, cbMax
, pcbData
);
1250 *pcbData
= sizeof(CLSID
) + len
* sizeof(WCHAR
);
1251 if (cbMax
< *pcbData
)
1252 return E_OUTOFMEMORY
;
1254 memcpy(pbData
, &CLSID_FileMoniker
, sizeof(CLSID
));
1255 pszFileName
= (LPWSTR
)(pbData
+sizeof(CLSID
));
1256 for (i
= 0; i
< len
; i
++)
1257 pszFileName
[i
] = toupperW(This1
->filePathName
[i
]);
1263 * Virtual function table for the FileMonikerImpl class which include IPersist,
1264 * IPersistStream and IMoniker functions.
1266 static const IMonikerVtbl VT_FileMonikerImpl
=
1268 FileMonikerImpl_QueryInterface
,
1269 FileMonikerImpl_AddRef
,
1270 FileMonikerImpl_Release
,
1271 FileMonikerImpl_GetClassID
,
1272 FileMonikerImpl_IsDirty
,
1273 FileMonikerImpl_Load
,
1274 FileMonikerImpl_Save
,
1275 FileMonikerImpl_GetSizeMax
,
1276 FileMonikerImpl_BindToObject
,
1277 FileMonikerImpl_BindToStorage
,
1278 FileMonikerImpl_Reduce
,
1279 FileMonikerImpl_ComposeWith
,
1280 FileMonikerImpl_Enum
,
1281 FileMonikerImpl_IsEqual
,
1282 FileMonikerImpl_Hash
,
1283 FileMonikerImpl_IsRunning
,
1284 FileMonikerImpl_GetTimeOfLastChange
,
1285 FileMonikerImpl_Inverse
,
1286 FileMonikerImpl_CommonPrefixWith
,
1287 FileMonikerImpl_RelativePathTo
,
1288 FileMonikerImpl_GetDisplayName
,
1289 FileMonikerImpl_ParseDisplayName
,
1290 FileMonikerImpl_IsSystemMoniker
1293 /* Virtual function table for the IROTData class. */
1294 static const IROTDataVtbl VT_ROTDataImpl
=
1296 FileMonikerROTDataImpl_QueryInterface
,
1297 FileMonikerROTDataImpl_AddRef
,
1298 FileMonikerROTDataImpl_Release
,
1299 FileMonikerROTDataImpl_GetComparisonData
1302 /******************************************************************************
1303 * FileMoniker_Construct (local function)
1305 static HRESULT WINAPI
1306 FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
1309 int sizeStr
=lstrlenW(lpszPathName
);
1311 static const WCHAR twoPoint
[]={'.','.',0};
1312 static const WCHAR bkSlash
[]={'\\',0};
1315 TRACE("(%p,%s)\n",This
,debugstr_w(lpszPathName
));
1317 /* Initialize the virtual fgunction table. */
1318 This
->lpvtbl1
= &VT_FileMonikerImpl
;
1319 This
->lpvtbl2
= &VT_ROTDataImpl
;
1321 This
->pMarshal
= NULL
;
1323 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
1325 if (This
->filePathName
==NULL
)
1326 return E_OUTOFMEMORY
;
1328 strcpyW(This
->filePathName
,lpszPathName
);
1330 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
1335 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
1340 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
1346 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
1354 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
1357 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
1359 *This
->filePathName
=0;
1361 for(i
=0;tabStr
[i
]!=NULL
;i
++)
1362 strcatW(This
->filePathName
,tabStr
[i
]);
1365 strcatW(This
->filePathName
,bkSlash
);
1368 for(i
=0; tabStr
[i
]!=NULL
;i
++)
1369 CoTaskMemFree(tabStr
[i
]);
1370 CoTaskMemFree(tabStr
);
1375 /******************************************************************************
1376 * CreateFileMoniker (OLE32.@)
1377 ******************************************************************************/
1378 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1380 FileMonikerImpl
* newFileMoniker
;
1383 TRACE("(%s,%p)\n",debugstr_w(lpszPathName
),ppmk
);
1393 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1395 if (!newFileMoniker
)
1396 return E_OUTOFMEMORY
;
1398 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1401 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&IID_IMoniker
,(void**)ppmk
);
1403 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1408 static HRESULT WINAPI
FileMonikerCF_QueryInterface(LPCLASSFACTORY iface
,
1409 REFIID riid
, LPVOID
*ppv
)
1412 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1415 IUnknown_AddRef(iface
);
1418 return E_NOINTERFACE
;
1421 static ULONG WINAPI
FileMonikerCF_AddRef(LPCLASSFACTORY iface
)
1423 return 2; /* non-heap based object */
1426 static ULONG WINAPI
FileMonikerCF_Release(LPCLASSFACTORY iface
)
1428 return 1; /* non-heap based object */
1431 static HRESULT WINAPI
FileMonikerCF_CreateInstance(LPCLASSFACTORY iface
,
1432 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1434 FileMonikerImpl
* newFileMoniker
;
1436 static const WCHAR wszEmpty
[] = { 0 };
1438 TRACE("(%p, %s, %p)\n", pUnk
, debugstr_guid(riid
), ppv
);
1443 return CLASS_E_NOAGGREGATION
;
1445 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1446 if (!newFileMoniker
)
1447 return E_OUTOFMEMORY
;
1449 hr
= FileMonikerImpl_Construct(newFileMoniker
, wszEmpty
);
1452 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
, riid
, ppv
);
1454 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1459 static HRESULT WINAPI
FileMonikerCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1461 FIXME("(%d), stub!\n",fLock
);
1465 static const IClassFactoryVtbl FileMonikerCFVtbl
=
1467 FileMonikerCF_QueryInterface
,
1468 FileMonikerCF_AddRef
,
1469 FileMonikerCF_Release
,
1470 FileMonikerCF_CreateInstance
,
1471 FileMonikerCF_LockServer
1473 static const IClassFactoryVtbl
*FileMonikerCF
= &FileMonikerCFVtbl
;
1475 HRESULT
FileMonikerCF_Create(REFIID riid
, LPVOID
*ppv
)
1477 return IClassFactory_QueryInterface((IClassFactory
*)&FileMonikerCF
, riid
, ppv
);