1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
15 #include "wine/obj_storage.h"
16 #include "wine/obj_base.h"
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(const 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 FileMonikerImpl_QueryInterface
,
91 FileMonikerImpl_AddRef
,
92 FileMonikerImpl_Release
,
93 FileMonikerImpl_GetClassID
,
94 FileMonikerImpl_IsDirty
,
97 FileMonikerImpl_GetSizeMax
,
98 FileMonikerImpl_BindToObject
,
99 FileMonikerImpl_BindToStorage
,
100 FileMonikerImpl_Reduce
,
101 FileMonikerImpl_ComposeWith
,
102 FileMonikerImpl_Enum
,
103 FileMonikerImpl_IsEqual
,
104 FileMonikerImpl_Hash
,
105 FileMonikerImpl_IsRunning
,
106 FileMonikerImpl_GetTimeOfLastChange
,
107 FileMonikerImpl_Inverse
,
108 FileMonikerImpl_CommonPrefixWith
,
109 FileMonikerImpl_RelativePathTo
,
110 FileMonikerImpl_GetDisplayName
,
111 FileMonikerImpl_ParseDisplayName
,
112 FileMonikerImpl_IsSystemMoniker
115 /********************************************************************************/
116 /* Virtual function table for the IROTData class. */
117 static ICOM_VTABLE(IROTData
) VT_ROTDataImpl
=
119 FileMonikerROTDataImpl_QueryInterface
,
120 FileMonikerROTDataImpl_AddRef
,
121 FileMonikerROTDataImpl_Release
,
122 FileMonikerROTDataImpl_GetComparaisonData
125 /*******************************************************************************
126 * FileMoniker_QueryInterface
127 *******************************************************************************/
128 HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
130 ICOM_THIS(FileMonikerImpl
,iface
);
132 TRACE(ole
,"(%p,%p,%p)\n",This
,riid
,ppvObject
);
134 /* Perform a sanity check on the parameters.*/
135 if ( (This
==0) || (ppvObject
==0) )
138 /* Initialize the return parameter */
141 /* Compare the riid with the interface IDs implemented by this object.*/
142 if (IsEqualIID(&IID_IUnknown
, riid
) ||
143 IsEqualIID(&IID_IPersist
, riid
) ||
144 IsEqualIID(&IID_IPersistStream
,riid
) ||
145 IsEqualIID(&IID_IMoniker
, riid
)
149 else if (IsEqualIID(&IID_IROTData
, riid
))
150 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
152 /* Check that we obtained an interface.*/
154 return E_NOINTERFACE
;
156 /* Query Interface always increases the reference count by one when it is successful */
157 FileMonikerImpl_AddRef(iface
);
162 /******************************************************************************
164 ******************************************************************************/
165 ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
)
167 ICOM_THIS(FileMonikerImpl
,iface
);
169 TRACE(ole
,"(%p)\n",iface
);
171 return ++(This
->ref
);
174 /******************************************************************************
175 * FileMoniker_Release
176 ******************************************************************************/
177 ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
)
179 ICOM_THIS(FileMonikerImpl
,iface
);
181 TRACE(ole
,"(%p)\n",iface
);
185 /* destroy the object if there's no more reference on it */
188 FileMonikerImpl_Destroy(This
);
195 /******************************************************************************
196 * FileMoniker_GetClassID
197 ******************************************************************************/
198 HRESULT WINAPI
FileMonikerImpl_GetClassID(const IMoniker
* iface
,
199 CLSID
*pClassID
)/* Pointer to CLSID of object */
201 TRACE(ole
,"(%p,%p),stub!\n",iface
,pClassID
);
206 *pClassID
= CLSID_FileMoniker
;
211 /******************************************************************************
212 * FileMoniker_IsDirty
213 ******************************************************************************/
214 HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
)
216 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
217 method in the OLE-provided moniker interfaces always return S_FALSE because
218 their internal state never changes. */
220 TRACE(ole
,"(%p)\n",iface
);
225 /******************************************************************************
227 ******************************************************************************/
228 HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
,IStream
* pStm
)
235 DWORD dwbuffer
,length
,i
,doubleLenHex
,doubleLenDec
;
237 ICOM_THIS(FileMonikerImpl
,iface
);
239 TRACE(ole
,"(%p,%p)\n",iface
,pStm
);
241 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
243 /* first WORD is non significative */
244 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
245 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
248 /* read filePath string length (plus one) */
249 res
=IStream_Read(pStm
,&length
,sizeof(DWORD
),&bread
);
250 if (bread
!= sizeof(DWORD
))
253 /* read filePath string */
254 filePathA
=HeapAlloc(GetProcessHeap(),0,length
);
255 res
=IStream_Read(pStm
,filePathA
,length
,&bread
);
259 /* read the first constant */
260 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
261 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
267 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
268 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
275 doubleLenHex
=doubleLenDec
=2*length
;
279 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
280 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
286 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
287 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
290 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
291 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
294 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
296 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
297 if (bread
!=doubleLenHex
)
300 if (This
->filePathName
!=NULL
)
301 HeapFree(GetProcessHeap(),0,This
->filePathName
);
303 This
->filePathName
=filePathW
;
305 HeapFree(GetProcessHeap(),0,filePathA
);
310 /******************************************************************************
312 ******************************************************************************/
313 HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
,
314 IStream
* pStm
,/* poniter to the stream where the object is to be saved */
315 BOOL fClearDirty
)/* Specifies whether to clear the dirty flag */
317 /* this function saves data of this object. In the begining I thougth that I have just to write
318 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
319 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
320 * what did this function do exactly ! but I have no idear a bout its logic !
321 * I guessed data who must be writen on stream wich is:
322 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
323 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
324 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
325 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
328 ICOM_THIS(FileMonikerImpl
,iface
);
331 LPOLESTR filePathW
=This
->filePathName
;
333 DWORD len
=1+lstrlenW(filePathW
);
335 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
336 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
343 TRACE(ole
,"(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
348 /* write a DWORD seted to 0 : constant */
349 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
351 /* write length of filePath string ( "\0" included )*/
352 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
354 /* write filePath string type A */
355 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
356 lstrcpyWtoA(filePathA
,filePathW
);
357 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
358 HeapFree(GetProcessHeap(),0,filePathA
);
360 /* write a DWORD seted to 0xDEADFFFF: constant */
361 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
364 /* write 10 times a DWORD seted to 0 : constants */
366 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
371 doubleLenHex
=doubleLenDec
=2*len
;
375 /* write double-length of the path string ( "\0" included )*/
376 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
381 /* write double-length (hexa representation) of the path string ( "\0" included ) */
382 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
384 /* write a WORD seted to 0x3: constant */
385 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
387 /* write path unicode string */
388 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
393 /******************************************************************************
394 * FileMoniker_GetSizeMax
395 ******************************************************************************/
396 HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
,
397 ULARGE_INTEGER
* pcbSize
)/* Pointer to size of stream needed to save object */
399 ICOM_THIS(FileMonikerImpl
,iface
);
400 DWORD len
=lstrlenW(This
->filePathName
);
403 TRACE(ole
,"(%p,%p)\n",iface
,pcbSize
);
408 /* for more details see FileMonikerImpl_Save coments */
410 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
411 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
412 (len
+1)+ /* filePath string */
413 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
414 10*sizeof(WORD
)+ /* 10 zero WORD */
415 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
417 if (len
==0 || len
> 8)
420 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
421 sizeof(WORD
)+ /* constant : 0x3 */
422 len
*sizeof(WCHAR
); /* unicde filePath string */
424 pcbSize
->LowPart
=sizeMAx
;
430 /******************************************************************************
431 * FileMoniker_Construct (local function)
432 *******************************************************************************/
433 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
436 int sizeStr
=lstrlenW(lpszPathName
);
438 WCHAR twoPoint
[]={'.','.',0};
439 WCHAR bkSlash
[]={'\\',0};
442 TRACE(ole
,"(%p,%p)\n",This
,lpszPathName
);
444 /* Initialize the virtual fgunction table. */
445 This
->lpvtbl1
= &VT_FileMonikerImpl
;
446 This
->lpvtbl2
= &VT_ROTDataImpl
;
449 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
451 if (This
->filePathName
==NULL
)
452 return E_OUTOFMEMORY
;
454 lstrcpyW(This
->filePathName
,lpszPathName
);
456 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
461 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
466 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
472 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
480 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
483 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
485 *This
->filePathName
=0;
487 for(i
=0;tabStr
[i
]!=NULL
;i
++)
488 lstrcatW(This
->filePathName
,tabStr
[i
]);
491 lstrcatW(This
->filePathName
,bkSlash
);
494 for(i
=0; tabStr
[i
]!=NULL
;i
++)
495 CoTaskMemFree(tabStr
[i
]);
496 CoTaskMemFree(tabStr
);
501 /******************************************************************************
502 * FileMoniker_Destroy (local function)
503 *******************************************************************************/
504 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
506 TRACE(ole
,"(%p)\n",This
);
508 if (This
->filePathName
!=NULL
)
509 HeapFree(GetProcessHeap(),0,This
->filePathName
);
511 HeapFree(GetProcessHeap(),0,This
);
516 /******************************************************************************
517 * FileMoniker_BindToObject
518 ******************************************************************************/
519 HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,
528 IRunningObjectTable
*prot
=0;
530 IClassFactory
*pcf
=0;
531 IClassActivator
*pca
=0;
533 ICOM_THIS(FileMonikerImpl
,iface
);
537 TRACE(ole
,"(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvResult
);
541 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
544 /* if the requested class was loaded befor ! we dont need to reload it */
545 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
548 /* first activation of this class */
549 res
=GetClassFile(This
->filePathName
,&clsID
);
552 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
555 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
559 IUnknown_AddRef(pObj
);
567 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
569 if (res
==E_NOINTERFACE
){
571 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
573 if (res
==E_NOINTERFACE
)
574 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
578 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
580 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
585 IUnknown_AddRef(pObj
);
592 /*res=GetClassFile(This->filePathName,&clsID);
596 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
601 IUnknown_AddRef(pObj);
608 /* get the requested interface from the loaded class */
609 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
611 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
613 IUnknown_Release(pObj
);
617 IRunningObjectTable_Release(prot
);
620 IPersistFile_Release(ppf
);
623 IClassActivator_Release(pca
);
626 IClassFactory_Release(pcf
);
631 /******************************************************************************
632 * FileMoniker_BindToStorage
633 ******************************************************************************/
634 HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,
644 TRACE(ole
,"(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
646 if (pmkToLeft
==NULL
){
648 if (IsEqualIID(&IID_IStorage
, riid
)){
650 /* get the file name */
651 FileMonikerImpl_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
653 /* verifie if the file contains a storage object */
654 res
=StgIsStorageFile(filePath
);
658 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
664 IStorage_AddRef(pstg
);
669 CoTaskMemFree(filePath
);
672 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
677 return E_NOINTERFACE
;
681 FIXME(ole
,"(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
688 /******************************************************************************
690 ******************************************************************************/
691 HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,
693 DWORD dwReduceHowFar
,
694 IMoniker
** ppmkToLeft
,
695 IMoniker
** ppmkReduced
)
697 TRACE(ole
,"(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
699 if (ppmkReduced
==NULL
)
702 FileMonikerImpl_AddRef(iface
);
706 return MK_S_REDUCED_TO_SELF
;
708 /******************************************************************************
709 * FileMoniker_ComposeWith
710 ******************************************************************************/
711 HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,
713 BOOL fOnlyIfNotGeneric
,
714 IMoniker
** ppmkComposite
)
717 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
718 WCHAR twoPoint
[]={'.','.',0};
719 WCHAR bkSlash
[]={'\\',0};
721 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
724 TRACE(ole
,"(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
726 if (ppmkComposite
==NULL
)
734 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
736 /* check if we have two filemonikers to compose or not */
737 if(mkSys
==MKSYS_FILEMONIKER
){
739 CreateBindCtx(0,&bind
);
741 FileMonikerImpl_GetDisplayName(iface
,bind
,NULL
,&str1
);
742 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
744 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
745 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
746 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
748 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
751 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
754 /* for etch "..\" in the left of str2 remove the right element from str1 */
755 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
760 /* the length of the composed path string is raised by the sum of the two paths lengths */
761 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
764 return E_OUTOFMEMORY
;
766 /* new path is the concatenation of the rest of str1 and str2 */
767 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
768 lstrcatW(newStr
,strDec1
[j
]);
770 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
771 lstrcatW(newStr
,bkSlash
);
773 for(j
=i
;j
<=lastIdx2
;j
++)
774 lstrcatW(newStr
,strDec2
[j
]);
776 /* create a new moniker with the new string */
777 res
=CreateFileMoniker(newStr
,ppmkComposite
);
779 /* free all strings space memory used by this function */
780 HeapFree(GetProcessHeap(),0,newStr
);
782 for(i
=0; strDec1
[i
]!=NULL
;i
++)
783 CoTaskMemFree(strDec1
[i
]);
784 for(i
=0; strDec2
[i
]!=NULL
;i
++)
785 CoTaskMemFree(strDec2
[i
]);
786 CoTaskMemFree(strDec1
);
787 CoTaskMemFree(strDec2
);
794 else if(mkSys
==MKSYS_ANTIMONIKER
){
799 else if (fOnlyIfNotGeneric
){
802 return MK_E_NEEDGENERIC
;
806 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
809 /******************************************************************************
811 ******************************************************************************/
812 HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
814 TRACE(ole
,"(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
816 if (ppenumMoniker
== NULL
)
819 *ppenumMoniker
= NULL
;
824 /******************************************************************************
825 * FileMoniker_IsEqual
826 ******************************************************************************/
827 HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
829 ICOM_THIS(FileMonikerImpl
,iface
);
835 TRACE(ole
,"(%p,%p)\n",iface
,pmkOtherMoniker
);
837 if (pmkOtherMoniker
==NULL
)
840 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
842 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
846 res
=CreateBindCtx(0,&bind
);
850 IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
);
852 if (lstrcmpiW(filePath
,
853 This
->filePathName
)!=0)
860 /******************************************************************************
862 ******************************************************************************/
863 HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
865 ICOM_THIS(FileMonikerImpl
,iface
);
867 int h
= 0,i
,skip
,len
;
874 val
= This
->filePathName
;
878 for (i
= len
; i
> 0; i
--) {
879 h
= (h
* 37) + val
[off
++];
882 /* only sample some characters */
884 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
885 h
= (h
* 39) + val
[off
];
894 /******************************************************************************
895 * FileMoniker_IsRunning
896 ******************************************************************************/
897 HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,
900 IMoniker
* pmkNewlyRunning
)
902 IRunningObjectTable
* rot
;
905 TRACE(ole
,"(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
907 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
913 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
918 res
= IRunningObjectTable_IsRunning(rot
,iface
);
920 IRunningObjectTable_Release(rot
);
925 /******************************************************************************
926 * FileMoniker_GetTimeOfLastChange
927 ******************************************************************************/
928 HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
,
933 ICOM_THIS(FileMonikerImpl
,iface
);
934 IRunningObjectTable
* rot
;
936 WIN32_FILE_ATTRIBUTE_DATA info
;
938 TRACE(ole
,"(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
946 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
951 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
953 if (FAILED(res
)){ /* the moniker is not registred */
955 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
956 return MK_E_NOOBJECT
;
958 *pFileTime
=info
.ftLastWriteTime
;
964 /******************************************************************************
965 * FileMoniker_Inverse
966 ******************************************************************************/
967 HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
970 TRACE(ole
,"(%p,%p)\n",iface
,ppmk
);
972 return CreateAntiMoniker(ppmk
);
975 /******************************************************************************
976 * FileMoniker_CommonPrefixWith
977 ******************************************************************************/
978 HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
981 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
984 ULONG nb1
,nb2
,i
,sameIdx
;
985 BOOL machimeNameCase
=FALSE
;
987 if (ppmkPrefix
==NULL
)
995 /* check if we have the same type of moniker */
996 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
998 if(mkSys
==MKSYS_FILEMONIKER
){
1000 CreateBindCtx(0,&pbind
);
1002 /* create a string based on common part of the two paths */
1004 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
1005 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
1007 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
1008 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
1010 if (nb1
==0 || nb2
==0)
1011 return MK_E_NOPREFIX
;
1013 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
1017 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
1018 (stringTable2
[sameIdx
]!=NULL
) &&
1019 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
1021 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1023 machimeNameCase
=TRUE
;
1025 for(i
=2;i
<sameIdx
;i
++)
1027 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1028 machimeNameCase
=FALSE
;
1033 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1036 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1037 return MK_E_NOPREFIX
;
1039 for(i
=0;i
<sameIdx
;i
++)
1040 lstrcatW(commonPath
,stringTable1
[i
]);
1043 CoTaskMemFree(stringTable1
[i
]);
1045 CoTaskMemFree(stringTable1
);
1048 CoTaskMemFree(stringTable2
[i
]);
1050 CoTaskMemFree(stringTable2
);
1052 HeapFree(GetProcessHeap(),0,commonPath
);
1054 return CreateFileMoniker(commonPath
,ppmkPrefix
);
1057 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1060 /******************************************************************************
1061 * DecomposePath (local function)
1062 ******************************************************************************/
1063 int WINAPI
FileMonikerImpl_DecomposePath(LPOLESTR str
, LPOLESTR
** stringTable
)
1065 WCHAR bSlash
[] = {'\\',0};
1067 int i
=0,j
,tabIndex
=0;
1068 LPOLESTR
*strgtable
;
1070 int len
=lstrlenW(str
);
1072 strgtable
=CoTaskMemAlloc(sizeof(LPOLESTR
[len
]));
1074 if (strgtable
==NULL
)
1075 return E_OUTOFMEMORY
;
1079 if(str
[i
]==bSlash
[0]){
1081 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1083 if (strgtable
[tabIndex
]==NULL
)
1084 return E_OUTOFMEMORY
;
1086 lstrcpyW(strgtable
[tabIndex
++],bSlash
);
1093 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1098 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1100 if (strgtable
[tabIndex
]==NULL
)
1101 return E_OUTOFMEMORY
;
1103 lstrcpyW(strgtable
[tabIndex
++],word
);
1106 strgtable
[tabIndex
]=NULL
;
1108 *stringTable
=strgtable
;
1113 /******************************************************************************
1114 * FileMoniker_RelativePathTo
1115 ******************************************************************************/
1116 HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1120 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1121 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1122 WCHAR back
[] ={'.','.','\\',0};
1124 TRACE(ole
,"(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1126 if (ppmkRelPath
==NULL
)
1130 return E_INVALIDARG
;
1132 res
=CreateBindCtx(0,&bind
);
1136 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1139 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1143 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1144 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1146 if (FAILED(len1
) || FAILED(len2
))
1147 return E_OUTOFMEMORY
;
1149 /* count the number of similar items from the begin of the two paths */
1150 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1151 (tabStr2
[sameIdx
]!=NULL
) &&
1152 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1154 /* begin the construction of relativePath */
1155 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1156 /* by "..\\" in the begin */
1157 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1161 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1162 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1163 if (*tabStr1
[j
]!='\\')
1164 lstrcatW(relPath
,back
);
1166 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1167 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1168 lstrcatW(relPath
,tabStr2
[j
]);
1170 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1172 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1173 CoTaskMemFree(tabStr1
[j
]);
1174 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1175 CoTaskMemFree(tabStr2
[j
]);
1176 CoTaskMemFree(tabStr1
);
1177 CoTaskMemFree(tabStr2
);
1178 CoTaskMemFree(str1
);
1179 CoTaskMemFree(str2
);
1180 HeapFree(GetProcessHeap(),0,relPath
);
1182 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1188 /******************************************************************************
1189 * FileMoniker_GetDisplayName
1190 ******************************************************************************/
1191 HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,
1193 IMoniker
* pmkToLeft
,
1194 LPOLESTR
*ppszDisplayName
)
1196 ICOM_THIS(FileMonikerImpl
,iface
);
1198 int len
=lstrlenW(This
->filePathName
);
1200 TRACE(ole
,"(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1202 if (ppszDisplayName
==NULL
)
1205 if (pmkToLeft
!=NULL
)
1206 return E_INVALIDARG
;
1208 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1209 if (*ppszDisplayName
==NULL
)
1210 return E_OUTOFMEMORY
;
1212 lstrcpyW(*ppszDisplayName
,This
->filePathName
);
1217 /******************************************************************************
1218 * FileMoniker_ParseDisplayName
1219 ******************************************************************************/
1220 HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,
1222 IMoniker
* pmkToLeft
,
1223 LPOLESTR pszDisplayName
,
1227 FIXME(ole
,"(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1231 /******************************************************************************
1232 * FileMoniker_IsSystemMonker
1233 ******************************************************************************/
1234 HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1236 TRACE(ole
,"(%p,%p)\n",iface
,pwdMksys
);
1241 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1246 /*******************************************************************************
1247 * FileMonikerIROTData_QueryInterface
1248 *******************************************************************************/
1249 HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1252 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1254 TRACE(ole
,"(%p,%p,%p)\n",This
,riid
,ppvObject
);
1256 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1259 /***********************************************************************
1260 * FileMonikerIROTData_AddRef
1262 ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1264 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1266 TRACE(ole
,"(%p)\n",This
);
1268 return FileMonikerImpl_AddRef(This
);
1271 /***********************************************************************
1272 * FileMonikerIROTData_Release
1274 ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
)
1276 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1278 TRACE(ole
,"(%p)\n",This
);
1280 return FileMonikerImpl_Release(This
);
1283 /******************************************************************************
1284 * FileMonikerIROTData_GetComparaisonData
1285 ******************************************************************************/
1286 HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,
1291 FIXME(ole
,"(),stub!\n");
1295 /******************************************************************************
1296 * CreateFileMoniker16
1297 ******************************************************************************/
1298 HRESULT WINAPI
CreateFileMoniker16(LPCOLESTR16 lpszPathName
,LPMONIKER
* ppmk
)
1301 FIXME(ole
,"(%s,%p),stub!\n",lpszPathName
,ppmk
);
1305 /******************************************************************************
1307 ******************************************************************************/
1308 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1310 FileMonikerImpl
* newFileMoniker
= 0;
1311 HRESULT hr
= E_FAIL
;
1312 IID riid
=IID_IMoniker
;
1314 TRACE(ole
,"(%p,%p)\n",lpszPathName
,ppmk
);
1319 if(lpszPathName
==NULL
)
1324 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1326 if (newFileMoniker
== 0)
1327 return E_OUTOFMEMORY
;
1329 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1332 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&riid
,(void**)ppmk
);
1334 HeapFree(GetProcessHeap(),0,newFileMoniker
);