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 ***************************************************************************************/
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
34 #include "compobj_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
38 const CLSID CLSID_FileMoniker
= {
39 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
42 /* filemoniker data structure */
43 typedef struct FileMonikerImpl
{
45 ICOM_VTABLE(IMoniker
)* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
47 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
48 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
50 ICOM_VTABLE(IROTData
)* lpvtbl2
; /* VTable relative to the IROTData interface.*/
52 ULONG ref
; /* reference counter for this object */
54 LPOLESTR filePathName
; /* path string identified by this filemoniker */
58 /********************************************************************************/
59 /* FileMoniker prototype functions : */
61 /* IUnknown prototype functions */
62 static HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
);
63 static ULONG WINAPI
FileMonikerImpl_AddRef(IMoniker
* iface
);
64 static ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
);
66 /* IPersist prototype functions */
67 static HRESULT WINAPI
FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
);
69 /* IPersistStream prototype functions */
70 static HRESULT WINAPI
FileMonikerImpl_IsDirty(IMoniker
* iface
);
71 static HRESULT WINAPI
FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
);
72 static HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
);
73 static HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
);
75 /* IMoniker prototype functions */
76 static HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
77 static HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, REFIID riid
, VOID
** ppvResult
);
78 static HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,IBindCtx
* pbc
, DWORD dwReduceHowFar
,IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
);
79 static HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,IMoniker
* pmkRight
,BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
);
80 static HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
);
81 static HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
);
82 static HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
);
83 static HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, IMoniker
* pmkNewlyRunning
);
84 static HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
, FILETIME
* pFileTime
);
85 static HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
);
86 static HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
, IMoniker
** ppmkPrefix
);
87 static HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
);
88 static HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
);
89 static HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,IBindCtx
* pbc
, IMoniker
* pmkToLeft
, LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
);
90 static HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
);
92 /********************************************************************************/
93 /* IROTData prototype functions */
95 /* IUnknown prototype functions */
96 static HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
* iface
,REFIID riid
,VOID
** ppvObject
);
97 static ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
* iface
);
98 static ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
);
100 /* IROTData prototype function */
101 static HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,BYTE
* pbData
,ULONG cbMax
,ULONG
* pcbData
);
103 /* Local function used by filemoniker implementation */
104 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
105 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
106 int WINAPI
FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** tabStr
);
109 /********************************************************************************/
110 /* Virtual function table for the FileMonikerImpl class which include IPersist,*/
111 /* IPersistStream and IMoniker functions. */
112 static ICOM_VTABLE(IMoniker
) VT_FileMonikerImpl
=
114 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
115 FileMonikerImpl_QueryInterface
,
116 FileMonikerImpl_AddRef
,
117 FileMonikerImpl_Release
,
118 FileMonikerImpl_GetClassID
,
119 FileMonikerImpl_IsDirty
,
120 FileMonikerImpl_Load
,
121 FileMonikerImpl_Save
,
122 FileMonikerImpl_GetSizeMax
,
123 FileMonikerImpl_BindToObject
,
124 FileMonikerImpl_BindToStorage
,
125 FileMonikerImpl_Reduce
,
126 FileMonikerImpl_ComposeWith
,
127 FileMonikerImpl_Enum
,
128 FileMonikerImpl_IsEqual
,
129 FileMonikerImpl_Hash
,
130 FileMonikerImpl_IsRunning
,
131 FileMonikerImpl_GetTimeOfLastChange
,
132 FileMonikerImpl_Inverse
,
133 FileMonikerImpl_CommonPrefixWith
,
134 FileMonikerImpl_RelativePathTo
,
135 FileMonikerImpl_GetDisplayName
,
136 FileMonikerImpl_ParseDisplayName
,
137 FileMonikerImpl_IsSystemMoniker
140 /********************************************************************************/
141 /* Virtual function table for the IROTData class. */
142 static ICOM_VTABLE(IROTData
) VT_ROTDataImpl
=
144 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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
);
285 /* read the first constant */
286 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
287 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
293 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
294 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
301 doubleLenHex
=doubleLenDec
=2*length
;
305 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
306 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
312 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
313 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
316 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
317 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
320 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
322 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
323 if (bread
!=doubleLenHex
)
326 if (This
->filePathName
!=NULL
)
327 HeapFree(GetProcessHeap(),0,This
->filePathName
);
329 This
->filePathName
=filePathW
;
331 HeapFree(GetProcessHeap(),0,filePathA
);
336 /******************************************************************************
338 ******************************************************************************/
339 HRESULT WINAPI
FileMonikerImpl_Save(IMoniker
* iface
,
340 IStream
* pStm
,/* pointer to the stream where the object is to be saved */
341 BOOL fClearDirty
)/* Specifies whether to clear the dirty flag */
343 /* this function saves data of this object. In the beginning I thougth
344 * that I have just to write the filePath string on Stream. But, when I
345 * tested this function whith windows programs samples, I noticed that it
346 * was not the case. So I analysed data written by this function on
347 * Windows and what this did function exactly ! But I have no idea about
349 * I guessed data which must be written on stream is:
350 * 1) WORD constant:zero
351 * 2) length of the path string ("\0" included)
352 * 3) path string type A
353 * 4) DWORD constant : 0xDEADFFFF
354 * 5) ten WORD constant: zero
355 * 6) DWORD: double-length of the the path string type W ("\0" not
357 * 7) WORD constant: 0x3
358 * 8) filePath unicode string.
359 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
362 ICOM_THIS(FileMonikerImpl
,iface
);
365 LPOLESTR filePathW
=This
->filePathName
;
369 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
370 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
377 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
382 /* write a DWORD set to 0 : constant */
383 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
385 /* write length of filePath string ( "\0" included )*/
386 len
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
387 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
389 /* write filePath string type A */
390 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
391 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, len
, NULL
, NULL
);
392 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
393 HeapFree(GetProcessHeap(),0,filePathA
);
395 /* write a DWORD set to 0xDEADFFFF: constant */
396 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
399 /* write 10 times a DWORD set to 0 : constants */
401 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
406 doubleLenHex
=doubleLenDec
=2*len
;
410 /* write double-length of the path string ( "\0" included )*/
411 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
416 /* write double-length (hexa representation) of the path string ( "\0" included ) */
417 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
419 /* write a WORD set to 0x3: constant */
420 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
422 /* write path unicode string */
423 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
428 /******************************************************************************
429 * FileMoniker_GetSizeMax
430 ******************************************************************************/
431 HRESULT WINAPI
FileMonikerImpl_GetSizeMax(IMoniker
* iface
,
432 ULARGE_INTEGER
* pcbSize
)/* Pointer to size of stream needed to save object */
434 ICOM_THIS(FileMonikerImpl
,iface
);
435 DWORD len
=lstrlenW(This
->filePathName
);
438 TRACE("(%p,%p)\n",iface
,pcbSize
);
443 /* for more details see FileMonikerImpl_Save coments */
445 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
446 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
447 (len
+1)+ /* filePath string */
448 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
449 10*sizeof(WORD
)+ /* 10 zero WORD */
450 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
452 if (len
==0 || len
> 8)
455 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
456 sizeof(WORD
)+ /* constant : 0x3 */
457 len
*sizeof(WCHAR
); /* unicde filePath string */
459 pcbSize
->s
.LowPart
=sizeMAx
;
460 pcbSize
->s
.HighPart
=0;
465 /******************************************************************************
466 * FileMoniker_Construct (local function)
467 *******************************************************************************/
468 HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
471 int sizeStr
=lstrlenW(lpszPathName
);
473 WCHAR twoPoint
[]={'.','.',0};
474 WCHAR bkSlash
[]={'\\',0};
477 TRACE("(%p,%p)\n",This
,lpszPathName
);
479 /* Initialize the virtual fgunction table. */
480 This
->lpvtbl1
= &VT_FileMonikerImpl
;
481 This
->lpvtbl2
= &VT_ROTDataImpl
;
484 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
486 if (This
->filePathName
==NULL
)
487 return E_OUTOFMEMORY
;
489 strcpyW(This
->filePathName
,lpszPathName
);
491 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
496 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
501 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
507 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
515 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
518 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
520 *This
->filePathName
=0;
522 for(i
=0;tabStr
[i
]!=NULL
;i
++)
523 strcatW(This
->filePathName
,tabStr
[i
]);
526 strcatW(This
->filePathName
,bkSlash
);
529 for(i
=0; tabStr
[i
]!=NULL
;i
++)
530 CoTaskMemFree(tabStr
[i
]);
531 CoTaskMemFree(tabStr
);
536 /******************************************************************************
537 * FileMoniker_Destroy (local function)
538 *******************************************************************************/
539 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
541 TRACE("(%p)\n",This
);
543 if (This
->filePathName
!=NULL
)
544 HeapFree(GetProcessHeap(),0,This
->filePathName
);
546 HeapFree(GetProcessHeap(),0,This
);
551 /******************************************************************************
552 * FileMoniker_BindToObject
553 ******************************************************************************/
554 HRESULT WINAPI
FileMonikerImpl_BindToObject(IMoniker
* iface
,
563 IRunningObjectTable
*prot
=0;
565 IClassFactory
*pcf
=0;
566 IClassActivator
*pca
=0;
568 ICOM_THIS(FileMonikerImpl
,iface
);
572 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvResult
);
576 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
579 /* if the requested class was loaded befor ! we dont need to reload it */
580 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
583 /* first activation of this class */
584 res
=GetClassFile(This
->filePathName
,&clsID
);
587 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
590 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
594 IUnknown_AddRef(pObj
);
602 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
604 if (res
==E_NOINTERFACE
){
606 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
608 if (res
==E_NOINTERFACE
)
609 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
613 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
615 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
620 IUnknown_AddRef(pObj
);
627 /*res=GetClassFile(This->filePathName,&clsID);
631 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
636 IUnknown_AddRef(pObj);
643 /* get the requested interface from the loaded class */
644 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
646 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
648 IUnknown_Release(pObj
);
652 IRunningObjectTable_Release(prot
);
655 IPersistFile_Release(ppf
);
658 IClassActivator_Release(pca
);
661 IClassFactory_Release(pcf
);
666 /******************************************************************************
667 * FileMoniker_BindToStorage
668 ******************************************************************************/
669 HRESULT WINAPI
FileMonikerImpl_BindToStorage(IMoniker
* iface
,
679 TRACE("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
681 if (pmkToLeft
==NULL
){
683 if (IsEqualIID(&IID_IStorage
, riid
)){
685 /* get the file name */
686 FileMonikerImpl_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
688 /* verifie if the file contains a storage object */
689 res
=StgIsStorageFile(filePath
);
693 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
699 IStorage_AddRef(pstg
);
704 CoTaskMemFree(filePath
);
707 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
712 return E_NOINTERFACE
;
716 FIXME("(%p,%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,riid
,ppvObject
);
723 /******************************************************************************
725 ******************************************************************************/
726 HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
* iface
,
728 DWORD dwReduceHowFar
,
729 IMoniker
** ppmkToLeft
,
730 IMoniker
** ppmkReduced
)
732 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
734 if (ppmkReduced
==NULL
)
737 FileMonikerImpl_AddRef(iface
);
741 return MK_S_REDUCED_TO_SELF
;
743 /******************************************************************************
744 * FileMoniker_ComposeWith
745 ******************************************************************************/
746 HRESULT WINAPI
FileMonikerImpl_ComposeWith(IMoniker
* iface
,
748 BOOL fOnlyIfNotGeneric
,
749 IMoniker
** ppmkComposite
)
752 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
753 WCHAR twoPoint
[]={'.','.',0};
754 WCHAR bkSlash
[]={'\\',0};
756 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
759 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
761 if (ppmkComposite
==NULL
)
769 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
771 /* check if we have two filemonikers to compose or not */
772 if(mkSys
==MKSYS_FILEMONIKER
){
774 CreateBindCtx(0,&bind
);
776 FileMonikerImpl_GetDisplayName(iface
,bind
,NULL
,&str1
);
777 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
779 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
780 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
781 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
783 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
786 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
789 /* for etch "..\" in the left of str2 remove the right element from str1 */
790 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
795 /* the length of the composed path string is raised by the sum of the two paths lengths */
796 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
799 return E_OUTOFMEMORY
;
801 /* new path is the concatenation of the rest of str1 and str2 */
802 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
803 strcatW(newStr
,strDec1
[j
]);
805 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
806 strcatW(newStr
,bkSlash
);
808 for(j
=i
;j
<=lastIdx2
;j
++)
809 strcatW(newStr
,strDec2
[j
]);
811 /* create a new moniker with the new string */
812 res
=CreateFileMoniker(newStr
,ppmkComposite
);
814 /* free all strings space memory used by this function */
815 HeapFree(GetProcessHeap(),0,newStr
);
817 for(i
=0; strDec1
[i
]!=NULL
;i
++)
818 CoTaskMemFree(strDec1
[i
]);
819 for(i
=0; strDec2
[i
]!=NULL
;i
++)
820 CoTaskMemFree(strDec2
[i
]);
821 CoTaskMemFree(strDec1
);
822 CoTaskMemFree(strDec2
);
829 else if(mkSys
==MKSYS_ANTIMONIKER
){
834 else if (fOnlyIfNotGeneric
){
837 return MK_E_NEEDGENERIC
;
841 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
844 /******************************************************************************
846 ******************************************************************************/
847 HRESULT WINAPI
FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
849 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
851 if (ppenumMoniker
== NULL
)
854 *ppenumMoniker
= NULL
;
859 /******************************************************************************
860 * FileMoniker_IsEqual
861 ******************************************************************************/
862 HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
864 ICOM_THIS(FileMonikerImpl
,iface
);
870 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
872 if (pmkOtherMoniker
==NULL
)
875 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
877 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
880 res
= CreateBindCtx(0,&bind
);
881 if (FAILED(res
)) return res
;
883 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
884 int result
= lstrcmpiW(filePath
, This
->filePathName
);
885 CoTaskMemFree(filePath
);
886 if ( result
== 0 ) return S_OK
;
892 /******************************************************************************
894 ******************************************************************************/
895 HRESULT WINAPI
FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
897 ICOM_THIS(FileMonikerImpl
,iface
);
899 int h
= 0,i
,skip
,len
;
906 val
= This
->filePathName
;
910 for (i
= len
; i
> 0; i
--) {
911 h
= (h
* 37) + val
[off
++];
914 /* only sample some characters */
916 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
917 h
= (h
* 39) + val
[off
];
926 /******************************************************************************
927 * FileMoniker_IsRunning
928 ******************************************************************************/
929 HRESULT WINAPI
FileMonikerImpl_IsRunning(IMoniker
* iface
,
932 IMoniker
* pmkNewlyRunning
)
934 IRunningObjectTable
* rot
;
937 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
939 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
945 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
950 res
= IRunningObjectTable_IsRunning(rot
,iface
);
952 IRunningObjectTable_Release(rot
);
957 /******************************************************************************
958 * FileMoniker_GetTimeOfLastChange
959 ******************************************************************************/
960 HRESULT WINAPI
FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
,
965 ICOM_THIS(FileMonikerImpl
,iface
);
966 IRunningObjectTable
* rot
;
968 WIN32_FILE_ATTRIBUTE_DATA info
;
970 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
978 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
983 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
985 if (FAILED(res
)){ /* the moniker is not registred */
987 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
988 return MK_E_NOOBJECT
;
990 *pFileTime
=info
.ftLastWriteTime
;
996 /******************************************************************************
997 * FileMoniker_Inverse
998 ******************************************************************************/
999 HRESULT WINAPI
FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
1002 TRACE("(%p,%p)\n",iface
,ppmk
);
1004 return CreateAntiMoniker(ppmk
);
1007 /******************************************************************************
1008 * FileMoniker_CommonPrefixWith
1009 ******************************************************************************/
1010 HRESULT WINAPI
FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
1013 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
1016 ULONG nb1
,nb2
,i
,sameIdx
;
1017 BOOL machimeNameCase
=FALSE
;
1019 if (ppmkPrefix
==NULL
)
1023 return E_INVALIDARG
;
1027 /* check if we have the same type of moniker */
1028 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
1030 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 return MK_E_NOPREFIX
;
1071 for(i
=0;i
<sameIdx
;i
++)
1072 strcatW(commonPath
,stringTable1
[i
]);
1075 CoTaskMemFree(stringTable1
[i
]);
1077 CoTaskMemFree(stringTable1
);
1080 CoTaskMemFree(stringTable2
[i
]);
1082 CoTaskMemFree(stringTable2
);
1084 HeapFree(GetProcessHeap(),0,commonPath
);
1086 return CreateFileMoniker(commonPath
,ppmkPrefix
);
1089 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
1092 /******************************************************************************
1093 * DecomposePath (local function)
1094 ******************************************************************************/
1095 int WINAPI
FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
1097 WCHAR bSlash
[] = {'\\',0};
1098 WCHAR word
[MAX_PATH
];
1099 int i
=0,j
,tabIndex
=0;
1100 LPOLESTR
*strgtable
;
1102 int len
=lstrlenW(str
);
1104 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
1106 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
1108 if (strgtable
==NULL
)
1109 return E_OUTOFMEMORY
;
1113 if(str
[i
]==bSlash
[0]){
1115 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
1117 if (strgtable
[tabIndex
]==NULL
)
1118 return E_OUTOFMEMORY
;
1120 strcpyW(strgtable
[tabIndex
++],bSlash
);
1127 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
1132 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1134 if (strgtable
[tabIndex
]==NULL
)
1135 return E_OUTOFMEMORY
;
1137 strcpyW(strgtable
[tabIndex
++],word
);
1140 strgtable
[tabIndex
]=NULL
;
1142 *stringTable
=strgtable
;
1147 /******************************************************************************
1148 * FileMoniker_RelativePathTo
1149 ******************************************************************************/
1150 HRESULT WINAPI
FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1154 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1155 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1156 WCHAR back
[] ={'.','.','\\',0};
1158 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1160 if (ppmkRelPath
==NULL
)
1164 return E_INVALIDARG
;
1166 res
=CreateBindCtx(0,&bind
);
1170 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1173 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1177 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1178 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1180 if (FAILED(len1
) || FAILED(len2
))
1181 return E_OUTOFMEMORY
;
1183 /* count the number of similar items from the begin of the two paths */
1184 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1185 (tabStr2
[sameIdx
]!=NULL
) &&
1186 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1188 /* begin the construction of relativePath */
1189 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1190 /* by "..\\" in the begin */
1191 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1195 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1196 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1197 if (*tabStr1
[j
]!='\\')
1198 strcatW(relPath
,back
);
1200 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1201 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1202 strcatW(relPath
,tabStr2
[j
]);
1204 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1206 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1207 CoTaskMemFree(tabStr1
[j
]);
1208 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1209 CoTaskMemFree(tabStr2
[j
]);
1210 CoTaskMemFree(tabStr1
);
1211 CoTaskMemFree(tabStr2
);
1212 CoTaskMemFree(str1
);
1213 CoTaskMemFree(str2
);
1214 HeapFree(GetProcessHeap(),0,relPath
);
1216 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1222 /******************************************************************************
1223 * FileMoniker_GetDisplayName
1224 ******************************************************************************/
1225 HRESULT WINAPI
FileMonikerImpl_GetDisplayName(IMoniker
* iface
,
1227 IMoniker
* pmkToLeft
,
1228 LPOLESTR
*ppszDisplayName
)
1230 ICOM_THIS(FileMonikerImpl
,iface
);
1232 int len
=lstrlenW(This
->filePathName
);
1234 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1236 if (ppszDisplayName
==NULL
)
1239 if (pmkToLeft
!=NULL
)
1240 return E_INVALIDARG
;
1242 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1243 if (*ppszDisplayName
==NULL
)
1244 return E_OUTOFMEMORY
;
1246 strcpyW(*ppszDisplayName
,This
->filePathName
);
1251 /******************************************************************************
1252 * FileMoniker_ParseDisplayName
1253 ******************************************************************************/
1254 HRESULT WINAPI
FileMonikerImpl_ParseDisplayName(IMoniker
* iface
,
1256 IMoniker
* pmkToLeft
,
1257 LPOLESTR pszDisplayName
,
1261 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1265 /******************************************************************************
1266 * FileMoniker_IsSystemMoniker
1267 ******************************************************************************/
1268 HRESULT WINAPI
FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1270 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1275 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1280 /*******************************************************************************
1281 * FileMonikerIROTData_QueryInterface
1282 *******************************************************************************/
1283 HRESULT WINAPI
FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1286 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1288 TRACE("(%p,%p,%p)\n",This
,riid
,ppvObject
);
1290 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1293 /***********************************************************************
1294 * FileMonikerIROTData_AddRef
1296 ULONG WINAPI
FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1298 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1300 TRACE("(%p)\n",This
);
1302 return FileMonikerImpl_AddRef(This
);
1305 /***********************************************************************
1306 * FileMonikerIROTData_Release
1308 ULONG WINAPI
FileMonikerROTDataImpl_Release(IROTData
* iface
)
1310 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1312 TRACE("(%p)\n",This
);
1314 return FileMonikerImpl_Release(This
);
1317 /******************************************************************************
1318 * FileMonikerIROTData_GetComparaisonData
1319 ******************************************************************************/
1320 HRESULT WINAPI
FileMonikerROTDataImpl_GetComparaisonData(IROTData
* iface
,
1325 FIXME("(),stub!\n");
1329 /******************************************************************************
1330 * CreateFileMoniker (OLE2.28)
1331 ******************************************************************************/
1332 HRESULT WINAPI
CreateFileMoniker16(LPCOLESTR16 lpszPathName
,LPMONIKER
* ppmk
)
1335 FIXME("(%s,%p),stub!\n",lpszPathName
,ppmk
);
1339 /******************************************************************************
1340 * CreateFileMoniker (OLE32.55)
1341 ******************************************************************************/
1342 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1344 FileMonikerImpl
* newFileMoniker
= 0;
1345 HRESULT hr
= E_FAIL
;
1346 IID riid
=IID_IMoniker
;
1348 TRACE("(%p,%p)\n",lpszPathName
,ppmk
);
1353 if(lpszPathName
==NULL
)
1358 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1360 if (newFileMoniker
== 0)
1361 return E_OUTOFMEMORY
;
1363 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1366 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&riid
,(void**)ppmk
);
1368 HeapFree(GetProcessHeap(),0,newFileMoniker
);