Replaced all lstr* calls from inside Wine code by their str* equivalent.
[wine/hacks.git] / dlls / ole32 / filemoniker.c
blob17aaaa3863017f2bc525c51188317647bdeaf96b
1 /***************************************************************************************
2 * FileMonikers implementation
4 * Copyright 1999 Noomen Hamza
5 ***************************************************************************************/
7 #include <assert.h>
8 #include "winbase.h"
9 #include "winerror.h"
10 #include "wine/unicode.h"
11 #include "debugtools.h"
12 #include "objbase.h"
13 #include "wine/obj_storage.h"
14 #include "wine/obj_moniker.h"
15 #include "wine/obj_base.h"
17 DEFAULT_DEBUG_CHANNEL(ole);
19 /* filemoniker data structure */
20 typedef struct FileMonikerImpl{
22 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
24 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
25 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
27 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
29 ULONG ref; /* reference counter for this object */
31 LPOLESTR filePathName; /* path string identified by this filemoniker */
33 } FileMonikerImpl;
35 /********************************************************************************/
36 /* FileMoniker prototype functions : */
38 /* IUnknown prototype functions */
39 static HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
40 static ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface);
41 static ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface);
43 /* IPersist prototype functions */
44 static HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
46 /* IPersistStream prototype functions */
47 static HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface);
48 static HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface, IStream* pStm);
49 static HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
50 static HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
52 /* IMoniker prototype functions */
53 static HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
54 static HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
55 static HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
56 static HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
57 static HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
58 static HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
59 static HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
60 static HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
61 static HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
62 static HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
63 static HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
64 static HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
65 static HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
66 static HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
67 static HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
69 /********************************************************************************/
70 /* IROTData prototype functions */
72 /* IUnknown prototype functions */
73 static HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
74 static ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData* iface);
75 static ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface);
77 /* IROTData prototype function */
78 static HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
80 /* Local function used by filemoniker implementation */
81 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* iface, LPCOLESTR lpszPathName);
82 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* iface);
83 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** tabStr);
86 /********************************************************************************/
87 /* Virtual function table for the FileMonikerImpl class witch include Ipersist,*/
88 /* IPersistStream and IMoniker functions. */
89 static ICOM_VTABLE(IMoniker) VT_FileMonikerImpl =
91 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
92 FileMonikerImpl_QueryInterface,
93 FileMonikerImpl_AddRef,
94 FileMonikerImpl_Release,
95 FileMonikerImpl_GetClassID,
96 FileMonikerImpl_IsDirty,
97 FileMonikerImpl_Load,
98 FileMonikerImpl_Save,
99 FileMonikerImpl_GetSizeMax,
100 FileMonikerImpl_BindToObject,
101 FileMonikerImpl_BindToStorage,
102 FileMonikerImpl_Reduce,
103 FileMonikerImpl_ComposeWith,
104 FileMonikerImpl_Enum,
105 FileMonikerImpl_IsEqual,
106 FileMonikerImpl_Hash,
107 FileMonikerImpl_IsRunning,
108 FileMonikerImpl_GetTimeOfLastChange,
109 FileMonikerImpl_Inverse,
110 FileMonikerImpl_CommonPrefixWith,
111 FileMonikerImpl_RelativePathTo,
112 FileMonikerImpl_GetDisplayName,
113 FileMonikerImpl_ParseDisplayName,
114 FileMonikerImpl_IsSystemMoniker
117 /********************************************************************************/
118 /* Virtual function table for the IROTData class. */
119 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
121 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
122 FileMonikerROTDataImpl_QueryInterface,
123 FileMonikerROTDataImpl_AddRef,
124 FileMonikerROTDataImpl_Release,
125 FileMonikerROTDataImpl_GetComparaisonData
128 /*******************************************************************************
129 * FileMoniker_QueryInterface
130 *******************************************************************************/
131 HRESULT WINAPI FileMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
133 ICOM_THIS(FileMonikerImpl,iface);
135 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
137 /* Perform a sanity check on the parameters.*/
138 if ( (This==0) || (ppvObject==0) )
139 return E_INVALIDARG;
141 /* Initialize the return parameter */
142 *ppvObject = 0;
144 /* Compare the riid with the interface IDs implemented by this object.*/
145 if (IsEqualIID(&IID_IUnknown, riid) ||
146 IsEqualIID(&IID_IPersist, riid) ||
147 IsEqualIID(&IID_IPersistStream,riid) ||
148 IsEqualIID(&IID_IMoniker, riid)
150 *ppvObject = iface;
152 else if (IsEqualIID(&IID_IROTData, riid))
153 *ppvObject = (IROTData*)&(This->lpvtbl2);
155 /* Check that we obtained an interface.*/
156 if ((*ppvObject)==0)
157 return E_NOINTERFACE;
159 /* Query Interface always increases the reference count by one when it is successful */
160 FileMonikerImpl_AddRef(iface);
162 return S_OK;
165 /******************************************************************************
166 * FileMoniker_AddRef
167 ******************************************************************************/
168 ULONG WINAPI FileMonikerImpl_AddRef(IMoniker* iface)
170 ICOM_THIS(FileMonikerImpl,iface);
172 TRACE("(%p)\n",iface);
174 return ++(This->ref);
177 /******************************************************************************
178 * FileMoniker_Release
179 ******************************************************************************/
180 ULONG WINAPI FileMonikerImpl_Release(IMoniker* iface)
182 ICOM_THIS(FileMonikerImpl,iface);
184 TRACE("(%p)\n",iface);
186 This->ref--;
188 /* destroy the object if there's no more reference on it */
189 if (This->ref==0){
191 FileMonikerImpl_Destroy(This);
193 return 0;
195 return This->ref;;
198 /******************************************************************************
199 * FileMoniker_GetClassID
200 ******************************************************************************/
201 HRESULT WINAPI FileMonikerImpl_GetClassID(IMoniker* iface,
202 CLSID *pClassID)/* Pointer to CLSID of object */
204 TRACE("(%p,%p),stub!\n",iface,pClassID);
206 if (pClassID==NULL)
207 return E_POINTER;
209 *pClassID = CLSID_FileMoniker;
211 return S_OK;
214 /******************************************************************************
215 * FileMoniker_IsDirty
216 ******************************************************************************/
217 HRESULT WINAPI FileMonikerImpl_IsDirty(IMoniker* iface)
219 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
220 method in the OLE-provided moniker interfaces always return S_FALSE because
221 their internal state never changes. */
223 TRACE("(%p)\n",iface);
225 return S_FALSE;
228 /******************************************************************************
229 * FileMoniker_Load
230 ******************************************************************************/
231 HRESULT WINAPI FileMonikerImpl_Load(IMoniker* iface,IStream* pStm)
233 HRESULT res;
234 CHAR* filePathA;
235 WCHAR* filePathW;
236 ULONG bread;
237 WORD wbuffer;
238 DWORD dwbuffer,length,i,doubleLenHex,doubleLenDec;
240 ICOM_THIS(FileMonikerImpl,iface);
242 TRACE("(%p,%p)\n",iface,pStm);
244 /* this function locate and read from the stream the filePath string writen by FileMonikerImpl_Save */
246 /* first WORD is non significative */
247 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
248 if (bread!=sizeof(WORD) || wbuffer!=0)
249 return E_FAIL;
251 /* read filePath string length (plus one) */
252 res=IStream_Read(pStm,&length,sizeof(DWORD),&bread);
253 if (bread != sizeof(DWORD))
254 return E_FAIL;
256 /* read filePath string */
257 filePathA=HeapAlloc(GetProcessHeap(),0,length);
258 res=IStream_Read(pStm,filePathA,length,&bread);
259 if (bread != length)
260 return E_FAIL;
262 /* read the first constant */
263 IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
264 if (bread != sizeof(DWORD) || dwbuffer != 0xDEADFFFF)
265 return E_FAIL;
267 length--;
269 for(i=0;i<10;i++){
270 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
271 if (bread!=sizeof(WORD) || wbuffer!=0)
272 return E_FAIL;
275 if (length>8)
276 length=0;
278 doubleLenHex=doubleLenDec=2*length;
279 if (length > 5)
280 doubleLenDec+=6;
282 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
283 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenDec)
284 return E_FAIL;
286 if (length==0)
287 return res;
289 res=IStream_Read(pStm,&dwbuffer,sizeof(DWORD),&bread);
290 if (bread!=sizeof(DWORD) || dwbuffer!=doubleLenHex)
291 return E_FAIL;
293 res=IStream_Read(pStm,&wbuffer,sizeof(WORD),&bread);
294 if (bread!=sizeof(WORD) || wbuffer!=0x3)
295 return E_FAIL;
297 filePathW=HeapAlloc(GetProcessHeap(),0,(length+1)*sizeof(WCHAR));
298 filePathW[length]=0;
299 res=IStream_Read(pStm,filePathW,doubleLenHex,&bread);
300 if (bread!=doubleLenHex)
301 return E_FAIL;
303 if (This->filePathName!=NULL)
304 HeapFree(GetProcessHeap(),0,This->filePathName);
306 This->filePathName=filePathW;
308 HeapFree(GetProcessHeap(),0,filePathA);
310 return res;
313 /******************************************************************************
314 * FileMoniker_Save
315 ******************************************************************************/
316 HRESULT WINAPI FileMonikerImpl_Save(IMoniker* iface,
317 IStream* pStm,/* poniter to the stream where the object is to be saved */
318 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
320 /* this function saves data of this object. In the begining I thougth that I have just to write
321 * the filePath string on Stream. But, when I tested this function whith windows programs samples !
322 * I noted that it was not the case. So I analysed data writen by this function on Windows system and
323 * what did this function do exactly ! but I have no idear a bout its logic !
324 * I guessed data who must be writen on stream wich is:
325 * 1) WORD constant:zero 2) length of the path string ("\0" included) 3) path string type A
326 * 4) DWORD constant : 0xDEADFFFF 5) ten WORD constant: zero 6) DWORD: double-length of the the path
327 * string type W ("\0" not included) 7) WORD constant: 0x3 8) filePath unicode string.
328 * if the length(filePath) > 8 or.length(filePath) == 8 stop at step 5)
331 ICOM_THIS(FileMonikerImpl,iface);
333 HRESULT res;
334 LPOLESTR filePathW=This->filePathName;
335 CHAR* filePathA;
336 DWORD len=1+lstrlenW(filePathW);
338 DWORD constant1 = 0xDEADFFFF; /* these constants are detected after analysing the data structure writen by */
339 WORD constant2 = 0x3; /* FileMoniker_Save function in a windows program system */
341 WORD zero=0;
342 DWORD doubleLenHex;
343 DWORD doubleLenDec;
344 int i=0;
346 TRACE("(%p,%p,%d)\n",iface,pStm,fClearDirty);
348 if (pStm==NULL)
349 return E_POINTER;
351 /* write a DWORD seted to 0 : constant */
352 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
354 /* write length of filePath string ( "\0" included )*/
355 res=IStream_Write(pStm,&len,sizeof(DWORD),NULL);
357 /* write filePath string type A */
358 filePathA=HeapAlloc(GetProcessHeap(),0,len);
359 lstrcpyWtoA(filePathA,filePathW);
360 res=IStream_Write(pStm,filePathA,len,NULL);
361 HeapFree(GetProcessHeap(),0,filePathA);
363 /* write a DWORD seted to 0xDEADFFFF: constant */
364 res=IStream_Write(pStm,&constant1,sizeof(DWORD),NULL);
366 len--;
367 /* write 10 times a DWORD seted to 0 : constants */
368 for(i=0;i<10;i++)
369 res=IStream_Write(pStm,&zero,sizeof(WORD),NULL);
371 if (len>8)
372 len=0;
374 doubleLenHex=doubleLenDec=2*len;
375 if (len > 5)
376 doubleLenDec+=6;
378 /* write double-length of the path string ( "\0" included )*/
379 res=IStream_Write(pStm,&doubleLenDec,sizeof(DWORD),NULL);
381 if (len==0)
382 return res;
384 /* write double-length (hexa representation) of the path string ( "\0" included ) */
385 res=IStream_Write(pStm,&doubleLenHex,sizeof(DWORD),NULL);
387 /* write a WORD seted to 0x3: constant */
388 res=IStream_Write(pStm,&constant2,sizeof(WORD),NULL);
390 /* write path unicode string */
391 res=IStream_Write(pStm,filePathW,doubleLenHex,NULL);
393 return res;
396 /******************************************************************************
397 * FileMoniker_GetSizeMax
398 ******************************************************************************/
399 HRESULT WINAPI FileMonikerImpl_GetSizeMax(IMoniker* iface,
400 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
402 ICOM_THIS(FileMonikerImpl,iface);
403 DWORD len=lstrlenW(This->filePathName);
404 DWORD sizeMAx;
406 TRACE("(%p,%p)\n",iface,pcbSize);
408 if (pcbSize!=NULL)
409 return E_POINTER;
411 /* for more details see FileMonikerImpl_Save coments */
413 sizeMAx = sizeof(WORD) + /* first WORD is 0 */
414 sizeof(DWORD)+ /* length of filePath including "\0" in the end of the string */
415 (len+1)+ /* filePath string */
416 sizeof(DWORD)+ /* constant : 0xDEADFFFF */
417 10*sizeof(WORD)+ /* 10 zero WORD */
418 sizeof(DWORD); /* size of the unicode filePath: "\0" not included */
420 if (len==0 || len > 8)
421 return S_OK;
423 sizeMAx += sizeof(DWORD)+ /* size of the unicode filePath: "\0" not included */
424 sizeof(WORD)+ /* constant : 0x3 */
425 len*sizeof(WCHAR); /* unicde filePath string */
427 pcbSize->s.LowPart=sizeMAx;
428 pcbSize->s.HighPart=0;
430 return S_OK;
433 /******************************************************************************
434 * FileMoniker_Construct (local function)
435 *******************************************************************************/
436 HRESULT WINAPI FileMonikerImpl_Construct(FileMonikerImpl* This, LPCOLESTR lpszPathName)
438 int nb=0,i;
439 int sizeStr=lstrlenW(lpszPathName);
440 LPOLESTR *tabStr=0;
441 WCHAR twoPoint[]={'.','.',0};
442 WCHAR bkSlash[]={'\\',0};
443 BYTE addBkSlash;
445 TRACE("(%p,%p)\n",This,lpszPathName);
447 /* Initialize the virtual fgunction table. */
448 This->lpvtbl1 = &VT_FileMonikerImpl;
449 This->lpvtbl2 = &VT_ROTDataImpl;
450 This->ref = 0;
452 This->filePathName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
454 if (This->filePathName==NULL)
455 return E_OUTOFMEMORY;
457 strcpyW(This->filePathName,lpszPathName);
459 nb=FileMonikerImpl_DecomposePath(This->filePathName,&tabStr);
461 if (nb > 0 ){
463 addBkSlash=1;
464 if (lstrcmpW(tabStr[0],twoPoint)!=0)
465 addBkSlash=0;
466 else
467 for(i=0;i<nb;i++){
469 if ( (lstrcmpW(tabStr[i],twoPoint)!=0) && (lstrcmpW(tabStr[i],bkSlash)!=0) ){
470 addBkSlash=0;
471 break;
473 else
475 if (lstrcmpW(tabStr[i],bkSlash)==0 && i<nb-1 && lstrcmpW(tabStr[i+1],bkSlash)==0){
476 *tabStr[i]=0;
477 sizeStr--;
478 addBkSlash=0;
479 break;
483 if (lstrcmpW(tabStr[nb-1],bkSlash)==0)
484 addBkSlash=0;
486 This->filePathName=HeapReAlloc(GetProcessHeap(),0,This->filePathName,(sizeStr+1)*sizeof(WCHAR));
488 *This->filePathName=0;
490 for(i=0;tabStr[i]!=NULL;i++)
491 strcatW(This->filePathName,tabStr[i]);
493 if (addBkSlash)
494 strcatW(This->filePathName,bkSlash);
497 for(i=0; tabStr[i]!=NULL;i++)
498 CoTaskMemFree(tabStr[i]);
499 CoTaskMemFree(tabStr);
501 return S_OK;
504 /******************************************************************************
505 * FileMoniker_Destroy (local function)
506 *******************************************************************************/
507 HRESULT WINAPI FileMonikerImpl_Destroy(FileMonikerImpl* This)
509 TRACE("(%p)\n",This);
511 if (This->filePathName!=NULL)
512 HeapFree(GetProcessHeap(),0,This->filePathName);
514 HeapFree(GetProcessHeap(),0,This);
516 return S_OK;
519 /******************************************************************************
520 * FileMoniker_BindToObject
521 ******************************************************************************/
522 HRESULT WINAPI FileMonikerImpl_BindToObject(IMoniker* iface,
523 IBindCtx* pbc,
524 IMoniker* pmkToLeft,
525 REFIID riid,
526 VOID** ppvResult)
528 HRESULT res=E_FAIL;
529 CLSID clsID;
530 IUnknown* pObj=0;
531 IRunningObjectTable *prot=0;
532 IPersistFile *ppf=0;
533 IClassFactory *pcf=0;
534 IClassActivator *pca=0;
536 ICOM_THIS(FileMonikerImpl,iface);
538 *ppvResult=0;
540 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
542 if(pmkToLeft==NULL){
544 res=IBindCtx_GetRunningObjectTable(pbc,&prot);
546 if (SUCCEEDED(res)){
547 /* if the requested class was loaded befor ! we dont need to reload it */
548 res = IRunningObjectTable_GetObject(prot,iface,&pObj);
550 if (res==S_FALSE){
551 /* first activation of this class */
552 res=GetClassFile(This->filePathName,&clsID);
553 if (SUCCEEDED(res)){
555 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IPersistFile,(void**)&ppf);
556 if (SUCCEEDED(res)){
558 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
559 if (SUCCEEDED(res)){
561 pObj=(IUnknown*)ppf;
562 IUnknown_AddRef(pObj);
569 else{
570 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassFactory,(void**)&pcf);
572 if (res==E_NOINTERFACE){
574 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IClassActivator,(void**)&pca);
576 if (res==E_NOINTERFACE)
577 return MK_E_INTERMEDIATEINTERFACENOTSUPPORTED;
579 if (pcf!=NULL){
581 IClassFactory_CreateInstance(pcf,NULL,&IID_IPersistFile,(void**)ppf);
583 res=IPersistFile_Load(ppf,This->filePathName,STGM_READ);
585 if (SUCCEEDED(res)){
587 pObj=(IUnknown*)ppf;
588 IUnknown_AddRef(pObj);
591 if (pca!=NULL){
593 FIXME("()");
595 /*res=GetClassFile(This->filePathName,&clsID);
597 if (SUCCEEDED(res)){
599 res=IClassActivator_GetClassObject(pca,&clsID,CLSCTX_ALL,0,&IID_IPersistFile,(void**)&ppf);
601 if (SUCCEEDED(res)){
603 pObj=(IUnknown*)ppf;
604 IUnknown_AddRef(pObj);
610 if (pObj!=NULL){
611 /* get the requested interface from the loaded class */
612 res= IUnknown_QueryInterface(pObj,riid,ppvResult);
614 IBindCtx_RegisterObjectBound(pbc,(IUnknown*)*ppvResult);
616 IUnknown_Release(pObj);
619 if (prot!=NULL)
620 IRunningObjectTable_Release(prot);
622 if (ppf!=NULL)
623 IPersistFile_Release(ppf);
625 if (pca!=NULL)
626 IClassActivator_Release(pca);
628 if (pcf!=NULL)
629 IClassFactory_Release(pcf);
631 return res;
634 /******************************************************************************
635 * FileMoniker_BindToStorage
636 ******************************************************************************/
637 HRESULT WINAPI FileMonikerImpl_BindToStorage(IMoniker* iface,
638 IBindCtx* pbc,
639 IMoniker* pmkToLeft,
640 REFIID riid,
641 VOID** ppvObject)
643 LPOLESTR filePath=0;
644 IStorage *pstg=0;
645 HRESULT res;
647 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
649 if (pmkToLeft==NULL){
651 if (IsEqualIID(&IID_IStorage, riid)){
653 /* get the file name */
654 FileMonikerImpl_GetDisplayName(iface,pbc,pmkToLeft,&filePath);
656 /* verifie if the file contains a storage object */
657 res=StgIsStorageFile(filePath);
659 if(res==S_OK){
661 res=StgOpenStorage(filePath,NULL,STGM_READWRITE|STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
663 if (SUCCEEDED(res)){
665 *ppvObject=pstg;
667 IStorage_AddRef(pstg);
669 return res;
672 CoTaskMemFree(filePath);
674 else
675 if ( (IsEqualIID(&IID_IStream, riid)) || (IsEqualIID(&IID_ILockBytes, riid)) )
677 return E_UNSPEC;
678 else
680 return E_NOINTERFACE;
682 else {
684 FIXME("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvObject);
686 return E_NOTIMPL;
688 return res;
691 /******************************************************************************
692 * FileMoniker_Reduce
693 ******************************************************************************/
694 HRESULT WINAPI FileMonikerImpl_Reduce(IMoniker* iface,
695 IBindCtx* pbc,
696 DWORD dwReduceHowFar,
697 IMoniker** ppmkToLeft,
698 IMoniker** ppmkReduced)
700 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
702 if (ppmkReduced==NULL)
703 return E_POINTER;
705 FileMonikerImpl_AddRef(iface);
707 *ppmkReduced=iface;
709 return MK_S_REDUCED_TO_SELF;
711 /******************************************************************************
712 * FileMoniker_ComposeWith
713 ******************************************************************************/
714 HRESULT WINAPI FileMonikerImpl_ComposeWith(IMoniker* iface,
715 IMoniker* pmkRight,
716 BOOL fOnlyIfNotGeneric,
717 IMoniker** ppmkComposite)
719 HRESULT res;
720 LPOLESTR str1=0,str2=0,*strDec1=0,*strDec2=0,newStr=0;
721 WCHAR twoPoint[]={'.','.',0};
722 WCHAR bkSlash[]={'\\',0};
723 IBindCtx *bind=0;
724 int i=0,j=0,lastIdx1=0,lastIdx2=0;
725 DWORD mkSys;
727 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
729 if (ppmkComposite==NULL)
730 return E_POINTER;
732 if (pmkRight==NULL)
733 return E_INVALIDARG;
735 *ppmkComposite=0;
737 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
739 /* check if we have two filemonikers to compose or not */
740 if(mkSys==MKSYS_FILEMONIKER){
742 CreateBindCtx(0,&bind);
744 FileMonikerImpl_GetDisplayName(iface,bind,NULL,&str1);
745 IMoniker_GetDisplayName(pmkRight,bind,NULL,&str2);
747 /* decompose pathnames of the two monikers : (to prepare the path merge operation ) */
748 lastIdx1=FileMonikerImpl_DecomposePath(str1,&strDec1)-1;
749 lastIdx2=FileMonikerImpl_DecomposePath(str2,&strDec2)-1;
751 if ((lastIdx1==-1 && lastIdx2>-1)||(lastIdx1==1 && lstrcmpW(strDec1[0],twoPoint)==0))
752 return MK_E_SYNTAX;
754 if(lstrcmpW(strDec1[lastIdx1],bkSlash)==0)
755 lastIdx1--;
757 /* for etch "..\" in the left of str2 remove the right element from str1 */
758 for(i=0; ( (lastIdx1>=0) && (strDec2[i]!=NULL) && (lstrcmpW(strDec2[i],twoPoint)==0) ) ;i+=2){
760 lastIdx1-=2;
763 /* the length of the composed path string is raised by the sum of the two paths lengths */
764 newStr=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(lstrlenW(str1)+lstrlenW(str2)+1));
766 if (newStr==NULL)
767 return E_OUTOFMEMORY;
769 /* new path is the concatenation of the rest of str1 and str2 */
770 for(*newStr=0,j=0;j<=lastIdx1;j++)
771 strcatW(newStr,strDec1[j]);
773 if ((strDec2[i]==NULL && lastIdx1>-1 && lastIdx2>-1) || lstrcmpW(strDec2[i],bkSlash)!=0)
774 strcatW(newStr,bkSlash);
776 for(j=i;j<=lastIdx2;j++)
777 strcatW(newStr,strDec2[j]);
779 /* create a new moniker with the new string */
780 res=CreateFileMoniker(newStr,ppmkComposite);
782 /* free all strings space memory used by this function */
783 HeapFree(GetProcessHeap(),0,newStr);
785 for(i=0; strDec1[i]!=NULL;i++)
786 CoTaskMemFree(strDec1[i]);
787 for(i=0; strDec2[i]!=NULL;i++)
788 CoTaskMemFree(strDec2[i]);
789 CoTaskMemFree(strDec1);
790 CoTaskMemFree(strDec2);
792 CoTaskMemFree(str1);
793 CoTaskMemFree(str2);
795 return res;
797 else if(mkSys==MKSYS_ANTIMONIKER){
799 *ppmkComposite=NULL;
800 return S_OK;
802 else if (fOnlyIfNotGeneric){
804 *ppmkComposite=NULL;
805 return MK_E_NEEDGENERIC;
807 else
809 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
812 /******************************************************************************
813 * FileMoniker_Enum
814 ******************************************************************************/
815 HRESULT WINAPI FileMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
817 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
819 if (ppenumMoniker == NULL)
820 return E_POINTER;
822 *ppenumMoniker = NULL;
824 return S_OK;
827 /******************************************************************************
828 * FileMoniker_IsEqual
829 ******************************************************************************/
830 HRESULT WINAPI FileMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
832 ICOM_THIS(FileMonikerImpl,iface);
833 CLSID clsid;
834 LPOLESTR filePath;
835 IBindCtx* bind;
836 HRESULT res;
838 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
840 if (pmkOtherMoniker==NULL)
841 return S_FALSE;
843 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
845 if (!IsEqualCLSID(&clsid,&CLSID_FileMoniker))
847 return S_FALSE;
849 res=CreateBindCtx(0,&bind);
850 if (FAILED(res))
851 return res;
853 IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&filePath);
855 if (lstrcmpiW(filePath,
856 This->filePathName)!=0)
858 return S_FALSE;
860 return S_OK;
863 /******************************************************************************
864 * FileMoniker_Hash
865 ******************************************************************************/
866 HRESULT WINAPI FileMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
868 ICOM_THIS(FileMonikerImpl,iface);
870 int h = 0,i,skip,len;
871 int off = 0;
872 LPOLESTR val;
874 if (pdwHash==NULL)
875 return E_POINTER;
877 val = This->filePathName;
878 len = lstrlenW(val);
880 if (len < 16) {
881 for (i = len ; i > 0; i--) {
882 h = (h * 37) + val[off++];
884 } else {
885 /* only sample some characters */
886 skip = len / 8;
887 for (i = len ; i > 0; i -= skip, off += skip) {
888 h = (h * 39) + val[off];
892 *pdwHash=h;
894 return S_OK;
897 /******************************************************************************
898 * FileMoniker_IsRunning
899 ******************************************************************************/
900 HRESULT WINAPI FileMonikerImpl_IsRunning(IMoniker* iface,
901 IBindCtx* pbc,
902 IMoniker* pmkToLeft,
903 IMoniker* pmkNewlyRunning)
905 IRunningObjectTable* rot;
906 HRESULT res;
908 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
910 if ( (pmkNewlyRunning!=NULL) && (IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK) )
911 return S_OK;
913 if (pbc==NULL)
914 return E_POINTER;
916 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
918 if (FAILED(res))
919 return res;
921 res = IRunningObjectTable_IsRunning(rot,iface);
923 IRunningObjectTable_Release(rot);
925 return res;
928 /******************************************************************************
929 * FileMoniker_GetTimeOfLastChange
930 ******************************************************************************/
931 HRESULT WINAPI FileMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
932 IBindCtx* pbc,
933 IMoniker* pmkToLeft,
934 FILETIME* pFileTime)
936 ICOM_THIS(FileMonikerImpl,iface);
937 IRunningObjectTable* rot;
938 HRESULT res;
939 WIN32_FILE_ATTRIBUTE_DATA info;
941 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pFileTime);
943 if (pFileTime==NULL)
944 return E_POINTER;
946 if (pmkToLeft!=NULL)
947 return E_INVALIDARG;
949 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
951 if (FAILED(res))
952 return res;
954 res= IRunningObjectTable_GetTimeOfLastChange(rot,iface,pFileTime);
956 if (FAILED(res)){ /* the moniker is not registred */
958 if (!GetFileAttributesExW(This->filePathName,GetFileExInfoStandard,&info))
959 return MK_E_NOOBJECT;
961 *pFileTime=info.ftLastWriteTime;
964 return S_OK;
967 /******************************************************************************
968 * FileMoniker_Inverse
969 ******************************************************************************/
970 HRESULT WINAPI FileMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
973 TRACE("(%p,%p)\n",iface,ppmk);
975 return CreateAntiMoniker(ppmk);
978 /******************************************************************************
979 * FileMoniker_CommonPrefixWith
980 ******************************************************************************/
981 HRESULT WINAPI FileMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
984 LPOLESTR pathThis,pathOther,*stringTable1,*stringTable2,commonPath;
985 IBindCtx *pbind;
986 DWORD mkSys;
987 ULONG nb1,nb2,i,sameIdx;
988 BOOL machimeNameCase=FALSE;
990 if (ppmkPrefix==NULL)
991 return E_POINTER;
993 if (pmkOther==NULL)
994 return E_INVALIDARG;
996 *ppmkPrefix=0;
998 /* check if we have the same type of moniker */
999 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
1001 if(mkSys==MKSYS_FILEMONIKER){
1003 CreateBindCtx(0,&pbind);
1005 /* create a string based on common part of the two paths */
1007 IMoniker_GetDisplayName(iface,pbind,NULL,&pathThis);
1008 IMoniker_GetDisplayName(pmkOther,pbind,NULL,&pathOther);
1010 nb1=FileMonikerImpl_DecomposePath(pathThis,&stringTable1);
1011 nb2=FileMonikerImpl_DecomposePath(pathOther,&stringTable2);
1013 if (nb1==0 || nb2==0)
1014 return MK_E_NOPREFIX;
1016 commonPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(min(lstrlenW(pathThis),lstrlenW(pathOther))+1));
1018 *commonPath=0;
1020 for(sameIdx=0; ( (stringTable1[sameIdx]!=NULL) &&
1021 (stringTable2[sameIdx]!=NULL) &&
1022 (lstrcmpiW(stringTable1[sameIdx],stringTable2[sameIdx])==0)); sameIdx++);
1024 if (sameIdx > 1 && *stringTable1[0]=='\\' && *stringTable2[1]=='\\'){
1026 machimeNameCase=TRUE;
1028 for(i=2;i<sameIdx;i++)
1030 if( (*stringTable1[i]=='\\') && (i+1 < sameIdx) && (*stringTable1[i+1]=='\\') ){
1031 machimeNameCase=FALSE;
1032 break;
1036 if (machimeNameCase && *stringTable1[sameIdx-1]=='\\')
1037 sameIdx--;
1039 if (machimeNameCase && (sameIdx<=3) && (nb1 > 3 || nb2 > 3) )
1040 return MK_E_NOPREFIX;
1042 for(i=0;i<sameIdx;i++)
1043 strcatW(commonPath,stringTable1[i]);
1045 for(i=0;i<nb1;i++)
1046 CoTaskMemFree(stringTable1[i]);
1048 CoTaskMemFree(stringTable1);
1050 for(i=0;i<nb2;i++)
1051 CoTaskMemFree(stringTable2[i]);
1053 CoTaskMemFree(stringTable2);
1055 HeapFree(GetProcessHeap(),0,commonPath);
1057 return CreateFileMoniker(commonPath,ppmkPrefix);
1059 else
1060 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
1063 /******************************************************************************
1064 * DecomposePath (local function)
1065 ******************************************************************************/
1066 int WINAPI FileMonikerImpl_DecomposePath(LPOLESTR str, LPOLESTR** stringTable)
1068 WCHAR bSlash[] = {'\\',0};
1069 WCHAR word[MAX_PATH];
1070 int i=0,j,tabIndex=0;
1071 LPOLESTR *strgtable ;
1073 int len=lstrlenW(str);
1075 strgtable =CoTaskMemAlloc(len*sizeof(LPOLESTR));
1077 if (strgtable==NULL)
1078 return E_OUTOFMEMORY;
1080 while(str[i]!=0){
1082 if(str[i]==bSlash[0]){
1084 strgtable[tabIndex]=CoTaskMemAlloc(2*sizeof(WCHAR));
1086 if (strgtable[tabIndex]==NULL)
1087 return E_OUTOFMEMORY;
1089 strcpyW(strgtable[tabIndex++],bSlash);
1091 i++;
1094 else {
1096 for(j=0; str[i]!=0 && str[i]!=bSlash[0] ; i++,j++)
1097 word[j]=str[i];
1099 word[j]=0;
1101 strgtable[tabIndex]=CoTaskMemAlloc(sizeof(WCHAR)*(j+1));
1103 if (strgtable[tabIndex]==NULL)
1104 return E_OUTOFMEMORY;
1106 strcpyW(strgtable[tabIndex++],word);
1109 strgtable[tabIndex]=NULL;
1111 *stringTable=strgtable;
1113 return tabIndex;
1116 /******************************************************************************
1117 * FileMoniker_RelativePathTo
1118 ******************************************************************************/
1119 HRESULT WINAPI FileMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
1121 IBindCtx *bind;
1122 HRESULT res;
1123 LPOLESTR str1=0,str2=0,*tabStr1=0,*tabStr2=0,relPath=0;
1124 DWORD len1=0,len2=0,sameIdx=0,j=0;
1125 WCHAR back[] ={'.','.','\\',0};
1127 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
1129 if (ppmkRelPath==NULL)
1130 return E_POINTER;
1132 if (pmOther==NULL)
1133 return E_INVALIDARG;
1135 res=CreateBindCtx(0,&bind);
1136 if (FAILED(res))
1137 return res;
1139 res=IMoniker_GetDisplayName(iface,bind,NULL,&str1);
1140 if (FAILED(res))
1141 return res;
1142 res=IMoniker_GetDisplayName(pmOther,bind,NULL,&str2);
1143 if (FAILED(res))
1144 return res;
1146 len1=FileMonikerImpl_DecomposePath(str1,&tabStr1);
1147 len2=FileMonikerImpl_DecomposePath(str2,&tabStr2);
1149 if (FAILED(len1) || FAILED(len2))
1150 return E_OUTOFMEMORY;
1152 /* count the number of similar items from the begin of the two paths */
1153 for(sameIdx=0; ( (tabStr1[sameIdx]!=NULL) &&
1154 (tabStr2[sameIdx]!=NULL) &&
1155 (lstrcmpiW(tabStr1[sameIdx],tabStr2[sameIdx])==0)); sameIdx++);
1157 /* begin the construction of relativePath */
1158 /* if the two paths have a consecutive similar item from the begin ! the relativePath will be composed */
1159 /* by "..\\" in the begin */
1160 relPath=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(1+lstrlenW(str1)+lstrlenW(str2)));
1162 *relPath=0;
1164 if (len2>0 && !(len1==1 && len2==1 && sameIdx==0))
1165 for(j=sameIdx;(tabStr1[j] != NULL); j++)
1166 if (*tabStr1[j]!='\\')
1167 strcatW(relPath,back);
1169 /* add items of the second path (similar items with the first path are not included) to the relativePath */
1170 for(j=sameIdx;tabStr2[j]!=NULL;j++)
1171 strcatW(relPath,tabStr2[j]);
1173 res=CreateFileMoniker(relPath,ppmkRelPath);
1175 for(j=0; tabStr1[j]!=NULL;j++)
1176 CoTaskMemFree(tabStr1[j]);
1177 for(j=0; tabStr2[j]!=NULL;j++)
1178 CoTaskMemFree(tabStr2[j]);
1179 CoTaskMemFree(tabStr1);
1180 CoTaskMemFree(tabStr2);
1181 CoTaskMemFree(str1);
1182 CoTaskMemFree(str2);
1183 HeapFree(GetProcessHeap(),0,relPath);
1185 if (len1==0 || len2==0 || (len1==1 && len2==1 && sameIdx==0))
1186 return MK_S_HIM;
1188 return res;
1191 /******************************************************************************
1192 * FileMoniker_GetDisplayName
1193 ******************************************************************************/
1194 HRESULT WINAPI FileMonikerImpl_GetDisplayName(IMoniker* iface,
1195 IBindCtx* pbc,
1196 IMoniker* pmkToLeft,
1197 LPOLESTR *ppszDisplayName)
1199 ICOM_THIS(FileMonikerImpl,iface);
1201 int len=lstrlenW(This->filePathName);
1203 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
1205 if (ppszDisplayName==NULL)
1206 return E_POINTER;
1208 if (pmkToLeft!=NULL)
1209 return E_INVALIDARG;
1211 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(len+1));
1212 if (*ppszDisplayName==NULL)
1213 return E_OUTOFMEMORY;
1215 strcpyW(*ppszDisplayName,This->filePathName);
1217 return S_OK;
1220 /******************************************************************************
1221 * FileMoniker_ParseDisplayName
1222 ******************************************************************************/
1223 HRESULT WINAPI FileMonikerImpl_ParseDisplayName(IMoniker* iface,
1224 IBindCtx* pbc,
1225 IMoniker* pmkToLeft,
1226 LPOLESTR pszDisplayName,
1227 ULONG* pchEaten,
1228 IMoniker** ppmkOut)
1230 FIXME("(%p,%p,%p,%p,%p,%p),stub!\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
1231 return E_NOTIMPL;
1234 /******************************************************************************
1235 * FileMoniker_IsSystemMonker
1236 ******************************************************************************/
1237 HRESULT WINAPI FileMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
1239 TRACE("(%p,%p)\n",iface,pwdMksys);
1241 if (!pwdMksys)
1242 return E_POINTER;
1244 (*pwdMksys)=MKSYS_FILEMONIKER;
1246 return S_OK;
1249 /*******************************************************************************
1250 * FileMonikerIROTData_QueryInterface
1251 *******************************************************************************/
1252 HRESULT WINAPI FileMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
1255 ICOM_THIS_From_IROTData(IMoniker, iface);
1257 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
1259 return FileMonikerImpl_QueryInterface(This, riid, ppvObject);
1262 /***********************************************************************
1263 * FileMonikerIROTData_AddRef
1265 ULONG WINAPI FileMonikerROTDataImpl_AddRef(IROTData *iface)
1267 ICOM_THIS_From_IROTData(IMoniker, iface);
1269 TRACE("(%p)\n",This);
1271 return FileMonikerImpl_AddRef(This);
1274 /***********************************************************************
1275 * FileMonikerIROTData_Release
1277 ULONG WINAPI FileMonikerROTDataImpl_Release(IROTData* iface)
1279 ICOM_THIS_From_IROTData(IMoniker, iface);
1281 TRACE("(%p)\n",This);
1283 return FileMonikerImpl_Release(This);
1286 /******************************************************************************
1287 * FileMonikerIROTData_GetComparaisonData
1288 ******************************************************************************/
1289 HRESULT WINAPI FileMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
1290 BYTE* pbData,
1291 ULONG cbMax,
1292 ULONG* pcbData)
1294 FIXME("(),stub!\n");
1295 return E_NOTIMPL;
1298 /******************************************************************************
1299 * CreateFileMoniker16
1300 ******************************************************************************/
1301 HRESULT WINAPI CreateFileMoniker16(LPCOLESTR16 lpszPathName,LPMONIKER* ppmk)
1304 FIXME("(%s,%p),stub!\n",lpszPathName,ppmk);
1305 return E_NOTIMPL;
1308 /******************************************************************************
1309 * CreateFileMoniker
1310 ******************************************************************************/
1311 HRESULT WINAPI CreateFileMoniker(LPCOLESTR lpszPathName, LPMONIKER * ppmk)
1313 FileMonikerImpl* newFileMoniker = 0;
1314 HRESULT hr = E_FAIL;
1315 IID riid=IID_IMoniker;
1317 TRACE("(%p,%p)\n",lpszPathName,ppmk);
1319 if (ppmk==NULL)
1320 return E_POINTER;
1322 if(lpszPathName==NULL)
1323 return MK_E_SYNTAX;
1325 *ppmk=0;
1327 newFileMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(FileMonikerImpl));
1329 if (newFileMoniker == 0)
1330 return E_OUTOFMEMORY;
1332 hr = FileMonikerImpl_Construct(newFileMoniker,lpszPathName);
1334 if (SUCCEEDED(hr))
1335 hr = FileMonikerImpl_QueryInterface((IMoniker*)newFileMoniker,&riid,(void**)ppmk);
1336 else
1337 HeapFree(GetProcessHeap(),0,newFileMoniker);
1339 return hr;