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 #define CHARS_IN_GUID 39
44 /* ClassMoniker data structure */
45 typedef struct ClassMoniker
47 const IMonikerVtbl* lpVtbl; /* VTable relative to the IMoniker interface.*/
48 const IROTDataVtbl* lpVtblRotData; /* VTable relative to the IROTData interface.*/
49 LONG ref; /* reference counter for this object */
50 CLSID clsid; /* clsid identified by this moniker */
51 IUnknown *pMarshal; /* custom marshaler */
54 static inline IMoniker *impl_from_IROTData( IROTData *iface )
56 return (IMoniker *)((char*)iface - FIELD_OFFSET(ClassMoniker, lpVtblRotData));
59 /*******************************************************************************
60 * ClassMoniker_QueryInterface
61 *******************************************************************************/
62 static HRESULT WINAPI ClassMoniker_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
64 ClassMoniker *This = (ClassMoniker *)iface;
66 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
68 /* Perform a sanity check on the parameters.*/
72 /* Initialize the return parameter */
75 /* Compare the riid with the interface IDs implemented by this object.*/
76 if (IsEqualIID(&IID_IUnknown, riid) ||
77 IsEqualIID(&IID_IPersist, riid) ||
78 IsEqualIID(&IID_IPersistStream, riid) ||
79 IsEqualIID(&IID_IMoniker, riid))
83 else if (IsEqualIID(&IID_IROTData, riid))
84 *ppvObject = (IROTData*)&(This->lpVtblRotData);
85 else if (IsEqualIID(&IID_IMarshal, riid))
89 hr = MonikerMarshal_Create(iface, &This->pMarshal);
92 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
95 /* Check that we obtained an interface.*/
99 /* Query Interface always increases the reference count by one when it is successful */
100 IMoniker_AddRef(iface);
105 /******************************************************************************
106 * ClassMoniker_AddRef
107 ******************************************************************************/
108 static ULONG WINAPI ClassMoniker_AddRef(IMoniker* iface)
110 ClassMoniker *This = (ClassMoniker *)iface;
112 TRACE("(%p)\n",This);
114 return InterlockedIncrement(&This->ref);
117 /******************************************************************************
118 * ClassMoniker_Destroy (local function)
119 *******************************************************************************/
120 static HRESULT WINAPI ClassMoniker_Destroy(ClassMoniker* This)
122 TRACE("(%p)\n",This);
124 if (This->pMarshal) IUnknown_Release(This->pMarshal);
126 HeapFree(GetProcessHeap(),0,This);
131 /******************************************************************************
132 * ClassMoniker_Release
133 ******************************************************************************/
134 static ULONG WINAPI ClassMoniker_Release(IMoniker* iface)
136 ClassMoniker *This = (ClassMoniker *)iface;
139 TRACE("(%p)\n",This);
141 ref = InterlockedDecrement(&This->ref);
143 /* destroy the object if there's no more reference on it */
144 if (ref == 0) ClassMoniker_Destroy(This);
149 /******************************************************************************
150 * ClassMoniker_GetClassID
151 ******************************************************************************/
152 static HRESULT WINAPI ClassMoniker_GetClassID(IMoniker* iface,CLSID *pClassID)
154 TRACE("(%p,%p),stub!\n",iface,pClassID);
159 *pClassID = CLSID_ClassMoniker;
164 /******************************************************************************
165 * ClassMoniker_IsDirty
166 ******************************************************************************/
167 static HRESULT WINAPI ClassMoniker_IsDirty(IMoniker* iface)
169 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
170 method in the OLE-provided moniker interfaces always return S_FALSE because
171 their internal state never changes. */
173 TRACE("(%p)\n",iface);
178 /******************************************************************************
180 ******************************************************************************/
181 static HRESULT WINAPI ClassMoniker_Load(IMoniker* iface,IStream* pStm)
183 ClassMoniker *This = (ClassMoniker *)iface;
187 TRACE("(%p)\n", pStm);
189 hr = IStream_Read(pStm, &This->clsid, sizeof(This->clsid), NULL);
190 if (hr != S_OK) return STG_E_READFAULT;
192 hr = IStream_Read(pStm, &zero, sizeof(zero), NULL);
193 if ((hr != S_OK) || (zero != 0)) return STG_E_READFAULT;
198 /******************************************************************************
200 ******************************************************************************/
201 static HRESULT WINAPI ClassMoniker_Save(IMoniker* iface,
202 IStream* pStm,/* pointer to the stream where the object is to be saved */
203 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
205 ClassMoniker *This = (ClassMoniker *)iface;
209 TRACE("(%p, %s)\n", pStm, fClearDirty ? "TRUE" : "FALSE");
211 hr = IStream_Write(pStm, &This->clsid, sizeof(This->clsid), NULL);
212 if (FAILED(hr)) return hr;
214 hr = IStream_Write(pStm, &zero, sizeof(zero), NULL);
219 /******************************************************************************
220 * ClassMoniker_GetSizeMax
221 ******************************************************************************/
222 static HRESULT WINAPI ClassMoniker_GetSizeMax(IMoniker* iface,
223 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
225 TRACE("(%p)\n", pcbSize);
227 pcbSize->QuadPart = sizeof(CLSID) + sizeof(DWORD);
232 /******************************************************************************
233 * ClassMoniker_BindToObject
234 ******************************************************************************/
235 static HRESULT WINAPI ClassMoniker_BindToObject(IMoniker* iface,
241 ClassMoniker *This = (ClassMoniker *)iface;
243 IClassActivator *pActivator;
246 TRACE("(%p,%p,%p,%p)\n", pbc, pmkToLeft, riid, ppvResult);
248 bindopts.cbStruct = sizeof(bindopts);
249 IBindCtx_GetBindOptions(pbc, (BIND_OPTS *)&bindopts);
252 return CoGetClassObject(&This->clsid, bindopts.dwClassContext, NULL,
256 hr = IMoniker_BindToObject(pmkToLeft, pbc, NULL, &IID_IClassActivator,
257 (void **)&pActivator);
258 if (FAILED(hr)) return hr;
260 hr = IClassActivator_GetClassObject(pActivator, &This->clsid,
261 bindopts.dwClassContext,
262 bindopts.locale, riid, ppvResult);
264 IClassActivator_Release(pActivator);
270 /******************************************************************************
271 * ClassMoniker_BindToStorage
272 ******************************************************************************/
273 static HRESULT WINAPI ClassMoniker_BindToStorage(IMoniker* iface,
279 TRACE("(%p,%p,%p,%p)\n",pbc, pmkToLeft, riid, ppvResult);
280 return ClassMoniker_BindToObject(iface, pbc, pmkToLeft, riid, ppvResult);
283 /******************************************************************************
284 * ClassMoniker_Reduce
285 ******************************************************************************/
286 static HRESULT WINAPI ClassMoniker_Reduce(IMoniker* iface,
288 DWORD dwReduceHowFar,
289 IMoniker** ppmkToLeft,
290 IMoniker** ppmkReduced)
292 TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
297 ClassMoniker_AddRef(iface);
299 *ppmkReduced = iface;
301 return MK_S_REDUCED_TO_SELF;
303 /******************************************************************************
304 * ClassMoniker_ComposeWith
305 ******************************************************************************/
306 static HRESULT WINAPI ClassMoniker_ComposeWith(IMoniker* iface,
308 BOOL fOnlyIfNotGeneric,
309 IMoniker** ppmkComposite)
313 IEnumMoniker* penumMk=0;
314 IMoniker *pmostLeftMk=0;
315 IMoniker* tempMkComposite=0;
317 TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
319 if ((ppmkComposite==NULL)||(pmkRight==NULL))
324 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
326 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
327 if(mkSys==MKSYS_ANTIMONIKER)
331 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
332 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
334 if(mkSys==MKSYS_GENERICCOMPOSITE){
336 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
341 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
343 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
345 if(mkSys2==MKSYS_ANTIMONIKER){
347 IMoniker_Release(pmostLeftMk);
349 tempMkComposite=iface;
350 IMoniker_AddRef(iface);
352 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
354 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
356 IMoniker_Release(tempMkComposite);
357 IMoniker_Release(pmostLeftMk);
359 tempMkComposite=*ppmkComposite;
360 IMoniker_AddRef(tempMkComposite);
365 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
367 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
368 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
369 a NULL moniker and a return value of MK_E_NEEDGENERIC */
371 if (!fOnlyIfNotGeneric)
372 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
375 return MK_E_NEEDGENERIC;
378 /******************************************************************************
380 ******************************************************************************/
381 static HRESULT WINAPI ClassMoniker_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
383 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
385 if (ppenumMoniker == NULL)
388 *ppenumMoniker = NULL;
393 /******************************************************************************
394 * ClassMoniker_IsEqual
395 ******************************************************************************/
396 static HRESULT WINAPI ClassMoniker_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
400 LPOLESTR dispName1,dispName2;
402 HRESULT res = S_FALSE;
404 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
406 if (!pmkOtherMoniker) return S_FALSE;
409 /* check if both are ClassMoniker */
410 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
411 if(!IsEqualCLSID(&clsid,&CLSID_ClassMoniker)) return S_FALSE;
413 /* check if both displaynames are the same */
414 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
415 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
416 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
417 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
418 CoTaskMemFree(dispName2);
420 CoTaskMemFree(dispName1);
426 /******************************************************************************
428 ******************************************************************************/
429 static HRESULT WINAPI ClassMoniker_Hash(IMoniker* iface,DWORD* pdwHash)
431 ClassMoniker *This = (ClassMoniker *)iface;
433 TRACE("(%p)\n", pdwHash);
435 *pdwHash = This->clsid.Data1;
440 /******************************************************************************
441 * ClassMoniker_IsRunning
442 ******************************************************************************/
443 static HRESULT WINAPI ClassMoniker_IsRunning(IMoniker* iface,
446 IMoniker* pmkNewlyRunning)
448 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
454 /******************************************************************************
455 * ClassMoniker_GetTimeOfLastChange
456 ******************************************************************************/
457 static HRESULT WINAPI ClassMoniker_GetTimeOfLastChange(IMoniker* iface,
462 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pItemTime);
464 return MK_E_UNAVAILABLE;
467 /******************************************************************************
468 * ClassMoniker_Inverse
469 ******************************************************************************/
470 static HRESULT WINAPI ClassMoniker_Inverse(IMoniker* iface,IMoniker** ppmk)
472 TRACE("(%p)\n",ppmk);
477 return CreateAntiMoniker(ppmk);
480 /******************************************************************************
481 * ClassMoniker_CommonPrefixWith
482 ******************************************************************************/
483 static HRESULT WINAPI ClassMoniker_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
487 TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
491 IMoniker_IsSystemMoniker(pmkOther, &mkSys);
493 /* If the other moniker is an class moniker that is equal to this moniker, this method sets *ppmkPrefix */
494 /* to this moniker and returns MK_S_US */
496 if (mkSys == MKSYS_CLASSMONIKER)
498 if (IMoniker_IsEqual(iface, pmkOther) == S_OK)
502 IMoniker_AddRef(iface);
507 return MK_E_NOPREFIX;
510 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
511 /* the case where the other moniker is a generic composite. */
512 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
515 /******************************************************************************
516 * ClassMoniker_RelativePathTo
517 ******************************************************************************/
518 static HRESULT WINAPI ClassMoniker_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
520 TRACE("(%p, %p)\n",pmOther,ppmkRelPath);
527 return MK_E_NOTBINDABLE;
530 /******************************************************************************
531 * ClassMoniker_GetDisplayName
532 ******************************************************************************/
533 static HRESULT WINAPI ClassMoniker_GetDisplayName(IMoniker* iface,
536 LPOLESTR *ppszDisplayName)
538 ClassMoniker *This = (ClassMoniker *)iface;
539 static const WCHAR wszClsidPrefix[] = {'c','l','s','i','d',':',0};
541 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
543 if (!ppszDisplayName)
549 *ppszDisplayName = CoTaskMemAlloc(sizeof(wszClsidPrefix) + (CHARS_IN_GUID-2) * sizeof(WCHAR));
551 StringFromGUID2(&This->clsid, *ppszDisplayName+sizeof(wszClsidPrefix)/sizeof(WCHAR)-2, CHARS_IN_GUID);
553 /* note: this overwrites the opening curly bracket of the CLSID string generated above */
554 memcpy(*ppszDisplayName, wszClsidPrefix, sizeof(wszClsidPrefix)-sizeof(WCHAR));
556 /* note: this overwrites the closing curly bracket of the CLSID string generated above */
557 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-2] = ':';
558 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-1] = '\0';
560 TRACE("string is %s\n", debugstr_w(*ppszDisplayName));
564 /******************************************************************************
565 * ClassMoniker_ParseDisplayName
566 ******************************************************************************/
567 static HRESULT WINAPI ClassMoniker_ParseDisplayName(IMoniker* iface,
570 LPOLESTR pszDisplayName,
574 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
578 /******************************************************************************
579 * ClassMoniker_IsSystemMoniker
580 ******************************************************************************/
581 static HRESULT WINAPI ClassMoniker_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
583 TRACE("(%p,%p)\n",iface,pwdMksys);
588 *pwdMksys = MKSYS_CLASSMONIKER;
593 /*******************************************************************************
594 * ClassMonikerIROTData_QueryInterface
595 *******************************************************************************/
596 static HRESULT WINAPI ClassMonikerROTData_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
599 IMoniker *This = impl_from_IROTData(iface);
601 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
603 return ClassMoniker_QueryInterface(This, riid, ppvObject);
606 /***********************************************************************
607 * ClassMonikerIROTData_AddRef
609 static ULONG WINAPI ClassMonikerROTData_AddRef(IROTData *iface)
611 IMoniker *This = impl_from_IROTData(iface);
613 TRACE("(%p)\n",iface);
615 return ClassMoniker_AddRef(This);
618 /***********************************************************************
619 * ClassMonikerIROTData_Release
621 static ULONG WINAPI ClassMonikerROTData_Release(IROTData* iface)
623 IMoniker *This = impl_from_IROTData(iface);
625 TRACE("(%p)\n",iface);
627 return ClassMoniker_Release(This);
630 /******************************************************************************
631 * ClassMonikerIROTData_GetComparaisonData
632 ******************************************************************************/
633 static HRESULT WINAPI ClassMonikerROTData_GetComparaisonData(IROTData* iface,
638 ClassMoniker *This = (ClassMoniker *)impl_from_IROTData(iface);
640 TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
642 *pcbData = 2*sizeof(CLSID);
643 if (cbMax < *pcbData)
644 return E_OUTOFMEMORY;
646 /* write CLSID of the moniker */
647 memcpy(pbData, &CLSID_ClassMoniker, sizeof(CLSID));
648 /* write CLSID the moniker represents */
649 memcpy(pbData+sizeof(CLSID), &This->clsid, sizeof(CLSID));
654 /********************************************************************************/
655 /* Virtual function table for the ClassMoniker class which include IPersist,*/
656 /* IPersistStream and IMoniker functions. */
657 static const IMonikerVtbl ClassMonikerVtbl =
659 ClassMoniker_QueryInterface,
661 ClassMoniker_Release,
662 ClassMoniker_GetClassID,
663 ClassMoniker_IsDirty,
666 ClassMoniker_GetSizeMax,
667 ClassMoniker_BindToObject,
668 ClassMoniker_BindToStorage,
670 ClassMoniker_ComposeWith,
672 ClassMoniker_IsEqual,
674 ClassMoniker_IsRunning,
675 ClassMoniker_GetTimeOfLastChange,
676 ClassMoniker_Inverse,
677 ClassMoniker_CommonPrefixWith,
678 ClassMoniker_RelativePathTo,
679 ClassMoniker_GetDisplayName,
680 ClassMoniker_ParseDisplayName,
681 ClassMoniker_IsSystemMoniker
684 /********************************************************************************/
685 /* Virtual function table for the IROTData class. */
686 static const IROTDataVtbl ROTDataVtbl =
688 ClassMonikerROTData_QueryInterface,
689 ClassMonikerROTData_AddRef,
690 ClassMonikerROTData_Release,
691 ClassMonikerROTData_GetComparaisonData
694 /******************************************************************************
695 * ClassMoniker_Construct (local function)
696 *******************************************************************************/
697 HRESULT WINAPI ClassMoniker_Construct(ClassMoniker* This, REFCLSID rclsid)
699 TRACE("(%p,%s)\n",This,debugstr_guid(rclsid));
701 /* Initialize the virtual function table. */
702 This->lpVtbl = &ClassMonikerVtbl;
703 This->lpVtblRotData = &ROTDataVtbl;
705 This->clsid = *rclsid;
706 This->pMarshal = NULL;
711 /******************************************************************************
712 * CreateClassMoniker [OLE32.@]
713 ******************************************************************************/
714 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker **ppmk)
716 ClassMoniker* newClassMoniker;
719 TRACE("(%s,%p)\n", debugstr_guid(rclsid), ppmk);
721 newClassMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassMoniker));
723 if (!newClassMoniker)
724 return STG_E_INSUFFICIENTMEMORY;
726 hr = ClassMoniker_Construct(newClassMoniker, rclsid);
730 HeapFree(GetProcessHeap(), 0, newClassMoniker);
734 return ClassMoniker_QueryInterface((IMoniker *)newClassMoniker, &IID_IMoniker, (void**)ppmk);
737 static HRESULT WINAPI ClassMonikerCF_QueryInterface(LPCLASSFACTORY iface,
738 REFIID riid, LPVOID *ppv)
741 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
744 IUnknown_AddRef(iface);
747 return E_NOINTERFACE;
750 static ULONG WINAPI ClassMonikerCF_AddRef(LPCLASSFACTORY iface)
752 return 2; /* non-heap based object */
755 static ULONG WINAPI ClassMonikerCF_Release(LPCLASSFACTORY iface)
757 return 1; /* non-heap based object */
760 static HRESULT WINAPI ClassMonikerCF_CreateInstance(LPCLASSFACTORY iface,
761 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
766 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
771 return CLASS_E_NOAGGREGATION;
773 hr = CreateClassMoniker(&CLSID_NULL, &pmk);
774 if (FAILED(hr)) return hr;
776 hr = IMoniker_QueryInterface(pmk, riid, ppv);
777 IMoniker_Release(pmk);
782 static HRESULT WINAPI ClassMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
784 FIXME("(%d), stub!\n",fLock);
788 static const IClassFactoryVtbl ClassMonikerCFVtbl =
790 ClassMonikerCF_QueryInterface,
791 ClassMonikerCF_AddRef,
792 ClassMonikerCF_Release,
793 ClassMonikerCF_CreateInstance,
794 ClassMonikerCF_LockServer
796 static const IClassFactoryVtbl *ClassMonikerCF = &ClassMonikerCFVtbl;
798 HRESULT ClassMonikerCF_Create(REFIID riid, LPVOID *ppv)
800 return IClassFactory_QueryInterface((IClassFactory *)&ClassMonikerCF, riid, ppv);