2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 * Copyright 2007 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSUNION
33 #include "wine/debug.h"
37 #include "compobj_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
41 /* filemoniker data structure */
42 typedef struct FileMonikerImpl
{
43 IMoniker IMoniker_iface
;
44 IROTData IROTData_iface
;
46 LPOLESTR filePathName
; /* path string identified by this filemoniker */
47 IUnknown
*pMarshal
; /* custom marshaler */
50 static inline FileMonikerImpl
*impl_from_IMoniker(IMoniker
*iface
)
52 return CONTAINING_RECORD(iface
, FileMonikerImpl
, IMoniker_iface
);
55 static inline FileMonikerImpl
*impl_from_IROTData(IROTData
*iface
)
57 return CONTAINING_RECORD(iface
, FileMonikerImpl
, IROTData_iface
);
60 static const IMonikerVtbl VT_FileMonikerImpl
;
62 static FileMonikerImpl
*unsafe_impl_from_IMoniker(IMoniker
*iface
)
64 if (iface
->lpVtbl
!= &VT_FileMonikerImpl
)
66 return CONTAINING_RECORD(iface
, FileMonikerImpl
, IMoniker_iface
);
69 /* Local function used by filemoniker implementation */
70 static HRESULT
FileMonikerImpl_Construct(FileMonikerImpl
* iface
, LPCOLESTR lpszPathName
);
72 /*******************************************************************************
73 * FileMoniker_QueryInterface
75 static HRESULT WINAPI
FileMonikerImpl_QueryInterface(IMoniker
*iface
, REFIID riid
, void **ppvObject
)
77 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
79 TRACE("%p, %s, %p.\n", iface
, debugstr_guid(riid
), ppvObject
);
86 if (IsEqualIID(&IID_IUnknown
, riid
) ||
87 IsEqualIID(&IID_IPersist
, riid
) ||
88 IsEqualIID(&IID_IPersistStream
,riid
) ||
89 IsEqualIID(&IID_IMoniker
, riid
) ||
90 IsEqualGUID(&CLSID_FileMoniker
, riid
))
94 else if (IsEqualIID(&IID_IROTData
, riid
))
95 *ppvObject
= &This
->IROTData_iface
;
96 else if (IsEqualIID(&IID_IMarshal
, riid
))
100 hr
= MonikerMarshal_Create(iface
, &This
->pMarshal
);
103 return IUnknown_QueryInterface(This
->pMarshal
, riid
, ppvObject
);
107 return E_NOINTERFACE
;
109 IMoniker_AddRef(iface
);
114 /******************************************************************************
118 FileMonikerImpl_AddRef(IMoniker
* iface
)
120 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
122 TRACE("(%p)\n",iface
);
124 return InterlockedIncrement(&This
->ref
);
127 static ULONG WINAPI
FileMonikerImpl_Release(IMoniker
* iface
)
129 FileMonikerImpl
*moniker
= impl_from_IMoniker(iface
);
130 ULONG ref
= InterlockedDecrement(&moniker
->ref
);
132 TRACE("(%p, refcount %d)\n", iface
, ref
);
136 if (moniker
->pMarshal
) IUnknown_Release(moniker
->pMarshal
);
137 HeapFree(GetProcessHeap(), 0, moniker
->filePathName
);
138 HeapFree(GetProcessHeap(), 0, moniker
);
144 /******************************************************************************
145 * FileMoniker_GetClassID
147 static HRESULT WINAPI
148 FileMonikerImpl_GetClassID(IMoniker
* iface
, CLSID
*pClassID
)
150 TRACE("(%p,%p)\n",iface
,pClassID
);
155 *pClassID
= CLSID_FileMoniker
;
160 /******************************************************************************
161 * FileMoniker_IsDirty
163 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
164 * method in the OLE-provided moniker interfaces always return S_FALSE because
165 * their internal state never changes.
167 static HRESULT WINAPI
168 FileMonikerImpl_IsDirty(IMoniker
* iface
)
171 TRACE("(%p)\n",iface
);
176 /******************************************************************************
179 * this function locates and reads from the stream the filePath string
180 * written by FileMonikerImpl_Save
182 static HRESULT WINAPI
183 FileMonikerImpl_Load(IMoniker
* iface
, IStream
* pStm
)
185 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
187 CHAR
* filePathA
= NULL
;
188 WCHAR
* filePathW
= NULL
;
191 DWORD dwbuffer
, bytesA
, bytesW
, len
;
195 TRACE("(%p,%p)\n",iface
,pStm
);
198 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
199 if (bread
!=sizeof(WORD
))
201 WARN("Couldn't read 0 word\n");
205 /* read filePath string length (plus one) */
206 res
=IStream_Read(pStm
,&bytesA
,sizeof(DWORD
),&bread
);
207 if (bread
!= sizeof(DWORD
))
209 WARN("Couldn't read file string length\n");
213 /* read filePath string */
214 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
221 res
=IStream_Read(pStm
,filePathA
,bytesA
,&bread
);
224 WARN("Couldn't read file path string\n");
228 /* read the unknown value */
229 IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
230 if (bread
!= sizeof(WORD
))
232 WARN("Couldn't read unknown value\n");
236 /* read the DEAD constant */
237 IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
238 if (bread
!= sizeof(WORD
))
240 WARN("Couldn't read DEAD constant\n");
246 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
247 if (bread
!=sizeof(DWORD
))
249 WARN("Couldn't read 0 padding\n");
254 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
255 if (bread
!=sizeof(DWORD
))
258 if (!dwbuffer
) /* No W-string */
261 len
=MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, bytesA
, NULL
, 0);
265 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
271 MultiByteToWideChar(CP_ACP
, MB_ERR_INVALID_CHARS
, filePathA
, -1, filePathW
, len
+1);
280 res
=IStream_Read(pStm
,&dwbuffer
,sizeof(DWORD
),&bread
);
281 if (bread
!=sizeof(DWORD
) || dwbuffer
!=bytesW
)
284 res
=IStream_Read(pStm
,&wbuffer
,sizeof(WORD
),&bread
);
285 if (bread
!=sizeof(WORD
) || wbuffer
!=0x3)
288 len
=bytesW
/sizeof(WCHAR
);
289 filePathW
=HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
295 res
=IStream_Read(pStm
,filePathW
,bytesW
,&bread
);
302 HeapFree(GetProcessHeap(),0,filePathA
);
303 HeapFree(GetProcessHeap(),0,This
->filePathName
);
304 This
->filePathName
=filePathW
;
309 HeapFree(GetProcessHeap(), 0, filePathA
);
310 HeapFree(GetProcessHeap(), 0, filePathW
);
317 /******************************************************************************
320 * This function saves data of this object. In the beginning I thought
321 * that I have just to write the filePath string on Stream. But, when I
322 * tested this function with windows program samples, I noticed that it
323 * was not the case. This implementation is based on XP SP2. Other versions
324 * of Windows have minor variations.
326 * Data which must be written on stream is:
327 * 1) WORD constant: zero (not validated by Windows)
328 * 2) length of the path string ("\0" included)
329 * 3) path string type A
330 * 4) Unknown WORD value: Frequently 0xFFFF, but not always. If set very large,
331 * Windows returns E_OUTOFMEMORY
332 * 5) WORD Constant: 0xDEAD (not validated by Windows)
333 * 6) five DWORD constant: zero (not validated by Windows)
334 * 7) If we're only writing the multibyte version,
335 * write a zero DWORD and finish.
337 * 8) DWORD: double-length of the path string type W ("\0" not
339 * 9) WORD constant: 0x3
340 * 10) filePath unicode string.
343 static HRESULT WINAPI
344 FileMonikerImpl_Save(IMoniker
* iface
, IStream
* pStm
, BOOL fClearDirty
)
346 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
348 LPOLESTR filePathW
=This
->filePathName
;
350 DWORD bytesA
, bytesW
, len
;
352 static const WORD FFFF
= 0xFFFF; /* Constants */
353 static const WORD DEAD
= 0xDEAD;
354 static const DWORD ZERO
= 0;
355 static const WORD THREE
= 0x3;
358 BOOL bUsedDefault
, bWriteWide
;
360 TRACE("(%p,%p,%d)\n",iface
,pStm
,fClearDirty
);
366 res
=IStream_Write(pStm
,&ZERO
,sizeof(WORD
),NULL
);
367 if (FAILED(res
)) return res
;
369 /* write length of filePath string ( 0 included )*/
370 bytesA
= WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, NULL
, 0, NULL
, NULL
);
371 res
=IStream_Write(pStm
,&bytesA
,sizeof(DWORD
),NULL
);
372 if (FAILED(res
)) return res
;
374 /* write A string (with '\0') */
375 filePathA
=HeapAlloc(GetProcessHeap(),0,bytesA
);
377 return E_OUTOFMEMORY
;
378 WideCharToMultiByte( CP_ACP
, 0, filePathW
, -1, filePathA
, bytesA
, NULL
, &bUsedDefault
);
379 res
=IStream_Write(pStm
,filePathA
,bytesA
,NULL
);
380 HeapFree(GetProcessHeap(),0,filePathA
);
381 if (FAILED(res
)) return res
;
383 /* write a WORD 0xFFFF */
384 res
=IStream_Write(pStm
,&FFFF
,sizeof(WORD
),NULL
);
385 if (FAILED(res
)) return res
;
387 /* write a WORD 0xDEAD */
388 res
=IStream_Write(pStm
,&DEAD
,sizeof(WORD
),NULL
);
389 if (FAILED(res
)) return res
;
391 /* write 5 zero DWORDs */
394 res
=IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
395 if (FAILED(res
)) return res
;
398 /* Write the wide version if:
399 * + couldn't convert to CP_ACP,
400 * or + it's a directory,
401 * or + there's a character > 0xFF
403 len
= lstrlenW(filePathW
);
404 bWriteWide
= (bUsedDefault
|| (len
> 0 && filePathW
[len
-1]=='\\' ));
408 for(pch
=filePathW
;*pch
;++pch
)
419 return IStream_Write(pStm
,&ZERO
,sizeof(DWORD
),NULL
);
421 /* write bytes needed for the filepathW (without 0) + 6 */
422 bytesW
= len
*sizeof(WCHAR
) + 6;
423 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
424 if (FAILED(res
)) return res
;
426 /* try again, without the extra 6 */
428 res
=IStream_Write(pStm
,&bytesW
,sizeof(DWORD
),NULL
);
429 if (FAILED(res
)) return res
;
432 res
=IStream_Write(pStm
,&THREE
,sizeof(WORD
),NULL
);
433 if (FAILED(res
)) return res
;
435 /* write W string (no 0) */
436 return IStream_Write(pStm
,filePathW
,bytesW
,NULL
);
439 /******************************************************************************
440 * FileMoniker_GetSizeMax
442 static HRESULT WINAPI
443 FileMonikerImpl_GetSizeMax(IMoniker
* iface
, ULARGE_INTEGER
* pcbSize
)
445 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
447 TRACE("(%p,%p)\n",iface
,pcbSize
);
452 /* We could calculate exactly (see ...::Save()) but instead
453 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
455 pcbSize
->u
.LowPart
= 0x38 + 4 * lstrlenW(This
->filePathName
);
456 pcbSize
->u
.HighPart
= 0;
461 /******************************************************************************
462 * FileMoniker_BindToObject
464 static HRESULT WINAPI
465 FileMonikerImpl_BindToObject(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
466 REFIID riid
, VOID
** ppvResult
)
468 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
472 IRunningObjectTable
*prot
=0;
474 IClassFactory
*pcf
=0;
475 IClassActivator
*pca
=0;
479 TRACE("(%p,%p,%p,%s,%p)\n",iface
,pbc
,pmkToLeft
,debugstr_guid(riid
),ppvResult
);
483 res
=IBindCtx_GetRunningObjectTable(pbc
,&prot
);
486 /* if the requested class was loaded before ! we don't need to reload it */
487 res
= IRunningObjectTable_GetObject(prot
,iface
,&pObj
);
490 /* first activation of this class */
491 res
=GetClassFile(This
->filePathName
,&clsID
);
494 res
=CoCreateInstance(&clsID
,NULL
,CLSCTX_SERVER
,&IID_IPersistFile
,(void**)&ppf
);
497 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
501 IUnknown_AddRef(pObj
);
509 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassFactory
,(void**)&pcf
);
511 if (res
==E_NOINTERFACE
){
513 res
=IMoniker_BindToObject(pmkToLeft
,pbc
,NULL
,&IID_IClassActivator
,(void**)&pca
);
515 if (res
==E_NOINTERFACE
)
516 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED
;
520 IClassFactory_CreateInstance(pcf
,NULL
,&IID_IPersistFile
,(void**)&ppf
);
522 res
=IPersistFile_Load(ppf
,This
->filePathName
,STGM_READ
);
527 IUnknown_AddRef(pObj
);
534 /*res=GetClassFile(This->filePathName,&clsID);
538 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
543 IUnknown_AddRef(pObj);
550 /* get the requested interface from the loaded class */
551 res
= IUnknown_QueryInterface(pObj
,riid
,ppvResult
);
553 IBindCtx_RegisterObjectBound(pbc
,*ppvResult
);
555 IUnknown_Release(pObj
);
559 IRunningObjectTable_Release(prot
);
562 IPersistFile_Release(ppf
);
565 IClassActivator_Release(pca
);
568 IClassFactory_Release(pcf
);
573 /******************************************************************************
574 * FileMoniker_BindToStorage
576 static HRESULT WINAPI
577 FileMonikerImpl_BindToStorage(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
578 REFIID riid
, void **object
)
580 FileMonikerImpl
*moniker
= impl_from_IMoniker(iface
);
584 TRACE("(%p,%p,%p,%s,%p)\n", iface
, pbc
, pmkToLeft
, debugstr_guid(riid
), object
);
589 bind_opts
.cbStruct
= sizeof(bind_opts
);
590 hr
= IBindCtx_GetBindOptions(pbc
, &bind_opts
);
596 if (IsEqualIID(&IID_IStorage
, riid
))
598 return StgOpenStorage(moniker
->filePathName
, NULL
, bind_opts
.grfMode
, NULL
, 0, (IStorage
**)object
);
600 else if ((IsEqualIID(&IID_IStream
, riid
)) || (IsEqualIID(&IID_ILockBytes
, riid
)))
603 return E_NOINTERFACE
;
606 FIXME("(%p,%p,%p,%s,%p)\n", iface
, pbc
, pmkToLeft
, debugstr_guid(riid
), object
);
611 static HRESULT WINAPI
FileMonikerImpl_Reduce(IMoniker
*iface
, IBindCtx
*pbc
, DWORD howfar
,
612 IMoniker
**toleft
, IMoniker
**reduced
)
614 TRACE("%p, %p, %d, %p, %p.\n", iface
, pbc
, howfar
, toleft
, reduced
);
616 if (!pbc
|| !reduced
)
619 IMoniker_AddRef(iface
);
622 return MK_S_REDUCED_TO_SELF
;
625 static void free_stringtable(LPOLESTR
*stringTable
)
629 for (i
=0; stringTable
[i
]!=NULL
; i
++)
630 CoTaskMemFree(stringTable
[i
]);
631 CoTaskMemFree(stringTable
);
634 static int FileMonikerImpl_DecomposePath(LPCOLESTR str
, LPOLESTR
** stringTable
)
637 int i
=0,j
,tabIndex
=0, ret
=0;
638 LPOLESTR
*strgtable
;
640 int len
=lstrlenW(str
);
642 TRACE("%s, %p\n", debugstr_w(str
), *stringTable
);
644 strgtable
= CoTaskMemAlloc((len
+ 1)*sizeof(*strgtable
));
647 return E_OUTOFMEMORY
;
649 word
= CoTaskMemAlloc((len
+ 1)*sizeof(WCHAR
));
662 strgtable
[tabIndex
]=CoTaskMemAlloc(2*sizeof(WCHAR
));
664 if (strgtable
[tabIndex
]==NULL
)
670 lstrcpyW(strgtable
[tabIndex
++], L
"\\");
677 for (j
= 0; str
[i
] && str
[i
] != L
'\\'; i
++, j
++)
682 strgtable
[tabIndex
]=CoTaskMemAlloc(sizeof(WCHAR
)*(j
+1));
684 if (strgtable
[tabIndex
]==NULL
)
690 lstrcpyW(strgtable
[tabIndex
++],word
);
693 strgtable
[tabIndex
]=NULL
;
695 *stringTable
=strgtable
;
702 for (i
= 0; i
< tabIndex
; i
++)
703 CoTaskMemFree(strgtable
[i
]);
705 CoTaskMemFree(strgtable
);
713 /******************************************************************************
714 * FileMoniker_ComposeWith
716 static HRESULT WINAPI
717 FileMonikerImpl_ComposeWith(IMoniker
* iface
, IMoniker
* pmkRight
,
718 BOOL fOnlyIfNotGeneric
, IMoniker
** ppmkComposite
)
721 LPOLESTR str1
=0,str2
=0,*strDec1
=0,*strDec2
=0,newStr
=0;
723 int i
=0,j
=0,lastIdx1
=0,lastIdx2
=0;
726 TRACE("(%p,%p,%d,%p)\n",iface
,pmkRight
,fOnlyIfNotGeneric
,ppmkComposite
);
728 if (ppmkComposite
==NULL
)
736 IMoniker_IsSystemMoniker(pmkRight
,&mkSys
);
738 /* check if we have two FileMonikers to compose or not */
739 if(mkSys
==MKSYS_FILEMONIKER
){
741 CreateBindCtx(0,&bind
);
743 IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
744 IMoniker_GetDisplayName(pmkRight
,bind
,NULL
,&str2
);
746 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
747 lastIdx1
=FileMonikerImpl_DecomposePath(str1
,&strDec1
)-1;
748 lastIdx2
=FileMonikerImpl_DecomposePath(str2
,&strDec2
)-1;
750 if ((lastIdx1
== -1 && lastIdx2
> -1) || (lastIdx1
== 1 && !wcscmp(strDec1
[0], L
"..")))
753 if (!wcscmp(strDec1
[lastIdx1
], L
"\\"))
756 /* for each "..\" in the left of str2 remove the right element from str1 */
757 for (i
= 0; lastIdx1
>= 0 && strDec2
[i
] && !wcscmp(strDec2
[i
], L
".."); i
+= 2)
760 /* the length of the composed path string is increased by the sum of the two paths' lengths */
761 newStr
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(lstrlenW(str1
)+lstrlenW(str2
)+1));
764 /* new path is the concatenation of the rest of str1 and str2 */
765 for(*newStr
=0,j
=0;j
<=lastIdx1
;j
++)
766 lstrcatW(newStr
,strDec1
[j
]);
768 if ((!strDec2
[i
] && lastIdx1
> -1 && lastIdx2
> -1) || wcscmp(strDec2
[i
], L
"\\"))
769 lstrcatW(newStr
, L
"\\");
771 for(j
=i
;j
<=lastIdx2
;j
++)
772 lstrcatW(newStr
,strDec2
[j
]);
774 /* create a new moniker with the new string */
775 res
=CreateFileMoniker(newStr
,ppmkComposite
);
777 /* free string memory used by this function */
778 HeapFree(GetProcessHeap(),0,newStr
);
780 else res
= E_OUTOFMEMORY
;
783 free_stringtable(strDec1
);
784 free_stringtable(strDec2
);
791 else if (is_anti_moniker(pmkRight
, &order
))
793 return order
> 1 ? create_anti_moniker(order
- 1, ppmkComposite
) : S_OK
;
795 else if (fOnlyIfNotGeneric
){
798 return MK_E_NEEDGENERIC
;
802 return CreateGenericComposite(iface
,pmkRight
,ppmkComposite
);
805 /******************************************************************************
808 static HRESULT WINAPI
809 FileMonikerImpl_Enum(IMoniker
* iface
,BOOL fForward
, IEnumMoniker
** ppenumMoniker
)
811 TRACE("(%p,%d,%p)\n",iface
,fForward
,ppenumMoniker
);
813 if (ppenumMoniker
== NULL
)
816 *ppenumMoniker
= NULL
;
821 static HRESULT WINAPI
FileMonikerImpl_IsEqual(IMoniker
*iface
, IMoniker
*other
)
823 FileMonikerImpl
*moniker
= impl_from_IMoniker(iface
), *other_moniker
;
825 TRACE("%p, %p.\n", iface
, other
);
830 other_moniker
= unsafe_impl_from_IMoniker(other
);
834 return !wcsicmp(moniker
->filePathName
, other_moniker
->filePathName
) ? S_OK
: S_FALSE
;
837 /******************************************************************************
840 static HRESULT WINAPI
841 FileMonikerImpl_Hash(IMoniker
* iface
,DWORD
* pdwHash
)
843 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
844 int h
= 0,i
,skip
,len
;
851 val
= This
->filePathName
;
855 for (i
= len
; i
> 0; i
--) {
856 h
= (h
* 37) + val
[off
++];
859 /* only sample some characters */
861 for (i
= len
; i
> 0; i
-= skip
, off
+= skip
) {
862 h
= (h
* 39) + val
[off
];
871 /******************************************************************************
872 * FileMoniker_IsRunning
874 static HRESULT WINAPI
875 FileMonikerImpl_IsRunning(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
876 IMoniker
* pmkNewlyRunning
)
878 IRunningObjectTable
* rot
;
881 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pmkNewlyRunning
);
883 if ( (pmkNewlyRunning
!=NULL
) && (IMoniker_IsEqual(pmkNewlyRunning
,iface
)==S_OK
) )
889 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
894 res
= IRunningObjectTable_IsRunning(rot
,iface
);
896 IRunningObjectTable_Release(rot
);
901 /******************************************************************************
902 * FileMoniker_GetTimeOfLastChange
903 ******************************************************************************/
904 static HRESULT WINAPI
905 FileMonikerImpl_GetTimeOfLastChange(IMoniker
* iface
, IBindCtx
* pbc
,
906 IMoniker
* pmkToLeft
, FILETIME
* pFileTime
)
908 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
909 IRunningObjectTable
* rot
;
911 WIN32_FILE_ATTRIBUTE_DATA info
;
913 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,pFileTime
);
921 res
=IBindCtx_GetRunningObjectTable(pbc
,&rot
);
926 res
= IRunningObjectTable_GetTimeOfLastChange(rot
,iface
,pFileTime
);
928 if (FAILED(res
)){ /* the moniker is not registered */
930 if (!GetFileAttributesExW(This
->filePathName
,GetFileExInfoStandard
,&info
))
931 return MK_E_NOOBJECT
;
933 *pFileTime
=info
.ftLastWriteTime
;
939 /******************************************************************************
940 * FileMoniker_Inverse
942 static HRESULT WINAPI
943 FileMonikerImpl_Inverse(IMoniker
* iface
,IMoniker
** ppmk
)
945 TRACE("(%p,%p)\n",iface
,ppmk
);
947 return CreateAntiMoniker(ppmk
);
950 /******************************************************************************
951 * FileMoniker_CommonPrefixWith
953 static HRESULT WINAPI
954 FileMonikerImpl_CommonPrefixWith(IMoniker
* iface
,IMoniker
* pmkOther
,IMoniker
** ppmkPrefix
)
957 LPOLESTR pathThis
= NULL
, pathOther
= NULL
, *stringTable1
= NULL
;
958 LPOLESTR
*stringTable2
= NULL
, commonPath
= NULL
;
961 ULONG nb1
,nb2
,i
,sameIdx
;
962 BOOL machineNameCase
= FALSE
;
965 if (ppmkPrefix
==NULL
)
973 /* check if we have the same type of moniker */
974 IMoniker_IsSystemMoniker(pmkOther
,&mkSys
);
975 if (mkSys
!= MKSYS_FILEMONIKER
)
976 return MonikerCommonPrefixWith(iface
, pmkOther
, ppmkPrefix
);
978 ret
= CreateBindCtx(0, &bindctx
);
982 /* create a string based on common part of the two paths */
983 ret
= IMoniker_GetDisplayName(iface
, bindctx
, NULL
, &pathThis
);
987 ret
= IMoniker_GetDisplayName(pmkOther
, bindctx
, NULL
, &pathOther
);
991 nb1
= FileMonikerImpl_DecomposePath(pathThis
, &stringTable1
);
997 nb2
= FileMonikerImpl_DecomposePath(pathOther
, &stringTable2
);
1003 if (nb1
== 0 || nb2
== 0) {
1004 ret
= MK_E_NOPREFIX
;
1008 commonPath
= CoTaskMemAlloc(sizeof(WCHAR
)*(min(lstrlenW(pathThis
),lstrlenW(pathOther
))+1));
1010 ret
= E_OUTOFMEMORY
;
1015 for(sameIdx
=0; ( (stringTable1
[sameIdx
]!=NULL
) &&
1016 (stringTable2
[sameIdx
]!=NULL
) &&
1017 (lstrcmpiW(stringTable1
[sameIdx
],stringTable2
[sameIdx
])==0)); sameIdx
++);
1019 if (sameIdx
> 1 && *stringTable1
[0]=='\\' && *stringTable2
[1]=='\\'){
1020 machineNameCase
= TRUE
;
1022 for(i
=2;i
<sameIdx
;i
++)
1023 if( (*stringTable1
[i
]=='\\') && (i
+1 < sameIdx
) && (*stringTable1
[i
+1]=='\\') ){
1024 machineNameCase
= FALSE
;
1029 if (machineNameCase
&& *stringTable1
[sameIdx
-1]=='\\')
1032 if (machineNameCase
&& (sameIdx
<=3) && (nb1
> 3 || nb2
> 3) )
1033 ret
= MK_E_NOPREFIX
;
1036 for (i
= 0; i
< sameIdx
; i
++)
1037 lstrcatW(commonPath
,stringTable1
[i
]);
1038 ret
= CreateFileMoniker(commonPath
, ppmkPrefix
);
1042 IBindCtx_Release(bindctx
);
1043 CoTaskMemFree(pathThis
);
1044 CoTaskMemFree(pathOther
);
1045 CoTaskMemFree(commonPath
);
1046 if (stringTable1
) free_stringtable(stringTable1
);
1047 if (stringTable2
) free_stringtable(stringTable2
);
1052 /******************************************************************************
1053 * FileMoniker_RelativePathTo
1055 static HRESULT WINAPI
1056 FileMonikerImpl_RelativePathTo(IMoniker
* iface
,IMoniker
* pmOther
, IMoniker
** ppmkRelPath
)
1060 LPOLESTR str1
=0,str2
=0,*tabStr1
=0,*tabStr2
=0,relPath
=0;
1061 DWORD len1
=0,len2
=0,sameIdx
=0,j
=0;
1063 TRACE("(%p,%p,%p)\n",iface
,pmOther
,ppmkRelPath
);
1065 if (ppmkRelPath
==NULL
)
1069 return E_INVALIDARG
;
1071 res
=CreateBindCtx(0,&bind
);
1075 res
=IMoniker_GetDisplayName(iface
,bind
,NULL
,&str1
);
1078 res
=IMoniker_GetDisplayName(pmOther
,bind
,NULL
,&str2
);
1082 len1
=FileMonikerImpl_DecomposePath(str1
,&tabStr1
);
1084 return E_OUTOFMEMORY
;
1085 len2
=FileMonikerImpl_DecomposePath(str2
,&tabStr2
);
1089 free_stringtable(tabStr1
);
1090 return E_OUTOFMEMORY
;
1093 /* count the number of similar items from the begin of the two paths */
1094 for(sameIdx
=0; ( (tabStr1
[sameIdx
]!=NULL
) &&
1095 (tabStr2
[sameIdx
]!=NULL
) &&
1096 (lstrcmpiW(tabStr1
[sameIdx
],tabStr2
[sameIdx
])==0)); sameIdx
++);
1098 /* begin the construction of relativePath */
1099 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1100 /* by "..\\" in the begin */
1101 relPath
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(1+lstrlenW(str1
)+lstrlenW(str2
)));
1105 if (len2
>0 && !(len1
==1 && len2
==1 && sameIdx
==0))
1106 for(j
=sameIdx
;(tabStr1
[j
] != NULL
); j
++)
1107 if (*tabStr1
[j
]!='\\')
1108 lstrcatW(relPath
, L
"..\\");
1110 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1111 for(j
=sameIdx
;tabStr2
[j
]!=NULL
;j
++)
1112 lstrcatW(relPath
,tabStr2
[j
]);
1114 res
=CreateFileMoniker(relPath
,ppmkRelPath
);
1116 free_stringtable(tabStr1
);
1117 free_stringtable(tabStr2
);
1118 CoTaskMemFree(str1
);
1119 CoTaskMemFree(str2
);
1120 HeapFree(GetProcessHeap(),0,relPath
);
1122 if (len1
==0 || len2
==0 || (len1
==1 && len2
==1 && sameIdx
==0))
1128 /******************************************************************************
1129 * FileMoniker_GetDisplayName
1131 static HRESULT WINAPI
1132 FileMonikerImpl_GetDisplayName(IMoniker
* iface
, IBindCtx
* pbc
,
1133 IMoniker
* pmkToLeft
, LPOLESTR
*ppszDisplayName
)
1135 FileMonikerImpl
*This
= impl_from_IMoniker(iface
);
1136 int len
=lstrlenW(This
->filePathName
);
1138 TRACE("(%p,%p,%p,%p)\n",iface
,pbc
,pmkToLeft
,ppszDisplayName
);
1140 if (ppszDisplayName
==NULL
)
1143 if (pmkToLeft
!=NULL
)
1144 return E_INVALIDARG
;
1146 *ppszDisplayName
=CoTaskMemAlloc(sizeof(WCHAR
)*(len
+1));
1147 if (*ppszDisplayName
==NULL
)
1148 return E_OUTOFMEMORY
;
1150 lstrcpyW(*ppszDisplayName
,This
->filePathName
);
1152 TRACE("-- %s\n", debugstr_w(*ppszDisplayName
));
1157 /******************************************************************************
1158 * FileMoniker_ParseDisplayName
1160 static HRESULT WINAPI
1161 FileMonikerImpl_ParseDisplayName(IMoniker
* iface
, IBindCtx
* pbc
, IMoniker
* pmkToLeft
,
1162 LPOLESTR pszDisplayName
, ULONG
* pchEaten
, IMoniker
** ppmkOut
)
1164 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface
,pbc
,pmkToLeft
,pszDisplayName
,pchEaten
,ppmkOut
);
1168 /******************************************************************************
1169 * FileMoniker_IsSystemMoniker
1171 static HRESULT WINAPI
1172 FileMonikerImpl_IsSystemMoniker(IMoniker
* iface
,DWORD
* pwdMksys
)
1174 TRACE("(%p,%p)\n",iface
,pwdMksys
);
1179 (*pwdMksys
)=MKSYS_FILEMONIKER
;
1184 /*******************************************************************************
1185 * FileMonikerIROTData_QueryInterface
1187 static HRESULT WINAPI
1188 FileMonikerROTDataImpl_QueryInterface(IROTData
*iface
,REFIID riid
,VOID
** ppvObject
)
1191 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1193 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
1195 return FileMonikerImpl_QueryInterface(&This
->IMoniker_iface
, riid
, ppvObject
);
1198 /***********************************************************************
1199 * FileMonikerIROTData_AddRef
1202 FileMonikerROTDataImpl_AddRef(IROTData
*iface
)
1204 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1206 TRACE("(%p)\n",This
);
1208 return IMoniker_AddRef(&This
->IMoniker_iface
);
1211 /***********************************************************************
1212 * FileMonikerIROTData_Release
1215 FileMonikerROTDataImpl_Release(IROTData
* iface
)
1217 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1219 TRACE("(%p)\n",This
);
1221 return FileMonikerImpl_Release(&This
->IMoniker_iface
);
1224 /******************************************************************************
1225 * FileMonikerIROTData_GetComparisonData
1227 static HRESULT WINAPI
1228 FileMonikerROTDataImpl_GetComparisonData(IROTData
* iface
, BYTE
* pbData
,
1229 ULONG cbMax
, ULONG
* pcbData
)
1231 FileMonikerImpl
*This
= impl_from_IROTData(iface
);
1232 int len
= lstrlenW(This
->filePathName
)+1;
1236 TRACE("(%p, %u, %p)\n", pbData
, cbMax
, pcbData
);
1238 *pcbData
= sizeof(CLSID
) + len
* sizeof(WCHAR
);
1239 if (cbMax
< *pcbData
)
1240 return E_OUTOFMEMORY
;
1242 memcpy(pbData
, &CLSID_FileMoniker
, sizeof(CLSID
));
1243 pszFileName
= (LPWSTR
)(pbData
+sizeof(CLSID
));
1244 for (i
= 0; i
< len
; i
++)
1245 pszFileName
[i
] = towupper(This
->filePathName
[i
]);
1251 * Virtual function table for the FileMonikerImpl class which include IPersist,
1252 * IPersistStream and IMoniker functions.
1254 static const IMonikerVtbl VT_FileMonikerImpl
=
1256 FileMonikerImpl_QueryInterface
,
1257 FileMonikerImpl_AddRef
,
1258 FileMonikerImpl_Release
,
1259 FileMonikerImpl_GetClassID
,
1260 FileMonikerImpl_IsDirty
,
1261 FileMonikerImpl_Load
,
1262 FileMonikerImpl_Save
,
1263 FileMonikerImpl_GetSizeMax
,
1264 FileMonikerImpl_BindToObject
,
1265 FileMonikerImpl_BindToStorage
,
1266 FileMonikerImpl_Reduce
,
1267 FileMonikerImpl_ComposeWith
,
1268 FileMonikerImpl_Enum
,
1269 FileMonikerImpl_IsEqual
,
1270 FileMonikerImpl_Hash
,
1271 FileMonikerImpl_IsRunning
,
1272 FileMonikerImpl_GetTimeOfLastChange
,
1273 FileMonikerImpl_Inverse
,
1274 FileMonikerImpl_CommonPrefixWith
,
1275 FileMonikerImpl_RelativePathTo
,
1276 FileMonikerImpl_GetDisplayName
,
1277 FileMonikerImpl_ParseDisplayName
,
1278 FileMonikerImpl_IsSystemMoniker
1281 /* Virtual function table for the IROTData class. */
1282 static const IROTDataVtbl VT_ROTDataImpl
=
1284 FileMonikerROTDataImpl_QueryInterface
,
1285 FileMonikerROTDataImpl_AddRef
,
1286 FileMonikerROTDataImpl_Release
,
1287 FileMonikerROTDataImpl_GetComparisonData
1290 /******************************************************************************
1291 * FileMoniker_Construct (local function)
1293 static HRESULT
FileMonikerImpl_Construct(FileMonikerImpl
* This
, LPCOLESTR lpszPathName
)
1296 int sizeStr
=lstrlenW(lpszPathName
);
1300 TRACE("(%p,%s)\n",This
,debugstr_w(lpszPathName
));
1302 /* Initialize the virtual function table. */
1303 This
->IMoniker_iface
.lpVtbl
= &VT_FileMonikerImpl
;
1304 This
->IROTData_iface
.lpVtbl
= &VT_ROTDataImpl
;
1306 This
->pMarshal
= NULL
;
1308 This
->filePathName
=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
)*(sizeStr
+1));
1310 if (This
->filePathName
==NULL
)
1311 return E_OUTOFMEMORY
;
1313 lstrcpyW(This
->filePathName
,lpszPathName
);
1315 nb
=FileMonikerImpl_DecomposePath(This
->filePathName
,&tabStr
);
1320 if (wcscmp(tabStr
[0], L
".."))
1325 if (wcscmp(tabStr
[i
], L
"..") && wcscmp(tabStr
[i
], L
"\\"))
1332 if (!wcscmp(tabStr
[i
], L
"\\") && i
< nb
- 1 && !wcscmp(tabStr
[i
+1], L
"\\"))
1341 if (!wcscmp(tabStr
[nb
-1], L
"\\"))
1344 This
->filePathName
=HeapReAlloc(GetProcessHeap(),0,This
->filePathName
,(sizeStr
+1)*sizeof(WCHAR
));
1346 *This
->filePathName
=0;
1348 for(i
=0;tabStr
[i
]!=NULL
;i
++)
1349 lstrcatW(This
->filePathName
,tabStr
[i
]);
1352 lstrcatW(This
->filePathName
, L
"\\");
1355 free_stringtable(tabStr
);
1360 /******************************************************************************
1361 * CreateFileMoniker (OLE32.@)
1362 ******************************************************************************/
1363 HRESULT WINAPI
CreateFileMoniker(LPCOLESTR lpszPathName
, IMoniker
**ppmk
)
1365 FileMonikerImpl
* newFileMoniker
;
1368 TRACE("(%s,%p)\n",debugstr_w(lpszPathName
),ppmk
);
1378 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1380 if (!newFileMoniker
)
1381 return E_OUTOFMEMORY
;
1383 hr
= FileMonikerImpl_Construct(newFileMoniker
,lpszPathName
);
1386 hr
= IMoniker_QueryInterface(&newFileMoniker
->IMoniker_iface
,&IID_IMoniker
,(void**)ppmk
);
1388 HeapFree(GetProcessHeap(),0,newFileMoniker
);
1393 /* find a character from a set in reverse without the string having to be null-terminated */
1394 static inline WCHAR
*memrpbrkW(const WCHAR
*ptr
, size_t n
, const WCHAR
*accept
)
1396 const WCHAR
*end
, *ret
= NULL
;
1397 for (end
= ptr
+ n
; ptr
< end
; ptr
++) if (wcschr(accept
, *ptr
)) ret
= ptr
;
1398 return (WCHAR
*)ret
;
1401 HRESULT
FileMoniker_CreateFromDisplayName(LPBC pbc
, LPCOLESTR szDisplayName
,
1402 LPDWORD pchEaten
, IMoniker
**ppmk
)
1406 for (end
= szDisplayName
+ lstrlenW(szDisplayName
);
1407 end
&& (end
!= szDisplayName
);
1408 end
= memrpbrkW(szDisplayName
, end
- szDisplayName
, L
":\\/!"))
1411 IRunningObjectTable
*rot
;
1412 IMoniker
*file_moniker
;
1413 LPWSTR file_display_name
;
1414 LPWSTR full_path_name
;
1415 DWORD full_path_name_len
;
1416 int len
= end
- szDisplayName
;
1418 file_display_name
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1419 if (!file_display_name
) return E_OUTOFMEMORY
;
1420 memcpy(file_display_name
, szDisplayName
, len
* sizeof(WCHAR
));
1421 file_display_name
[len
] = '\0';
1423 hr
= CreateFileMoniker(file_display_name
, &file_moniker
);
1426 HeapFree(GetProcessHeap(), 0, file_display_name
);
1430 hr
= IBindCtx_GetRunningObjectTable(pbc
, &rot
);
1433 HeapFree(GetProcessHeap(), 0, file_display_name
);
1434 IMoniker_Release(file_moniker
);
1438 hr
= IRunningObjectTable_IsRunning(rot
, file_moniker
);
1439 IRunningObjectTable_Release(rot
);
1442 HeapFree(GetProcessHeap(), 0, file_display_name
);
1443 IMoniker_Release(file_moniker
);
1448 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name
));
1450 *ppmk
= file_moniker
;
1451 HeapFree(GetProcessHeap(), 0, file_display_name
);
1455 full_path_name_len
= GetFullPathNameW(file_display_name
, 0, NULL
, NULL
);
1456 if (!full_path_name_len
)
1458 HeapFree(GetProcessHeap(), 0, file_display_name
);
1459 IMoniker_Release(file_moniker
);
1462 full_path_name
= HeapAlloc(GetProcessHeap(), 0, full_path_name_len
* sizeof(WCHAR
));
1463 if (!full_path_name
)
1465 HeapFree(GetProcessHeap(), 0, file_display_name
);
1466 IMoniker_Release(file_moniker
);
1467 return E_OUTOFMEMORY
;
1469 GetFullPathNameW(file_display_name
, full_path_name_len
, full_path_name
, NULL
);
1471 if (GetFileAttributesW(full_path_name
) == INVALID_FILE_ATTRIBUTES
)
1472 TRACE("couldn't open file %s\n", debugstr_w(full_path_name
));
1475 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName
));
1477 *ppmk
= file_moniker
;
1478 HeapFree(GetProcessHeap(), 0, file_display_name
);
1479 HeapFree(GetProcessHeap(), 0, full_path_name
);
1482 HeapFree(GetProcessHeap(), 0, file_display_name
);
1483 HeapFree(GetProcessHeap(), 0, full_path_name
);
1484 IMoniker_Release(file_moniker
);
1487 return MK_E_CANTOPENFILE
;
1491 HRESULT WINAPI
FileMoniker_CreateInstance(IClassFactory
*iface
, IUnknown
*pUnk
, REFIID riid
, void **ppv
)
1493 FileMonikerImpl
* newFileMoniker
;
1496 TRACE("(%p, %s, %p)\n", pUnk
, debugstr_guid(riid
), ppv
);
1501 return CLASS_E_NOAGGREGATION
;
1503 newFileMoniker
= HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl
));
1504 if (!newFileMoniker
)
1505 return E_OUTOFMEMORY
;
1507 hr
= FileMonikerImpl_Construct(newFileMoniker
, L
"");
1510 hr
= IMoniker_QueryInterface(&newFileMoniker
->IMoniker_iface
, riid
, ppv
);
1512 HeapFree(GetProcessHeap(),0,newFileMoniker
);