1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
10 #include "wine/unicode.h"
11 #include "debugtools.h"
13 #include "wine/obj_storage.h"
14 #include "wine/obj_moniker.h"
15 #include "wine/obj_base.h"
17 #include "compobj_private.h"
19 DEFAULT_DEBUG_CHANNEL(ole
);
21 /* filemoniker data structure */
22 typedef struct FileMonikerImpl
{
24 ICOM_VTABLE(IMoniker
)* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
26 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
27 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
29 ICOM_VTABLE(IROTData
)* lpvtbl2
; /* VTable relative to the IROTData interface.*/
31 ULONG ref
; /* reference counter for this object */
33 LPOLESTR filePathName
; /* path string identified by this filemoniker */
37 /********************************************************************************/
38 /* FileMoniker prototype functions : */
40 /* IUnknown prototype functions */
41 static HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
);
42 static ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
);
43 static ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
);
45 /* IPersist prototype functions */
46 static HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
);
48 /* IPersistStream prototype functions */
49 static HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
);
50 static HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
);
51 static HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
);
52 static HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
);
54 /* IMoniker prototype functions */
55 static HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
56 static HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
57 static HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,IBindCtx
* pbc
, DWORD dwReduceHowFar
,IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
);
58 static HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,IMoniker
* pmkRight
,BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
);
59 static HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
);
60 static HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
);
61 static HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
);
62 static HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, IMoniker
* pmkNewlyRunning
);
63 static HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
, FILETIME
* pFileTime
);
64 static HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
);
65 static HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
, IMoniker
** ppmkPrefix
);
66 static HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
);
67 static HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
);
68 static HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
);
69 static HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
);
71 /********************************************************************************/
72 /* IROTData prototype functions */
74 /* IUnknown prototype functions */
75 static HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
* iface
,REFIID riid
,VOID
** ppvObject
);
76 static ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
* iface
);
77 static ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
);
79 /* IROTData prototype function */
80 static HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,BYTE
* pbData
,ULONG cbMax
,ULONG
* pcbData
);
82 /* Local function used by filemoniker implementation */
83 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
84 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
85 int WINAPI
FileMonikerImpl_DecomposePath(LPOLESTR str
, LPOLESTR
** tabStr
);
88 /********************************************************************************/
89 /* Virtual function table for the FileMonikerImpl class witch include Ipersist,*/
90 /* IPersistStream and IMoniker functions. */
91 static ICOM_VTABLE(IMoniker
) VT_FileMonikerImpl
=
93 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
94 FileMonikerImpl_QueryInterface
,
95 FileMonikerImpl_AddRef
,
96 FileMonikerImpl_Release
,
97 FileMonikerImpl_GetClassID
,
98 FileMonikerImpl_IsDirty
,
100 FileMonikerImpl_Save
,
101 FileMonikerImpl_GetSizeMax
,
102 FileMonikerImpl_BindToObject
,
103 FileMonikerImpl_BindToStorage
,
104 FileMonikerImpl_Reduce
,
105 FileMonikerImpl_ComposeWith
,
106 FileMonikerImpl_Enum
,
107 FileMonikerImpl_IsEqual
,
108 FileMonikerImpl_Hash
,
109 FileMonikerImpl_IsRunning
,
110 FileMonikerImpl_GetTimeOfLastChange
,
111 FileMonikerImpl_Inverse
,
112 FileMonikerImpl_CommonPrefixWith
,
113 FileMonikerImpl_RelativePathTo
,
114 FileMonikerImpl_GetDisplayName
,
115 FileMonikerImpl_ParseDisplayName
,
116 FileMonikerImpl_IsSystemMoniker
119 /********************************************************************************/
120 /* Virtual function table for the IROTData class. */
121 static ICOM_VTABLE(IROTData
) VT_ROTDataImpl
=
123 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
124 FileMonikerROTDataImpl_QueryInterface
,
125 FileMonikerROTDataImpl_AddRef
,
126 FileMonikerROTDataImpl_Release
,
127 FileMonikerROTDataImpl_GetComparaisonData
130 /*******************************************************************************
131 * FileMoniker_QueryInterface
132 *******************************************************************************/
133 HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
135 ICOM_THIS(FileMonikerImpl
,iface
);
137 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
139 /* Perform a sanity check on the parameters.*/
140 if ( (This
==0) || (ppvObject
==0) )
143 /* Initialize the return parameter */
146 /* Compare the riid with the interface IDs implemented by this object.*/
147 if (IsEqualIID(&IID_IUnknown
, riid
) ||
148 IsEqualIID(&IID_IPersist
, riid
) ||
149 IsEqualIID(&IID_IPersistStream
,riid
) ||
150 IsEqualIID(&IID_IMoniker
, riid
)
154 else if (IsEqualIID(&IID_IROTData
, riid
))
155 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
157 /* Check that we obtained an interface.*/
159 return E_NOINTERFACE
;
161 /* Query Interface always increases the reference count by one when it is successful */
162 FileMonikerImpl_AddRef(iface
);
167 /******************************************************************************
169 ******************************************************************************/
170 ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
)
172 ICOM_THIS(FileMonikerImpl
,iface
);
174 TRACE("(%p)\n",iface
);
176 return ++(This
->ref
);
179 /******************************************************************************
180 * FileMoniker_Release
181 ******************************************************************************/
182 ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
)
184 ICOM_THIS(FileMonikerImpl
,iface
);
186 TRACE("(%p)\n",iface
);
190 /* destroy the object if there's no more reference on it */
193 FileMonikerImpl_Destroy(This
);
200 /******************************************************************************
201 * FileMoniker_GetClassID
202 ******************************************************************************/
203 HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
,
204 CLSID
*pClassID
)/* Pointer to CLSID of object */
206 TRACE("(%p,%p),stub!\n",iface
,pClassID
);
211 *pClassID
= CLSID_FileMoniker
;
216 /******************************************************************************
217 * FileMoniker_IsDirty
218 ******************************************************************************/
219 HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
)
221 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
222 method in the OLE-provided moniker interfaces always return S_FALSE because
223 their internal state never changes. */
225 TRACE("(%p)\n",iface
);
230 /******************************************************************************
232 ******************************************************************************/
233 HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
,IStream
* pStm
)
240 DWORD dwbuffer
,length
,i
,doubleLenHex
,doubleLenDec
;
242 ICOM_THIS(FileMonikerImpl
,iface
);
244 TRACE("(%p,%p)\n",iface
,pStm
);
246 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
248 /* first WORD is non significative */
249 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
250 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
253 /* read filePath string length (plus one) */
254 res
=IStream_Read(pStm
,&length
,sizeof(DWORD
),&bread
);
255 if (bread
!= sizeof(DWORD
))
258 /* read filePath string */
259 filePathA
=HeapAlloc(GetProcessHeap(),0,length
);
260 res
=IStream_Read(pStm
,filePathA
,length
,&bread
);
264 /* read the first constant */
265 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
266 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
272 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
273 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
280 doubleLenHex
=doubleLenDec
=2*length
;
284 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
285 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
291 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
292 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
295 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
296 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
299 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
301 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
302 if (bread
!=doubleLenHex
)
305 if (This
->filePathName
!=NULL
)
306 HeapFree(GetProcessHeap(),0,This
->filePathName
);
308 This
->filePathName
=filePathW
;
310 HeapFree(GetProcessHeap(),0,filePathA
);
315 /******************************************************************************
317 ******************************************************************************/
318 HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
,
319 IStream
* pStm
,/* poniter to the stream where the object is to be saved */
320 BOOL fClearDirty
)/* Specifies whether to clear the dirty flag */
322 /* this function saves data of this object. In the begining I thougth that I have just to write
323 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
324 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
325 * what did this function do exactly ! but I have no idear a bout its logic !
326 * I guessed data who must be writen on stream wich is:
327 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
328 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
329 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
330 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
333 ICOM_THIS(FileMonikerImpl
,iface
);
336 LPOLESTR filePathW
=This
->filePathName
;
340 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
341 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
348 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
353 /* write a DWORD seted to 0 : constant */
354 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
356 /* write length of filePath string ( "\0" included )*/
357 len
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
358 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
360 /* write filePath string type A */
361 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
362 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, len
, NULL
, NULL
);
363 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
364 HeapFree(GetProcessHeap(),0,filePathA
);
366 /* write a DWORD seted to 0xDEADFFFF: constant */
367 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
370 /* write 10 times a DWORD seted to 0 : constants */
372 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
377 doubleLenHex
=doubleLenDec
=2*len
;
381 /* write double-length of the path string ( "\0" included )*/
382 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
387 /* write double-length (hexa representation) of the path string ( "\0" included ) */
388 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
390 /* write a WORD seted to 0x3: constant */
391 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
393 /* write path unicode string */
394 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
399 /******************************************************************************
400 * FileMoniker_GetSizeMax
401 ******************************************************************************/
402 HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
,
403 ULARGE_INTEGER
* pcbSize
)/* Pointer to size of stream needed to save object */
405 ICOM_THIS(FileMonikerImpl
,iface
);
406 DWORD len
=lstrlenW(This
->filePathName
);
409 TRACE("(%p,%p)\n",iface
,pcbSize
);
414 /* for more details see FileMonikerImpl_Save coments */
416 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
417 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
418 (len
+1)+ /* filePath string */
419 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
420 10*sizeof(WORD
)+ /* 10 zero WORD */
421 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
423 if (len
==0 || len
> 8)
426 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
427 sizeof(WORD
)+ /* constant : 0x3 */
428 len
*sizeof(WCHAR
); /* unicde filePath string */
430 pcbSize
->s
.LowPart
=sizeMAx
;
431 pcbSize
->s
.HighPart
=0;
436 /******************************************************************************
437 * FileMoniker_Construct (local function)
438 *******************************************************************************/
439 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
442 int sizeStr
=lstrlenW(lpszPathName
);
444 WCHAR twoPoint
[]={'.','.',0};
445 WCHAR bkSlash
[]={'\\',0};
448 TRACE("(%p,%p)\n",This
,lpszPathName
);
450 /* Initialize the virtual fgunction table. */
451 This
->lpvtbl1
= &VT_FileMonikerImpl
;
452 This
->lpvtbl2
= &VT_ROTDataImpl
;
455 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
457 if (This
->filePathName
==NULL
)
458 return E_OUTOFMEMORY
;
460 strcpyW(This
->filePathName
,lpszPathName
);
462 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
467 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
472 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
478 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
486 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
489 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
491 *This
->filePathName
=0;
493 for(i
=0;tabStr
[i
]!=NULL
;i
++)
494 strcatW(This
->filePathName
,tabStr
[i
]);
497 strcatW(This
->filePathName
,bkSlash
);
500 for(i
=0; tabStr
[i
]!=NULL
;i
++)
501 CoTaskMemFree(tabStr
[i
]);
502 CoTaskMemFree(tabStr
);
507 /******************************************************************************
508 * FileMoniker_Destroy (local function)
509 *******************************************************************************/
510 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
512 TRACE("(%p)\n",This
);
514 if (This
->filePathName
!=NULL
)
515 HeapFree(GetProcessHeap(),0,This
->filePathName
);
517 HeapFree(GetProcessHeap(),0,This
);
522 /******************************************************************************
523 * FileMoniker_BindToObject
524 ******************************************************************************/
525 HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,
534 IRunningObjectTable
*prot
=0;
536 IClassFactory
*pcf
=0;
537 IClassActivator
*pca
=0;
539 ICOM_THIS(FileMonikerImpl
,iface
);
543 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvResult
);
547 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
550 /* if the requested class was loaded befor ! we dont need to reload it */
551 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
554 /* first activation of this class */
555 res
=GetClassFile(This
->filePathName
,&clsID
);
558 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
561 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
565 IUnknown_AddRef(pObj
);
573 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
575 if (res
==E_NOINTERFACE
){
577 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
579 if (res
==E_NOINTERFACE
)
580 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
584 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
586 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
591 IUnknown_AddRef(pObj
);
598 /*res=GetClassFile(This->filePathName,&clsID);
602 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
607 IUnknown_AddRef(pObj);
614 /* get the requested interface from the loaded class */
615 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
617 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
619 IUnknown_Release(pObj
);
623 IRunningObjectTable_Release(prot
);
626 IPersistFile_Release(ppf
);
629 IClassActivator_Release(pca
);
632 IClassFactory_Release(pcf
);
637 /******************************************************************************
638 * FileMoniker_BindToStorage
639 ******************************************************************************/
640 HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,
650 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
652 if (pmkToLeft
==NULL
){
654 if (IsEqualIID(&IID_IStorage
, riid
)){
656 /* get the file name */
657 FileMonikerImpl_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
659 /* verifie if the file contains a storage object */
660 res
=StgIsStorageFile(filePath
);
664 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
670 IStorage_AddRef(pstg
);
675 CoTaskMemFree(filePath
);
678 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
683 return E_NOINTERFACE
;
687 FIXME("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
694 /******************************************************************************
696 ******************************************************************************/
697 HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,
699 DWORD dwReduceHowFar
,
700 IMoniker
** ppmkToLeft
,
701 IMoniker
** ppmkReduced
)
703 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
705 if (ppmkReduced
==NULL
)
708 FileMonikerImpl_AddRef(iface
);
712 return MK_S_REDUCED_TO_SELF
;
714 /******************************************************************************
715 * FileMoniker_ComposeWith
716 ******************************************************************************/
717 HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,
719 BOOL fOnlyIfNotGeneric
,
720 IMoniker
** ppmkComposite
)
723 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
724 WCHAR twoPoint
[]={'.','.',0};
725 WCHAR bkSlash
[]={'\\',0};
727 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
730 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
732 if (ppmkComposite
==NULL
)
740 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
742 /* check if we have two filemonikers to compose or not */
743 if(mkSys
==MKSYS_FILEMONIKER
){
745 CreateBindCtx(0,&bind
);
747 FileMonikerImpl_GetDisplayName(iface
,bind
,NULL
,&str1
);
748 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
750 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
751 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
752 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
754 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
757 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
760 /* for etch "..\" in the left of str2 remove the right element from str1 */
761 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
766 /* the length of the composed path string is raised by the sum of the two paths lengths */
767 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
770 return E_OUTOFMEMORY
;
772 /* new path is the concatenation of the rest of str1 and str2 */
773 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
774 strcatW(newStr
,strDec1
[j
]);
776 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
777 strcatW(newStr
,bkSlash
);
779 for(j
=i
;j
<=lastIdx2
;j
++)
780 strcatW(newStr
,strDec2
[j
]);
782 /* create a new moniker with the new string */
783 res
=CreateFileMoniker(newStr
,ppmkComposite
);
785 /* free all strings space memory used by this function */
786 HeapFree(GetProcessHeap(),0,newStr
);
788 for(i
=0; strDec1
[i
]!=NULL
;i
++)
789 CoTaskMemFree(strDec1
[i
]);
790 for(i
=0; strDec2
[i
]!=NULL
;i
++)
791 CoTaskMemFree(strDec2
[i
]);
792 CoTaskMemFree(strDec1
);
793 CoTaskMemFree(strDec2
);
800 else if(mkSys
==MKSYS_ANTIMONIKER
){
805 else if (fOnlyIfNotGeneric
){
808 return MK_E_NEEDGENERIC
;
812 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
815 /******************************************************************************
817 ******************************************************************************/
818 HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
820 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
822 if (ppenumMoniker
== NULL
)
825 *ppenumMoniker
= NULL
;
830 /******************************************************************************
831 * FileMoniker_IsEqual
832 ******************************************************************************/
833 HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
835 ICOM_THIS(FileMonikerImpl
,iface
);
841 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
843 if (pmkOtherMoniker
==NULL
)
846 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
848 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
852 res
=CreateBindCtx(0,&bind
);
856 IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
);
858 if (lstrcmpiW(filePath
,
859 This
->filePathName
)!=0)
866 /******************************************************************************
868 ******************************************************************************/
869 HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
871 ICOM_THIS(FileMonikerImpl
,iface
);
873 int h
= 0,i
,skip
,len
;
880 val
= This
->filePathName
;
884 for (i
= len
; i
> 0; i
--) {
885 h
= (h
* 37) + val
[off
++];
888 /* only sample some characters */
890 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
891 h
= (h
* 39) + val
[off
];
900 /******************************************************************************
901 * FileMoniker_IsRunning
902 ******************************************************************************/
903 HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,
906 IMoniker
* pmkNewlyRunning
)
908 IRunningObjectTable
* rot
;
911 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
913 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
919 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
924 res
= IRunningObjectTable_IsRunning(rot
,iface
);
926 IRunningObjectTable_Release(rot
);
931 /******************************************************************************
932 * FileMoniker_GetTimeOfLastChange
933 ******************************************************************************/
934 HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
,
939 ICOM_THIS(FileMonikerImpl
,iface
);
940 IRunningObjectTable
* rot
;
942 WIN32_FILE_ATTRIBUTE_DATA info
;
944 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
952 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
957 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
959 if (FAILED(res
)){ /* the moniker is not registred */
961 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
962 return MK_E_NOOBJECT
;
964 *pFileTime
=info
.ftLastWriteTime
;
970 /******************************************************************************
971 * FileMoniker_Inverse
972 ******************************************************************************/
973 HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
976 TRACE("(%p,%p)\n",iface
,ppmk
);
978 return CreateAntiMoniker(ppmk
);
981 /******************************************************************************
982 * FileMoniker_CommonPrefixWith
983 ******************************************************************************/
984 HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
987 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
990 ULONG nb1
,nb2
,i
,sameIdx
;
991 BOOL machimeNameCase
=FALSE
;
993 if (ppmkPrefix
==NULL
)
1001 /* check if we have the same type of moniker */
1002 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
1004 if(mkSys
==MKSYS_FILEMONIKER
){
1006 CreateBindCtx(0,&pbind
);
1008 /* create a string based on common part of the two paths */
1010 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
1011 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
1013 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
1014 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
1016 if (nb1
==0 || nb2
==0)
1017 return MK_E_NOPREFIX
;
1019 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
1023 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
1024 (stringTable2
[sameIdx
]!=NULL
) &&
1025 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
1027 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1029 machimeNameCase
=TRUE
;
1031 for(i
=2;i
<sameIdx
;i
++)
1033 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1034 machimeNameCase
=FALSE
;
1039 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1042 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1043 return MK_E_NOPREFIX
;
1045 for(i
=0;i
<sameIdx
;i
++)
1046 strcatW(commonPath
,stringTable1
[i
]);
1049 CoTaskMemFree(stringTable1
[i
]);
1051 CoTaskMemFree(stringTable1
);
1054 CoTaskMemFree(stringTable2
[i
]);
1056 CoTaskMemFree(stringTable2
);
1058 HeapFree(GetProcessHeap(),0,commonPath
);
1060 return CreateFileMoniker(commonPath
,ppmkPrefix
);
1063 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1066 /******************************************************************************
1067 * DecomposePath (local function)
1068 ******************************************************************************/
1069 int WINAPI
FileMonikerImpl_DecomposePath(LPOLESTR str
, LPOLESTR
** stringTable
)
1071 WCHAR bSlash
[] = {'\\',0};
1072 WCHAR word
[MAX_PATH
];
1073 int i
=0,j
,tabIndex
=0;
1074 LPOLESTR
*strgtable
;
1076 int len
=lstrlenW(str
);
1078 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1080 if (strgtable
==NULL
)
1081 return E_OUTOFMEMORY
;
1085 if(str
[i
]==bSlash
[0]){
1087 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1089 if (strgtable
[tabIndex
]==NULL
)
1090 return E_OUTOFMEMORY
;
1092 strcpyW(strgtable
[tabIndex
++],bSlash
);
1099 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1104 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1106 if (strgtable
[tabIndex
]==NULL
)
1107 return E_OUTOFMEMORY
;
1109 strcpyW(strgtable
[tabIndex
++],word
);
1112 strgtable
[tabIndex
]=NULL
;
1114 *stringTable
=strgtable
;
1119 /******************************************************************************
1120 * FileMoniker_RelativePathTo
1121 ******************************************************************************/
1122 HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1126 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1127 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1128 WCHAR back
[] ={'.','.','\\',0};
1130 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1132 if (ppmkRelPath
==NULL
)
1136 return E_INVALIDARG
;
1138 res
=CreateBindCtx(0,&bind
);
1142 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1145 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1149 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1150 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1152 if (FAILED(len1
) || FAILED(len2
))
1153 return E_OUTOFMEMORY
;
1155 /* count the number of similar items from the begin of the two paths */
1156 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1157 (tabStr2
[sameIdx
]!=NULL
) &&
1158 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1160 /* begin the construction of relativePath */
1161 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1162 /* by "..\\" in the begin */
1163 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1167 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1168 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1169 if (*tabStr1
[j
]!='\\')
1170 strcatW(relPath
,back
);
1172 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1173 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1174 strcatW(relPath
,tabStr2
[j
]);
1176 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1178 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1179 CoTaskMemFree(tabStr1
[j
]);
1180 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1181 CoTaskMemFree(tabStr2
[j
]);
1182 CoTaskMemFree(tabStr1
);
1183 CoTaskMemFree(tabStr2
);
1184 CoTaskMemFree(str1
);
1185 CoTaskMemFree(str2
);
1186 HeapFree(GetProcessHeap(),0,relPath
);
1188 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1194 /******************************************************************************
1195 * FileMoniker_GetDisplayName
1196 ******************************************************************************/
1197 HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,
1199 IMoniker
* pmkToLeft
,
1200 LPOLESTR
*ppszDisplayName
)
1202 ICOM_THIS(FileMonikerImpl
,iface
);
1204 int len
=lstrlenW(This
->filePathName
);
1206 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1208 if (ppszDisplayName
==NULL
)
1211 if (pmkToLeft
!=NULL
)
1212 return E_INVALIDARG
;
1214 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1215 if (*ppszDisplayName
==NULL
)
1216 return E_OUTOFMEMORY
;
1218 strcpyW(*ppszDisplayName
,This
->filePathName
);
1223 /******************************************************************************
1224 * FileMoniker_ParseDisplayName
1225 ******************************************************************************/
1226 HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,
1228 IMoniker
* pmkToLeft
,
1229 LPOLESTR pszDisplayName
,
1233 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1237 /******************************************************************************
1238 * FileMoniker_IsSystemMonker
1239 ******************************************************************************/
1240 HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1242 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1247 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1252 /*******************************************************************************
1253 * FileMonikerIROTData_QueryInterface
1254 *******************************************************************************/
1255 HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1258 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1260 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
1262 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1265 /***********************************************************************
1266 * FileMonikerIROTData_AddRef
1268 ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1270 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1272 TRACE("(%p)\n",This
);
1274 return FileMonikerImpl_AddRef(This
);
1277 /***********************************************************************
1278 * FileMonikerIROTData_Release
1280 ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
)
1282 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1284 TRACE("(%p)\n",This
);
1286 return FileMonikerImpl_Release(This
);
1289 /******************************************************************************
1290 * FileMonikerIROTData_GetComparaisonData
1291 ******************************************************************************/
1292 HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,
1297 FIXME("(),stub!\n");
1301 /******************************************************************************
1302 * CreateFileMoniker16
1303 ******************************************************************************/
1304 HRESULT WINAPI
CreateFileMoniker16(LPCOLESTR16 lpszPathName
,LPMONIKER
* ppmk
)
1307 FIXME("(%s,%p),stub!\n",lpszPathName
,ppmk
);
1311 /******************************************************************************
1313 ******************************************************************************/
1314 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1316 FileMonikerImpl
* newFileMoniker
= 0;
1317 HRESULT hr
= E_FAIL
;
1318 IID riid
=IID_IMoniker
;
1320 TRACE("(%p,%p)\n",lpszPathName
,ppmk
);
1325 if(lpszPathName
==NULL
)
1330 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1332 if (newFileMoniker
== 0)
1333 return E_OUTOFMEMORY
;
1335 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1338 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&riid
,(void**)ppmk
);
1340 HeapFree(GetProcessHeap(),0,newFileMoniker
);