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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
38 #include "compobj_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
42 const CLSID CLSID_FileMoniker
= {
43 0x303, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
46 /* filemoniker data structure */
47 typedef struct FileMonikerImpl
{
49 IMonikerVtbl
* lpvtbl1
; /* VTable relative to the IMoniker interface.*/
51 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
52 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
54 IROTDataVtbl
* lpvtbl2
; /* VTable relative to the IROTData interface.*/
56 ULONG ref
; /* reference counter for this object */
58 LPOLESTR filePathName
; /* path string identified by this filemoniker */
60 IUnknown
*pMarshal
; /* custom marshaler */
63 /* Local function used by filemoniker implementation */
64 static HRESULT WINAPI
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
65 static HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* iface
);
67 /*******************************************************************************
68 * FileMoniker_QueryInterface
71 FileMonikerImpl_QueryInterface(IMoniker
* iface
,REFIID riid
,void** ppvObject
)
73 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
75 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
77 /* Perform a sanity check on the parameters.*/
78 if ( (This
==0) || (ppvObject
==0) )
81 /* Initialize the return parameter */
84 /* Compare the riid with the interface IDs implemented by this object.*/
85 if (IsEqualIID(&IID_IUnknown
, riid
) ||
86 IsEqualIID(&IID_IPersist
, riid
) ||
87 IsEqualIID(&IID_IPersistStream
,riid
) ||
88 IsEqualIID(&IID_IMoniker
, riid
)
92 else if (IsEqualIID(&IID_IROTData
, riid
))
93 *ppvObject
= (IROTData
*)&(This
->lpvtbl2
);
94 else if (IsEqualIID(&IID_IMarshal
, riid
))
98 hr
= MonikerMarshal_Create(iface
, &This
->pMarshal
);
101 return IUnknown_QueryInterface(This
->pMarshal
, riid
, ppvObject
);
104 /* Check that we obtained an interface.*/
106 return E_NOINTERFACE
;
108 /* Query Interface always increases the reference count by one when it is successful */
109 IMoniker_AddRef(iface
);
114 /******************************************************************************
118 FileMonikerImpl_AddRef(IMoniker
* iface
)
120 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
122 TRACE("(%p)\n",iface
);
124 return InterlockedIncrement(&This
->ref
);
127 /******************************************************************************
128 * FileMoniker_Release
131 FileMonikerImpl_Release(IMoniker
* iface
)
133 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
136 TRACE("(%p)\n",iface
);
138 ref
= InterlockedDecrement(&This
->ref
);
140 /* destroy the object if there's no more reference on it */
141 if (ref
== 0) FileMonikerImpl_Destroy(This
);
146 /******************************************************************************
147 * FileMoniker_GetClassID
149 static HRESULT WINAPI
150 FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
)
152 TRACE("(%p,%p)\n",iface
,pClassID
);
157 *pClassID
= CLSID_FileMoniker
;
162 /******************************************************************************
163 * FileMoniker_IsDirty
165 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
166 * method in the OLE-provided moniker interfaces always return S_FALSE because
167 * their internal state never changes.
169 static HRESULT WINAPI
170 FileMonikerImpl_IsDirty(IMoniker
* iface
)
173 TRACE("(%p)\n",iface
);
178 /******************************************************************************
181 * this function locates and reads from the stream the filePath string
182 * written by FileMonikerImpl_Save
184 static HRESULT WINAPI
185 FileMonikerImpl_Load(IMoniker
* iface
,IStream
* pStm
)
192 DWORD dwbuffer
,length
,i
,doubleLenHex
,doubleLenDec
;
194 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
196 TRACE("(%p,%p)\n",iface
,pStm
);
198 /* first WORD is non significative */
199 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
200 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
202 ERR("Couldn't read 0 word\n");
206 /* read filePath string length (plus one) */
207 res
=IStream_Read(pStm
,&length
,sizeof(DWORD
),&bread
);
208 if (bread
!= sizeof(DWORD
))
210 ERR("Couldn't read file string length\n");
214 /* read filePath string */
215 filePathA
=HeapAlloc(GetProcessHeap(),0,length
);
216 res
=IStream_Read(pStm
,filePathA
,length
,&bread
);
217 HeapFree(GetProcessHeap(),0,filePathA
);
220 ERR("Couldn't read file path string\n");
224 /* read the first constant */
225 IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
226 if (bread
!= sizeof(DWORD
) || dwbuffer
!= 0xDEADFFFF)
228 ERR("Couldn't read 0xDEADFFFF constant\n");
235 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
236 if (bread
!=sizeof(WORD
) || wbuffer
!=0)
238 ERR("Couldn't read 0 padding\n");
246 doubleLenHex
=doubleLenDec
=2*length
;
250 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
251 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenDec
)
257 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
258 if (bread
!=sizeof(DWORD
) || dwbuffer
!=doubleLenHex
)
261 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
262 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
265 filePathW
=HeapAlloc(GetProcessHeap(),0,(length
+1)*sizeof(WCHAR
));
267 res
=IStream_Read(pStm
,filePathW
,doubleLenHex
,&bread
);
268 if (bread
!=doubleLenHex
) {
269 HeapFree(GetProcessHeap(), 0, filePathW
);
273 HeapFree(GetProcessHeap(),0,This
->filePathName
);
275 This
->filePathName
=filePathW
;
280 /******************************************************************************
283 * This function saves data of this object. In the beginning I thougth
284 * that I have just to write the filePath string on Stream. But, when I
285 * tested this function whith windows programs samples, I noticed that it
286 * was not the case. So I analysed data written by this function on
287 * Windows and what this did function exactly ! But I have no idea about
289 * I guessed data which must be written on stream is:
290 * 1) WORD constant:zero
291 * 2) length of the path string ("\0" included)
292 * 3) path string type A
293 * 4) DWORD constant : 0xDEADFFFF
294 * 5) ten WORD constant: zero
295 * 6) DWORD: double-length of the the path string type W ("\0" not
297 * 7) WORD constant: 0x3
298 * 8) filePath unicode string.
299 * if the length(filePath) > 8 or length(filePath) == 8 stop at step 5)
301 static HRESULT WINAPI
302 FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
)
304 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
307 LPOLESTR filePathW
=This
->filePathName
;
311 DWORD constant1
= 0xDEADFFFF; /* these constants are detected after analysing the data structure written by */
312 WORD constant2
= 0x3; /* FileMoniker_Save function in a windows program system */
319 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
324 /* write a DWORD set to 0 : constant */
325 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
327 /* write length of filePath string ( "\0" included )*/
328 len
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
329 res
=IStream_Write(pStm
,&len
,sizeof(DWORD
),NULL
);
331 /* write filePath string type A */
332 filePathA
=HeapAlloc(GetProcessHeap(),0,len
);
333 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, len
, NULL
, NULL
);
334 res
=IStream_Write(pStm
,filePathA
,len
,NULL
);
335 HeapFree(GetProcessHeap(),0,filePathA
);
337 /* write a DWORD set to 0xDEADFFFF: constant */
338 res
=IStream_Write(pStm
,&constant1
,sizeof(DWORD
),NULL
);
341 /* write 10 times a DWORD set to 0 : constants */
343 res
=IStream_Write(pStm
,&zero
,sizeof(WORD
),NULL
);
348 doubleLenHex
=doubleLenDec
=2*len
;
352 /* write double-length of the path string ( "\0" included )*/
353 res
=IStream_Write(pStm
,&doubleLenDec
,sizeof(DWORD
),NULL
);
358 /* write double-length (hexa representation) of the path string ( "\0" included ) */
359 res
=IStream_Write(pStm
,&doubleLenHex
,sizeof(DWORD
),NULL
);
361 /* write a WORD set to 0x3: constant */
362 res
=IStream_Write(pStm
,&constant2
,sizeof(WORD
),NULL
);
364 /* write path unicode string */
365 res
=IStream_Write(pStm
,filePathW
,doubleLenHex
,NULL
);
370 /******************************************************************************
371 * FileMoniker_GetSizeMax
373 static HRESULT WINAPI
374 FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
)
376 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
377 DWORD len
=lstrlenW(This
->filePathName
);
380 TRACE("(%p,%p)\n",iface
,pcbSize
);
385 /* for more details see FileMonikerImpl_Save coments */
387 sizeMAx
= sizeof(WORD
) + /* first WORD is 0 */
388 sizeof(DWORD
)+ /* length of filePath including "\0" in the end of the string */
389 (len
+1)+ /* filePath string */
390 sizeof(DWORD
)+ /* constant : 0xDEADFFFF */
391 10*sizeof(WORD
)+ /* 10 zero WORD */
392 sizeof(DWORD
); /* size of the unicode filePath: "\0" not included */
394 if (len
==0 || len
> 8)
397 sizeMAx
+= sizeof(DWORD
)+ /* size of the unicode filePath: "\0" not included */
398 sizeof(WORD
)+ /* constant : 0x3 */
399 len
*sizeof(WCHAR
); /* unicde filePath string */
401 pcbSize
->u
.LowPart
=sizeMAx
;
402 pcbSize
->u
.HighPart
=0;
407 /******************************************************************************
408 * FileMoniker_Destroy (local function)
409 *******************************************************************************/
410 HRESULT WINAPI
FileMonikerImpl_Destroy(FileMonikerImpl
* This
)
412 TRACE("(%p)\n",This
);
414 if (This
->pMarshal
) IUnknown_Release(This
->pMarshal
);
415 HeapFree(GetProcessHeap(),0,This
->filePathName
);
416 HeapFree(GetProcessHeap(),0,This
);
421 /******************************************************************************
422 * FileMoniker_BindToObject
424 static HRESULT WINAPI
425 FileMonikerImpl_BindToObject(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
426 REFIID riid
, VOID
** ppvResult
)
431 IRunningObjectTable
*prot
=0;
433 IClassFactory
*pcf
=0;
434 IClassActivator
*pca
=0;
436 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
440 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvResult
);
444 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
447 /* if the requested class was loaded before ! we don't need to reload it */
448 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
451 /* first activation of this class */
452 res
=GetClassFile(This
->filePathName
,&clsID
);
455 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_ALL
,&IID_IPersistFile
,(void**)&ppf
);
458 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
462 IUnknown_AddRef(pObj
);
470 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
472 if (res
==E_NOINTERFACE
){
474 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
476 if (res
==E_NOINTERFACE
)
477 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
481 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)ppf
);
483 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
488 IUnknown_AddRef(pObj
);
495 /*res=GetClassFile(This->filePathName,&clsID);
499 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
504 IUnknown_AddRef(pObj);
511 /* get the requested interface from the loaded class */
512 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
514 IBindCtx_RegisterObjectBound(pbc
,(IUnknown
*)*ppvResult
);
516 IUnknown_Release(pObj
);
520 IRunningObjectTable_Release(prot
);
523 IPersistFile_Release(ppf
);
526 IClassActivator_Release(pca
);
529 IClassFactory_Release(pcf
);
534 /******************************************************************************
535 * FileMoniker_BindToStorage
537 static HRESULT WINAPI
538 FileMonikerImpl_BindToStorage(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
539 REFIID riid
, VOID
** ppvObject
)
545 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
547 if (pmkToLeft
==NULL
){
549 if (IsEqualIID(&IID_IStorage
, riid
)){
551 /* get the file name */
552 IMoniker_GetDisplayName(iface
,pbc
,pmkToLeft
,&filePath
);
554 /* verifie if the file contains a storage object */
555 res
=StgIsStorageFile(filePath
);
559 res
=StgOpenStorage(filePath
,NULL
,STGM_READWRITE
|STGM_SHARE_DENY_WRITE
,NULL
,0,&pstg
);
565 IStorage_AddRef(pstg
);
570 CoTaskMemFree(filePath
);
573 if ( (IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)) )
576 return E_NOINTERFACE
;
580 FIXME("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvObject
);
587 /******************************************************************************
589 ******************************************************************************/
590 static HRESULT WINAPI
591 FileMonikerImpl_Reduce(IMoniker
* iface
, IBindCtx
* pbc
, DWORD dwReduceHowFar
,
592 IMoniker
** ppmkToLeft
, IMoniker
** ppmkReduced
)
594 TRACE("(%p,%p,%ld,%p,%p)\n",iface
,pbc
,dwReduceHowFar
,ppmkToLeft
,ppmkReduced
);
596 if (ppmkReduced
==NULL
)
599 IMoniker_AddRef(iface
);
603 return MK_S_REDUCED_TO_SELF
;
606 /******************************************************************************
607 * FileMoniker_ComposeWith
609 static HRESULT WINAPI
610 FileMonikerImpl_ComposeWith(IMoniker
* iface
, IMoniker
* pmkRight
,
611 BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
)
614 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
615 static const WCHAR twoPoint
[]={'.','.',0};
616 static const WCHAR bkSlash
[]={'\\',0};
618 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
621 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
623 if (ppmkComposite
==NULL
)
631 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
633 /* check if we have two filemonikers to compose or not */
634 if(mkSys
==MKSYS_FILEMONIKER
){
636 CreateBindCtx(0,&bind
);
638 IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
639 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
641 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
642 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
643 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
645 if ((lastIdx1
==-1 && lastIdx2
>-1)||(lastIdx1
==1 && lstrcmpW(strDec1
[0],twoPoint
)==0))
648 if(lstrcmpW(strDec1
[lastIdx1
],bkSlash
)==0)
651 /* for etch "..\" in the left of str2 remove the right element from str1 */
652 for(i
=0; ( (lastIdx1
>=0) && (strDec2
[i
]!=NULL
) && (lstrcmpW(strDec2
[i
],twoPoint
)==0) ) ;i
+=2){
657 /* the length of the composed path string is raised by the sum of the two paths lengths */
658 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
661 return E_OUTOFMEMORY
;
663 /* new path is the concatenation of the rest of str1 and str2 */
664 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
665 strcatW(newStr
,strDec1
[j
]);
667 if ((strDec2
[i
]==NULL
&& lastIdx1
>-1 && lastIdx2
>-1) || lstrcmpW(strDec2
[i
],bkSlash
)!=0)
668 strcatW(newStr
,bkSlash
);
670 for(j
=i
;j
<=lastIdx2
;j
++)
671 strcatW(newStr
,strDec2
[j
]);
673 /* create a new moniker with the new string */
674 res
=CreateFileMoniker(newStr
,ppmkComposite
);
676 /* free all strings space memory used by this function */
677 HeapFree(GetProcessHeap(),0,newStr
);
679 for(i
=0; strDec1
[i
]!=NULL
;i
++)
680 CoTaskMemFree(strDec1
[i
]);
681 for(i
=0; strDec2
[i
]!=NULL
;i
++)
682 CoTaskMemFree(strDec2
[i
]);
683 CoTaskMemFree(strDec1
);
684 CoTaskMemFree(strDec2
);
691 else if(mkSys
==MKSYS_ANTIMONIKER
){
696 else if (fOnlyIfNotGeneric
){
699 return MK_E_NEEDGENERIC
;
703 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
706 /******************************************************************************
709 static HRESULT WINAPI
710 FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
712 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
714 if (ppenumMoniker
== NULL
)
717 *ppenumMoniker
= NULL
;
722 /******************************************************************************
723 * FileMoniker_IsEqual
725 static HRESULT WINAPI
726 FileMonikerImpl_IsEqual(IMoniker
* iface
,IMoniker
* pmkOtherMoniker
)
728 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
734 TRACE("(%p,%p)\n",iface
,pmkOtherMoniker
);
736 if (pmkOtherMoniker
==NULL
)
739 IMoniker_GetClassID(pmkOtherMoniker
,&clsid
);
741 if (!IsEqualCLSID(&clsid
,&CLSID_FileMoniker
))
744 res
= CreateBindCtx(0,&bind
);
745 if (FAILED(res
)) return res
;
747 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker
,bind
,NULL
,&filePath
))) {
748 int result
= lstrcmpiW(filePath
, This
->filePathName
);
749 CoTaskMemFree(filePath
);
750 if ( result
== 0 ) return S_OK
;
756 /******************************************************************************
759 static HRESULT WINAPI
760 FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
762 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
764 int h
= 0,i
,skip
,len
;
771 val
= This
->filePathName
;
775 for (i
= len
; i
> 0; i
--) {
776 h
= (h
* 37) + val
[off
++];
779 /* only sample some characters */
781 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
782 h
= (h
* 39) + val
[off
];
791 /******************************************************************************
792 * FileMoniker_IsRunning
794 static HRESULT WINAPI
795 FileMonikerImpl_IsRunning(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
796 IMoniker
* pmkNewlyRunning
)
798 IRunningObjectTable
* rot
;
801 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
803 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
809 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
814 res
= IRunningObjectTable_IsRunning(rot
,iface
);
816 IRunningObjectTable_Release(rot
);
821 /******************************************************************************
822 * FileMoniker_GetTimeOfLastChange
823 ******************************************************************************/
824 static HRESULT WINAPI
825 FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
,
826 IMoniker
* pmkToLeft
, FILETIME
* pFileTime
)
828 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
829 IRunningObjectTable
* rot
;
831 WIN32_FILE_ATTRIBUTE_DATA info
;
833 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
841 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
846 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
848 if (FAILED(res
)){ /* the moniker is not registered */
850 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
851 return MK_E_NOOBJECT
;
853 *pFileTime
=info
.ftLastWriteTime
;
859 /******************************************************************************
860 * FileMoniker_Inverse
862 static HRESULT WINAPI
863 FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
865 TRACE("(%p,%p)\n",iface
,ppmk
);
867 return CreateAntiMoniker(ppmk
);
870 /******************************************************************************
871 * FileMoniker_CommonPrefixWith
873 static HRESULT WINAPI
874 FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
877 LPOLESTR pathThis
,pathOther
,*stringTable1
,*stringTable2
,commonPath
;
880 ULONG nb1
,nb2
,i
,sameIdx
;
881 BOOL machimeNameCase
=FALSE
;
883 if (ppmkPrefix
==NULL
)
891 /* check if we have the same type of moniker */
892 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
894 if(mkSys
==MKSYS_FILEMONIKER
){
897 CreateBindCtx(0,&pbind
);
899 /* create a string based on common part of the two paths */
901 IMoniker_GetDisplayName(iface
,pbind
,NULL
,&pathThis
);
902 IMoniker_GetDisplayName(pmkOther
,pbind
,NULL
,&pathOther
);
904 nb1
=FileMonikerImpl_DecomposePath(pathThis
,&stringTable1
);
905 nb2
=FileMonikerImpl_DecomposePath(pathOther
,&stringTable2
);
907 if (nb1
==0 || nb2
==0)
908 return MK_E_NOPREFIX
;
910 commonPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
914 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
915 (stringTable2
[sameIdx
]!=NULL
) &&
916 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
918 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
920 machimeNameCase
=TRUE
;
922 for(i
=2;i
<sameIdx
;i
++)
924 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
925 machimeNameCase
=FALSE
;
930 if (machimeNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
933 if (machimeNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
937 for(i
=0;i
<sameIdx
;i
++)
938 strcatW(commonPath
,stringTable1
[i
]);
941 CoTaskMemFree(stringTable1
[i
]);
943 CoTaskMemFree(stringTable1
);
946 CoTaskMemFree(stringTable2
[i
]);
948 CoTaskMemFree(stringTable2
);
950 ret
= CreateFileMoniker(commonPath
,ppmkPrefix
);
952 HeapFree(GetProcessHeap(),0,commonPath
);
956 return MonikerCommonPrefixWith(iface
,pmkOther
,ppmkPrefix
);
959 /******************************************************************************
960 * DecomposePath (local function)
962 int FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
964 static const WCHAR bSlash
[] = {'\\',0};
965 WCHAR word
[MAX_PATH
];
966 int i
=0,j
,tabIndex
=0;
967 LPOLESTR
*strgtable
;
969 int len
=lstrlenW(str
);
971 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
973 strgtable
=CoTaskMemAlloc(len
*sizeof(LPOLESTR
));
976 return E_OUTOFMEMORY
;
980 if(str
[i
]==bSlash
[0]){
982 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
984 if (strgtable
[tabIndex
]==NULL
)
985 return E_OUTOFMEMORY
;
987 strcpyW(strgtable
[tabIndex
++],bSlash
);
994 for(j
=0; str
[i
]!=0 && str
[i
]!=bSlash
[0] ; i
++,j
++)
999 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
1001 if (strgtable
[tabIndex
]==NULL
)
1002 return E_OUTOFMEMORY
;
1004 strcpyW(strgtable
[tabIndex
++],word
);
1007 strgtable
[tabIndex
]=NULL
;
1009 *stringTable
=strgtable
;
1014 /******************************************************************************
1015 * FileMoniker_RelativePathTo
1017 static HRESULT WINAPI
1018 FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1022 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1023 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1024 static const WCHAR back
[] ={'.','.','\\',0};
1026 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1028 if (ppmkRelPath
==NULL
)
1032 return E_INVALIDARG
;
1034 res
=CreateBindCtx(0,&bind
);
1038 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1041 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1045 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1046 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1048 if (FAILED(len1
) || FAILED(len2
))
1049 return E_OUTOFMEMORY
;
1051 /* count the number of similar items from the begin of the two paths */
1052 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1053 (tabStr2
[sameIdx
]!=NULL
) &&
1054 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1056 /* begin the construction of relativePath */
1057 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1058 /* by "..\\" in the begin */
1059 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1063 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1064 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1065 if (*tabStr1
[j
]!='\\')
1066 strcatW(relPath
,back
);
1068 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1069 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1070 strcatW(relPath
,tabStr2
[j
]);
1072 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1074 for(j
=0; tabStr1
[j
]!=NULL
;j
++)
1075 CoTaskMemFree(tabStr1
[j
]);
1076 for(j
=0; tabStr2
[j
]!=NULL
;j
++)
1077 CoTaskMemFree(tabStr2
[j
]);
1078 CoTaskMemFree(tabStr1
);
1079 CoTaskMemFree(tabStr2
);
1080 CoTaskMemFree(str1
);
1081 CoTaskMemFree(str2
);
1082 HeapFree(GetProcessHeap(),0,relPath
);
1084 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1090 /******************************************************************************
1091 * FileMoniker_GetDisplayName
1093 static HRESULT WINAPI
1094 FileMonikerImpl_GetDisplayName(IMoniker
* iface
, IBindCtx
* pbc
,
1095 IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
)
1097 FileMonikerImpl
*This
= (FileMonikerImpl
*)iface
;
1099 int len
=lstrlenW(This
->filePathName
);
1101 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1103 if (ppszDisplayName
==NULL
)
1106 if (pmkToLeft
!=NULL
)
1107 return E_INVALIDARG
;
1109 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1110 if (*ppszDisplayName
==NULL
)
1111 return E_OUTOFMEMORY
;
1113 strcpyW(*ppszDisplayName
,This
->filePathName
);
1115 TRACE("-- %s\n", debugstr_w(*ppszDisplayName
));
1120 /******************************************************************************
1121 * FileMoniker_ParseDisplayName
1123 static HRESULT WINAPI
1124 FileMonikerImpl_ParseDisplayName(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
1125 LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
)
1127 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1131 /******************************************************************************
1132 * FileMoniker_IsSystemMoniker
1134 static HRESULT WINAPI
1135 FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1137 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1142 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1147 /*******************************************************************************
1148 * FileMonikerIROTData_QueryInterface
1150 static HRESULT WINAPI
1151 FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1154 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1156 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1158 return FileMonikerImpl_QueryInterface(This
, riid
, ppvObject
);
1161 /***********************************************************************
1162 * FileMonikerIROTData_AddRef
1165 FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1167 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1169 TRACE("(%p)\n",This
);
1171 return IMoniker_AddRef(This
);
1174 /***********************************************************************
1175 * FileMonikerIROTData_Release
1178 FileMonikerROTDataImpl_Release(IROTData
* iface
)
1180 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1182 TRACE("(%p)\n",This
);
1184 return FileMonikerImpl_Release(This
);
1187 /******************************************************************************
1188 * FileMonikerIROTData_GetComparaisonData
1190 static HRESULT WINAPI
1191 FileMonikerROTDataImpl_GetComparisonData(IROTData
* iface
, BYTE
* pbData
,
1192 ULONG cbMax
, ULONG
* pcbData
)
1194 ICOM_THIS_From_IROTData(IMoniker
, iface
);
1195 FileMonikerImpl
*This1
= (FileMonikerImpl
*)This
;
1196 int len
= (strlenW(This1
->filePathName
)+1);
1200 TRACE("(%p, %lu, %p)\n", pbData
, cbMax
, pcbData
);
1202 *pcbData
= sizeof(CLSID
) + len
* sizeof(WCHAR
);
1203 if (cbMax
< *pcbData
)
1204 return E_OUTOFMEMORY
;
1206 memcpy(pbData
, &CLSID_FileMoniker
, sizeof(CLSID
));
1207 pszFileName
= (LPWSTR
)(pbData
+sizeof(CLSID
));
1208 for (i
= 0; i
< len
; i
++)
1209 pszFileName
[i
] = toupperW(This1
->filePathName
[i
]);
1215 * Virtual function table for the FileMonikerImpl class which include IPersist,
1216 * IPersistStream and IMoniker functions.
1218 static IMonikerVtbl VT_FileMonikerImpl
=
1220 FileMonikerImpl_QueryInterface
,
1221 FileMonikerImpl_AddRef
,
1222 FileMonikerImpl_Release
,
1223 FileMonikerImpl_GetClassID
,
1224 FileMonikerImpl_IsDirty
,
1225 FileMonikerImpl_Load
,
1226 FileMonikerImpl_Save
,
1227 FileMonikerImpl_GetSizeMax
,
1228 FileMonikerImpl_BindToObject
,
1229 FileMonikerImpl_BindToStorage
,
1230 FileMonikerImpl_Reduce
,
1231 FileMonikerImpl_ComposeWith
,
1232 FileMonikerImpl_Enum
,
1233 FileMonikerImpl_IsEqual
,
1234 FileMonikerImpl_Hash
,
1235 FileMonikerImpl_IsRunning
,
1236 FileMonikerImpl_GetTimeOfLastChange
,
1237 FileMonikerImpl_Inverse
,
1238 FileMonikerImpl_CommonPrefixWith
,
1239 FileMonikerImpl_RelativePathTo
,
1240 FileMonikerImpl_GetDisplayName
,
1241 FileMonikerImpl_ParseDisplayName
,
1242 FileMonikerImpl_IsSystemMoniker
1245 /* Virtual function table for the IROTData class. */
1246 static IROTDataVtbl VT_ROTDataImpl
=
1248 FileMonikerROTDataImpl_QueryInterface
,
1249 FileMonikerROTDataImpl_AddRef
,
1250 FileMonikerROTDataImpl_Release
,
1251 FileMonikerROTDataImpl_GetComparisonData
1254 /******************************************************************************
1255 * FileMoniker_Construct (local function)
1257 static HRESULT WINAPI
1258 FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
1261 int sizeStr
=lstrlenW(lpszPathName
);
1263 static const WCHAR twoPoint
[]={'.','.',0};
1264 static const WCHAR bkSlash
[]={'\\',0};
1267 TRACE("(%p,%s)\n",This
,debugstr_w(lpszPathName
));
1269 /* Initialize the virtual fgunction table. */
1270 This
->lpvtbl1
= &VT_FileMonikerImpl
;
1271 This
->lpvtbl2
= &VT_ROTDataImpl
;
1273 This
->pMarshal
= NULL
;
1275 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
1277 if (This
->filePathName
==NULL
)
1278 return E_OUTOFMEMORY
;
1280 strcpyW(This
->filePathName
,lpszPathName
);
1282 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
1287 if (lstrcmpW(tabStr
[0],twoPoint
)!=0)
1292 if ( (lstrcmpW(tabStr
[i
],twoPoint
)!=0) && (lstrcmpW(tabStr
[i
],bkSlash
)!=0) ){
1298 if (lstrcmpW(tabStr
[i
],bkSlash
)==0 && i
<nb
-1 && lstrcmpW(tabStr
[i
+1],bkSlash
)==0){
1306 if (lstrcmpW(tabStr
[nb
-1],bkSlash
)==0)
1309 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
1311 *This
->filePathName
=0;
1313 for(i
=0;tabStr
[i
]!=NULL
;i
++)
1314 strcatW(This
->filePathName
,tabStr
[i
]);
1317 strcatW(This
->filePathName
,bkSlash
);
1320 for(i
=0; tabStr
[i
]!=NULL
;i
++)
1321 CoTaskMemFree(tabStr
[i
]);
1322 CoTaskMemFree(tabStr
);
1327 /******************************************************************************
1328 * CreateFileMoniker (OLE32.@)
1329 ******************************************************************************/
1330 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, LPMONIKER
* ppmk
)
1332 FileMonikerImpl
* newFileMoniker
;
1335 TRACE("(%s,%p)\n",debugstr_w(lpszPathName
),ppmk
);
1345 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1347 if (!newFileMoniker
)
1348 return E_OUTOFMEMORY
;
1350 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1353 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
,&IID_IMoniker
,(void**)ppmk
);
1355 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1360 static HRESULT WINAPI
FileMonikerCF_QueryInterface(LPCLASSFACTORY iface
,
1361 REFIID riid
, LPVOID
*ppv
)
1364 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1367 IUnknown_AddRef(iface
);
1370 return E_NOINTERFACE
;
1373 static ULONG WINAPI
FileMonikerCF_AddRef(LPCLASSFACTORY iface
)
1375 return 2; /* non-heap based object */
1378 static ULONG WINAPI
FileMonikerCF_Release(LPCLASSFACTORY iface
)
1380 return 1; /* non-heap based object */
1383 static HRESULT WINAPI
FileMonikerCF_CreateInstance(LPCLASSFACTORY iface
,
1384 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1386 FileMonikerImpl
* newFileMoniker
;
1388 static const WCHAR wszEmpty
[] = { 0 };
1390 TRACE("(%p, %s, %p)\n", pUnk
, debugstr_guid(riid
), ppv
);
1395 return CLASS_E_NOAGGREGATION
;
1397 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1398 if (!newFileMoniker
)
1399 return E_OUTOFMEMORY
;
1401 hr
= FileMonikerImpl_Construct(newFileMoniker
, wszEmpty
);
1404 hr
= FileMonikerImpl_QueryInterface((IMoniker
*)newFileMoniker
, riid
, ppv
);
1406 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1411 static HRESULT WINAPI
FileMonikerCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1413 FIXME("(%d), stub!\n",fLock
);
1417 static const IClassFactoryVtbl FileMonikerCFVtbl
=
1419 FileMonikerCF_QueryInterface
,
1420 FileMonikerCF_AddRef
,
1421 FileMonikerCF_Release
,
1422 FileMonikerCF_CreateInstance
,
1423 FileMonikerCF_LockServer
1425 static const IClassFactoryVtbl
*FileMonikerCF
= &FileMonikerCFVtbl
;
1427 HRESULT
FileMonikerCF_Create(REFIID riid
, LPVOID
*ppv
)
1429 return IClassFactory_QueryInterface((IClassFactory
*)&FileMonikerCF
, riid
, ppv
);