d3d9/tests: Move a Present out of the loops in fog_with_shader_test().
[wine/multimedia.git] / dlls / ole32 / filemoniker.c
blobc7bbb6641ccff3514508570a8ed4f1b1512fcd9a
1 /*
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
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <string.h>
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winnls.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "objbase.h"
37 #include "moniker.h"
39 #include "compobj_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* filemoniker data structure */
44 typedef struct FileMonikerImpl{
45 IMoniker IMoniker_iface;
46 IROTData IROTData_iface;
47 LONG ref;
48 LPOLESTR filePathName; /* path string identified by this filemoniker */
49 IUnknown *pMarshal; /* custom marshaler */
50 } FileMonikerImpl;
52 static inline FileMonikerImpl *impl_from_IMoniker(IMoniker *iface)
54 return CONTAINING_RECORD(iface, FileMonikerImpl, IMoniker_iface);
57 static inline FileMonikerImpl *impl_from_IROTData(IROTData *iface)
59 return CONTAINING_RECORD(iface, FileMonikerImpl, IROTData_iface);
62 /* Local function used by filemoniker implementation */
63 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
64 static HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* iface);
66 /*******************************************************************************
67 * FileMoniker_QueryInterface
69 static HRESULT WINAPI
70 FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
72 FileMonikerImpl *This = impl_from_IMoniker(iface);
74 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
76 /* Perform a sanity check on the parameters.*/
77 if ( ppvObject==0 )
78 return E_INVALIDARG;
80 /* Initialize the return parameter */
81 *ppvObject = 0;
83 /* Compare the riid with the interface IDs implemented by this object.*/
84 if (IsEqualIID(&IID_IUnknown, riid) ||
85 IsEqualIID(&IID_IPersist, riid) ||
86 IsEqualIID(&IID_IPersistStream,riid) ||
87 IsEqualIID(&IID_IMoniker, riid)
89 *ppvObject = iface;
91 else if (IsEqualIID(&IID_IROTData, riid))
92 *ppvObject = &This->IROTData_iface;
93 else if (IsEqualIID(&IID_IMarshal, riid))
95 HRESULT hr = S_OK;
96 if (!This->pMarshal)
97 hr = MonikerMarshal_Create(iface, &This->pMarshal);
98 if (hr != S_OK)
99 return hr;
100 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
103 /* Check that we obtained an interface.*/
104 if ((*ppvObject)==0)
105 return E_NOINTERFACE;
107 /* Query Interface always increases the reference count by one when it is successful */
108 IMoniker_AddRef(iface);
110 return S_OK;
113 /******************************************************************************
114 * FileMoniker_AddRef
116 static ULONG WINAPI
117 FileMonikerImpl_AddRef(IMoniker* iface)
119 FileMonikerImpl *This = impl_from_IMoniker(iface);
121 TRACE("(%p)\n",iface);
123 return InterlockedIncrement(&This->ref);
126 /******************************************************************************
127 * FileMoniker_Release
129 static ULONG WINAPI
130 FileMonikerImpl_Release(IMoniker* iface)
132 FileMonikerImpl *This = impl_from_IMoniker(iface);
133 ULONG ref;
135 TRACE("(%p)\n",iface);
137 ref = InterlockedDecrement(&This->ref);
139 /* destroy the object if there are no more references to it */
140 if (ref == 0) FileMonikerImpl_Destroy(This);
142 return ref;
145 /******************************************************************************
146 * FileMoniker_GetClassID
148 static HRESULT WINAPI
149 FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID)
151 TRACE("(%p,%p)\n",iface,pClassID);
153 if (pClassID==NULL)
154 return E_POINTER;
156 *pClassID = CLSID_FileMoniker;
158 return S_OK;
161 /******************************************************************************
162 * FileMoniker_IsDirty
164 * Note that the OLE-provided implementations of the IPersistStream::IsDirty
165 * method in the OLE-provided moniker interfaces always return S_FALSE because
166 * their internal state never changes.
168 static HRESULT WINAPI
169 FileMonikerImpl_IsDirty(IMoniker* iface)
172 TRACE("(%p)\n",iface);
174 return S_FALSE;
177 /******************************************************************************
178 * FileMoniker_Load
180 * this function locates and reads from the stream the filePath string
181 * written by FileMonikerImpl_Save
183 static HRESULT WINAPI
184 FileMonikerImpl_Load(IMoniker* iface, IStream* pStm)
186 FileMonikerImpl *This = impl_from_IMoniker(iface);
187 HRESULT res;
188 CHAR* filePathA = NULL;
189 WCHAR* filePathW = NULL;
190 ULONG bread;
191 WORD wbuffer;
192 DWORD dwbuffer, bytesA, bytesW, len;
193 int i;
196 TRACE("(%p,%p)\n",iface,pStm);
198 /* first WORD */
199 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
200 if (bread!=sizeof(WORD))
202 WARN("Couldn't read 0 word\n");
203 goto fail;
206 /* read filePath string length (plus one) */
207 res=IStream_Read(pStm,&bytesA,sizeof(DWORD),&bread);
208 if (bread != sizeof(DWORD))
210 WARN("Couldn't read file string length\n");
211 goto fail;
214 /* read filePath string */
215 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
216 if (!filePathA)
218 res = E_OUTOFMEMORY;
219 goto fail;
222 res=IStream_Read(pStm,filePathA,bytesA,&bread);
223 if (bread != bytesA)
225 WARN("Couldn't read file path string\n");
226 goto fail;
229 /* read the unknown value */
230 IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
231 if (bread != sizeof(WORD))
233 WARN("Couldn't read unknown value\n");
234 goto fail;
237 /* read the DEAD constant */
238 IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
239 if (bread != sizeof(WORD))
241 WARN("Couldn't read DEAD constant\n");
242 goto fail;
245 for(i=0;i<5;i++)
247 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
248 if (bread!=sizeof(DWORD))
250 WARN("Couldn't read 0 padding\n");
251 goto fail;
255 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
256 if (bread!=sizeof(DWORD))
257 goto fail;
259 if (!dwbuffer) /* No W-string */
261 bytesA--;
262 len=MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, bytesA, NULL, 0);
263 if (!len)
264 goto fail;
266 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
267 if (!filePathW)
269 res = E_OUTOFMEMORY;
270 goto fail;
272 MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, filePathA, -1, filePathW, len+1);
273 goto succeed;
276 if (dwbuffer < 6)
277 goto fail;
279 bytesW=dwbuffer - 6;
281 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
282 if (bread!=sizeof(DWORD) || dwbuffer!=bytesW)
283 goto fail;
285 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
286 if (bread!=sizeof(WORD) || wbuffer!=0x3)
287 goto fail;
289 len=bytesW/sizeof(WCHAR);
290 filePathW=HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
291 if(!filePathW)
293 res = E_OUTOFMEMORY;
294 goto fail;
296 res=IStream_Read(pStm,filePathW,bytesW,&bread);
297 if (bread!=bytesW)
298 goto fail;
300 filePathW[len]=0;
302 succeed:
303 HeapFree(GetProcessHeap(),0,filePathA);
304 HeapFree(GetProcessHeap(),0,This->filePathName);
305 This->filePathName=filePathW;
307 return S_OK;
309 fail:
310 HeapFree(GetProcessHeap(), 0, filePathA);
311 HeapFree(GetProcessHeap(), 0, filePathW);
313 if (SUCCEEDED(res))
314 res = E_FAIL;
315 return res;
318 /******************************************************************************
319 * FileMoniker_Save
321 * This function saves data of this object. In the beginning I thought
322 * that I have just to write the filePath string on Stream. But, when I
323 * tested this function with windows program samples, I noticed that it
324 * was not the case. This implementation is based on XP SP2. Other versions
325 * of Windows have minor variations.
327 * Data which must be written on stream is:
328 * 1) WORD constant: zero (not validated by Windows)
329 * 2) length of the path string ("\0" included)
330 * 3) path string type A
331 * 4) Unknown WORD value: Frequently 0xFFFF, but not always. If set very large,
332 * Windows returns E_OUTOFMEMORY
333 * 5) WORD Constant: 0xDEAD (not validated by Windows)
334 * 6) five DWORD constant: zero (not validated by Windows)
335 * 7) If we're only writing the multibyte version,
336 * write a zero DWORD and finish.
338 * 8) DWORD: double-length of the path string type W ("\0" not
339 * included)
340 * 9) WORD constant: 0x3
341 * 10) filePath unicode string.
344 static HRESULT WINAPI
345 FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
347 FileMonikerImpl *This = impl_from_IMoniker(iface);
348 HRESULT res;
349 LPOLESTR filePathW=This->filePathName;
350 CHAR* filePathA;
351 DWORD bytesA, bytesW, len;
353 static const WORD FFFF = 0xFFFF; /* Constants */
354 static const WORD DEAD = 0xDEAD;
355 static const DWORD ZERO = 0;
356 static const WORD THREE = 0x3;
358 int i;
359 BOOL bUsedDefault, bWriteWide;
361 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
363 if (pStm==NULL)
364 return E_POINTER;
366 /* write a 0 WORD */
367 res=IStream_Write(pStm,&ZERO,sizeof(WORD),NULL);
368 if (FAILED(res)) return res;
370 /* write length of filePath string ( 0 included )*/
371 bytesA = WideCharToMultiByte( CP_ACP, 0, filePathW, -1, NULL, 0, NULL, NULL );
372 res=IStream_Write(pStm,&bytesA,sizeof(DWORD),NULL);
373 if (FAILED(res)) return res;
375 /* write A string (with '\0') */
376 filePathA=HeapAlloc(GetProcessHeap(),0,bytesA);
377 if (!filePathA)
378 return E_OUTOFMEMORY;
379 WideCharToMultiByte( CP_ACP, 0, filePathW, -1, filePathA, bytesA, NULL, &bUsedDefault);
380 res=IStream_Write(pStm,filePathA,bytesA,NULL);
381 HeapFree(GetProcessHeap(),0,filePathA);
382 if (FAILED(res)) return res;
384 /* write a WORD 0xFFFF */
385 res=IStream_Write(pStm,&FFFF,sizeof(WORD),NULL);
386 if (FAILED(res)) return res;
388 /* write a WORD 0xDEAD */
389 res=IStream_Write(pStm,&DEAD,sizeof(WORD),NULL);
390 if (FAILED(res)) return res;
392 /* write 5 zero DWORDs */
393 for(i=0;i<5;i++)
395 res=IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
396 if (FAILED(res)) return res;
399 /* Write the wide version if:
400 * + couldn't convert to CP_ACP,
401 * or + it's a directory,
402 * or + there's a character > 0xFF
404 len = lstrlenW(filePathW);
405 bWriteWide = (bUsedDefault || (len > 0 && filePathW[len-1]=='\\' ));
406 if (!bWriteWide)
408 WCHAR* pch;
409 for(pch=filePathW;*pch;++pch)
411 if (*pch > 0xFF)
413 bWriteWide = TRUE;
414 break;
419 if (!bWriteWide)
420 return IStream_Write(pStm,&ZERO,sizeof(DWORD),NULL);
422 /* write bytes needed for the filepathW (without 0) + 6 */
423 bytesW = len*sizeof(WCHAR) + 6;
424 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
425 if (FAILED(res)) return res;
427 /* try again, without the extra 6 */
428 bytesW -= 6;
429 res=IStream_Write(pStm,&bytesW,sizeof(DWORD),NULL);
430 if (FAILED(res)) return res;
432 /* write a WORD 3 */
433 res=IStream_Write(pStm,&THREE,sizeof(WORD),NULL);
434 if (FAILED(res)) return res;
436 /* write W string (no 0) */
437 return IStream_Write(pStm,filePathW,bytesW,NULL);
440 /******************************************************************************
441 * FileMoniker_GetSizeMax
443 static HRESULT WINAPI
444 FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
446 FileMonikerImpl *This = impl_from_IMoniker(iface);
448 TRACE("(%p,%p)\n",iface,pcbSize);
450 if (!pcbSize)
451 return E_POINTER;
453 /* We could calculate exactly (see ...::Save()) but instead
454 * we'll make a quick over-estimate, like Windows (NT4, XP) does.
456 pcbSize->u.LowPart = 0x38 + 4 * lstrlenW(This->filePathName);
457 pcbSize->u.HighPart = 0;
459 return S_OK;
462 /******************************************************************************
463 * FileMoniker_Destroy (local function)
464 *******************************************************************************/
465 HRESULT FileMonikerImpl_Destroy(FileMonikerImpl* This)
467 TRACE("(%p)\n",This);
469 if (This->pMarshal) IUnknown_Release(This->pMarshal);
470 HeapFree(GetProcessHeap(),0,This->filePathName);
471 HeapFree(GetProcessHeap(),0,This);
473 return S_OK;
476 /******************************************************************************
477 * FileMoniker_BindToObject
479 static HRESULT WINAPI
480 FileMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
481 REFIID riid, VOID** ppvResult)
483 FileMonikerImpl *This = impl_from_IMoniker(iface);
484 HRESULT res=E_FAIL;
485 CLSID clsID;
486 IUnknown* pObj=0;
487 IRunningObjectTable *prot=0;
488 IPersistFile *ppf=0;
489 IClassFactory *pcf=0;
490 IClassActivator *pca=0;
492 *ppvResult=0;
494 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
496 if(pmkToLeft==NULL){
498 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
500 if (SUCCEEDED(res)){
501 /* if the requested class was loaded before ! we don't need to reload it */
502 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
504 if (res==S_FALSE){
505 /* first activation of this class */
506 res=GetClassFile(This->filePathName,&clsID);
507 if (SUCCEEDED(res)){
509 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
510 if (SUCCEEDED(res)){
512 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
513 if (SUCCEEDED(res)){
515 pObj=(IUnknown*)ppf;
516 IUnknown_AddRef(pObj);
523 else{
524 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
526 if (res==E_NOINTERFACE){
528 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
530 if (res==E_NOINTERFACE)
531 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
533 if (pcf!=NULL){
535 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)&ppf);
537 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
539 if (SUCCEEDED(res)){
541 pObj=(IUnknown*)ppf;
542 IUnknown_AddRef(pObj);
545 if (pca!=NULL){
547 FIXME("()\n");
549 /*res=GetClassFile(This->filePathName,&clsID);
551 if (SUCCEEDED(res)){
553 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
555 if (SUCCEEDED(res)){
557 pObj=(IUnknown*)ppf;
558 IUnknown_AddRef(pObj);
564 if (pObj!=NULL){
565 /* get the requested interface from the loaded class */
566 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
568 IBindCtx_RegisterObjectBound(pbc,*ppvResult);
570 IUnknown_Release(pObj);
573 if (prot!=NULL)
574 IRunningObjectTable_Release(prot);
576 if (ppf!=NULL)
577 IPersistFile_Release(ppf);
579 if (pca!=NULL)
580 IClassActivator_Release(pca);
582 if (pcf!=NULL)
583 IClassFactory_Release(pcf);
585 return res;
588 /******************************************************************************
589 * FileMoniker_BindToStorage
591 static HRESULT WINAPI
592 FileMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
593 REFIID riid, VOID** ppvObject)
595 LPOLESTR filePath=0;
596 IStorage *pstg=0;
597 HRESULT res;
599 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
601 if (pmkToLeft==NULL){
603 if (IsEqualIID(&IID_IStorage, riid)){
605 /* get the file name */
606 IMoniker_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
608 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
610 if (SUCCEEDED(res))
611 *ppvObject=pstg;
613 CoTaskMemFree(filePath);
615 else
616 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
617 return E_FAIL;
618 else
619 return E_NOINTERFACE;
621 else {
623 FIXME("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
625 return E_NOTIMPL;
627 return res;
630 /******************************************************************************
631 * FileMoniker_Reduce
632 ******************************************************************************/
633 static HRESULT WINAPI
634 FileMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
635 IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
637 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
639 if (ppmkReduced==NULL)
640 return E_POINTER;
642 IMoniker_AddRef(iface);
644 *ppmkReduced=iface;
646 return MK_S_REDUCED_TO_SELF;
649 static void free_stringtable(LPOLESTR *stringTable)
651 int i;
653 for (i=0; stringTable[i]!=NULL; i++)
654 CoTaskMemFree(stringTable[i]);
655 CoTaskMemFree(stringTable);
658 /******************************************************************************
659 * FileMoniker_ComposeWith
661 static HRESULT WINAPI
662 FileMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
663 BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
665 HRESULT res;
666 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
667 static const WCHAR twoPoint[]={'.','.',0};
668 static const WCHAR bkSlash[]={'\\',0};
669 IBindCtx *bind=0;
670 int i=0,j=0,lastIdx1=0,lastIdx2=0;
671 DWORD mkSys;
673 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
675 if (ppmkComposite==NULL)
676 return E_POINTER;
678 if (pmkRight==NULL)
679 return E_INVALIDARG;
681 *ppmkComposite=0;
683 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
685 /* check if we have two FileMonikers to compose or not */
686 if(mkSys==MKSYS_FILEMONIKER){
688 CreateBindCtx(0,&bind);
690 IMoniker_GetDisplayName(iface,bind,NULL,&str1);
691 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
693 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
694 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
695 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
697 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
698 return MK_E_SYNTAX;
700 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
701 lastIdx1--;
703 /* for etch "..\" in the left of str2 remove the right element from str1 */
704 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
706 lastIdx1-=2;
709 /* the length of the composed path string is raised by the sum of the two paths lengths */
710 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
712 if (newStr)
714 /* new path is the concatenation of the rest of str1 and str2 */
715 for(*newStr=0,j=0;j<=lastIdx1;j++)
716 strcatW(newStr,strDec1[j]);
718 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
719 strcatW(newStr,bkSlash);
721 for(j=i;j<=lastIdx2;j++)
722 strcatW(newStr,strDec2[j]);
724 /* create a new moniker with the new string */
725 res=CreateFileMoniker(newStr,ppmkComposite);
727 /* free all strings space memory used by this function */
728 HeapFree(GetProcessHeap(),0,newStr);
730 else res = E_OUTOFMEMORY;
732 free_stringtable(strDec1);
733 free_stringtable(strDec2);
735 CoTaskMemFree(str1);
736 CoTaskMemFree(str2);
738 return res;
740 else if(mkSys==MKSYS_ANTIMONIKER){
742 *ppmkComposite=NULL;
743 return S_OK;
745 else if (fOnlyIfNotGeneric){
747 *ppmkComposite=NULL;
748 return MK_E_NEEDGENERIC;
750 else
752 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
755 /******************************************************************************
756 * FileMoniker_Enum
758 static HRESULT WINAPI
759 FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
761 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
763 if (ppenumMoniker == NULL)
764 return E_POINTER;
766 *ppenumMoniker = NULL;
768 return S_OK;
771 /******************************************************************************
772 * FileMoniker_IsEqual
774 static HRESULT WINAPI
775 FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
777 FileMonikerImpl *This = impl_from_IMoniker(iface);
778 CLSID clsid;
779 LPOLESTR filePath;
780 IBindCtx* bind;
781 HRESULT res;
783 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
785 if (pmkOtherMoniker==NULL)
786 return S_FALSE;
788 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
790 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
791 return S_FALSE;
793 res = CreateBindCtx(0,&bind);
794 if (FAILED(res)) return res;
796 res = S_FALSE;
797 if (SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath))) {
798 if (!lstrcmpiW(filePath, This->filePathName))
799 res = S_OK;
800 CoTaskMemFree(filePath);
803 IBindCtx_Release(bind);
804 return res;
807 /******************************************************************************
808 * FileMoniker_Hash
810 static HRESULT WINAPI
811 FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
813 FileMonikerImpl *This = impl_from_IMoniker(iface);
814 int h = 0,i,skip,len;
815 int off = 0;
816 LPOLESTR val;
818 if (pdwHash==NULL)
819 return E_POINTER;
821 val = This->filePathName;
822 len = lstrlenW(val);
824 if (len < 16) {
825 for (i = len ; i > 0; i--) {
826 h = (h * 37) + val[off++];
828 } else {
829 /* only sample some characters */
830 skip = len / 8;
831 for (i = len ; i > 0; i -= skip, off += skip) {
832 h = (h * 39) + val[off];
836 *pdwHash=h;
838 return S_OK;
841 /******************************************************************************
842 * FileMoniker_IsRunning
844 static HRESULT WINAPI
845 FileMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
846 IMoniker* pmkNewlyRunning)
848 IRunningObjectTable* rot;
849 HRESULT res;
851 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
853 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
854 return S_OK;
856 if (pbc==NULL)
857 return E_POINTER;
859 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
861 if (FAILED(res))
862 return res;
864 res = IRunningObjectTable_IsRunning(rot,iface);
866 IRunningObjectTable_Release(rot);
868 return res;
871 /******************************************************************************
872 * FileMoniker_GetTimeOfLastChange
873 ******************************************************************************/
874 static HRESULT WINAPI
875 FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc,
876 IMoniker* pmkToLeft, FILETIME* pFileTime)
878 FileMonikerImpl *This = impl_from_IMoniker(iface);
879 IRunningObjectTable* rot;
880 HRESULT res;
881 WIN32_FILE_ATTRIBUTE_DATA info;
883 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
885 if (pFileTime==NULL)
886 return E_POINTER;
888 if (pmkToLeft!=NULL)
889 return E_INVALIDARG;
891 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
893 if (FAILED(res))
894 return res;
896 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
898 if (FAILED(res)){ /* the moniker is not registered */
900 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
901 return MK_E_NOOBJECT;
903 *pFileTime=info.ftLastWriteTime;
906 return S_OK;
909 /******************************************************************************
910 * FileMoniker_Inverse
912 static HRESULT WINAPI
913 FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
915 TRACE("(%p,%p)\n",iface,ppmk);
917 return CreateAntiMoniker(ppmk);
920 /******************************************************************************
921 * FileMoniker_CommonPrefixWith
923 static HRESULT WINAPI
924 FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
927 LPOLESTR pathThis = NULL, pathOther = NULL,*stringTable1,*stringTable2,commonPath = NULL;
928 IBindCtx *bindctx;
929 DWORD mkSys;
930 ULONG nb1,nb2,i,sameIdx;
931 BOOL machineNameCase = FALSE;
932 HRESULT ret;
934 if (ppmkPrefix==NULL)
935 return E_POINTER;
937 if (pmkOther==NULL)
938 return E_INVALIDARG;
940 *ppmkPrefix=0;
942 /* check if we have the same type of moniker */
943 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
944 if (mkSys != MKSYS_FILEMONIKER)
945 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
947 ret = CreateBindCtx(0, &bindctx);
948 if (FAILED(ret))
949 return ret;
951 /* create a string based on common part of the two paths */
952 ret = IMoniker_GetDisplayName(iface, bindctx, NULL, &pathThis);
953 if (FAILED(ret))
954 goto failed;
956 ret = IMoniker_GetDisplayName(pmkOther, bindctx, NULL, &pathOther);
957 if (FAILED(ret))
958 goto failed;
960 nb1 = FileMonikerImpl_DecomposePath(pathThis, &stringTable1);
961 if (FAILED(nb1)) {
962 ret = nb1;
963 goto failed;
966 nb2 = FileMonikerImpl_DecomposePath(pathOther, &stringTable2);
967 if (FAILED(nb2)) {
968 ret = nb2;
969 goto failed;
972 if (nb1 == 0 || nb2 == 0) {
973 ret = MK_E_NOPREFIX;
974 goto failed;
977 commonPath = CoTaskMemAlloc(sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
978 if (!commonPath) {
979 ret = E_OUTOFMEMORY;
980 goto failed;
983 *commonPath = 0;
984 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
985 (stringTable2[sameIdx]!=NULL) &&
986 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
988 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
989 machineNameCase = TRUE;
991 for(i=2;i<sameIdx;i++)
992 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
993 machineNameCase = FALSE;
994 break;
998 if (machineNameCase && *stringTable1[sameIdx-1]=='\\')
999 sameIdx--;
1001 if (machineNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1002 ret = MK_E_NOPREFIX;
1003 else
1005 for (i = 0; i < sameIdx; i++)
1006 strcatW(commonPath,stringTable1[i]);
1007 ret = CreateFileMoniker(commonPath, ppmkPrefix);
1010 failed:
1011 IBindCtx_Release(bindctx);
1012 CoTaskMemFree(pathThis);
1013 CoTaskMemFree(pathOther);
1014 CoTaskMemFree(commonPath);
1015 free_stringtable(stringTable1);
1016 free_stringtable(stringTable2);
1018 return ret;
1021 /******************************************************************************
1022 * DecomposePath (local function)
1024 int FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable)
1026 static const WCHAR bSlash[] = {'\\',0};
1027 LPOLESTR word;
1028 int i=0,j,tabIndex=0, ret=0;
1029 LPOLESTR *strgtable ;
1031 int len=lstrlenW(str);
1033 TRACE("%s, %p\n", debugstr_w(str), *stringTable);
1035 strgtable = CoTaskMemAlloc((len + 1)*sizeof(*strgtable));
1037 if (strgtable==NULL)
1038 return E_OUTOFMEMORY;
1040 word = CoTaskMemAlloc((len + 1)*sizeof(WCHAR));
1042 if (word==NULL)
1044 ret = E_OUTOFMEMORY;
1045 goto lend;
1048 while(str[i]!=0){
1050 if(str[i]==bSlash[0]){
1052 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1054 if (strgtable[tabIndex]==NULL)
1056 ret = E_OUTOFMEMORY;
1057 goto lend;
1060 strcpyW(strgtable[tabIndex++],bSlash);
1062 i++;
1065 else {
1067 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1068 word[j]=str[i];
1070 word[j]=0;
1072 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1074 if (strgtable[tabIndex]==NULL)
1076 ret = E_OUTOFMEMORY;
1077 goto lend;
1080 strcpyW(strgtable[tabIndex++],word);
1083 strgtable[tabIndex]=NULL;
1085 *stringTable=strgtable;
1087 ret = tabIndex;
1089 lend:
1090 if (ret < 0)
1092 for (i = 0; i < tabIndex; i++)
1093 CoTaskMemFree(strgtable[i]);
1095 CoTaskMemFree(strgtable);
1098 CoTaskMemFree(word);
1100 return ret;
1103 /******************************************************************************
1104 * FileMoniker_RelativePathTo
1106 static HRESULT WINAPI
1107 FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1109 IBindCtx *bind;
1110 HRESULT res;
1111 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1112 DWORD len1=0,len2=0,sameIdx=0,j=0;
1113 static const WCHAR back[] ={'.','.','\\',0};
1115 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1117 if (ppmkRelPath==NULL)
1118 return E_POINTER;
1120 if (pmOther==NULL)
1121 return E_INVALIDARG;
1123 res=CreateBindCtx(0,&bind);
1124 if (FAILED(res))
1125 return res;
1127 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1128 if (FAILED(res))
1129 return res;
1130 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1131 if (FAILED(res))
1132 return res;
1134 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1135 if (FAILED(len1))
1136 return E_OUTOFMEMORY;
1137 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1139 if (FAILED(len2))
1141 free_stringtable(tabStr1);
1142 return E_OUTOFMEMORY;
1145 /* count the number of similar items from the begin of the two paths */
1146 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1147 (tabStr2[sameIdx]!=NULL) &&
1148 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1150 /* begin the construction of relativePath */
1151 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1152 /* by "..\\" in the begin */
1153 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1155 *relPath=0;
1157 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1158 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1159 if (*tabStr1[j]!='\\')
1160 strcatW(relPath,back);
1162 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1163 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1164 strcatW(relPath,tabStr2[j]);
1166 res=CreateFileMoniker(relPath,ppmkRelPath);
1168 free_stringtable(tabStr1);
1169 free_stringtable(tabStr2);
1170 CoTaskMemFree(str1);
1171 CoTaskMemFree(str2);
1172 HeapFree(GetProcessHeap(),0,relPath);
1174 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1175 return MK_S_HIM;
1177 return res;
1180 /******************************************************************************
1181 * FileMoniker_GetDisplayName
1183 static HRESULT WINAPI
1184 FileMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
1185 IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
1187 FileMonikerImpl *This = impl_from_IMoniker(iface);
1188 int len=lstrlenW(This->filePathName);
1190 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1192 if (ppszDisplayName==NULL)
1193 return E_POINTER;
1195 if (pmkToLeft!=NULL)
1196 return E_INVALIDARG;
1198 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1199 if (*ppszDisplayName==NULL)
1200 return E_OUTOFMEMORY;
1202 strcpyW(*ppszDisplayName,This->filePathName);
1204 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
1206 return S_OK;
1209 /******************************************************************************
1210 * FileMoniker_ParseDisplayName
1212 static HRESULT WINAPI
1213 FileMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
1214 LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut)
1216 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1217 return E_NOTIMPL;
1220 /******************************************************************************
1221 * FileMoniker_IsSystemMoniker
1223 static HRESULT WINAPI
1224 FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1226 TRACE("(%p,%p)\n",iface,pwdMksys);
1228 if (!pwdMksys)
1229 return E_POINTER;
1231 (*pwdMksys)=MKSYS_FILEMONIKER;
1233 return S_OK;
1236 /*******************************************************************************
1237 * FileMonikerIROTData_QueryInterface
1239 static HRESULT WINAPI
1240 FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1243 FileMonikerImpl *This = impl_from_IROTData(iface);
1245 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
1247 return FileMonikerImpl_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
1250 /***********************************************************************
1251 * FileMonikerIROTData_AddRef
1253 static ULONG WINAPI
1254 FileMonikerROTDataImpl_AddRef(IROTData *iface)
1256 FileMonikerImpl *This = impl_from_IROTData(iface);
1258 TRACE("(%p)\n",This);
1260 return IMoniker_AddRef(&This->IMoniker_iface);
1263 /***********************************************************************
1264 * FileMonikerIROTData_Release
1266 static ULONG WINAPI
1267 FileMonikerROTDataImpl_Release(IROTData* iface)
1269 FileMonikerImpl *This = impl_from_IROTData(iface);
1271 TRACE("(%p)\n",This);
1273 return FileMonikerImpl_Release(&This->IMoniker_iface);
1276 /******************************************************************************
1277 * FileMonikerIROTData_GetComparisonData
1279 static HRESULT WINAPI
1280 FileMonikerROTDataImpl_GetComparisonData(IROTData* iface, BYTE* pbData,
1281 ULONG cbMax, ULONG* pcbData)
1283 FileMonikerImpl *This = impl_from_IROTData(iface);
1284 int len = strlenW(This->filePathName)+1;
1285 int i;
1286 LPWSTR pszFileName;
1288 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
1290 *pcbData = sizeof(CLSID) + len * sizeof(WCHAR);
1291 if (cbMax < *pcbData)
1292 return E_OUTOFMEMORY;
1294 memcpy(pbData, &CLSID_FileMoniker, sizeof(CLSID));
1295 pszFileName = (LPWSTR)(pbData+sizeof(CLSID));
1296 for (i = 0; i < len; i++)
1297 pszFileName[i] = toupperW(This->filePathName[i]);
1299 return S_OK;
1303 * Virtual function table for the FileMonikerImpl class which include IPersist,
1304 * IPersistStream and IMoniker functions.
1306 static const IMonikerVtbl VT_FileMonikerImpl =
1308 FileMonikerImpl_QueryInterface,
1309 FileMonikerImpl_AddRef,
1310 FileMonikerImpl_Release,
1311 FileMonikerImpl_GetClassID,
1312 FileMonikerImpl_IsDirty,
1313 FileMonikerImpl_Load,
1314 FileMonikerImpl_Save,
1315 FileMonikerImpl_GetSizeMax,
1316 FileMonikerImpl_BindToObject,
1317 FileMonikerImpl_BindToStorage,
1318 FileMonikerImpl_Reduce,
1319 FileMonikerImpl_ComposeWith,
1320 FileMonikerImpl_Enum,
1321 FileMonikerImpl_IsEqual,
1322 FileMonikerImpl_Hash,
1323 FileMonikerImpl_IsRunning,
1324 FileMonikerImpl_GetTimeOfLastChange,
1325 FileMonikerImpl_Inverse,
1326 FileMonikerImpl_CommonPrefixWith,
1327 FileMonikerImpl_RelativePathTo,
1328 FileMonikerImpl_GetDisplayName,
1329 FileMonikerImpl_ParseDisplayName,
1330 FileMonikerImpl_IsSystemMoniker
1333 /* Virtual function table for the IROTData class. */
1334 static const IROTDataVtbl VT_ROTDataImpl =
1336 FileMonikerROTDataImpl_QueryInterface,
1337 FileMonikerROTDataImpl_AddRef,
1338 FileMonikerROTDataImpl_Release,
1339 FileMonikerROTDataImpl_GetComparisonData
1342 /******************************************************************************
1343 * FileMoniker_Construct (local function)
1345 static HRESULT FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
1347 int nb=0,i;
1348 int sizeStr=lstrlenW(lpszPathName);
1349 LPOLESTR *tabStr=0;
1350 static const WCHAR twoPoint[]={'.','.',0};
1351 static const WCHAR bkSlash[]={'\\',0};
1352 BOOL addBkSlash;
1354 TRACE("(%p,%s)\n",This,debugstr_w(lpszPathName));
1356 /* Initialize the virtual function table. */
1357 This->IMoniker_iface.lpVtbl = &VT_FileMonikerImpl;
1358 This->IROTData_iface.lpVtbl = &VT_ROTDataImpl;
1359 This->ref = 0;
1360 This->pMarshal = NULL;
1362 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
1364 if (This->filePathName==NULL)
1365 return E_OUTOFMEMORY;
1367 strcpyW(This->filePathName,lpszPathName);
1369 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
1371 if (nb > 0 ){
1373 addBkSlash = TRUE;
1374 if (lstrcmpW(tabStr[0],twoPoint)!=0)
1375 addBkSlash = FALSE;
1376 else
1377 for(i=0;i<nb;i++){
1379 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
1380 addBkSlash = FALSE;
1381 break;
1383 else
1385 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
1386 *tabStr[i]=0;
1387 sizeStr--;
1388 addBkSlash = FALSE;
1389 break;
1393 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
1394 addBkSlash = FALSE;
1396 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
1398 *This->filePathName=0;
1400 for(i=0;tabStr[i]!=NULL;i++)
1401 strcatW(This->filePathName,tabStr[i]);
1403 if (addBkSlash)
1404 strcatW(This->filePathName,bkSlash);
1407 free_stringtable(tabStr);
1409 return S_OK;
1412 /******************************************************************************
1413 * CreateFileMoniker (OLE32.@)
1414 ******************************************************************************/
1415 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, IMoniker **ppmk)
1417 FileMonikerImpl* newFileMoniker;
1418 HRESULT hr;
1420 TRACE("(%s,%p)\n",debugstr_w(lpszPathName),ppmk);
1422 if (!ppmk)
1423 return E_POINTER;
1425 if(!lpszPathName)
1426 return MK_E_SYNTAX;
1428 *ppmk=NULL;
1430 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1432 if (!newFileMoniker)
1433 return E_OUTOFMEMORY;
1435 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1437 if (SUCCEEDED(hr))
1438 hr = IMoniker_QueryInterface(&newFileMoniker->IMoniker_iface,&IID_IMoniker,(void**)ppmk);
1439 else
1440 HeapFree(GetProcessHeap(),0,newFileMoniker);
1442 return hr;
1445 /* find a character from a set in reverse without the string having to be null-terminated */
1446 static inline WCHAR *memrpbrkW(const WCHAR *ptr, size_t n, const WCHAR *accept)
1448 const WCHAR *end, *ret = NULL;
1449 for (end = ptr + n; ptr < end; ptr++) if (strchrW(accept, *ptr)) ret = ptr;
1450 return (WCHAR *)ret;
1453 HRESULT FileMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1454 LPDWORD pchEaten, IMoniker **ppmk)
1456 LPCWSTR end;
1457 static const WCHAR wszSeparators[] = {':','\\','/','!',0};
1459 for (end = szDisplayName + strlenW(szDisplayName);
1460 end && (end != szDisplayName);
1461 end = memrpbrkW(szDisplayName, end - szDisplayName, wszSeparators))
1463 HRESULT hr;
1464 IRunningObjectTable *rot;
1465 IMoniker *file_moniker;
1466 LPWSTR file_display_name;
1467 LPWSTR full_path_name;
1468 DWORD full_path_name_len;
1469 int len = end - szDisplayName;
1471 file_display_name = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1472 if (!file_display_name) return E_OUTOFMEMORY;
1473 memcpy(file_display_name, szDisplayName, len * sizeof(WCHAR));
1474 file_display_name[len] = '\0';
1476 hr = CreateFileMoniker(file_display_name, &file_moniker);
1477 if (FAILED(hr))
1479 HeapFree(GetProcessHeap(), 0, file_display_name);
1480 return hr;
1483 hr = IBindCtx_GetRunningObjectTable(pbc, &rot);
1484 if (FAILED(hr))
1486 HeapFree(GetProcessHeap(), 0, file_display_name);
1487 IMoniker_Release(file_moniker);
1488 return hr;
1491 hr = IRunningObjectTable_IsRunning(rot, file_moniker);
1492 IRunningObjectTable_Release(rot);
1493 if (FAILED(hr))
1495 HeapFree(GetProcessHeap(), 0, file_display_name);
1496 IMoniker_Release(file_moniker);
1497 return hr;
1499 if (hr == S_OK)
1501 TRACE("found running file moniker for %s\n", debugstr_w(file_display_name));
1502 *pchEaten = len;
1503 *ppmk = file_moniker;
1504 HeapFree(GetProcessHeap(), 0, file_display_name);
1505 return S_OK;
1508 full_path_name_len = GetFullPathNameW(file_display_name, 0, NULL, NULL);
1509 if (!full_path_name_len)
1511 HeapFree(GetProcessHeap(), 0, file_display_name);
1512 IMoniker_Release(file_moniker);
1513 return MK_E_SYNTAX;
1515 full_path_name = HeapAlloc(GetProcessHeap(), 0, full_path_name_len * sizeof(WCHAR));
1516 if (!full_path_name)
1518 HeapFree(GetProcessHeap(), 0, file_display_name);
1519 IMoniker_Release(file_moniker);
1520 return E_OUTOFMEMORY;
1522 GetFullPathNameW(file_display_name, full_path_name_len, full_path_name, NULL);
1524 if (GetFileAttributesW(full_path_name) == INVALID_FILE_ATTRIBUTES)
1525 TRACE("couldn't open file %s\n", debugstr_w(full_path_name));
1526 else
1528 TRACE("got file moniker for %s\n", debugstr_w(szDisplayName));
1529 *pchEaten = len;
1530 *ppmk = file_moniker;
1531 HeapFree(GetProcessHeap(), 0, file_display_name);
1532 HeapFree(GetProcessHeap(), 0, full_path_name);
1533 return S_OK;
1535 HeapFree(GetProcessHeap(), 0, file_display_name);
1536 HeapFree(GetProcessHeap(), 0, full_path_name);
1537 IMoniker_Release(file_moniker);
1540 return MK_E_CANTOPENFILE;
1544 static HRESULT WINAPI FileMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1545 REFIID riid, LPVOID *ppv)
1547 *ppv = NULL;
1548 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1550 *ppv = iface;
1551 IClassFactory_AddRef(iface);
1552 return S_OK;
1554 return E_NOINTERFACE;
1557 static ULONG WINAPI FileMonikerCF_AddRef(LPCLASSFACTORY iface)
1559 return 2; /* non-heap based object */
1562 static ULONG WINAPI FileMonikerCF_Release(LPCLASSFACTORY iface)
1564 return 1; /* non-heap based object */
1567 static HRESULT WINAPI FileMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1568 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1570 FileMonikerImpl* newFileMoniker;
1571 HRESULT hr;
1572 static const WCHAR wszEmpty[] = { 0 };
1574 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1576 *ppv = NULL;
1578 if (pUnk)
1579 return CLASS_E_NOAGGREGATION;
1581 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1582 if (!newFileMoniker)
1583 return E_OUTOFMEMORY;
1585 hr = FileMonikerImpl_Construct(newFileMoniker, wszEmpty);
1587 if (SUCCEEDED(hr))
1588 hr = IMoniker_QueryInterface(&newFileMoniker->IMoniker_iface, riid, ppv);
1589 if (FAILED(hr))
1590 HeapFree(GetProcessHeap(),0,newFileMoniker);
1592 return hr;
1595 static HRESULT WINAPI FileMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1597 FIXME("(%d), stub!\n",fLock);
1598 return S_OK;
1601 static const IClassFactoryVtbl FileMonikerCFVtbl =
1603 FileMonikerCF_QueryInterface,
1604 FileMonikerCF_AddRef,
1605 FileMonikerCF_Release,
1606 FileMonikerCF_CreateInstance,
1607 FileMonikerCF_LockServer
1609 static const IClassFactoryVtbl *FileMonikerCF = &FileMonikerCFVtbl;
1611 HRESULT FileMonikerCF_Create(REFIID riid, LPVOID *ppv)
1613 return IClassFactory_QueryInterface((IClassFactory *)&FileMonikerCF, riid, ppv);