ole32: Declare some functions static.
[wine/wine64.git] / dlls / ole32 / itemmoniker.c
blob2a8ed22a2058ae8ee3e888cc595b551edfce9768
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
35 #include "ole2.h"
36 #include "wine/unicode.h"
37 #include "moniker.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 /* ItemMoniker data structure */
42 typedef struct ItemMonikerImpl{
44 const IMonikerVtbl* lpvtbl1; /* VTable relative to the IMoniker interface.*/
46 /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
47 * two monikers are equal. That's whay IROTData interface is implemented by monikers.
49 const IROTDataVtbl* lpvtbl2; /* VTable relative to the IROTData interface.*/
51 LONG ref; /* reference counter for this object */
53 LPOLESTR itemName; /* item name identified by this ItemMoniker */
55 LPOLESTR itemDelimiter; /* Delimiter string */
57 IUnknown *pMarshal; /* custom marshaler */
58 } ItemMonikerImpl;
60 static inline IMoniker *impl_from_IROTData( IROTData *iface )
62 return (IMoniker *)((char*)iface - FIELD_OFFSET(ItemMonikerImpl, lpvtbl2));
65 /********************************************************************************/
66 /* ItemMoniker prototype functions : */
68 /* IUnknown prototype functions */
69 static HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
70 static ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface);
71 static ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface);
73 /* IPersist prototype functions */
74 static HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
76 /* IPersistStream prototype functions */
77 static HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface);
78 static HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface, IStream* pStm);
79 static HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
80 static HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
82 /* IMoniker prototype functions */
83 static HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
84 static HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
85 static HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
86 static HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
87 static HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
88 static HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
89 static HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
90 static HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
91 static HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pItemTime);
92 static HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
93 static HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
94 static HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
95 static HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
96 static HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
97 static HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
99 /* Local function used by ItemMoniker implementation */
100 static HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* iface, LPCOLESTR lpszDelim,LPCOLESTR lpszPathName);
101 static HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* iface);
103 /********************************************************************************/
104 /* IROTData prototype functions */
106 /* IUnknown prototype functions */
107 static HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
108 static ULONG WINAPI ItemMonikerROTDataImpl_AddRef(IROTData* iface);
109 static ULONG WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface);
111 /* IROTData prototype function */
112 static HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
114 /********************************************************************************/
115 /* Virtual function table for the ItemMonikerImpl class which include IPersist,*/
116 /* IPersistStream and IMoniker functions. */
117 static const IMonikerVtbl VT_ItemMonikerImpl =
119 ItemMonikerImpl_QueryInterface,
120 ItemMonikerImpl_AddRef,
121 ItemMonikerImpl_Release,
122 ItemMonikerImpl_GetClassID,
123 ItemMonikerImpl_IsDirty,
124 ItemMonikerImpl_Load,
125 ItemMonikerImpl_Save,
126 ItemMonikerImpl_GetSizeMax,
127 ItemMonikerImpl_BindToObject,
128 ItemMonikerImpl_BindToStorage,
129 ItemMonikerImpl_Reduce,
130 ItemMonikerImpl_ComposeWith,
131 ItemMonikerImpl_Enum,
132 ItemMonikerImpl_IsEqual,
133 ItemMonikerImpl_Hash,
134 ItemMonikerImpl_IsRunning,
135 ItemMonikerImpl_GetTimeOfLastChange,
136 ItemMonikerImpl_Inverse,
137 ItemMonikerImpl_CommonPrefixWith,
138 ItemMonikerImpl_RelativePathTo,
139 ItemMonikerImpl_GetDisplayName,
140 ItemMonikerImpl_ParseDisplayName,
141 ItemMonikerImpl_IsSystemMoniker
144 /********************************************************************************/
145 /* Virtual function table for the IROTData class. */
146 static const IROTDataVtbl VT_ROTDataImpl =
148 ItemMonikerROTDataImpl_QueryInterface,
149 ItemMonikerROTDataImpl_AddRef,
150 ItemMonikerROTDataImpl_Release,
151 ItemMonikerROTDataImpl_GetComparisonData
154 /*******************************************************************************
155 * ItemMoniker_QueryInterface
156 *******************************************************************************/
157 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
159 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
161 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
163 /* Perform a sanity check on the parameters.*/
164 if ( (This==0) || (ppvObject==0) )
165 return E_INVALIDARG;
167 /* Initialize the return parameter */
168 *ppvObject = 0;
170 /* Compare the riid with the interface IDs implemented by this object.*/
171 if (IsEqualIID(&IID_IUnknown, riid) ||
172 IsEqualIID(&IID_IPersist, riid) ||
173 IsEqualIID(&IID_IPersistStream, riid) ||
174 IsEqualIID(&IID_IMoniker, riid)
176 *ppvObject = iface;
178 else if (IsEqualIID(&IID_IROTData, riid))
179 *ppvObject = (IROTData*)&(This->lpvtbl2);
180 else if (IsEqualIID(&IID_IMarshal, riid))
182 HRESULT hr = S_OK;
183 if (!This->pMarshal)
184 hr = MonikerMarshal_Create(iface, &This->pMarshal);
185 if (hr != S_OK)
186 return hr;
187 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
190 /* Check that we obtained an interface.*/
191 if ((*ppvObject)==0)
192 return E_NOINTERFACE;
194 /* Query Interface always increases the reference count by one when it is successful */
195 ItemMonikerImpl_AddRef(iface);
197 return S_OK;
200 /******************************************************************************
201 * ItemMoniker_AddRef
202 ******************************************************************************/
203 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
205 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
207 TRACE("(%p)\n",This);
209 return InterlockedIncrement(&This->ref);
212 /******************************************************************************
213 * ItemMoniker_Release
214 ******************************************************************************/
215 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
217 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
218 ULONG ref;
220 TRACE("(%p)\n",This);
222 ref = InterlockedDecrement(&This->ref);
224 /* destroy the object if there's no more reference on it */
225 if (ref == 0) ItemMonikerImpl_Destroy(This);
227 return ref;
230 /******************************************************************************
231 * ItemMoniker_GetClassID
232 ******************************************************************************/
233 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
235 TRACE("(%p,%p)\n",iface,pClassID);
237 if (pClassID==NULL)
238 return E_POINTER;
240 *pClassID = CLSID_ItemMoniker;
242 return S_OK;
245 /******************************************************************************
246 * ItemMoniker_IsDirty
247 ******************************************************************************/
248 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
250 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
251 method in the OLE-provided moniker interfaces always return S_FALSE because
252 their internal state never changes. */
254 TRACE("(%p)\n",iface);
256 return S_FALSE;
259 /******************************************************************************
260 * ItemMoniker_Load
261 ******************************************************************************/
262 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
265 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
266 HRESULT res;
267 DWORD delimiterLength,nameLength,lenW;
268 CHAR *itemNameA,*itemDelimiterA;
269 ULONG bread;
271 TRACE("\n");
273 /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
275 /* read item delimiter string length + 1 */
276 res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
277 if (bread != sizeof(DWORD))
278 return E_FAIL;
280 /* read item delimiter string */
281 if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
282 return E_OUTOFMEMORY;
283 res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
284 if (bread != delimiterLength)
286 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
287 return E_FAIL;
290 lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
291 This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
292 if (!This->itemDelimiter)
294 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
295 return E_OUTOFMEMORY;
297 MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
298 HeapFree( GetProcessHeap(), 0, itemDelimiterA );
300 /* read item name string length + 1*/
301 res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
302 if (bread != sizeof(DWORD))
303 return E_FAIL;
305 /* read item name string */
306 if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
307 return E_OUTOFMEMORY;
308 res=IStream_Read(pStm,itemNameA,nameLength,&bread);
309 if (bread != nameLength)
311 HeapFree( GetProcessHeap(), 0, itemNameA );
312 return E_FAIL;
315 lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
316 This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
317 if (!This->itemName)
319 HeapFree( GetProcessHeap(), 0, itemNameA );
320 return E_OUTOFMEMORY;
322 MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
323 HeapFree( GetProcessHeap(), 0, itemNameA );
325 return res;
328 /******************************************************************************
329 * ItemMoniker_Save
330 ******************************************************************************/
331 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
332 IStream* pStm,/* pointer to the stream where the object is to be saved */
333 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
335 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
336 HRESULT res;
337 CHAR *itemNameA,*itemDelimiterA;
339 /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
340 /* 2) String (type A): item delimiter string ('\0' included) */
341 /* 3) DWORD : size of item name string ('\0' included) */
342 /* 4) String (type A): item name string ('\0' included) */
344 DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
345 DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
346 itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
347 itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
348 WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
349 WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
351 TRACE("%p, %s\n", pStm, fClearDirty ? "TRUE" : "FALSE");
353 res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
354 res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
355 res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
356 res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
358 return res;
361 /******************************************************************************
362 * ItemMoniker_GetSizeMax
363 ******************************************************************************/
364 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
365 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
367 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
368 DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
369 DWORD nameLength=lstrlenW(This->itemName)+1;
371 TRACE("(%p,%p)\n",iface,pcbSize);
373 if (!pcbSize)
374 return E_POINTER;
376 /* for more details see ItemMonikerImpl_Save coments */
378 pcbSize->u.LowPart = sizeof(DWORD) + /* DWORD which contains delimiter length */
379 delimiterLength*4 + /* item delimiter string */
380 sizeof(DWORD) + /* DWORD which contains item name length */
381 nameLength*4 + /* item name string */
382 18; /* strange, but true */
383 pcbSize->u.HighPart=0;
385 return S_OK;
388 /******************************************************************************
389 * ItemMoniker_Construct (local function)
390 *******************************************************************************/
391 static HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
394 int sizeStr1=lstrlenW(lpszItem), sizeStr2;
395 static const OLECHAR emptystr[1];
396 LPCOLESTR delim;
398 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszDelim),debugstr_w(lpszItem));
400 /* Initialize the virtual fgunction table. */
401 This->lpvtbl1 = &VT_ItemMonikerImpl;
402 This->lpvtbl2 = &VT_ROTDataImpl;
403 This->ref = 0;
404 This->pMarshal = NULL;
406 This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
407 if (!This->itemName)
408 return E_OUTOFMEMORY;
409 lstrcpyW(This->itemName,lpszItem);
411 if (!lpszDelim)
412 FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
414 delim = lpszDelim ? lpszDelim : emptystr;
416 sizeStr2=lstrlenW(delim);
417 This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
418 if (!This->itemDelimiter) {
419 HeapFree(GetProcessHeap(),0,This->itemName);
420 return E_OUTOFMEMORY;
422 lstrcpyW(This->itemDelimiter,delim);
423 return S_OK;
426 /******************************************************************************
427 * ItemMoniker_Destroy (local function)
428 *******************************************************************************/
429 static HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
431 TRACE("(%p)\n",This);
433 if (This->pMarshal) IUnknown_Release(This->pMarshal);
434 HeapFree(GetProcessHeap(),0,This->itemName);
435 HeapFree(GetProcessHeap(),0,This->itemDelimiter);
436 HeapFree(GetProcessHeap(),0,This);
438 return S_OK;
441 /******************************************************************************
442 * ItemMoniker_BindToObject
443 ******************************************************************************/
444 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
445 IBindCtx* pbc,
446 IMoniker* pmkToLeft,
447 REFIID riid,
448 VOID** ppvResult)
450 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
452 HRESULT res;
453 IID refid=IID_IOleItemContainer;
454 IOleItemContainer *poic=0;
456 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
458 if(ppvResult ==NULL)
459 return E_POINTER;
461 if(pmkToLeft==NULL)
462 return E_INVALIDARG;
464 *ppvResult=0;
466 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)&poic);
468 if (SUCCEEDED(res)){
470 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
472 IOleItemContainer_Release(poic);
475 return res;
478 /******************************************************************************
479 * ItemMoniker_BindToStorage
480 ******************************************************************************/
481 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
482 IBindCtx* pbc,
483 IMoniker* pmkToLeft,
484 REFIID riid,
485 VOID** ppvResult)
487 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
489 HRESULT res;
490 IOleItemContainer *poic=0;
492 TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
494 *ppvResult=0;
496 if(pmkToLeft==NULL)
497 return E_INVALIDARG;
499 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
501 if (SUCCEEDED(res)){
503 res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
505 IOleItemContainer_Release(poic);
508 return res;
511 /******************************************************************************
512 * ItemMoniker_Reduce
513 ******************************************************************************/
514 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
515 IBindCtx* pbc,
516 DWORD dwReduceHowFar,
517 IMoniker** ppmkToLeft,
518 IMoniker** ppmkReduced)
520 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
522 if (ppmkReduced==NULL)
523 return E_POINTER;
525 ItemMonikerImpl_AddRef(iface);
527 *ppmkReduced=iface;
529 return MK_S_REDUCED_TO_SELF;
531 /******************************************************************************
532 * ItemMoniker_ComposeWith
533 ******************************************************************************/
534 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
535 IMoniker* pmkRight,
536 BOOL fOnlyIfNotGeneric,
537 IMoniker** ppmkComposite)
539 HRESULT res=S_OK;
540 DWORD mkSys,mkSys2;
541 IEnumMoniker* penumMk=0;
542 IMoniker *pmostLeftMk=0;
543 IMoniker* tempMkComposite=0;
545 TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
547 if ((ppmkComposite==NULL)||(pmkRight==NULL))
548 return E_POINTER;
550 *ppmkComposite=0;
552 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
554 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
555 if(mkSys==MKSYS_ANTIMONIKER)
556 return res;
558 else
559 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
560 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
562 if(mkSys==MKSYS_GENERICCOMPOSITE){
564 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
566 if (FAILED(res))
567 return res;
569 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
571 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
573 if(mkSys2==MKSYS_ANTIMONIKER){
575 IMoniker_Release(pmostLeftMk);
577 tempMkComposite=iface;
578 IMoniker_AddRef(iface);
580 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
582 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
584 IMoniker_Release(tempMkComposite);
585 IMoniker_Release(pmostLeftMk);
587 tempMkComposite=*ppmkComposite;
588 IMoniker_AddRef(tempMkComposite);
590 return res;
592 else
593 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
595 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
596 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
597 a NULL moniker and a return value of MK_E_NEEDGENERIC */
598 else
599 if (!fOnlyIfNotGeneric)
600 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
602 else
603 return MK_E_NEEDGENERIC;
606 /******************************************************************************
607 * ItemMoniker_Enum
608 ******************************************************************************/
609 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
611 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
613 if (ppenumMoniker == NULL)
614 return E_POINTER;
616 *ppenumMoniker = NULL;
618 return S_OK;
621 /******************************************************************************
622 * ItemMoniker_IsEqual
623 ******************************************************************************/
624 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
627 CLSID clsid;
628 LPOLESTR dispName1,dispName2;
629 IBindCtx* bind;
630 HRESULT res = S_FALSE;
632 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
634 if (!pmkOtherMoniker) return S_FALSE;
637 /* check if both are ItemMoniker */
638 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
639 if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
641 /* check if both displaynames are the same */
642 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
643 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
644 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
645 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
646 CoTaskMemFree(dispName2);
648 CoTaskMemFree(dispName1);
651 return res;
654 /******************************************************************************
655 * ItemMoniker_Hash
656 ******************************************************************************/
657 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
659 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
660 DWORD h = 0;
661 int i,len;
662 int off = 0;
663 LPOLESTR val;
665 if (pdwHash==NULL)
666 return E_POINTER;
668 val = This->itemName;
669 len = lstrlenW(val);
671 for (i = len ; i > 0; i--)
672 h = (h * 3) ^ toupperW(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 ItemMonikerImpl *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;
792 TRACE("(%p,%p)\n", pmkOther, ppmkPrefix);
794 IMoniker_IsSystemMoniker(pmkOther,&mkSys);
795 /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
796 /* to this moniker and returns MK_S_US */
798 if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
800 *ppmkPrefix=iface;
802 IMoniker_AddRef(iface);
804 return MK_S_US;
806 else
807 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
808 /* the case where the other moniker is a generic composite. */
809 return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
812 /******************************************************************************
813 * ItemMoniker_RelativePathTo
814 ******************************************************************************/
815 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
817 TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
819 if (ppmkRelPath==NULL)
820 return E_POINTER;
822 *ppmkRelPath=0;
824 return MK_E_NOTBINDABLE;
827 /******************************************************************************
828 * ItemMoniker_GetDisplayName
829 ******************************************************************************/
830 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
831 IBindCtx* pbc,
832 IMoniker* pmkToLeft,
833 LPOLESTR *ppszDisplayName)
835 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
837 TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
839 if (ppszDisplayName==NULL)
840 return E_POINTER;
842 if (pmkToLeft!=NULL){
843 return E_INVALIDARG;
846 *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
848 if (*ppszDisplayName==NULL)
849 return E_OUTOFMEMORY;
851 lstrcpyW(*ppszDisplayName,This->itemDelimiter);
852 lstrcatW(*ppszDisplayName,This->itemName);
854 TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
856 return S_OK;
859 /******************************************************************************
860 * ItemMoniker_ParseDisplayName
861 ******************************************************************************/
862 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
863 IBindCtx* pbc,
864 IMoniker* pmkToLeft,
865 LPOLESTR pszDisplayName,
866 ULONG* pchEaten,
867 IMoniker** ppmkOut)
869 IOleItemContainer* poic=0;
870 IParseDisplayName* ppdn=0;
871 LPOLESTR displayName;
872 HRESULT res;
873 ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
875 TRACE("%s\n", debugstr_w(pszDisplayName));
877 /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
878 if (pmkToLeft==NULL)
880 return MK_E_SYNTAX;
882 else{
883 /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
884 /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
885 /* name to IParseDisplayName::ParseDisplayName */
886 res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
888 if (SUCCEEDED(res)){
890 res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
892 res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
894 res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
896 IOleItemContainer_Release(poic);
897 IParseDisplayName_Release(ppdn);
900 return res;
903 /******************************************************************************
904 * ItemMoniker_IsSystemMoniker
905 ******************************************************************************/
906 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
908 TRACE("(%p,%p)\n",iface,pwdMksys);
910 if (!pwdMksys)
911 return E_POINTER;
913 (*pwdMksys)=MKSYS_ITEMMONIKER;
915 return S_OK;
918 /*******************************************************************************
919 * ItemMonikerIROTData_QueryInterface
920 *******************************************************************************/
921 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
924 IMoniker *This = impl_from_IROTData(iface);
926 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
928 return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
931 /***********************************************************************
932 * ItemMonikerIROTData_AddRef
934 ULONG WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
936 IMoniker *This = impl_from_IROTData(iface);
938 TRACE("(%p)\n",iface);
940 return ItemMonikerImpl_AddRef(This);
943 /***********************************************************************
944 * ItemMonikerIROTData_Release
946 ULONG WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
948 IMoniker *This = impl_from_IROTData(iface);
950 TRACE("(%p)\n",iface);
952 return ItemMonikerImpl_Release(This);
955 /******************************************************************************
956 * ItemMonikerIROTData_GetComparaisonData
957 ******************************************************************************/
958 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,
959 BYTE* pbData,
960 ULONG cbMax,
961 ULONG* pcbData)
963 IMoniker *This = impl_from_IROTData(iface);
964 ItemMonikerImpl *This1 = (ItemMonikerImpl *)This;
965 int len = (strlenW(This1->itemName)+1);
966 int i;
967 LPWSTR pszItemName;
968 LPWSTR pszItemDelimiter;
970 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
972 *pcbData = sizeof(CLSID) + sizeof(WCHAR) + len * sizeof(WCHAR);
973 if (cbMax < *pcbData)
974 return E_OUTOFMEMORY;
976 /* write CLSID */
977 memcpy(pbData, &CLSID_ItemMoniker, sizeof(CLSID));
978 /* write delimiter */
979 pszItemDelimiter = (LPWSTR)(pbData+sizeof(CLSID));
980 *pszItemDelimiter = *This1->itemDelimiter;
981 /* write name */
982 pszItemName = pszItemDelimiter + 1;
983 for (i = 0; i < len; i++)
984 pszItemName[i] = toupperW(This1->itemName[i]);
986 return S_OK;
989 /******************************************************************************
990 * CreateItemMoniker [OLE32.@]
991 ******************************************************************************/
992 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR lpszItem, LPMONIKER * ppmk)
994 ItemMonikerImpl* newItemMoniker;
995 HRESULT hr;
997 TRACE("(%s,%s,%p)\n",debugstr_w(lpszDelim),debugstr_w(lpszItem),ppmk);
999 newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1001 if (!newItemMoniker)
1002 return STG_E_INSUFFICIENTMEMORY;
1004 hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
1006 if (FAILED(hr)){
1008 HeapFree(GetProcessHeap(),0,newItemMoniker);
1009 return hr;
1012 return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&IID_IMoniker,(void**)ppmk);
1015 static HRESULT WINAPI ItemMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1016 REFIID riid, LPVOID *ppv)
1018 *ppv = NULL;
1019 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1021 *ppv = iface;
1022 IUnknown_AddRef(iface);
1023 return S_OK;
1025 return E_NOINTERFACE;
1028 static ULONG WINAPI ItemMonikerCF_AddRef(LPCLASSFACTORY iface)
1030 return 2; /* non-heap based object */
1033 static ULONG WINAPI ItemMonikerCF_Release(LPCLASSFACTORY iface)
1035 return 1; /* non-heap based object */
1038 static HRESULT WINAPI ItemMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1039 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1041 ItemMonikerImpl* newItemMoniker;
1042 HRESULT hr;
1043 static const WCHAR wszEmpty[] = { 0 };
1045 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1047 *ppv = NULL;
1049 if (pUnk)
1050 return CLASS_E_NOAGGREGATION;
1052 newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1053 if (!newItemMoniker)
1054 return E_OUTOFMEMORY;
1056 hr = ItemMonikerImpl_Construct(newItemMoniker, wszEmpty, wszEmpty);
1058 if (SUCCEEDED(hr))
1059 hr = ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker, riid, ppv);
1060 if (FAILED(hr))
1061 HeapFree(GetProcessHeap(),0,newItemMoniker);
1063 return hr;
1066 static HRESULT WINAPI ItemMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1068 FIXME("(%d), stub!\n",fLock);
1069 return S_OK;
1072 static const IClassFactoryVtbl ItemMonikerCFVtbl =
1074 ItemMonikerCF_QueryInterface,
1075 ItemMonikerCF_AddRef,
1076 ItemMonikerCF_Release,
1077 ItemMonikerCF_CreateInstance,
1078 ItemMonikerCF_LockServer
1080 static const IClassFactoryVtbl *ItemMonikerCF = &ItemMonikerCFVtbl;
1082 HRESULT ItemMonikerCF_Create(REFIID riid, LPVOID *ppv)
1084 return IClassFactory_QueryInterface((IClassFactory *)&ItemMonikerCF, riid, ppv);