4 * Copyright 1999 Noomen Hamza
5 * Copyright 2005 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
35 #include "wine/debug.h"
37 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 const GUID CLSID_ClassMoniker = {
43 0x31A, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
46 #define CHARS_IN_GUID 39
48 /* ClassMoniker data structure */
49 typedef struct ClassMoniker
51 const IMonikerVtbl* lpVtbl; /* VTable relative to the IMoniker interface.*/
52 const IROTDataVtbl* lpVtblRotData; /* VTable relative to the IROTData interface.*/
53 LONG ref; /* reference counter for this object */
54 CLSID clsid; /* clsid identified by this moniker */
55 IUnknown *pMarshal; /* custom marshaler */
58 static inline IMoniker *impl_from_IROTData( IROTData *iface )
60 return (IMoniker *)((char*)iface - FIELD_OFFSET(ClassMoniker, lpVtblRotData));
63 /*******************************************************************************
64 * ClassMoniker_QueryInterface
65 *******************************************************************************/
66 static HRESULT WINAPI ClassMoniker_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
68 ClassMoniker *This = (ClassMoniker *)iface;
70 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
72 /* Perform a sanity check on the parameters.*/
76 /* Initialize the return parameter */
79 /* Compare the riid with the interface IDs implemented by this object.*/
80 if (IsEqualIID(&IID_IUnknown, riid) ||
81 IsEqualIID(&IID_IPersist, riid) ||
82 IsEqualIID(&IID_IPersistStream, riid) ||
83 IsEqualIID(&IID_IMoniker, riid))
87 else if (IsEqualIID(&IID_IROTData, riid))
88 *ppvObject = (IROTData*)&(This->lpVtblRotData);
89 else if (IsEqualIID(&IID_IMarshal, riid))
93 hr = MonikerMarshal_Create(iface, &This->pMarshal);
96 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
99 /* Check that we obtained an interface.*/
101 return E_NOINTERFACE;
103 /* Query Interface always increases the reference count by one when it is successful */
104 IMoniker_AddRef(iface);
109 /******************************************************************************
110 * ClassMoniker_AddRef
111 ******************************************************************************/
112 static ULONG WINAPI ClassMoniker_AddRef(IMoniker* iface)
114 ClassMoniker *This = (ClassMoniker *)iface;
116 TRACE("(%p)\n",This);
118 return InterlockedIncrement(&This->ref);
121 /******************************************************************************
122 * ClassMoniker_Destroy (local function)
123 *******************************************************************************/
124 static HRESULT WINAPI ClassMoniker_Destroy(ClassMoniker* This)
126 TRACE("(%p)\n",This);
128 if (This->pMarshal) IUnknown_Release(This->pMarshal);
130 HeapFree(GetProcessHeap(),0,This);
135 /******************************************************************************
136 * ClassMoniker_Release
137 ******************************************************************************/
138 static ULONG WINAPI ClassMoniker_Release(IMoniker* iface)
140 ClassMoniker *This = (ClassMoniker *)iface;
143 TRACE("(%p)\n",This);
145 ref = InterlockedDecrement(&This->ref);
147 /* destroy the object if there's no more reference on it */
148 if (ref == 0) ClassMoniker_Destroy(This);
153 /******************************************************************************
154 * ClassMoniker_GetClassID
155 ******************************************************************************/
156 static HRESULT WINAPI ClassMoniker_GetClassID(IMoniker* iface,CLSID *pClassID)
158 TRACE("(%p,%p),stub!\n",iface,pClassID);
163 *pClassID = CLSID_ClassMoniker;
168 /******************************************************************************
169 * ClassMoniker_IsDirty
170 ******************************************************************************/
171 static HRESULT WINAPI ClassMoniker_IsDirty(IMoniker* iface)
173 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
174 method in the OLE-provided moniker interfaces always return S_FALSE because
175 their internal state never changes. */
177 TRACE("(%p)\n",iface);
182 /******************************************************************************
184 ******************************************************************************/
185 static HRESULT WINAPI ClassMoniker_Load(IMoniker* iface,IStream* pStm)
187 ClassMoniker *This = (ClassMoniker *)iface;
191 TRACE("(%p)\n", pStm);
193 hr = IStream_Read(pStm, &This->clsid, sizeof(This->clsid), NULL);
194 if (hr != S_OK) return STG_E_READFAULT;
196 hr = IStream_Read(pStm, &zero, sizeof(zero), NULL);
197 if ((hr != S_OK) || (zero != 0)) return STG_E_READFAULT;
202 /******************************************************************************
204 ******************************************************************************/
205 static HRESULT WINAPI ClassMoniker_Save(IMoniker* iface,
206 IStream* pStm,/* pointer to the stream where the object is to be saved */
207 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
209 ClassMoniker *This = (ClassMoniker *)iface;
213 TRACE("(%p, %s)\n", pStm, fClearDirty ? "TRUE" : "FALSE");
215 hr = IStream_Write(pStm, &This->clsid, sizeof(This->clsid), NULL);
216 if (FAILED(hr)) return hr;
218 hr = IStream_Write(pStm, &zero, sizeof(zero), NULL);
223 /******************************************************************************
224 * ClassMoniker_GetSizeMax
225 ******************************************************************************/
226 static HRESULT WINAPI ClassMoniker_GetSizeMax(IMoniker* iface,
227 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
229 TRACE("(%p)\n", pcbSize);
231 pcbSize->QuadPart = sizeof(CLSID) + sizeof(DWORD);
236 /******************************************************************************
237 * ClassMoniker_BindToObject
238 ******************************************************************************/
239 static HRESULT WINAPI ClassMoniker_BindToObject(IMoniker* iface,
245 ClassMoniker *This = (ClassMoniker *)iface;
247 IClassActivator *pActivator;
250 TRACE("(%p,%p,%p,%p)\n", pbc, pmkToLeft, riid, ppvResult);
252 bindopts.cbStruct = sizeof(bindopts);
253 IBindCtx_GetBindOptions(pbc, (BIND_OPTS *)&bindopts);
256 return CoGetClassObject(&This->clsid, bindopts.dwClassContext, NULL,
260 hr = IMoniker_BindToObject(pmkToLeft, pbc, NULL, &IID_IClassActivator,
261 (void **)&pActivator);
262 if (FAILED(hr)) return hr;
264 hr = IClassActivator_GetClassObject(pActivator, &This->clsid,
265 bindopts.dwClassContext,
266 bindopts.locale, riid, ppvResult);
268 IClassActivator_Release(pActivator);
274 /******************************************************************************
275 * ClassMoniker_BindToStorage
276 ******************************************************************************/
277 static HRESULT WINAPI ClassMoniker_BindToStorage(IMoniker* iface,
283 TRACE("(%p,%p,%p,%p)\n",pbc, pmkToLeft, riid, ppvResult);
284 return ClassMoniker_BindToObject(iface, pbc, pmkToLeft, riid, ppvResult);
287 /******************************************************************************
288 * ClassMoniker_Reduce
289 ******************************************************************************/
290 static HRESULT WINAPI ClassMoniker_Reduce(IMoniker* iface,
292 DWORD dwReduceHowFar,
293 IMoniker** ppmkToLeft,
294 IMoniker** ppmkReduced)
296 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
301 ClassMoniker_AddRef(iface);
303 *ppmkReduced = iface;
305 return MK_S_REDUCED_TO_SELF;
307 /******************************************************************************
308 * ClassMoniker_ComposeWith
309 ******************************************************************************/
310 static HRESULT WINAPI ClassMoniker_ComposeWith(IMoniker* iface,
312 BOOL fOnlyIfNotGeneric,
313 IMoniker** ppmkComposite)
317 IEnumMoniker* penumMk=0;
318 IMoniker *pmostLeftMk=0;
319 IMoniker* tempMkComposite=0;
321 TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
323 if ((ppmkComposite==NULL)||(pmkRight==NULL))
328 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
330 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
331 if(mkSys==MKSYS_ANTIMONIKER)
335 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
336 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
338 if(mkSys==MKSYS_GENERICCOMPOSITE){
340 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
345 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
347 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
349 if(mkSys2==MKSYS_ANTIMONIKER){
351 IMoniker_Release(pmostLeftMk);
353 tempMkComposite=iface;
354 IMoniker_AddRef(iface);
356 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
358 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
360 IMoniker_Release(tempMkComposite);
361 IMoniker_Release(pmostLeftMk);
363 tempMkComposite=*ppmkComposite;
364 IMoniker_AddRef(tempMkComposite);
369 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
371 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
372 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
373 a NULL moniker and a return value of MK_E_NEEDGENERIC */
375 if (!fOnlyIfNotGeneric)
376 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
379 return MK_E_NEEDGENERIC;
382 /******************************************************************************
384 ******************************************************************************/
385 static HRESULT WINAPI ClassMoniker_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
387 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
389 if (ppenumMoniker == NULL)
392 *ppenumMoniker = NULL;
397 /******************************************************************************
398 * ClassMoniker_IsEqual
399 ******************************************************************************/
400 static HRESULT WINAPI ClassMoniker_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
404 LPOLESTR dispName1,dispName2;
406 HRESULT res = S_FALSE;
408 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
410 if (!pmkOtherMoniker) return S_FALSE;
413 /* check if both are ClassMoniker */
414 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
415 if(!IsEqualCLSID(&clsid,&CLSID_ClassMoniker)) return S_FALSE;
417 /* check if both displaynames are the same */
418 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
419 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
420 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
421 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
422 CoTaskMemFree(dispName2);
424 CoTaskMemFree(dispName1);
430 /******************************************************************************
432 ******************************************************************************/
433 static HRESULT WINAPI ClassMoniker_Hash(IMoniker* iface,DWORD* pdwHash)
435 ClassMoniker *This = (ClassMoniker *)iface;
437 TRACE("(%p)\n", pdwHash);
439 *pdwHash = This->clsid.Data1;
444 /******************************************************************************
445 * ClassMoniker_IsRunning
446 ******************************************************************************/
447 static HRESULT WINAPI ClassMoniker_IsRunning(IMoniker* iface,
450 IMoniker* pmkNewlyRunning)
452 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
458 /******************************************************************************
459 * ClassMoniker_GetTimeOfLastChange
460 ******************************************************************************/
461 static HRESULT WINAPI ClassMoniker_GetTimeOfLastChange(IMoniker* iface,
466 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pItemTime);
468 return MK_E_UNAVAILABLE;
471 /******************************************************************************
472 * ClassMoniker_Inverse
473 ******************************************************************************/
474 static HRESULT WINAPI ClassMoniker_Inverse(IMoniker* iface,IMoniker** ppmk)
476 TRACE("(%p)\n",ppmk);
481 return CreateAntiMoniker(ppmk);
484 /******************************************************************************
485 * ClassMoniker_CommonPrefixWith
486 ******************************************************************************/
487 static HRESULT WINAPI ClassMoniker_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
491 TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
495 IMoniker_IsSystemMoniker(pmkOther, &mkSys);
497 /* If the other moniker is an class moniker that is equal to this moniker, this method sets *ppmkPrefix */
498 /* to this moniker and returns MK_S_US */
500 if (mkSys == MKSYS_CLASSMONIKER)
502 if (IMoniker_IsEqual(iface, pmkOther) == S_OK)
506 IMoniker_AddRef(iface);
511 return MK_E_NOPREFIX;
514 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
515 /* the case where the other moniker is a generic composite. */
516 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
519 /******************************************************************************
520 * ClassMoniker_RelativePathTo
521 ******************************************************************************/
522 static HRESULT WINAPI ClassMoniker_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
524 TRACE("(%p, %p)\n",pmOther,ppmkRelPath);
531 return MK_E_NOTBINDABLE;
534 /******************************************************************************
535 * ClassMoniker_GetDisplayName
536 ******************************************************************************/
537 static HRESULT WINAPI ClassMoniker_GetDisplayName(IMoniker* iface,
540 LPOLESTR *ppszDisplayName)
542 ClassMoniker *This = (ClassMoniker *)iface;
543 static const WCHAR wszClsidPrefix[] = {'c','l','s','i','d',':',0};
545 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
547 if (!ppszDisplayName)
553 *ppszDisplayName = CoTaskMemAlloc(sizeof(wszClsidPrefix) + (CHARS_IN_GUID-2) * sizeof(WCHAR));
555 StringFromGUID2(&This->clsid, *ppszDisplayName+sizeof(wszClsidPrefix)/sizeof(WCHAR)-2, CHARS_IN_GUID);
557 /* note: this overwrites the opening curly bracket of the CLSID string generated above */
558 memcpy(*ppszDisplayName, wszClsidPrefix, sizeof(wszClsidPrefix)-sizeof(WCHAR));
560 /* note: this overwrites the closing curly bracket of the CLSID string generated above */
561 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-2] = ':';
562 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-1] = '\0';
564 TRACE("string is %s\n", debugstr_w(*ppszDisplayName));
568 /******************************************************************************
569 * ClassMoniker_ParseDisplayName
570 ******************************************************************************/
571 static HRESULT WINAPI ClassMoniker_ParseDisplayName(IMoniker* iface,
574 LPOLESTR pszDisplayName,
578 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
582 /******************************************************************************
583 * ClassMoniker_IsSystemMoniker
584 ******************************************************************************/
585 static HRESULT WINAPI ClassMoniker_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
587 TRACE("(%p,%p)\n",iface,pwdMksys);
592 *pwdMksys = MKSYS_CLASSMONIKER;
597 /*******************************************************************************
598 * ClassMonikerIROTData_QueryInterface
599 *******************************************************************************/
600 static HRESULT WINAPI ClassMonikerROTData_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
603 IMoniker *This = impl_from_IROTData(iface);
605 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
607 return ClassMoniker_QueryInterface(This, riid, ppvObject);
610 /***********************************************************************
611 * ClassMonikerIROTData_AddRef
613 static ULONG WINAPI ClassMonikerROTData_AddRef(IROTData *iface)
615 IMoniker *This = impl_from_IROTData(iface);
617 TRACE("(%p)\n",iface);
619 return ClassMoniker_AddRef(This);
622 /***********************************************************************
623 * ClassMonikerIROTData_Release
625 static ULONG WINAPI ClassMonikerROTData_Release(IROTData* iface)
627 IMoniker *This = impl_from_IROTData(iface);
629 TRACE("(%p)\n",iface);
631 return ClassMoniker_Release(This);
634 /******************************************************************************
635 * ClassMonikerIROTData_GetComparaisonData
636 ******************************************************************************/
637 static HRESULT WINAPI ClassMonikerROTData_GetComparaisonData(IROTData* iface,
642 ClassMoniker *This = (ClassMoniker *)impl_from_IROTData(iface);
644 TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
646 *pcbData = 2*sizeof(CLSID);
647 if (cbMax < *pcbData)
648 return E_OUTOFMEMORY;
650 /* write CLSID of the moniker */
651 memcpy(pbData, &CLSID_ClassMoniker, sizeof(CLSID));
652 /* write CLSID the moniker represents */
653 memcpy(pbData+sizeof(CLSID), &This->clsid, sizeof(CLSID));
658 /********************************************************************************/
659 /* Virtual function table for the ClassMoniker class which include IPersist,*/
660 /* IPersistStream and IMoniker functions. */
661 static const IMonikerVtbl ClassMonikerVtbl =
663 ClassMoniker_QueryInterface,
665 ClassMoniker_Release,
666 ClassMoniker_GetClassID,
667 ClassMoniker_IsDirty,
670 ClassMoniker_GetSizeMax,
671 ClassMoniker_BindToObject,
672 ClassMoniker_BindToStorage,
674 ClassMoniker_ComposeWith,
676 ClassMoniker_IsEqual,
678 ClassMoniker_IsRunning,
679 ClassMoniker_GetTimeOfLastChange,
680 ClassMoniker_Inverse,
681 ClassMoniker_CommonPrefixWith,
682 ClassMoniker_RelativePathTo,
683 ClassMoniker_GetDisplayName,
684 ClassMoniker_ParseDisplayName,
685 ClassMoniker_IsSystemMoniker
688 /********************************************************************************/
689 /* Virtual function table for the IROTData class. */
690 static const IROTDataVtbl ROTDataVtbl =
692 ClassMonikerROTData_QueryInterface,
693 ClassMonikerROTData_AddRef,
694 ClassMonikerROTData_Release,
695 ClassMonikerROTData_GetComparaisonData
698 /******************************************************************************
699 * ClassMoniker_Construct (local function)
700 *******************************************************************************/
701 HRESULT WINAPI ClassMoniker_Construct(ClassMoniker* This, REFCLSID rclsid)
703 TRACE("(%p,%s)\n",This,debugstr_guid(rclsid));
705 /* Initialize the virtual function table. */
706 This->lpVtbl = &ClassMonikerVtbl;
707 This->lpVtblRotData = &ROTDataVtbl;
709 This->clsid = *rclsid;
710 This->pMarshal = NULL;
715 /******************************************************************************
716 * CreateClassMoniker [OLE32.@]
717 ******************************************************************************/
718 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker **ppmk)
720 ClassMoniker* newClassMoniker;
723 TRACE("(%s,%p)\n", debugstr_guid(rclsid), ppmk);
725 newClassMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassMoniker));
727 if (!newClassMoniker)
728 return STG_E_INSUFFICIENTMEMORY;
730 hr = ClassMoniker_Construct(newClassMoniker, rclsid);
734 HeapFree(GetProcessHeap(), 0, newClassMoniker);
738 return ClassMoniker_QueryInterface((IMoniker *)newClassMoniker, &IID_IMoniker, (void**)ppmk);
741 static HRESULT WINAPI ClassMonikerCF_QueryInterface(LPCLASSFACTORY iface,
742 REFIID riid, LPVOID *ppv)
745 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
748 IUnknown_AddRef(iface);
751 return E_NOINTERFACE;
754 static ULONG WINAPI ClassMonikerCF_AddRef(LPCLASSFACTORY iface)
756 return 2; /* non-heap based object */
759 static ULONG WINAPI ClassMonikerCF_Release(LPCLASSFACTORY iface)
761 return 1; /* non-heap based object */
764 static HRESULT WINAPI ClassMonikerCF_CreateInstance(LPCLASSFACTORY iface,
765 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
770 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
775 return CLASS_E_NOAGGREGATION;
777 hr = CreateClassMoniker(&CLSID_NULL, &pmk);
778 if (FAILED(hr)) return hr;
780 hr = IMoniker_QueryInterface(pmk, riid, ppv);
781 IMoniker_Release(pmk);
786 static HRESULT WINAPI ClassMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
788 FIXME("(%d), stub!\n",fLock);
792 static const IClassFactoryVtbl ClassMonikerCFVtbl =
794 ClassMonikerCF_QueryInterface,
795 ClassMonikerCF_AddRef,
796 ClassMonikerCF_Release,
797 ClassMonikerCF_CreateInstance,
798 ClassMonikerCF_LockServer
800 static const IClassFactoryVtbl *ClassMonikerCF = &ClassMonikerCFVtbl;
802 HRESULT ClassMonikerCF_Create(REFIID riid, LPVOID *ppv)
804 return IClassFactory_QueryInterface((IClassFactory *)&ClassMonikerCF, riid, ppv);