1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************************/
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
36 #include "compobj_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
40 const CLSID CLSID_FileMoniker
= {
41 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
44 /* filemoniker data structure */
45 typedef struct FileMonikerImpl
{
47 IMonikerVtbl
* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
49 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
50 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
52 IROTDataVtbl
* lpvtbl2
; /* VTable relative to the IROTData interface.*/
54 ULONG ref
; /* reference counter for this object */
56 LPOLESTR filePathName
; /* path string identified by this filemoniker */
60 /********************************************************************************/
61 /* FileMoniker prototype functions : */
63 /* IUnknown prototype functions */
64 static HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
);
65 static ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
);
66 static ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
);
68 /* IPersist prototype functions */
69 static HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
);
71 /* IPersistStream prototype functions */
72 static HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
);
73 static HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
);
74 static HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
);
75 static HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
);
77 /* IMoniker prototype functions */
78 static HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
79 static HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
80 static HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,IBindCtx
* pbc
, DWORD dwReduceHowFar
,IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
);
81 static HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,IMoniker
* pmkRight
,BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
);
82 static HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
);
83 static HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
);
84 static HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
);
85 static HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, IMoniker
* pmkNewlyRunning
);
86 static HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
, FILETIME
* pFileTime
);
87 static HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
);
88 static HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
, IMoniker
** ppmkPrefix
);
89 static HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
);
90 static HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
);
91 static HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
);
92 static HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
);
94 /********************************************************************************/
95 /* IROTData prototype functions */
97 /* IUnknown prototype functions */
98 static HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
* iface
,REFIID riid
,VOID
** ppvObject
);
99 static ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
* iface
);
100 static ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
);
102 /* IROTData prototype function */
103 static HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,BYTE
* pbData
,ULONG cbMax
,ULONG
* pcbData
);
105 /* Local function used by filemoniker implementation */
106 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
107 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
108 int WINAPI
FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** tabStr
);
111 /********************************************************************************/
112 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
113 /* IPersistStream and IMoniker functions. */
114 static IMonikerVtbl VT_FileMonikerImpl
=
116 FileMonikerImpl_QueryInterface
,
117 FileMonikerImpl_AddRef
,
118 FileMonikerImpl_Release
,
119 FileMonikerImpl_GetClassID
,
120 FileMonikerImpl_IsDirty
,
121 FileMonikerImpl_Load
,
122 FileMonikerImpl_Save
,
123 FileMonikerImpl_GetSizeMax
,
124 FileMonikerImpl_BindToObject
,
125 FileMonikerImpl_BindToStorage
,
126 FileMonikerImpl_Reduce
,
127 FileMonikerImpl_ComposeWith
,
128 FileMonikerImpl_Enum
,
129 FileMonikerImpl_IsEqual
,
130 FileMonikerImpl_Hash
,
131 FileMonikerImpl_IsRunning
,
132 FileMonikerImpl_GetTimeOfLastChange
,
133 FileMonikerImpl_Inverse
,
134 FileMonikerImpl_CommonPrefixWith
,
135 FileMonikerImpl_RelativePathTo
,
136 FileMonikerImpl_GetDisplayName
,
137 FileMonikerImpl_ParseDisplayName
,
138 FileMonikerImpl_IsSystemMoniker
141 /********************************************************************************/
142 /* Virtual function table for the IROTData class. */
143 static IROTDataVtbl VT_ROTDataImpl
=
145 FileMonikerROTDataImpl_QueryInterface
,
146 FileMonikerROTDataImpl_AddRef
,
147 FileMonikerROTDataImpl_Release
,
148 FileMonikerROTDataImpl_GetComparaisonData
151 /*******************************************************************************
152 * FileMoniker_QueryInterface
153 *******************************************************************************/
154 HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
156 ICOM_THIS(FileMonikerImpl
,iface
);
158 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
160 /* Perform a sanity check on the parameters.*/
161 if ( (This
==0) || (ppvObject
==0) )
164 /* Initialize the return parameter */
167 /* Compare the riid with the interface IDs implemented by this object.*/
168 if (IsEqualIID(&IID_IUnknown
, riid
) ||
169 IsEqualIID(&IID_IPersist
, riid
) ||
170 IsEqualIID(&IID_IPersistStream
,riid
) ||
171 IsEqualIID(&IID_IMoniker
, riid
)
175 else if (IsEqualIID(&IID_IROTData
, riid
))
176 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
178 /* Check that we obtained an interface.*/
180 return E_NOINTERFACE
;
182 /* Query Interface always increases the reference count by one when it is successful */
183 FileMonikerImpl_AddRef(iface
);
188 /******************************************************************************
190 ******************************************************************************/
191 ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
)
193 ICOM_THIS(FileMonikerImpl
,iface
);
195 TRACE("(%p)\n",iface
);
197 return ++(This
->ref
);
200 /******************************************************************************
201 * FileMoniker_Release
202 ******************************************************************************/
203 ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
)
205 ICOM_THIS(FileMonikerImpl
,iface
);
207 TRACE("(%p)\n",iface
);
211 /* destroy the object if there's no more reference on it */
214 FileMonikerImpl_Destroy(This
);
221 /******************************************************************************
222 * FileMoniker_GetClassID
223 ******************************************************************************/
224 HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
,
225 CLSID
*pClassID
)/* Pointer to CLSID of object */
227 TRACE("(%p,%p),stub!\n",iface
,pClassID
);
232 *pClassID
= CLSID_FileMoniker
;
237 /******************************************************************************
238 * FileMoniker_IsDirty
239 ******************************************************************************/
240 HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
)
242 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
243 method in the OLE-provided moniker interfaces always return S_FALSE because
244 their internal state never changes. */
246 TRACE("(%p)\n",iface
);
251 /******************************************************************************
253 ******************************************************************************/
254 HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
,IStream
* pStm
)
261 DWORD dwbuffer
,length
,i
,doubleLenHex
,doubleLenDec
;
263 ICOM_THIS(FileMonikerImpl
,iface
);
265 TRACE("(%p,%p)\n",iface
,pStm
);
267 /* this function locates and reads from the stream the filePath string written by FileMonikerImpl_Save */
269 /* first WORD is non significative */
270 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
271 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
274 /* read filePath string length (plus one) */
275 res
=IStream_Read(pStm
,&length
,sizeof(DWORD
),&bread
);
276 if (bread
!= sizeof(DWORD
))
279 /* read filePath string */
280 filePathA
=HeapAlloc(GetProcessHeap(),0,length
);
281 res
=IStream_Read(pStm
,filePathA
,length
,&bread
);
282 HeapFree(GetProcessHeap(),0,filePathA
);
286 /* read the first constant */
287 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
288 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
294 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
295 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
302 doubleLenHex
=doubleLenDec
=2*length
;
306 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
307 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
313 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
314 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
317 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
318 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
321 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
323 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
324 if (bread
!=doubleLenHex
) {
325 HeapFree(GetProcessHeap(), 0, filePathW
);
329 if (This
->filePathName
!=NULL
)
330 HeapFree(GetProcessHeap(),0,This
->filePathName
);
332 This
->filePathName
=filePathW
;
337 /******************************************************************************
339 ******************************************************************************/
340 HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
,
341 IStream
* pStm
,/* pointer to the stream where the object is to be saved */
342 BOOL fClearDirty
)/* Specifies whether to clear the dirty flag */
344 /* this function saves data of this object. In the beginning I thougth
345 * that I have just to write the filePath string on Stream. But, when I
346 * tested this function whith windows programs samples, I noticed that it
347 * was not the case. So I analysed data written by this function on
348 * Windows and what this did function exactly ! But I have no idea about
350 * I guessed data which must be written on stream is:
351 * 1) WORD constant:zero
352 * 2) length of the path string ("\0" included)
353 * 3) path string type A
354 * 4) DWORD constant : 0xDEADFFFF
355 * 5) ten WORD constant: zero
356 * 6) DWORD: double-length of the the path string type W ("\0" not
358 * 7) WORD constant: 0x3
359 * 8) filePath unicode string.
360 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
363 ICOM_THIS(FileMonikerImpl
,iface
);
366 LPOLESTR filePathW
=This
->filePathName
;
370 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
371 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
378 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
383 /* write a DWORD set to 0 : constant */
384 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
386 /* write length of filePath string ( "\0" included )*/
387 len
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
388 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
390 /* write filePath string type A */
391 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
392 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, len
, NULL
, NULL
);
393 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
394 HeapFree(GetProcessHeap(),0,filePathA
);
396 /* write a DWORD set to 0xDEADFFFF: constant */
397 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
400 /* write 10 times a DWORD set to 0 : constants */
402 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
407 doubleLenHex
=doubleLenDec
=2*len
;
411 /* write double-length of the path string ( "\0" included )*/
412 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
417 /* write double-length (hexa representation) of the path string ( "\0" included ) */
418 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
420 /* write a WORD set to 0x3: constant */
421 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
423 /* write path unicode string */
424 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
429 /******************************************************************************
430 * FileMoniker_GetSizeMax
431 ******************************************************************************/
432 HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
,
433 ULARGE_INTEGER
* pcbSize
)/* Pointer to size of stream needed to save object */
435 ICOM_THIS(FileMonikerImpl
,iface
);
436 DWORD len
=lstrlenW(This
->filePathName
);
439 TRACE("(%p,%p)\n",iface
,pcbSize
);
444 /* for more details see FileMonikerImpl_Save coments */
446 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
447 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
448 (len
+1)+ /* filePath string */
449 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
450 10*sizeof(WORD
)+ /* 10 zero WORD */
451 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
453 if (len
==0 || len
> 8)
456 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
457 sizeof(WORD
)+ /* constant : 0x3 */
458 len
*sizeof(WCHAR
); /* unicde filePath string */
460 pcbSize
->u
.LowPart
=sizeMAx
;
461 pcbSize
->u
.HighPart
=0;
466 /******************************************************************************
467 * FileMoniker_Construct (local function)
468 *******************************************************************************/
469 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
472 int sizeStr
=lstrlenW(lpszPathName
);
474 static const WCHAR twoPoint
[]={'.','.',0};
475 static const WCHAR bkSlash
[]={'\\',0};
478 TRACE("(%p,%p)\n",This
,lpszPathName
);
480 /* Initialize the virtual fgunction table. */
481 This
->lpvtbl1
= &VT_FileMonikerImpl
;
482 This
->lpvtbl2
= &VT_ROTDataImpl
;
485 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
487 if (This
->filePathName
==NULL
)
488 return E_OUTOFMEMORY
;
490 strcpyW(This
->filePathName
,lpszPathName
);
492 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
497 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
502 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
508 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
516 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
519 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
521 *This
->filePathName
=0;
523 for(i
=0;tabStr
[i
]!=NULL
;i
++)
524 strcatW(This
->filePathName
,tabStr
[i
]);
527 strcatW(This
->filePathName
,bkSlash
);
530 for(i
=0; tabStr
[i
]!=NULL
;i
++)
531 CoTaskMemFree(tabStr
[i
]);
532 CoTaskMemFree(tabStr
);
537 /******************************************************************************
538 * FileMoniker_Destroy (local function)
539 *******************************************************************************/
540 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
542 TRACE("(%p)\n",This
);
544 if (This
->filePathName
!=NULL
)
545 HeapFree(GetProcessHeap(),0,This
->filePathName
);
547 HeapFree(GetProcessHeap(),0,This
);
552 /******************************************************************************
553 * FileMoniker_BindToObject
554 ******************************************************************************/
555 HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,
564 IRunningObjectTable
*prot
=0;
566 IClassFactory
*pcf
=0;
567 IClassActivator
*pca
=0;
569 ICOM_THIS(FileMonikerImpl
,iface
);
573 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvResult
);
577 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
580 /* if the requested class was loaded before ! we don't need to reload it */
581 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
584 /* first activation of this class */
585 res
=GetClassFile(This
->filePathName
,&clsID
);
588 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
591 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
595 IUnknown_AddRef(pObj
);
603 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
605 if (res
==E_NOINTERFACE
){
607 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
609 if (res
==E_NOINTERFACE
)
610 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
614 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
616 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
621 IUnknown_AddRef(pObj
);
628 /*res=GetClassFile(This->filePathName,&clsID);
632 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
637 IUnknown_AddRef(pObj);
644 /* get the requested interface from the loaded class */
645 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
647 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
649 IUnknown_Release(pObj
);
653 IRunningObjectTable_Release(prot
);
656 IPersistFile_Release(ppf
);
659 IClassActivator_Release(pca
);
662 IClassFactory_Release(pcf
);
667 /******************************************************************************
668 * FileMoniker_BindToStorage
669 ******************************************************************************/
670 HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,
680 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
682 if (pmkToLeft
==NULL
){
684 if (IsEqualIID(&IID_IStorage
, riid
)){
686 /* get the file name */
687 FileMonikerImpl_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
689 /* verifie if the file contains a storage object */
690 res
=StgIsStorageFile(filePath
);
694 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
700 IStorage_AddRef(pstg
);
705 CoTaskMemFree(filePath
);
708 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
711 return E_NOINTERFACE
;
715 FIXME("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
722 /******************************************************************************
724 ******************************************************************************/
725 HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,
727 DWORD dwReduceHowFar
,
728 IMoniker
** ppmkToLeft
,
729 IMoniker
** ppmkReduced
)
731 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
733 if (ppmkReduced
==NULL
)
736 FileMonikerImpl_AddRef(iface
);
740 return MK_S_REDUCED_TO_SELF
;
742 /******************************************************************************
743 * FileMoniker_ComposeWith
744 ******************************************************************************/
745 HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,
747 BOOL fOnlyIfNotGeneric
,
748 IMoniker
** ppmkComposite
)
751 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
752 static const WCHAR twoPoint
[]={'.','.',0};
753 static const WCHAR bkSlash
[]={'\\',0};
755 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
758 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
760 if (ppmkComposite
==NULL
)
768 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
770 /* check if we have two filemonikers to compose or not */
771 if(mkSys
==MKSYS_FILEMONIKER
){
773 CreateBindCtx(0,&bind
);
775 FileMonikerImpl_GetDisplayName(iface
,bind
,NULL
,&str1
);
776 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
778 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
779 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
780 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
782 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
785 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
788 /* for etch "..\" in the left of str2 remove the right element from str1 */
789 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
794 /* the length of the composed path string is raised by the sum of the two paths lengths */
795 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
798 return E_OUTOFMEMORY
;
800 /* new path is the concatenation of the rest of str1 and str2 */
801 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
802 strcatW(newStr
,strDec1
[j
]);
804 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
805 strcatW(newStr
,bkSlash
);
807 for(j
=i
;j
<=lastIdx2
;j
++)
808 strcatW(newStr
,strDec2
[j
]);
810 /* create a new moniker with the new string */
811 res
=CreateFileMoniker(newStr
,ppmkComposite
);
813 /* free all strings space memory used by this function */
814 HeapFree(GetProcessHeap(),0,newStr
);
816 for(i
=0; strDec1
[i
]!=NULL
;i
++)
817 CoTaskMemFree(strDec1
[i
]);
818 for(i
=0; strDec2
[i
]!=NULL
;i
++)
819 CoTaskMemFree(strDec2
[i
]);
820 CoTaskMemFree(strDec1
);
821 CoTaskMemFree(strDec2
);
828 else if(mkSys
==MKSYS_ANTIMONIKER
){
833 else if (fOnlyIfNotGeneric
){
836 return MK_E_NEEDGENERIC
;
840 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
843 /******************************************************************************
845 ******************************************************************************/
846 HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
848 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
850 if (ppenumMoniker
== NULL
)
853 *ppenumMoniker
= NULL
;
858 /******************************************************************************
859 * FileMoniker_IsEqual
860 ******************************************************************************/
861 HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
863 ICOM_THIS(FileMonikerImpl
,iface
);
869 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
871 if (pmkOtherMoniker
==NULL
)
874 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
876 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
879 res
= CreateBindCtx(0,&bind
);
880 if (FAILED(res
)) return res
;
882 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
883 int result
= lstrcmpiW(filePath
, This
->filePathName
);
884 CoTaskMemFree(filePath
);
885 if ( result
== 0 ) return S_OK
;
891 /******************************************************************************
893 ******************************************************************************/
894 HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
896 ICOM_THIS(FileMonikerImpl
,iface
);
898 int h
= 0,i
,skip
,len
;
905 val
= This
->filePathName
;
909 for (i
= len
; i
> 0; i
--) {
910 h
= (h
* 37) + val
[off
++];
913 /* only sample some characters */
915 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
916 h
= (h
* 39) + val
[off
];
925 /******************************************************************************
926 * FileMoniker_IsRunning
927 ******************************************************************************/
928 HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,
931 IMoniker
* pmkNewlyRunning
)
933 IRunningObjectTable
* rot
;
936 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
938 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
944 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
949 res
= IRunningObjectTable_IsRunning(rot
,iface
);
951 IRunningObjectTable_Release(rot
);
956 /******************************************************************************
957 * FileMoniker_GetTimeOfLastChange
958 ******************************************************************************/
959 HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
,
964 ICOM_THIS(FileMonikerImpl
,iface
);
965 IRunningObjectTable
* rot
;
967 WIN32_FILE_ATTRIBUTE_DATA info
;
969 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
977 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
982 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
984 if (FAILED(res
)){ /* the moniker is not registered */
986 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
987 return MK_E_NOOBJECT
;
989 *pFileTime
=info
.ftLastWriteTime
;
995 /******************************************************************************
996 * FileMoniker_Inverse
997 ******************************************************************************/
998 HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
1001 TRACE("(%p,%p)\n",iface
,ppmk
);
1003 return CreateAntiMoniker(ppmk
);
1006 /******************************************************************************
1007 * FileMoniker_CommonPrefixWith
1008 ******************************************************************************/
1009 HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
1012 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
1015 ULONG nb1
,nb2
,i
,sameIdx
;
1016 BOOL machimeNameCase
=FALSE
;
1018 if (ppmkPrefix
==NULL
)
1022 return E_INVALIDARG
;
1026 /* check if we have the same type of moniker */
1027 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
1029 if(mkSys
==MKSYS_FILEMONIKER
){
1032 CreateBindCtx(0,&pbind
);
1034 /* create a string based on common part of the two paths */
1036 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
1037 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
1039 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
1040 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
1042 if (nb1
==0 || nb2
==0)
1043 return MK_E_NOPREFIX
;
1045 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
1049 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
1050 (stringTable2
[sameIdx
]!=NULL
) &&
1051 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
1053 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1055 machimeNameCase
=TRUE
;
1057 for(i
=2;i
<sameIdx
;i
++)
1059 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1060 machimeNameCase
=FALSE
;
1065 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1068 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1069 ret
= MK_E_NOPREFIX
;
1072 for(i
=0;i
<sameIdx
;i
++)
1073 strcatW(commonPath
,stringTable1
[i
]);
1076 CoTaskMemFree(stringTable1
[i
]);
1078 CoTaskMemFree(stringTable1
);
1081 CoTaskMemFree(stringTable2
[i
]);
1083 CoTaskMemFree(stringTable2
);
1085 ret
= CreateFileMoniker(commonPath
,ppmkPrefix
);
1087 HeapFree(GetProcessHeap(),0,commonPath
);
1091 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1094 /******************************************************************************
1095 * DecomposePath (local function)
1096 ******************************************************************************/
1097 int WINAPI
FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
1099 static const WCHAR bSlash
[] = {'\\',0};
1100 WCHAR word
[MAX_PATH
];
1101 int i
=0,j
,tabIndex
=0;
1102 LPOLESTR
*strgtable
;
1104 int len
=lstrlenW(str
);
1106 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
1108 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1110 if (strgtable
==NULL
)
1111 return E_OUTOFMEMORY
;
1115 if(str
[i
]==bSlash
[0]){
1117 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1119 if (strgtable
[tabIndex
]==NULL
)
1120 return E_OUTOFMEMORY
;
1122 strcpyW(strgtable
[tabIndex
++],bSlash
);
1129 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1134 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1136 if (strgtable
[tabIndex
]==NULL
)
1137 return E_OUTOFMEMORY
;
1139 strcpyW(strgtable
[tabIndex
++],word
);
1142 strgtable
[tabIndex
]=NULL
;
1144 *stringTable
=strgtable
;
1149 /******************************************************************************
1150 * FileMoniker_RelativePathTo
1151 ******************************************************************************/
1152 HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1156 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1157 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1158 static const WCHAR back
[] ={'.','.','\\',0};
1160 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1162 if (ppmkRelPath
==NULL
)
1166 return E_INVALIDARG
;
1168 res
=CreateBindCtx(0,&bind
);
1172 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1175 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1179 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1180 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1182 if (FAILED(len1
) || FAILED(len2
))
1183 return E_OUTOFMEMORY
;
1185 /* count the number of similar items from the begin of the two paths */
1186 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1187 (tabStr2
[sameIdx
]!=NULL
) &&
1188 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1190 /* begin the construction of relativePath */
1191 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1192 /* by "..\\" in the begin */
1193 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1197 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1198 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1199 if (*tabStr1
[j
]!='\\')
1200 strcatW(relPath
,back
);
1202 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1203 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1204 strcatW(relPath
,tabStr2
[j
]);
1206 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1208 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1209 CoTaskMemFree(tabStr1
[j
]);
1210 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1211 CoTaskMemFree(tabStr2
[j
]);
1212 CoTaskMemFree(tabStr1
);
1213 CoTaskMemFree(tabStr2
);
1214 CoTaskMemFree(str1
);
1215 CoTaskMemFree(str2
);
1216 HeapFree(GetProcessHeap(),0,relPath
);
1218 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1224 /******************************************************************************
1225 * FileMoniker_GetDisplayName
1226 ******************************************************************************/
1227 HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,
1229 IMoniker
* pmkToLeft
,
1230 LPOLESTR
*ppszDisplayName
)
1232 ICOM_THIS(FileMonikerImpl
,iface
);
1234 int len
=lstrlenW(This
->filePathName
);
1236 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1238 if (ppszDisplayName
==NULL
)
1241 if (pmkToLeft
!=NULL
)
1242 return E_INVALIDARG
;
1244 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1245 if (*ppszDisplayName
==NULL
)
1246 return E_OUTOFMEMORY
;
1248 strcpyW(*ppszDisplayName
,This
->filePathName
);
1253 /******************************************************************************
1254 * FileMoniker_ParseDisplayName
1255 ******************************************************************************/
1256 HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,
1258 IMoniker
* pmkToLeft
,
1259 LPOLESTR pszDisplayName
,
1263 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1267 /******************************************************************************
1268 * FileMoniker_IsSystemMoniker
1269 ******************************************************************************/
1270 HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1272 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1277 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1282 /*******************************************************************************
1283 * FileMonikerIROTData_QueryInterface
1284 *******************************************************************************/
1285 HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1288 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1290 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
1292 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1295 /***********************************************************************
1296 * FileMonikerIROTData_AddRef
1298 ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1300 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1302 TRACE("(%p)\n",This
);
1304 return FileMonikerImpl_AddRef(This
);
1307 /***********************************************************************
1308 * FileMonikerIROTData_Release
1310 ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
)
1312 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1314 TRACE("(%p)\n",This
);
1316 return FileMonikerImpl_Release(This
);
1319 /******************************************************************************
1320 * FileMonikerIROTData_GetComparaisonData
1321 ******************************************************************************/
1322 HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,
1327 FIXME("(),stub!\n");
1331 /******************************************************************************
1332 * CreateFileMoniker (OLE32.@)
1333 ******************************************************************************/
1334 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1336 FileMonikerImpl
* newFileMoniker
= 0;
1337 HRESULT hr
= E_FAIL
;
1338 IID riid
=IID_IMoniker
;
1340 TRACE("(%p,%p)\n",lpszPathName
,ppmk
);
1345 if(lpszPathName
==NULL
)
1350 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1352 if (newFileMoniker
== 0)
1353 return E_OUTOFMEMORY
;
1355 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1358 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&riid
,(void**)ppmk
);
1360 HeapFree(GetProcessHeap(),0,newFileMoniker
);