Revert broken strict aliasing fix.
[wine.git] / dlls / ole32 / itemmoniker.c
blobb426f712f132660f1602447bc528306c620e17bc
1 /***************************************************************************************
2 * ItemMonikers implementation
4 * Copyright 1999 Noomen Hamza
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***************************************************************************************/
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "wine/unicode.h"
35 #include "moniker.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 const CLSID CLSID_ItemMoniker = {
40 0x304, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
43 /* ItemMoniker data structure */
44 typedef struct ItemMonikerImpl{
46 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
48 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
49 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
51 ICOM_VTABLE(IROTData)* lpvtbl2; /* VTable relative to the IROTData interface.*/
53 ULONG ref; /* reference counter for this object */
55 LPOLESTR itemName; /* item name identified by this ItemMoniker */
57 LPOLESTR itemDelimiter; /* Delimiter string */
59 } ItemMonikerImpl;
61 /********************************************************************************/
62 /* ItemMoniker prototype functions : */
64 /* IUnknown prototype functions */
65 static HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
66 static ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface);
67 static ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface);
69 /* IPersist prototype functions */
70 static HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
72 /* IPersistStream prototype functions */
73 static HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface);
74 static HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface, IStream* pStm);
75 static HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
76 static HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
78 /* IMoniker prototype functions */
79 static HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
80 static HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
81 static HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
82 static HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
83 static HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
84 static HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
85 static HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
86 static HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
87 static HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pItemTime);
88 static HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
89 static HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
90 static HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
91 static HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
92 static HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
93 static HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
95 /* Local function used by ItemMoniker implementation */
96 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* iface, LPCOLESTR lpszDelim,LPCOLESTR lpszPathName);
97 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* iface);
99 /********************************************************************************/
100 /* IROTData prototype functions */
102 /* IUnknown prototype functions */
103 static HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
104 static ULONG WINAPI ItemMonikerROTDataImpl_AddRef(IROTData* iface);
105 static ULONG WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface);
107 /* IROTData prototype function */
108 static HRESULT WINAPI ItemMonikerROTDataImpl_GetComparaisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
110 /********************************************************************************/
111 /* Virtual function table for the ItemMonikerImpl class which include IPersist,*/
112 /* IPersistStream and IMoniker functions. */
113 static ICOM_VTABLE(IMoniker) VT_ItemMonikerImpl =
115 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
116 ItemMonikerImpl_QueryInterface,
117 ItemMonikerImpl_AddRef,
118 ItemMonikerImpl_Release,
119 ItemMonikerImpl_GetClassID,
120 ItemMonikerImpl_IsDirty,
121 ItemMonikerImpl_Load,
122 ItemMonikerImpl_Save,
123 ItemMonikerImpl_GetSizeMax,
124 ItemMonikerImpl_BindToObject,
125 ItemMonikerImpl_BindToStorage,
126 ItemMonikerImpl_Reduce,
127 ItemMonikerImpl_ComposeWith,
128 ItemMonikerImpl_Enum,
129 ItemMonikerImpl_IsEqual,
130 ItemMonikerImpl_Hash,
131 ItemMonikerImpl_IsRunning,
132 ItemMonikerImpl_GetTimeOfLastChange,
133 ItemMonikerImpl_Inverse,
134 ItemMonikerImpl_CommonPrefixWith,
135 ItemMonikerImpl_RelativePathTo,
136 ItemMonikerImpl_GetDisplayName,
137 ItemMonikerImpl_ParseDisplayName,
138 ItemMonikerImpl_IsSystemMoniker
141 /********************************************************************************/
142 /* Virtual function table for the IROTData class. */
143 static ICOM_VTABLE(IROTData) VT_ROTDataImpl =
145 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
146 ItemMonikerROTDataImpl_QueryInterface,
147 ItemMonikerROTDataImpl_AddRef,
148 ItemMonikerROTDataImpl_Release,
149 ItemMonikerROTDataImpl_GetComparaisonData
152 /*******************************************************************************
153 * ItemMoniker_QueryInterface
154 *******************************************************************************/
155 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
157 ICOM_THIS(ItemMonikerImpl,iface);
159 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
161 /* Perform a sanity check on the parameters.*/
162 if ( (This==0) || (ppvObject==0) )
163 return E_INVALIDARG;
165 /* Initialize the return parameter */
166 *ppvObject = 0;
168 /* Compare the riid with the interface IDs implemented by this object.*/
169 if (IsEqualIID(&IID_IUnknown, riid) ||
170 IsEqualIID(&IID_IPersist, riid) ||
171 IsEqualIID(&IID_IPersistStream, riid) ||
172 IsEqualIID(&IID_IMoniker, riid)
174 *ppvObject = iface;
176 else if (IsEqualIID(&IID_IROTData, riid))
177 *ppvObject = (IROTData*)&(This->lpvtbl2);
179 /* Check that we obtained an interface.*/
180 if ((*ppvObject)==0)
181 return E_NOINTERFACE;
183 /* Query Interface always increases the reference count by one when it is successful */
184 ItemMonikerImpl_AddRef(iface);
186 return S_OK;
189 /******************************************************************************
190 * ItemMoniker_AddRef
191 ******************************************************************************/
192 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
194 ICOM_THIS(ItemMonikerImpl,iface);
196 TRACE("(%p)\n",This);
198 return ++(This->ref);
201 /******************************************************************************
202 * ItemMoniker_Release
203 ******************************************************************************/
204 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
206 ICOM_THIS(ItemMonikerImpl,iface);
208 TRACE("(%p)\n",This);
210 This->ref--;
212 /* destroy the object if there's no more reference on it */
213 if (This->ref==0){
215 ItemMonikerImpl_Destroy(This);
217 return 0;
219 return This->ref;
222 /******************************************************************************
223 * ItemMoniker_GetClassID
224 ******************************************************************************/
225 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
227 TRACE("(%p,%p),stub!\n",iface,pClassID);
229 if (pClassID==NULL)
230 return E_POINTER;
232 *pClassID = CLSID_ItemMoniker;
234 return S_OK;
237 /******************************************************************************
238 * ItemMoniker_IsDirty
239 ******************************************************************************/
240 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
242 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
243 method in the OLE-provided moniker interfaces always return S_FALSE because
244 their internal state never changes. */
246 TRACE("(%p)\n",iface);
248 return S_FALSE;
251 /******************************************************************************
252 * ItemMoniker_Load
253 ******************************************************************************/
254 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
257 ICOM_THIS(ItemMonikerImpl,iface);
258 HRESULT res;
259 DWORD delimiterLength,nameLength,lenW;
260 CHAR *itemNameA,*itemDelimiterA;
261 ULONG bread;
263 /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
265 /* read item delimiter string length + 1 */
266 res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
267 if (bread != sizeof(DWORD))
268 return E_FAIL;
270 /* read item delimiter string */
271 if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
272 return E_OUTOFMEMORY;
273 res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
274 if (bread != delimiterLength)
276 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
277 return E_FAIL;
280 lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
281 This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
282 if (!This->itemDelimiter)
284 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
285 return E_OUTOFMEMORY;
287 MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
288 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
290 /* read item name string length + 1*/
291 res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
292 if (bread != sizeof(DWORD))
293 return E_FAIL;
295 /* read item name string */
296 if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
297 return E_OUTOFMEMORY;
298 res=IStream_Read(pStm,itemNameA,nameLength,&bread);
299 if (bread != nameLength)
301 HeapFree( GetProcessHeap(), 0, itemNameA );
302 return E_FAIL;
305 lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
306 This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
307 if (!This->itemName)
309 HeapFree( GetProcessHeap(), 0, itemNameA );
310 return E_OUTOFMEMORY;
312 MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
313 HeapFree( GetProcessHeap(), 0, itemNameA );
315 return res;
318 /******************************************************************************
319 * ItemMoniker_Save
320 ******************************************************************************/
321 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
322 IStream* pStm,/* pointer to the stream where the object is to be saved */
323 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
325 ICOM_THIS(ItemMonikerImpl,iface);
326 HRESULT res;
327 CHAR *itemNameA,*itemDelimiterA;
329 /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
330 /* 2) String (type A): item delimiter string ('\0' included) */
331 /* 3) DWORD : size of item name string ('\0' included) */
332 /* 4) String (type A): item name string ('\0' included) */
334 DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
335 DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
336 itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
337 itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
338 WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
339 WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
341 res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
342 res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
343 res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
344 res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
346 return res;
349 /******************************************************************************
350 * ItemMoniker_GetSizeMax
351 ******************************************************************************/
352 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
353 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
355 ICOM_THIS(ItemMonikerImpl,iface);
356 DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
357 DWORD nameLength=lstrlenW(This->itemName)+1;
359 TRACE("(%p,%p)\n",iface,pcbSize);
361 if (pcbSize!=NULL)
362 return E_POINTER;
364 /* for more details see ItemMonikerImpl_Save coments */
366 pcbSize->u.LowPart = sizeof(DWORD) + /* DWORD which contains delimiter length */
367 delimiterLength + /* item delimiter string */
368 sizeof(DWORD) + /* DWORD which contains item name length */
369 nameLength + /* item name string */
370 34; /* this constant was added ! because when I tested this function it usually */
371 /* returns 34 bytes more than the number of bytes used by IMoniker::Save function */
372 pcbSize->u.HighPart=0;
374 return S_OK;
377 /******************************************************************************
378 * ItemMoniker_Construct (local function)
379 *******************************************************************************/
380 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
383 int sizeStr1=lstrlenW(lpszItem), sizeStr2;
384 static const OLECHAR emptystr[1];
385 LPCOLESTR delim;
387 TRACE("(%p,%p)\n",This,lpszItem);
389 /* Initialize the virtual fgunction table. */
390 This->lpvtbl1 = &VT_ItemMonikerImpl;
391 This->lpvtbl2 = &VT_ROTDataImpl;
392 This->ref = 0;
394 This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
395 if (!This->itemName)
396 return E_OUTOFMEMORY;
397 lstrcpyW(This->itemName,lpszItem);
399 if (!lpszDelim)
400 FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
402 delim = lpszDelim ? lpszDelim : emptystr;
404 sizeStr2=lstrlenW(delim);
405 This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
406 if (!This->itemDelimiter) {
407 HeapFree(GetProcessHeap(),0,This->itemName);
408 return E_OUTOFMEMORY;
410 lstrcpyW(This->itemDelimiter,delim);
411 return S_OK;
414 /******************************************************************************
415 * ItemMoniker_Destroy (local function)
416 *******************************************************************************/
417 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
419 TRACE("(%p)\n",This);
421 if (This->itemName)
422 HeapFree(GetProcessHeap(),0,This->itemName);
424 if (This->itemDelimiter)
425 HeapFree(GetProcessHeap(),0,This->itemDelimiter);
427 HeapFree(GetProcessHeap(),0,This);
429 return S_OK;
432 /******************************************************************************
433 * ItemMoniker_BindToObject
434 ******************************************************************************/
435 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
436 IBindCtx* pbc,
437 IMoniker* pmkToLeft,
438 REFIID riid,
439 VOID** ppvResult)
441 ICOM_THIS(ItemMonikerImpl,iface);
443 HRESULT res;
444 IID refid=IID_IOleItemContainer;
445 IOleItemContainer *poic=0;
447 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
449 if(ppvResult ==NULL)
450 return E_POINTER;
452 if(pmkToLeft==NULL)
453 return E_INVALIDARG;
455 *ppvResult=0;
457 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)&poic);
459 if (SUCCEEDED(res)){
461 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
463 IOleItemContainer_Release(poic);
466 return res;
469 /******************************************************************************
470 * ItemMoniker_BindToStorage
471 ******************************************************************************/
472 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
473 IBindCtx* pbc,
474 IMoniker* pmkToLeft,
475 REFIID riid,
476 VOID** ppvResult)
478 ICOM_THIS(ItemMonikerImpl,iface);
480 HRESULT res;
481 IOleItemContainer *poic=0;
483 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
485 *ppvResult=0;
487 if(pmkToLeft==NULL)
488 return E_INVALIDARG;
490 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
492 if (SUCCEEDED(res)){
494 res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
496 IOleItemContainer_Release(poic);
499 return res;
502 /******************************************************************************
503 * ItemMoniker_Reduce
504 ******************************************************************************/
505 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
506 IBindCtx* pbc,
507 DWORD dwReduceHowFar,
508 IMoniker** ppmkToLeft,
509 IMoniker** ppmkReduced)
511 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
513 if (ppmkReduced==NULL)
514 return E_POINTER;
516 ItemMonikerImpl_AddRef(iface);
518 *ppmkReduced=iface;
520 return MK_S_REDUCED_TO_SELF;
522 /******************************************************************************
523 * ItemMoniker_ComposeWith
524 ******************************************************************************/
525 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
526 IMoniker* pmkRight,
527 BOOL fOnlyIfNotGeneric,
528 IMoniker** ppmkComposite)
530 HRESULT res=S_OK;
531 DWORD mkSys,mkSys2;
532 IEnumMoniker* penumMk=0;
533 IMoniker *pmostLeftMk=0;
534 IMoniker* tempMkComposite=0;
536 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
538 if ((ppmkComposite==NULL)||(pmkRight==NULL))
539 return E_POINTER;
541 *ppmkComposite=0;
543 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
545 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
546 if(mkSys==MKSYS_ANTIMONIKER)
547 return res;
549 else
550 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
551 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
553 if(mkSys==MKSYS_GENERICCOMPOSITE){
555 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
557 if (FAILED(res))
558 return res;
560 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
562 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
564 if(mkSys2==MKSYS_ANTIMONIKER){
566 IMoniker_Release(pmostLeftMk);
568 tempMkComposite=iface;
569 IMoniker_AddRef(iface);
571 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
573 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
575 IMoniker_Release(tempMkComposite);
576 IMoniker_Release(pmostLeftMk);
578 tempMkComposite=*ppmkComposite;
579 IMoniker_AddRef(tempMkComposite);
581 return res;
583 else
584 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
586 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
587 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
588 a NULL moniker and a return value of MK_E_NEEDGENERIC */
589 else
590 if (!fOnlyIfNotGeneric)
591 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
593 else
594 return MK_E_NEEDGENERIC;
597 /******************************************************************************
598 * ItemMoniker_Enum
599 ******************************************************************************/
600 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
602 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
604 if (ppenumMoniker == NULL)
605 return E_POINTER;
607 *ppenumMoniker = NULL;
609 return S_OK;
612 /******************************************************************************
613 * ItemMoniker_IsEqual
614 ******************************************************************************/
615 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
618 CLSID clsid;
619 LPOLESTR dispName1,dispName2;
620 IBindCtx* bind;
621 HRESULT res = S_FALSE;
623 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
625 if (!pmkOtherMoniker) return S_FALSE;
628 /* check if both are ItemMoniker */
629 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
630 if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
632 /* check if both displaynames are the same */
633 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
634 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
635 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
636 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
637 CoTaskMemFree(dispName2);
639 CoTaskMemFree(dispName1);
642 return res;
645 /******************************************************************************
646 * ItemMoniker_Hash
647 ******************************************************************************/
648 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
650 ICOM_THIS(ItemMonikerImpl,iface);
652 int h = 0,i,skip,len;
653 int off = 0;
654 LPOLESTR val;
656 if (pdwHash==NULL)
657 return E_POINTER;
659 val = This->itemName;
660 len = lstrlenW(val);
662 if (len < 16) {
663 for (i = len ; i > 0; i--) {
664 h = (h * 37) + val[off++];
666 } else {
667 /* only sample some characters */
668 skip = len / 8;
669 for (i = len ; i > 0; i -= skip, off += skip) {
670 h = (h * 39) + val[off];
674 *pdwHash=h;
676 return S_OK;
679 /******************************************************************************
680 * ItemMoniker_IsRunning
681 ******************************************************************************/
682 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
683 IBindCtx* pbc,
684 IMoniker* pmkToLeft,
685 IMoniker* pmkNewlyRunning)
687 IRunningObjectTable* rot;
688 HRESULT res;
689 IOleItemContainer *poic=0;
690 ICOM_THIS(ItemMonikerImpl,iface);
692 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
694 /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
695 /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running. */
696 if (pmkToLeft==NULL)
697 if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
698 return S_OK;
699 else {
700 if (pbc==NULL)
701 return E_POINTER;
703 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
705 if (FAILED(res))
706 return res;
708 res = IRunningObjectTable_IsRunning(rot,iface);
710 IRunningObjectTable_Release(rot);
712 else{
714 /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter, */
715 /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
716 /* passing the string contained within this moniker. */
718 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
720 if (SUCCEEDED(res)){
722 res=IOleItemContainer_IsRunning(poic,This->itemName);
724 IOleItemContainer_Release(poic);
728 return res;
731 /******************************************************************************
732 * ItemMoniker_GetTimeOfLastChange
733 ******************************************************************************/
734 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
735 IBindCtx* pbc,
736 IMoniker* pmkToLeft,
737 FILETIME* pItemTime)
739 IRunningObjectTable* rot;
740 HRESULT res;
741 IMoniker *compositeMk;
743 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
745 if (pItemTime==NULL)
746 return E_INVALIDARG;
748 /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
749 if (pmkToLeft==NULL)
751 return MK_E_NOTBINDABLE;
752 else {
754 /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access */
755 /* the time of last change. If the object is not in the ROT, the method calls */
756 /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter. */
758 res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
760 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
762 if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
764 res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
766 IMoniker_Release(compositeMk);
769 return res;
772 /******************************************************************************
773 * ItemMoniker_Inverse
774 ******************************************************************************/
775 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
777 TRACE("(%p,%p)\n",iface,ppmk);
779 if (ppmk==NULL)
780 return E_POINTER;
782 return CreateAntiMoniker(ppmk);
785 /******************************************************************************
786 * ItemMoniker_CommonPrefixWith
787 ******************************************************************************/
788 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
790 DWORD mkSys;
791 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
792 /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
793 /* to this moniker and returns MK_S_US */
795 if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
797 *ppmkPrefix=iface;
799 IMoniker_AddRef(iface);
801 return MK_S_US;
803 else
804 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
805 /* the case where the other moniker is a generic composite. */
806 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
809 /******************************************************************************
810 * ItemMoniker_RelativePathTo
811 ******************************************************************************/
812 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
814 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
816 if (ppmkRelPath==NULL)
817 return E_POINTER;
819 *ppmkRelPath=0;
821 return MK_E_NOTBINDABLE;
824 /******************************************************************************
825 * ItemMoniker_GetDisplayName
826 ******************************************************************************/
827 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
828 IBindCtx* pbc,
829 IMoniker* pmkToLeft,
830 LPOLESTR *ppszDisplayName)
832 ICOM_THIS(ItemMonikerImpl,iface);
834 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
836 if (ppszDisplayName==NULL)
837 return E_POINTER;
839 if (pmkToLeft!=NULL){
840 return E_INVALIDARG;
843 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
845 if (*ppszDisplayName==NULL)
846 return E_OUTOFMEMORY;
848 lstrcpyW(*ppszDisplayName,This->itemDelimiter);
849 lstrcatW(*ppszDisplayName,This->itemName);
851 return S_OK;
854 /******************************************************************************
855 * ItemMoniker_ParseDisplayName
856 ******************************************************************************/
857 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
858 IBindCtx* pbc,
859 IMoniker* pmkToLeft,
860 LPOLESTR pszDisplayName,
861 ULONG* pchEaten,
862 IMoniker** ppmkOut)
864 IOleItemContainer* poic=0;
865 IParseDisplayName* ppdn=0;
866 LPOLESTR displayName;
867 HRESULT res;
868 ICOM_THIS(ItemMonikerImpl,iface);
870 /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
871 if (pmkToLeft==NULL)
873 return MK_E_SYNTAX;
875 else{
876 /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
877 /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
878 /* name to IParseDisplayName::ParseDisplayName */
879 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
881 if (SUCCEEDED(res)){
883 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
885 res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
887 res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
889 IOleItemContainer_Release(poic);
890 IParseDisplayName_Release(ppdn);
893 return res;
896 /******************************************************************************
897 * ItemMoniker_IsSystemMoniker
898 ******************************************************************************/
899 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
901 TRACE("(%p,%p)\n",iface,pwdMksys);
903 if (!pwdMksys)
904 return E_POINTER;
906 (*pwdMksys)=MKSYS_ITEMMONIKER;
908 return S_OK;
911 /*******************************************************************************
912 * ItemMonikerIROTData_QueryInterface
913 *******************************************************************************/
914 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
917 ICOM_THIS_From_IROTData(IMoniker, iface);
919 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
921 return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
924 /***********************************************************************
925 * ItemMonikerIROTData_AddRef
927 ULONG WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
929 ICOM_THIS_From_IROTData(IMoniker, iface);
931 TRACE("(%p)\n",iface);
933 return ItemMonikerImpl_AddRef(This);
936 /***********************************************************************
937 * ItemMonikerIROTData_Release
939 ULONG WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
941 ICOM_THIS_From_IROTData(IMoniker, iface);
943 TRACE("(%p)\n",iface);
945 return ItemMonikerImpl_Release(This);
948 /******************************************************************************
949 * ItemMonikerIROTData_GetComparaisonData
950 ******************************************************************************/
951 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
952 BYTE* pbData,
953 ULONG cbMax,
954 ULONG* pcbData)
956 FIXME("(),stub!\n");
957 return E_NOTIMPL;
960 /******************************************************************************
961 * CreateItemMoniker [OLE32.@]
962 ******************************************************************************/
963 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR lpszItem, LPMONIKER * ppmk)
965 ItemMonikerImpl* newItemMoniker = 0;
966 HRESULT hr = S_OK;
967 IID riid=IID_IMoniker;
969 TRACE("(%p,%p,%p)\n",lpszDelim,lpszItem,ppmk);
971 newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
973 if (newItemMoniker == 0)
974 return STG_E_INSUFFICIENTMEMORY;
976 hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
978 if (FAILED(hr)){
980 HeapFree(GetProcessHeap(),0,newItemMoniker);
981 return hr;
984 return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&riid,(void**)ppmk);