Ref count increment/decrement cleanup.
[wine/multimedia.git] / dlls / ole32 / itemmoniker.c
blob79d81b244143b436fbdee9109cb2eb54d7379270
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 IMonikerVtbl* 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 IROTDataVtbl* 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 IMonikerVtbl VT_ItemMonikerImpl =
115 ItemMonikerImpl_QueryInterface,
116 ItemMonikerImpl_AddRef,
117 ItemMonikerImpl_Release,
118 ItemMonikerImpl_GetClassID,
119 ItemMonikerImpl_IsDirty,
120 ItemMonikerImpl_Load,
121 ItemMonikerImpl_Save,
122 ItemMonikerImpl_GetSizeMax,
123 ItemMonikerImpl_BindToObject,
124 ItemMonikerImpl_BindToStorage,
125 ItemMonikerImpl_Reduce,
126 ItemMonikerImpl_ComposeWith,
127 ItemMonikerImpl_Enum,
128 ItemMonikerImpl_IsEqual,
129 ItemMonikerImpl_Hash,
130 ItemMonikerImpl_IsRunning,
131 ItemMonikerImpl_GetTimeOfLastChange,
132 ItemMonikerImpl_Inverse,
133 ItemMonikerImpl_CommonPrefixWith,
134 ItemMonikerImpl_RelativePathTo,
135 ItemMonikerImpl_GetDisplayName,
136 ItemMonikerImpl_ParseDisplayName,
137 ItemMonikerImpl_IsSystemMoniker
140 /********************************************************************************/
141 /* Virtual function table for the IROTData class. */
142 static IROTDataVtbl VT_ROTDataImpl =
144 ItemMonikerROTDataImpl_QueryInterface,
145 ItemMonikerROTDataImpl_AddRef,
146 ItemMonikerROTDataImpl_Release,
147 ItemMonikerROTDataImpl_GetComparaisonData
150 /*******************************************************************************
151 * ItemMoniker_QueryInterface
152 *******************************************************************************/
153 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
155 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
157 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
159 /* Perform a sanity check on the parameters.*/
160 if ( (This==0) || (ppvObject==0) )
161 return E_INVALIDARG;
163 /* Initialize the return parameter */
164 *ppvObject = 0;
166 /* Compare the riid with the interface IDs implemented by this object.*/
167 if (IsEqualIID(&IID_IUnknown, riid) ||
168 IsEqualIID(&IID_IPersist, riid) ||
169 IsEqualIID(&IID_IPersistStream, riid) ||
170 IsEqualIID(&IID_IMoniker, riid)
172 *ppvObject = iface;
174 else if (IsEqualIID(&IID_IROTData, riid))
175 *ppvObject = (IROTData*)&(This->lpvtbl2);
177 /* Check that we obtained an interface.*/
178 if ((*ppvObject)==0)
179 return E_NOINTERFACE;
181 /* Query Interface always increases the reference count by one when it is successful */
182 ItemMonikerImpl_AddRef(iface);
184 return S_OK;
187 /******************************************************************************
188 * ItemMoniker_AddRef
189 ******************************************************************************/
190 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
192 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
194 TRACE("(%p)\n",This);
196 return InterlockedIncrement(&This->ref);
199 /******************************************************************************
200 * ItemMoniker_Release
201 ******************************************************************************/
202 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
204 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
205 ULONG ref;
207 TRACE("(%p)\n",This);
209 ref = InterlockedDecrement(&This->ref);
211 /* destroy the object if there's no more reference on it */
212 if (ref == 0) ItemMonikerImpl_Destroy(This);
214 return ref;
217 /******************************************************************************
218 * ItemMoniker_GetClassID
219 ******************************************************************************/
220 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
222 TRACE("(%p,%p),stub!\n",iface,pClassID);
224 if (pClassID==NULL)
225 return E_POINTER;
227 *pClassID = CLSID_ItemMoniker;
229 return S_OK;
232 /******************************************************************************
233 * ItemMoniker_IsDirty
234 ******************************************************************************/
235 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
237 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
238 method in the OLE-provided moniker interfaces always return S_FALSE because
239 their internal state never changes. */
241 TRACE("(%p)\n",iface);
243 return S_FALSE;
246 /******************************************************************************
247 * ItemMoniker_Load
248 ******************************************************************************/
249 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
252 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
253 HRESULT res;
254 DWORD delimiterLength,nameLength,lenW;
255 CHAR *itemNameA,*itemDelimiterA;
256 ULONG bread;
258 /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
260 /* read item delimiter string length + 1 */
261 res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
262 if (bread != sizeof(DWORD))
263 return E_FAIL;
265 /* read item delimiter string */
266 if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
267 return E_OUTOFMEMORY;
268 res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
269 if (bread != delimiterLength)
271 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
272 return E_FAIL;
275 lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
276 This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
277 if (!This->itemDelimiter)
279 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
280 return E_OUTOFMEMORY;
282 MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
283 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
285 /* read item name string length + 1*/
286 res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
287 if (bread != sizeof(DWORD))
288 return E_FAIL;
290 /* read item name string */
291 if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
292 return E_OUTOFMEMORY;
293 res=IStream_Read(pStm,itemNameA,nameLength,&bread);
294 if (bread != nameLength)
296 HeapFree( GetProcessHeap(), 0, itemNameA );
297 return E_FAIL;
300 lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
301 This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
302 if (!This->itemName)
304 HeapFree( GetProcessHeap(), 0, itemNameA );
305 return E_OUTOFMEMORY;
307 MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
308 HeapFree( GetProcessHeap(), 0, itemNameA );
310 return res;
313 /******************************************************************************
314 * ItemMoniker_Save
315 ******************************************************************************/
316 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
317 IStream* pStm,/* pointer to the stream where the object is to be saved */
318 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
320 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
321 HRESULT res;
322 CHAR *itemNameA,*itemDelimiterA;
324 /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
325 /* 2) String (type A): item delimiter string ('\0' included) */
326 /* 3) DWORD : size of item name string ('\0' included) */
327 /* 4) String (type A): item name string ('\0' included) */
329 DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
330 DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
331 itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
332 itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
333 WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
334 WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
336 res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
337 res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
338 res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
339 res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
341 return res;
344 /******************************************************************************
345 * ItemMoniker_GetSizeMax
346 ******************************************************************************/
347 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
348 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
350 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
351 DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
352 DWORD nameLength=lstrlenW(This->itemName)+1;
354 TRACE("(%p,%p)\n",iface,pcbSize);
356 if (pcbSize!=NULL)
357 return E_POINTER;
359 /* for more details see ItemMonikerImpl_Save coments */
361 pcbSize->u.LowPart = sizeof(DWORD) + /* DWORD which contains delimiter length */
362 delimiterLength + /* item delimiter string */
363 sizeof(DWORD) + /* DWORD which contains item name length */
364 nameLength + /* item name string */
365 34; /* this constant was added ! because when I tested this function it usually */
366 /* returns 34 bytes more than the number of bytes used by IMoniker::Save function */
367 pcbSize->u.HighPart=0;
369 return S_OK;
372 /******************************************************************************
373 * ItemMoniker_Construct (local function)
374 *******************************************************************************/
375 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
378 int sizeStr1=lstrlenW(lpszItem), sizeStr2;
379 static const OLECHAR emptystr[1];
380 LPCOLESTR delim;
382 TRACE("(%p,%p)\n",This,lpszItem);
384 /* Initialize the virtual fgunction table. */
385 This->lpvtbl1 = &VT_ItemMonikerImpl;
386 This->lpvtbl2 = &VT_ROTDataImpl;
387 This->ref = 0;
389 This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
390 if (!This->itemName)
391 return E_OUTOFMEMORY;
392 lstrcpyW(This->itemName,lpszItem);
394 if (!lpszDelim)
395 FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
397 delim = lpszDelim ? lpszDelim : emptystr;
399 sizeStr2=lstrlenW(delim);
400 This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
401 if (!This->itemDelimiter) {
402 HeapFree(GetProcessHeap(),0,This->itemName);
403 return E_OUTOFMEMORY;
405 lstrcpyW(This->itemDelimiter,delim);
406 return S_OK;
409 /******************************************************************************
410 * ItemMoniker_Destroy (local function)
411 *******************************************************************************/
412 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
414 TRACE("(%p)\n",This);
416 if (This->itemName)
417 HeapFree(GetProcessHeap(),0,This->itemName);
419 if (This->itemDelimiter)
420 HeapFree(GetProcessHeap(),0,This->itemDelimiter);
422 HeapFree(GetProcessHeap(),0,This);
424 return S_OK;
427 /******************************************************************************
428 * ItemMoniker_BindToObject
429 ******************************************************************************/
430 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
431 IBindCtx* pbc,
432 IMoniker* pmkToLeft,
433 REFIID riid,
434 VOID** ppvResult)
436 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
438 HRESULT res;
439 IID refid=IID_IOleItemContainer;
440 IOleItemContainer *poic=0;
442 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
444 if(ppvResult ==NULL)
445 return E_POINTER;
447 if(pmkToLeft==NULL)
448 return E_INVALIDARG;
450 *ppvResult=0;
452 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)&poic);
454 if (SUCCEEDED(res)){
456 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
458 IOleItemContainer_Release(poic);
461 return res;
464 /******************************************************************************
465 * ItemMoniker_BindToStorage
466 ******************************************************************************/
467 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
468 IBindCtx* pbc,
469 IMoniker* pmkToLeft,
470 REFIID riid,
471 VOID** ppvResult)
473 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
475 HRESULT res;
476 IOleItemContainer *poic=0;
478 TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
480 *ppvResult=0;
482 if(pmkToLeft==NULL)
483 return E_INVALIDARG;
485 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
487 if (SUCCEEDED(res)){
489 res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
491 IOleItemContainer_Release(poic);
494 return res;
497 /******************************************************************************
498 * ItemMoniker_Reduce
499 ******************************************************************************/
500 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
501 IBindCtx* pbc,
502 DWORD dwReduceHowFar,
503 IMoniker** ppmkToLeft,
504 IMoniker** ppmkReduced)
506 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
508 if (ppmkReduced==NULL)
509 return E_POINTER;
511 ItemMonikerImpl_AddRef(iface);
513 *ppmkReduced=iface;
515 return MK_S_REDUCED_TO_SELF;
517 /******************************************************************************
518 * ItemMoniker_ComposeWith
519 ******************************************************************************/
520 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
521 IMoniker* pmkRight,
522 BOOL fOnlyIfNotGeneric,
523 IMoniker** ppmkComposite)
525 HRESULT res=S_OK;
526 DWORD mkSys,mkSys2;
527 IEnumMoniker* penumMk=0;
528 IMoniker *pmostLeftMk=0;
529 IMoniker* tempMkComposite=0;
531 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
533 if ((ppmkComposite==NULL)||(pmkRight==NULL))
534 return E_POINTER;
536 *ppmkComposite=0;
538 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
540 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
541 if(mkSys==MKSYS_ANTIMONIKER)
542 return res;
544 else
545 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
546 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
548 if(mkSys==MKSYS_GENERICCOMPOSITE){
550 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
552 if (FAILED(res))
553 return res;
555 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
557 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
559 if(mkSys2==MKSYS_ANTIMONIKER){
561 IMoniker_Release(pmostLeftMk);
563 tempMkComposite=iface;
564 IMoniker_AddRef(iface);
566 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
568 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
570 IMoniker_Release(tempMkComposite);
571 IMoniker_Release(pmostLeftMk);
573 tempMkComposite=*ppmkComposite;
574 IMoniker_AddRef(tempMkComposite);
576 return res;
578 else
579 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
581 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
582 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
583 a NULL moniker and a return value of MK_E_NEEDGENERIC */
584 else
585 if (!fOnlyIfNotGeneric)
586 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
588 else
589 return MK_E_NEEDGENERIC;
592 /******************************************************************************
593 * ItemMoniker_Enum
594 ******************************************************************************/
595 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
597 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
599 if (ppenumMoniker == NULL)
600 return E_POINTER;
602 *ppenumMoniker = NULL;
604 return S_OK;
607 /******************************************************************************
608 * ItemMoniker_IsEqual
609 ******************************************************************************/
610 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
613 CLSID clsid;
614 LPOLESTR dispName1,dispName2;
615 IBindCtx* bind;
616 HRESULT res = S_FALSE;
618 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
620 if (!pmkOtherMoniker) return S_FALSE;
623 /* check if both are ItemMoniker */
624 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
625 if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
627 /* check if both displaynames are the same */
628 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
629 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
630 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
631 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
632 CoTaskMemFree(dispName2);
634 CoTaskMemFree(dispName1);
637 return res;
640 /******************************************************************************
641 * ItemMoniker_Hash
642 ******************************************************************************/
643 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
645 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
647 int h = 0,i,skip,len;
648 int off = 0;
649 LPOLESTR val;
651 if (pdwHash==NULL)
652 return E_POINTER;
654 val = This->itemName;
655 len = lstrlenW(val);
657 if (len < 16) {
658 for (i = len ; i > 0; i--) {
659 h = (h * 37) + val[off++];
661 } else {
662 /* only sample some characters */
663 skip = len / 8;
664 for (i = len ; i > 0; i -= skip, off += skip) {
665 h = (h * 39) + val[off];
669 *pdwHash=h;
671 return S_OK;
674 /******************************************************************************
675 * ItemMoniker_IsRunning
676 ******************************************************************************/
677 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
678 IBindCtx* pbc,
679 IMoniker* pmkToLeft,
680 IMoniker* pmkNewlyRunning)
682 IRunningObjectTable* rot;
683 HRESULT res;
684 IOleItemContainer *poic=0;
685 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
687 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
689 /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
690 /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running. */
691 if (pmkToLeft==NULL)
692 if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
693 return S_OK;
694 else {
695 if (pbc==NULL)
696 return E_POINTER;
698 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
700 if (FAILED(res))
701 return res;
703 res = IRunningObjectTable_IsRunning(rot,iface);
705 IRunningObjectTable_Release(rot);
707 else{
709 /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter, */
710 /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
711 /* passing the string contained within this moniker. */
713 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
715 if (SUCCEEDED(res)){
717 res=IOleItemContainer_IsRunning(poic,This->itemName);
719 IOleItemContainer_Release(poic);
723 return res;
726 /******************************************************************************
727 * ItemMoniker_GetTimeOfLastChange
728 ******************************************************************************/
729 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
730 IBindCtx* pbc,
731 IMoniker* pmkToLeft,
732 FILETIME* pItemTime)
734 IRunningObjectTable* rot;
735 HRESULT res;
736 IMoniker *compositeMk;
738 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
740 if (pItemTime==NULL)
741 return E_INVALIDARG;
743 /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
744 if (pmkToLeft==NULL)
746 return MK_E_NOTBINDABLE;
747 else {
749 /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access */
750 /* the time of last change. If the object is not in the ROT, the method calls */
751 /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter. */
753 res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
755 res=IBindCtx_GetRunningObjectTable(pbc,&rot);
757 if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
759 res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
761 IMoniker_Release(compositeMk);
764 return res;
767 /******************************************************************************
768 * ItemMoniker_Inverse
769 ******************************************************************************/
770 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
772 TRACE("(%p,%p)\n",iface,ppmk);
774 if (ppmk==NULL)
775 return E_POINTER;
777 return CreateAntiMoniker(ppmk);
780 /******************************************************************************
781 * ItemMoniker_CommonPrefixWith
782 ******************************************************************************/
783 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
785 DWORD mkSys;
786 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
787 /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
788 /* to this moniker and returns MK_S_US */
790 if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
792 *ppmkPrefix=iface;
794 IMoniker_AddRef(iface);
796 return MK_S_US;
798 else
799 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
800 /* the case where the other moniker is a generic composite. */
801 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
804 /******************************************************************************
805 * ItemMoniker_RelativePathTo
806 ******************************************************************************/
807 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
809 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
811 if (ppmkRelPath==NULL)
812 return E_POINTER;
814 *ppmkRelPath=0;
816 return MK_E_NOTBINDABLE;
819 /******************************************************************************
820 * ItemMoniker_GetDisplayName
821 ******************************************************************************/
822 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
823 IBindCtx* pbc,
824 IMoniker* pmkToLeft,
825 LPOLESTR *ppszDisplayName)
827 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
829 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
831 if (ppszDisplayName==NULL)
832 return E_POINTER;
834 if (pmkToLeft!=NULL){
835 return E_INVALIDARG;
838 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
840 if (*ppszDisplayName==NULL)
841 return E_OUTOFMEMORY;
843 lstrcpyW(*ppszDisplayName,This->itemDelimiter);
844 lstrcatW(*ppszDisplayName,This->itemName);
846 return S_OK;
849 /******************************************************************************
850 * ItemMoniker_ParseDisplayName
851 ******************************************************************************/
852 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
853 IBindCtx* pbc,
854 IMoniker* pmkToLeft,
855 LPOLESTR pszDisplayName,
856 ULONG* pchEaten,
857 IMoniker** ppmkOut)
859 IOleItemContainer* poic=0;
860 IParseDisplayName* ppdn=0;
861 LPOLESTR displayName;
862 HRESULT res;
863 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
865 /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
866 if (pmkToLeft==NULL)
868 return MK_E_SYNTAX;
870 else{
871 /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
872 /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
873 /* name to IParseDisplayName::ParseDisplayName */
874 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
876 if (SUCCEEDED(res)){
878 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
880 res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
882 res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
884 IOleItemContainer_Release(poic);
885 IParseDisplayName_Release(ppdn);
888 return res;
891 /******************************************************************************
892 * ItemMoniker_IsSystemMoniker
893 ******************************************************************************/
894 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
896 TRACE("(%p,%p)\n",iface,pwdMksys);
898 if (!pwdMksys)
899 return E_POINTER;
901 (*pwdMksys)=MKSYS_ITEMMONIKER;
903 return S_OK;
906 /*******************************************************************************
907 * ItemMonikerIROTData_QueryInterface
908 *******************************************************************************/
909 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
912 ICOM_THIS_From_IROTData(IMoniker, iface);
914 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
916 return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
919 /***********************************************************************
920 * ItemMonikerIROTData_AddRef
922 ULONG WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
924 ICOM_THIS_From_IROTData(IMoniker, iface);
926 TRACE("(%p)\n",iface);
928 return ItemMonikerImpl_AddRef(This);
931 /***********************************************************************
932 * ItemMonikerIROTData_Release
934 ULONG WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
936 ICOM_THIS_From_IROTData(IMoniker, iface);
938 TRACE("(%p)\n",iface);
940 return ItemMonikerImpl_Release(This);
943 /******************************************************************************
944 * ItemMonikerIROTData_GetComparaisonData
945 ******************************************************************************/
946 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparaisonData(IROTData* iface,
947 BYTE* pbData,
948 ULONG cbMax,
949 ULONG* pcbData)
951 FIXME("(),stub!\n");
952 return E_NOTIMPL;
955 /******************************************************************************
956 * CreateItemMoniker [OLE32.@]
957 ******************************************************************************/
958 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR lpszItem, LPMONIKER * ppmk)
960 ItemMonikerImpl* newItemMoniker = 0;
961 HRESULT hr = S_OK;
962 IID riid=IID_IMoniker;
964 TRACE("(%p,%p,%p)\n",lpszDelim,lpszItem,ppmk);
966 newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
968 if (newItemMoniker == 0)
969 return STG_E_INSUFFICIENTMEMORY;
971 hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
973 if (FAILED(hr)){
975 HeapFree(GetProcessHeap(),0,newItemMoniker);
976 return hr;
979 return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&riid,(void**)ppmk);