1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
10 #include "debugtools.h"
12 #include "wine/obj_storage.h"
13 #include "wine/obj_moniker.h"
14 #include "wine/obj_base.h"
16 DEFAULT_DEBUG_CHANNEL(ole
)
18 /* filemoniker data structure */
19 typedef struct FileMonikerImpl
{
21 ICOM_VTABLE(IMoniker
)* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
23 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
24 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
26 ICOM_VTABLE(IROTData
)* lpvtbl2
; /* VTable relative to the IROTData interface.*/
28 ULONG ref
; /* reference counter for this object */
30 LPOLESTR filePathName
; /* path string identified by this filemoniker */
34 /********************************************************************************/
35 /* FileMoniker prototype functions : */
37 /* IUnknown prototype functions */
38 static HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
);
39 static ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
);
40 static ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
);
42 /* IPersist prototype functions */
43 static HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
);
45 /* IPersistStream prototype functions */
46 static HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
);
47 static HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
);
48 static HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
);
49 static HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
);
51 /* IMoniker prototype functions */
52 static HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
53 static HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
54 static HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,IBindCtx
* pbc
, DWORD dwReduceHowFar
,IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
);
55 static HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,IMoniker
* pmkRight
,BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
);
56 static HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
);
57 static HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
);
58 static HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
);
59 static HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, IMoniker
* pmkNewlyRunning
);
60 static HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
, FILETIME
* pFileTime
);
61 static HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
);
62 static HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
, IMoniker
** ppmkPrefix
);
63 static HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
);
64 static HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
);
65 static HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
);
66 static HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
);
68 /********************************************************************************/
69 /* IROTData prototype functions */
71 /* IUnknown prototype functions */
72 static HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
* iface
,REFIID riid
,VOID
** ppvObject
);
73 static ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
* iface
);
74 static ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
);
76 /* IROTData prototype function */
77 static HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,BYTE
* pbData
,ULONG cbMax
,ULONG
* pcbData
);
79 /* Local function used by filemoniker implementation */
80 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
81 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
82 int WINAPI
FileMonikerImpl_DecomposePath(LPOLESTR str
, LPOLESTR
** tabStr
);
85 /********************************************************************************/
86 /* Virtual function table for the FileMonikerImpl class witch include Ipersist,*/
87 /* IPersistStream and IMoniker functions. */
88 static ICOM_VTABLE(IMoniker
) VT_FileMonikerImpl
=
90 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
91 FileMonikerImpl_QueryInterface
,
92 FileMonikerImpl_AddRef
,
93 FileMonikerImpl_Release
,
94 FileMonikerImpl_GetClassID
,
95 FileMonikerImpl_IsDirty
,
98 FileMonikerImpl_GetSizeMax
,
99 FileMonikerImpl_BindToObject
,
100 FileMonikerImpl_BindToStorage
,
101 FileMonikerImpl_Reduce
,
102 FileMonikerImpl_ComposeWith
,
103 FileMonikerImpl_Enum
,
104 FileMonikerImpl_IsEqual
,
105 FileMonikerImpl_Hash
,
106 FileMonikerImpl_IsRunning
,
107 FileMonikerImpl_GetTimeOfLastChange
,
108 FileMonikerImpl_Inverse
,
109 FileMonikerImpl_CommonPrefixWith
,
110 FileMonikerImpl_RelativePathTo
,
111 FileMonikerImpl_GetDisplayName
,
112 FileMonikerImpl_ParseDisplayName
,
113 FileMonikerImpl_IsSystemMoniker
116 /********************************************************************************/
117 /* Virtual function table for the IROTData class. */
118 static ICOM_VTABLE(IROTData
) VT_ROTDataImpl
=
120 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
121 FileMonikerROTDataImpl_QueryInterface
,
122 FileMonikerROTDataImpl_AddRef
,
123 FileMonikerROTDataImpl_Release
,
124 FileMonikerROTDataImpl_GetComparaisonData
127 /*******************************************************************************
128 * FileMoniker_QueryInterface
129 *******************************************************************************/
130 HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
132 ICOM_THIS(FileMonikerImpl
,iface
);
134 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
136 /* Perform a sanity check on the parameters.*/
137 if ( (This
==0) || (ppvObject
==0) )
140 /* Initialize the return parameter */
143 /* Compare the riid with the interface IDs implemented by this object.*/
144 if (IsEqualIID(&IID_IUnknown
, riid
) ||
145 IsEqualIID(&IID_IPersist
, riid
) ||
146 IsEqualIID(&IID_IPersistStream
,riid
) ||
147 IsEqualIID(&IID_IMoniker
, riid
)
151 else if (IsEqualIID(&IID_IROTData
, riid
))
152 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
154 /* Check that we obtained an interface.*/
156 return E_NOINTERFACE
;
158 /* Query Interface always increases the reference count by one when it is successful */
159 FileMonikerImpl_AddRef(iface
);
164 /******************************************************************************
166 ******************************************************************************/
167 ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
)
169 ICOM_THIS(FileMonikerImpl
,iface
);
171 TRACE("(%p)\n",iface
);
173 return ++(This
->ref
);
176 /******************************************************************************
177 * FileMoniker_Release
178 ******************************************************************************/
179 ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
)
181 ICOM_THIS(FileMonikerImpl
,iface
);
183 TRACE("(%p)\n",iface
);
187 /* destroy the object if there's no more reference on it */
190 FileMonikerImpl_Destroy(This
);
197 /******************************************************************************
198 * FileMoniker_GetClassID
199 ******************************************************************************/
200 HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
,
201 CLSID
*pClassID
)/* Pointer to CLSID of object */
203 TRACE("(%p,%p),stub!\n",iface
,pClassID
);
208 *pClassID
= CLSID_FileMoniker
;
213 /******************************************************************************
214 * FileMoniker_IsDirty
215 ******************************************************************************/
216 HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
)
218 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
219 method in the OLE-provided moniker interfaces always return S_FALSE because
220 their internal state never changes. */
222 TRACE("(%p)\n",iface
);
227 /******************************************************************************
229 ******************************************************************************/
230 HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
,IStream
* pStm
)
237 DWORD dwbuffer
,length
,i
,doubleLenHex
,doubleLenDec
;
239 ICOM_THIS(FileMonikerImpl
,iface
);
241 TRACE("(%p,%p)\n",iface
,pStm
);
243 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
245 /* first WORD is non significative */
246 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
247 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
250 /* read filePath string length (plus one) */
251 res
=IStream_Read(pStm
,&length
,sizeof(DWORD
),&bread
);
252 if (bread
!= sizeof(DWORD
))
255 /* read filePath string */
256 filePathA
=HeapAlloc(GetProcessHeap(),0,length
);
257 res
=IStream_Read(pStm
,filePathA
,length
,&bread
);
261 /* read the first constant */
262 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
263 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
269 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
270 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
277 doubleLenHex
=doubleLenDec
=2*length
;
281 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
282 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
288 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
289 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
292 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
293 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
296 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
298 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
299 if (bread
!=doubleLenHex
)
302 if (This
->filePathName
!=NULL
)
303 HeapFree(GetProcessHeap(),0,This
->filePathName
);
305 This
->filePathName
=filePathW
;
307 HeapFree(GetProcessHeap(),0,filePathA
);
312 /******************************************************************************
314 ******************************************************************************/
315 HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
,
316 IStream
* pStm
,/* poniter to the stream where the object is to be saved */
317 BOOL fClearDirty
)/* Specifies whether to clear the dirty flag */
319 /* this function saves data of this object. In the begining I thougth that I have just to write
320 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
321 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
322 * what did this function do exactly ! but I have no idear a bout its logic !
323 * I guessed data who must be writen on stream wich is:
324 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
325 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
326 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
327 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
330 ICOM_THIS(FileMonikerImpl
,iface
);
333 LPOLESTR filePathW
=This
->filePathName
;
335 DWORD len
=1+lstrlenW(filePathW
);
337 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
338 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
345 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
350 /* write a DWORD seted to 0 : constant */
351 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
353 /* write length of filePath string ( "\0" included )*/
354 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
356 /* write filePath string type A */
357 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
358 lstrcpyWtoA(filePathA
,filePathW
);
359 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
360 HeapFree(GetProcessHeap(),0,filePathA
);
362 /* write a DWORD seted to 0xDEADFFFF: constant */
363 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
366 /* write 10 times a DWORD seted to 0 : constants */
368 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
373 doubleLenHex
=doubleLenDec
=2*len
;
377 /* write double-length of the path string ( "\0" included )*/
378 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
383 /* write double-length (hexa representation) of the path string ( "\0" included ) */
384 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
386 /* write a WORD seted to 0x3: constant */
387 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
389 /* write path unicode string */
390 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
395 /******************************************************************************
396 * FileMoniker_GetSizeMax
397 ******************************************************************************/
398 HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
,
399 ULARGE_INTEGER
* pcbSize
)/* Pointer to size of stream needed to save object */
401 ICOM_THIS(FileMonikerImpl
,iface
);
402 DWORD len
=lstrlenW(This
->filePathName
);
405 TRACE("(%p,%p)\n",iface
,pcbSize
);
410 /* for more details see FileMonikerImpl_Save coments */
412 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
413 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
414 (len
+1)+ /* filePath string */
415 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
416 10*sizeof(WORD
)+ /* 10 zero WORD */
417 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
419 if (len
==0 || len
> 8)
422 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
423 sizeof(WORD
)+ /* constant : 0x3 */
424 len
*sizeof(WCHAR
); /* unicde filePath string */
426 pcbSize
->LowPart
=sizeMAx
;
432 /******************************************************************************
433 * FileMoniker_Construct (local function)
434 *******************************************************************************/
435 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
438 int sizeStr
=lstrlenW(lpszPathName
);
440 WCHAR twoPoint
[]={'.','.',0};
441 WCHAR bkSlash
[]={'\\',0};
444 TRACE("(%p,%p)\n",This
,lpszPathName
);
446 /* Initialize the virtual fgunction table. */
447 This
->lpvtbl1
= &VT_FileMonikerImpl
;
448 This
->lpvtbl2
= &VT_ROTDataImpl
;
451 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
453 if (This
->filePathName
==NULL
)
454 return E_OUTOFMEMORY
;
456 lstrcpyW(This
->filePathName
,lpszPathName
);
458 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
463 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
468 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
474 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
482 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
485 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
487 *This
->filePathName
=0;
489 for(i
=0;tabStr
[i
]!=NULL
;i
++)
490 lstrcatW(This
->filePathName
,tabStr
[i
]);
493 lstrcatW(This
->filePathName
,bkSlash
);
496 for(i
=0; tabStr
[i
]!=NULL
;i
++)
497 CoTaskMemFree(tabStr
[i
]);
498 CoTaskMemFree(tabStr
);
503 /******************************************************************************
504 * FileMoniker_Destroy (local function)
505 *******************************************************************************/
506 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
508 TRACE("(%p)\n",This
);
510 if (This
->filePathName
!=NULL
)
511 HeapFree(GetProcessHeap(),0,This
->filePathName
);
513 HeapFree(GetProcessHeap(),0,This
);
518 /******************************************************************************
519 * FileMoniker_BindToObject
520 ******************************************************************************/
521 HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,
530 IRunningObjectTable
*prot
=0;
532 IClassFactory
*pcf
=0;
533 IClassActivator
*pca
=0;
535 ICOM_THIS(FileMonikerImpl
,iface
);
539 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvResult
);
543 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
546 /* if the requested class was loaded befor ! we dont need to reload it */
547 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
550 /* first activation of this class */
551 res
=GetClassFile(This
->filePathName
,&clsID
);
554 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
557 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
561 IUnknown_AddRef(pObj
);
569 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
571 if (res
==E_NOINTERFACE
){
573 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
575 if (res
==E_NOINTERFACE
)
576 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
580 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
582 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
587 IUnknown_AddRef(pObj
);
594 /*res=GetClassFile(This->filePathName,&clsID);
598 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
603 IUnknown_AddRef(pObj);
610 /* get the requested interface from the loaded class */
611 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
613 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
615 IUnknown_Release(pObj
);
619 IRunningObjectTable_Release(prot
);
622 IPersistFile_Release(ppf
);
625 IClassActivator_Release(pca
);
628 IClassFactory_Release(pcf
);
633 /******************************************************************************
634 * FileMoniker_BindToStorage
635 ******************************************************************************/
636 HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,
646 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
648 if (pmkToLeft
==NULL
){
650 if (IsEqualIID(&IID_IStorage
, riid
)){
652 /* get the file name */
653 FileMonikerImpl_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
655 /* verifie if the file contains a storage object */
656 res
=StgIsStorageFile(filePath
);
660 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
666 IStorage_AddRef(pstg
);
671 CoTaskMemFree(filePath
);
674 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
679 return E_NOINTERFACE
;
683 FIXME("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
690 /******************************************************************************
692 ******************************************************************************/
693 HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,
695 DWORD dwReduceHowFar
,
696 IMoniker
** ppmkToLeft
,
697 IMoniker
** ppmkReduced
)
699 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
701 if (ppmkReduced
==NULL
)
704 FileMonikerImpl_AddRef(iface
);
708 return MK_S_REDUCED_TO_SELF
;
710 /******************************************************************************
711 * FileMoniker_ComposeWith
712 ******************************************************************************/
713 HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,
715 BOOL fOnlyIfNotGeneric
,
716 IMoniker
** ppmkComposite
)
719 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
720 WCHAR twoPoint
[]={'.','.',0};
721 WCHAR bkSlash
[]={'\\',0};
723 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
726 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
728 if (ppmkComposite
==NULL
)
736 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
738 /* check if we have two filemonikers to compose or not */
739 if(mkSys
==MKSYS_FILEMONIKER
){
741 CreateBindCtx(0,&bind
);
743 FileMonikerImpl_GetDisplayName(iface
,bind
,NULL
,&str1
);
744 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
746 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
747 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
748 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
750 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
753 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
756 /* for etch "..\" in the left of str2 remove the right element from str1 */
757 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
762 /* the length of the composed path string is raised by the sum of the two paths lengths */
763 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
766 return E_OUTOFMEMORY
;
768 /* new path is the concatenation of the rest of str1 and str2 */
769 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
770 lstrcatW(newStr
,strDec1
[j
]);
772 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
773 lstrcatW(newStr
,bkSlash
);
775 for(j
=i
;j
<=lastIdx2
;j
++)
776 lstrcatW(newStr
,strDec2
[j
]);
778 /* create a new moniker with the new string */
779 res
=CreateFileMoniker(newStr
,ppmkComposite
);
781 /* free all strings space memory used by this function */
782 HeapFree(GetProcessHeap(),0,newStr
);
784 for(i
=0; strDec1
[i
]!=NULL
;i
++)
785 CoTaskMemFree(strDec1
[i
]);
786 for(i
=0; strDec2
[i
]!=NULL
;i
++)
787 CoTaskMemFree(strDec2
[i
]);
788 CoTaskMemFree(strDec1
);
789 CoTaskMemFree(strDec2
);
796 else if(mkSys
==MKSYS_ANTIMONIKER
){
801 else if (fOnlyIfNotGeneric
){
804 return MK_E_NEEDGENERIC
;
808 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
811 /******************************************************************************
813 ******************************************************************************/
814 HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
816 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
818 if (ppenumMoniker
== NULL
)
821 *ppenumMoniker
= NULL
;
826 /******************************************************************************
827 * FileMoniker_IsEqual
828 ******************************************************************************/
829 HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
831 ICOM_THIS(FileMonikerImpl
,iface
);
837 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
839 if (pmkOtherMoniker
==NULL
)
842 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
844 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
848 res
=CreateBindCtx(0,&bind
);
852 IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
);
854 if (lstrcmpiW(filePath
,
855 This
->filePathName
)!=0)
862 /******************************************************************************
864 ******************************************************************************/
865 HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
867 ICOM_THIS(FileMonikerImpl
,iface
);
869 int h
= 0,i
,skip
,len
;
876 val
= This
->filePathName
;
880 for (i
= len
; i
> 0; i
--) {
881 h
= (h
* 37) + val
[off
++];
884 /* only sample some characters */
886 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
887 h
= (h
* 39) + val
[off
];
896 /******************************************************************************
897 * FileMoniker_IsRunning
898 ******************************************************************************/
899 HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,
902 IMoniker
* pmkNewlyRunning
)
904 IRunningObjectTable
* rot
;
907 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
909 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
915 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
920 res
= IRunningObjectTable_IsRunning(rot
,iface
);
922 IRunningObjectTable_Release(rot
);
927 /******************************************************************************
928 * FileMoniker_GetTimeOfLastChange
929 ******************************************************************************/
930 HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
,
935 ICOM_THIS(FileMonikerImpl
,iface
);
936 IRunningObjectTable
* rot
;
938 WIN32_FILE_ATTRIBUTE_DATA info
;
940 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
948 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
953 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
955 if (FAILED(res
)){ /* the moniker is not registred */
957 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
958 return MK_E_NOOBJECT
;
960 *pFileTime
=info
.ftLastWriteTime
;
966 /******************************************************************************
967 * FileMoniker_Inverse
968 ******************************************************************************/
969 HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
972 TRACE("(%p,%p)\n",iface
,ppmk
);
974 return CreateAntiMoniker(ppmk
);
977 /******************************************************************************
978 * FileMoniker_CommonPrefixWith
979 ******************************************************************************/
980 HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
983 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
986 ULONG nb1
,nb2
,i
,sameIdx
;
987 BOOL machimeNameCase
=FALSE
;
989 if (ppmkPrefix
==NULL
)
997 /* check if we have the same type of moniker */
998 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
1000 if(mkSys
==MKSYS_FILEMONIKER
){
1002 CreateBindCtx(0,&pbind
);
1004 /* create a string based on common part of the two paths */
1006 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
1007 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
1009 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
1010 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
1012 if (nb1
==0 || nb2
==0)
1013 return MK_E_NOPREFIX
;
1015 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
1019 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
1020 (stringTable2
[sameIdx
]!=NULL
) &&
1021 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
1023 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1025 machimeNameCase
=TRUE
;
1027 for(i
=2;i
<sameIdx
;i
++)
1029 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1030 machimeNameCase
=FALSE
;
1035 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1038 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1039 return MK_E_NOPREFIX
;
1041 for(i
=0;i
<sameIdx
;i
++)
1042 lstrcatW(commonPath
,stringTable1
[i
]);
1045 CoTaskMemFree(stringTable1
[i
]);
1047 CoTaskMemFree(stringTable1
);
1050 CoTaskMemFree(stringTable2
[i
]);
1052 CoTaskMemFree(stringTable2
);
1054 HeapFree(GetProcessHeap(),0,commonPath
);
1056 return CreateFileMoniker(commonPath
,ppmkPrefix
);
1059 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1062 /******************************************************************************
1063 * DecomposePath (local function)
1064 ******************************************************************************/
1065 int WINAPI
FileMonikerImpl_DecomposePath(LPOLESTR str
, LPOLESTR
** stringTable
)
1067 WCHAR bSlash
[] = {'\\',0};
1069 int i
=0,j
,tabIndex
=0;
1070 LPOLESTR
*strgtable
;
1072 int len
=lstrlenW(str
);
1074 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1076 if (strgtable
==NULL
)
1077 return E_OUTOFMEMORY
;
1081 if(str
[i
]==bSlash
[0]){
1083 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1085 if (strgtable
[tabIndex
]==NULL
)
1086 return E_OUTOFMEMORY
;
1088 lstrcpyW(strgtable
[tabIndex
++],bSlash
);
1095 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1100 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1102 if (strgtable
[tabIndex
]==NULL
)
1103 return E_OUTOFMEMORY
;
1105 lstrcpyW(strgtable
[tabIndex
++],word
);
1108 strgtable
[tabIndex
]=NULL
;
1110 *stringTable
=strgtable
;
1115 /******************************************************************************
1116 * FileMoniker_RelativePathTo
1117 ******************************************************************************/
1118 HRESULT WINAPI
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 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
);
1146 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1148 if (FAILED(len1
) || FAILED(len2
))
1149 return E_OUTOFMEMORY
;
1151 /* count the number of similar items from the begin of the two paths */
1152 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1153 (tabStr2
[sameIdx
]!=NULL
) &&
1154 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1156 /* begin the construction of relativePath */
1157 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1158 /* by "..\\" in the begin */
1159 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1163 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1164 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1165 if (*tabStr1
[j
]!='\\')
1166 lstrcatW(relPath
,back
);
1168 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1169 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1170 lstrcatW(relPath
,tabStr2
[j
]);
1172 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1174 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1175 CoTaskMemFree(tabStr1
[j
]);
1176 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1177 CoTaskMemFree(tabStr2
[j
]);
1178 CoTaskMemFree(tabStr1
);
1179 CoTaskMemFree(tabStr2
);
1180 CoTaskMemFree(str1
);
1181 CoTaskMemFree(str2
);
1182 HeapFree(GetProcessHeap(),0,relPath
);
1184 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1190 /******************************************************************************
1191 * FileMoniker_GetDisplayName
1192 ******************************************************************************/
1193 HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,
1195 IMoniker
* pmkToLeft
,
1196 LPOLESTR
*ppszDisplayName
)
1198 ICOM_THIS(FileMonikerImpl
,iface
);
1200 int len
=lstrlenW(This
->filePathName
);
1202 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1204 if (ppszDisplayName
==NULL
)
1207 if (pmkToLeft
!=NULL
)
1208 return E_INVALIDARG
;
1210 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1211 if (*ppszDisplayName
==NULL
)
1212 return E_OUTOFMEMORY
;
1214 lstrcpyW(*ppszDisplayName
,This
->filePathName
);
1219 /******************************************************************************
1220 * FileMoniker_ParseDisplayName
1221 ******************************************************************************/
1222 HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,
1224 IMoniker
* pmkToLeft
,
1225 LPOLESTR pszDisplayName
,
1229 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1233 /******************************************************************************
1234 * FileMoniker_IsSystemMonker
1235 ******************************************************************************/
1236 HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1238 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1243 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1248 /*******************************************************************************
1249 * FileMonikerIROTData_QueryInterface
1250 *******************************************************************************/
1251 HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1254 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1256 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
1258 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1261 /***********************************************************************
1262 * FileMonikerIROTData_AddRef
1264 ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1266 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1268 TRACE("(%p)\n",This
);
1270 return FileMonikerImpl_AddRef(This
);
1273 /***********************************************************************
1274 * FileMonikerIROTData_Release
1276 ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
)
1278 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1280 TRACE("(%p)\n",This
);
1282 return FileMonikerImpl_Release(This
);
1285 /******************************************************************************
1286 * FileMonikerIROTData_GetComparaisonData
1287 ******************************************************************************/
1288 HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,
1293 FIXME("(),stub!\n");
1297 /******************************************************************************
1298 * CreateFileMoniker16
1299 ******************************************************************************/
1300 HRESULT WINAPI
CreateFileMoniker16(LPCOLESTR16 lpszPathName
,LPMONIKER
* ppmk
)
1303 FIXME("(%s,%p),stub!\n",lpszPathName
,ppmk
);
1307 /******************************************************************************
1309 ******************************************************************************/
1310 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1312 FileMonikerImpl
* newFileMoniker
= 0;
1313 HRESULT hr
= E_FAIL
;
1314 IID riid
=IID_IMoniker
;
1316 TRACE("(%p,%p)\n",lpszPathName
,ppmk
);
1321 if(lpszPathName
==NULL
)
1326 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1328 if (newFileMoniker
== 0)
1329 return E_OUTOFMEMORY
;
1331 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1334 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&riid
,(void**)ppmk
);
1336 HeapFree(GetProcessHeap(),0,newFileMoniker
);