Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ole32 / classmoniker.c
blob8b927f85db65a04a969e25a7d5ccfa94c232e941
1 /*
2 * Class Monikers
4 * Copyright 1999 Noomen Hamza
5 * Copyright 2005-2007 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
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <string.h>
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.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 #define CHARS_IN_GUID 39
43 /* ClassMoniker data structure */
44 typedef struct ClassMoniker
46 const IMonikerVtbl* lpVtbl; /* VTable relative to the IMoniker interface.*/
47 const IROTDataVtbl* lpVtblRotData; /* VTable relative to the IROTData interface.*/
48 LONG ref; /* reference counter for this object */
49 CLSID clsid; /* clsid identified by this moniker */
50 IUnknown *pMarshal; /* custom marshaler */
51 } ClassMoniker;
53 static inline IMoniker *impl_from_IROTData( IROTData *iface )
55 return (IMoniker *)((char*)iface - FIELD_OFFSET(ClassMoniker, lpVtblRotData));
58 /*******************************************************************************
59 * ClassMoniker_QueryInterface
60 *******************************************************************************/
61 static HRESULT WINAPI ClassMoniker_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
63 ClassMoniker *This = (ClassMoniker *)iface;
65 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
67 /* Perform a sanity check on the parameters.*/
68 if (!ppvObject)
69 return E_POINTER;
71 /* Initialize the return parameter */
72 *ppvObject = 0;
74 /* Compare the riid with the interface IDs implemented by this object.*/
75 if (IsEqualIID(&IID_IUnknown, riid) ||
76 IsEqualIID(&IID_IPersist, riid) ||
77 IsEqualIID(&IID_IPersistStream, riid) ||
78 IsEqualIID(&IID_IMoniker, riid))
80 *ppvObject = iface;
82 else if (IsEqualIID(&IID_IROTData, riid))
83 *ppvObject = &This->lpVtblRotData;
84 else if (IsEqualIID(&IID_IMarshal, riid))
86 HRESULT hr = S_OK;
87 if (!This->pMarshal)
88 hr = MonikerMarshal_Create(iface, &This->pMarshal);
89 if (hr != S_OK)
90 return hr;
91 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
94 /* Check that we obtained an interface.*/
95 if (!*ppvObject)
96 return E_NOINTERFACE;
98 /* Query Interface always increases the reference count by one when it is successful */
99 IMoniker_AddRef(iface);
101 return S_OK;
104 /******************************************************************************
105 * ClassMoniker_AddRef
106 ******************************************************************************/
107 static ULONG WINAPI ClassMoniker_AddRef(IMoniker* iface)
109 ClassMoniker *This = (ClassMoniker *)iface;
111 TRACE("(%p)\n",This);
113 return InterlockedIncrement(&This->ref);
116 /******************************************************************************
117 * ClassMoniker_Destroy (local function)
118 *******************************************************************************/
119 static HRESULT ClassMoniker_Destroy(ClassMoniker* This)
121 TRACE("(%p)\n",This);
123 if (This->pMarshal) IUnknown_Release(This->pMarshal);
125 HeapFree(GetProcessHeap(),0,This);
127 return S_OK;
130 /******************************************************************************
131 * ClassMoniker_Release
132 ******************************************************************************/
133 static ULONG WINAPI ClassMoniker_Release(IMoniker* iface)
135 ClassMoniker *This = (ClassMoniker *)iface;
136 ULONG ref;
138 TRACE("(%p)\n",This);
140 ref = InterlockedDecrement(&This->ref);
142 /* destroy the object if there's no more reference on it */
143 if (ref == 0) ClassMoniker_Destroy(This);
145 return ref;
148 /******************************************************************************
149 * ClassMoniker_GetClassID
150 ******************************************************************************/
151 static HRESULT WINAPI ClassMoniker_GetClassID(IMoniker* iface,CLSID *pClassID)
153 TRACE("(%p,%p),stub!\n",iface,pClassID);
155 if (pClassID==NULL)
156 return E_POINTER;
158 *pClassID = CLSID_ClassMoniker;
160 return S_OK;
163 /******************************************************************************
164 * ClassMoniker_IsDirty
165 ******************************************************************************/
166 static HRESULT WINAPI ClassMoniker_IsDirty(IMoniker* iface)
168 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
169 method in the OLE-provided moniker interfaces always return S_FALSE because
170 their internal state never changes. */
172 TRACE("(%p)\n",iface);
174 return S_FALSE;
177 /******************************************************************************
178 * ClassMoniker_Load
179 ******************************************************************************/
180 static HRESULT WINAPI ClassMoniker_Load(IMoniker* iface,IStream* pStm)
182 ClassMoniker *This = (ClassMoniker *)iface;
183 HRESULT hr;
184 DWORD zero;
186 TRACE("(%p)\n", pStm);
188 hr = IStream_Read(pStm, &This->clsid, sizeof(This->clsid), NULL);
189 if (hr != S_OK) return STG_E_READFAULT;
191 hr = IStream_Read(pStm, &zero, sizeof(zero), NULL);
192 if ((hr != S_OK) || (zero != 0)) return STG_E_READFAULT;
194 return S_OK;
197 /******************************************************************************
198 * ClassMoniker_Save
199 ******************************************************************************/
200 static HRESULT WINAPI ClassMoniker_Save(IMoniker* iface,
201 IStream* pStm,/* pointer to the stream where the object is to be saved */
202 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
204 ClassMoniker *This = (ClassMoniker *)iface;
205 HRESULT hr;
206 DWORD zero = 0;
208 TRACE("(%p, %s)\n", pStm, fClearDirty ? "TRUE" : "FALSE");
210 hr = IStream_Write(pStm, &This->clsid, sizeof(This->clsid), NULL);
211 if (FAILED(hr)) return hr;
213 return IStream_Write(pStm, &zero, sizeof(zero), NULL);
216 /******************************************************************************
217 * ClassMoniker_GetSizeMax
218 ******************************************************************************/
219 static HRESULT WINAPI ClassMoniker_GetSizeMax(IMoniker* iface,
220 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
222 TRACE("(%p)\n", pcbSize);
224 pcbSize->QuadPart = sizeof(CLSID) + sizeof(DWORD);
226 return S_OK;
229 /******************************************************************************
230 * ClassMoniker_BindToObject
231 ******************************************************************************/
232 static HRESULT WINAPI ClassMoniker_BindToObject(IMoniker* iface,
233 IBindCtx* pbc,
234 IMoniker* pmkToLeft,
235 REFIID riid,
236 VOID** ppvResult)
238 ClassMoniker *This = (ClassMoniker *)iface;
239 BIND_OPTS2 bindopts;
240 IClassActivator *pActivator;
241 HRESULT hr;
243 TRACE("(%p,%p,%p,%p)\n", pbc, pmkToLeft, riid, ppvResult);
245 bindopts.cbStruct = sizeof(bindopts);
246 IBindCtx_GetBindOptions(pbc, (BIND_OPTS *)&bindopts);
248 if (!pmkToLeft)
249 return CoGetClassObject(&This->clsid, bindopts.dwClassContext, NULL,
250 riid, ppvResult);
251 else
253 hr = IMoniker_BindToObject(pmkToLeft, pbc, NULL, &IID_IClassActivator,
254 (void **)&pActivator);
255 if (FAILED(hr)) return hr;
257 hr = IClassActivator_GetClassObject(pActivator, &This->clsid,
258 bindopts.dwClassContext,
259 bindopts.locale, riid, ppvResult);
261 IClassActivator_Release(pActivator);
263 return hr;
267 /******************************************************************************
268 * ClassMoniker_BindToStorage
269 ******************************************************************************/
270 static HRESULT WINAPI ClassMoniker_BindToStorage(IMoniker* iface,
271 IBindCtx* pbc,
272 IMoniker* pmkToLeft,
273 REFIID riid,
274 VOID** ppvResult)
276 TRACE("(%p,%p,%p,%p)\n",pbc, pmkToLeft, riid, ppvResult);
277 return ClassMoniker_BindToObject(iface, pbc, pmkToLeft, riid, ppvResult);
280 /******************************************************************************
281 * ClassMoniker_Reduce
282 ******************************************************************************/
283 static HRESULT WINAPI ClassMoniker_Reduce(IMoniker* iface,
284 IBindCtx* pbc,
285 DWORD dwReduceHowFar,
286 IMoniker** ppmkToLeft,
287 IMoniker** ppmkReduced)
289 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
291 if (!ppmkReduced)
292 return E_POINTER;
294 ClassMoniker_AddRef(iface);
296 *ppmkReduced = iface;
298 return MK_S_REDUCED_TO_SELF;
300 /******************************************************************************
301 * ClassMoniker_ComposeWith
302 ******************************************************************************/
303 static HRESULT WINAPI ClassMoniker_ComposeWith(IMoniker* iface,
304 IMoniker* pmkRight,
305 BOOL fOnlyIfNotGeneric,
306 IMoniker** ppmkComposite)
308 HRESULT res=S_OK;
309 DWORD mkSys,mkSys2;
310 IEnumMoniker* penumMk=0;
311 IMoniker *pmostLeftMk=0;
312 IMoniker* tempMkComposite=0;
314 TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
316 if ((ppmkComposite==NULL)||(pmkRight==NULL))
317 return E_POINTER;
319 *ppmkComposite=0;
321 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
323 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
324 if(mkSys==MKSYS_ANTIMONIKER)
325 return res;
327 else
328 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
329 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
331 if(mkSys==MKSYS_GENERICCOMPOSITE){
333 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
335 if (FAILED(res))
336 return res;
338 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
340 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
342 if(mkSys2==MKSYS_ANTIMONIKER){
344 IMoniker_Release(pmostLeftMk);
346 tempMkComposite=iface;
347 IMoniker_AddRef(iface);
349 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
351 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
353 IMoniker_Release(tempMkComposite);
354 IMoniker_Release(pmostLeftMk);
356 tempMkComposite=*ppmkComposite;
357 IMoniker_AddRef(tempMkComposite);
359 return res;
361 else
362 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
364 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
365 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
366 a NULL moniker and a return value of MK_E_NEEDGENERIC */
367 else
368 if (!fOnlyIfNotGeneric)
369 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
371 else
372 return MK_E_NEEDGENERIC;
375 /******************************************************************************
376 * ClassMoniker_Enum
377 ******************************************************************************/
378 static HRESULT WINAPI ClassMoniker_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
380 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
382 if (ppenumMoniker == NULL)
383 return E_POINTER;
385 *ppenumMoniker = NULL;
387 return S_OK;
390 /******************************************************************************
391 * ClassMoniker_IsEqual
392 ******************************************************************************/
393 static HRESULT WINAPI ClassMoniker_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
396 CLSID clsid;
397 LPOLESTR dispName1,dispName2;
398 IBindCtx* bind;
399 HRESULT res = S_FALSE;
401 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
403 if (!pmkOtherMoniker) return S_FALSE;
406 /* check if both are ClassMoniker */
407 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
408 if(!IsEqualCLSID(&clsid,&CLSID_ClassMoniker)) return S_FALSE;
410 /* check if both displaynames are the same */
411 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
412 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
413 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
414 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
415 CoTaskMemFree(dispName2);
417 CoTaskMemFree(dispName1);
420 return res;
423 /******************************************************************************
424 * ClassMoniker_Hash
425 ******************************************************************************/
426 static HRESULT WINAPI ClassMoniker_Hash(IMoniker* iface,DWORD* pdwHash)
428 ClassMoniker *This = (ClassMoniker *)iface;
430 TRACE("(%p)\n", pdwHash);
432 *pdwHash = This->clsid.Data1;
434 return S_OK;
437 /******************************************************************************
438 * ClassMoniker_IsRunning
439 ******************************************************************************/
440 static HRESULT WINAPI ClassMoniker_IsRunning(IMoniker* iface,
441 IBindCtx* pbc,
442 IMoniker* pmkToLeft,
443 IMoniker* pmkNewlyRunning)
445 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
447 /* as in native */
448 return E_NOTIMPL;
451 /******************************************************************************
452 * ClassMoniker_GetTimeOfLastChange
453 ******************************************************************************/
454 static HRESULT WINAPI ClassMoniker_GetTimeOfLastChange(IMoniker* iface,
455 IBindCtx* pbc,
456 IMoniker* pmkToLeft,
457 FILETIME* pItemTime)
459 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pItemTime);
461 return MK_E_UNAVAILABLE;
464 /******************************************************************************
465 * ClassMoniker_Inverse
466 ******************************************************************************/
467 static HRESULT WINAPI ClassMoniker_Inverse(IMoniker* iface,IMoniker** ppmk)
469 TRACE("(%p)\n",ppmk);
471 if (!ppmk)
472 return E_POINTER;
474 return CreateAntiMoniker(ppmk);
477 /******************************************************************************
478 * ClassMoniker_CommonPrefixWith
479 ******************************************************************************/
480 static HRESULT WINAPI ClassMoniker_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
482 DWORD mkSys;
484 TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
486 *ppmkPrefix = NULL;
488 IMoniker_IsSystemMoniker(pmkOther, &mkSys);
490 /* If the other moniker is an class moniker that is equal to this moniker, this method sets *ppmkPrefix */
491 /* to this moniker and returns MK_S_US */
493 if (mkSys == MKSYS_CLASSMONIKER)
495 if (IMoniker_IsEqual(iface, pmkOther) == S_OK)
497 *ppmkPrefix = iface;
499 IMoniker_AddRef(iface);
501 return MK_S_US;
503 else
504 return MK_E_NOPREFIX;
506 else
507 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
508 /* the case where the other moniker is a generic composite. */
509 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
512 /******************************************************************************
513 * ClassMoniker_RelativePathTo
514 ******************************************************************************/
515 static HRESULT WINAPI ClassMoniker_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
517 TRACE("(%p, %p)\n",pmOther,ppmkRelPath);
519 if (!ppmkRelPath)
520 return E_POINTER;
522 *ppmkRelPath = NULL;
524 return MK_E_NOTBINDABLE;
527 /******************************************************************************
528 * ClassMoniker_GetDisplayName
529 ******************************************************************************/
530 static HRESULT WINAPI ClassMoniker_GetDisplayName(IMoniker* iface,
531 IBindCtx* pbc,
532 IMoniker* pmkToLeft,
533 LPOLESTR *ppszDisplayName)
535 ClassMoniker *This = (ClassMoniker *)iface;
536 static const WCHAR wszClsidPrefix[] = {'c','l','s','i','d',':',0};
538 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
540 if (!ppszDisplayName)
541 return E_POINTER;
543 if (pmkToLeft)
544 return E_INVALIDARG;
546 *ppszDisplayName = CoTaskMemAlloc(sizeof(wszClsidPrefix) + (CHARS_IN_GUID-2) * sizeof(WCHAR));
548 StringFromGUID2(&This->clsid, *ppszDisplayName+sizeof(wszClsidPrefix)/sizeof(WCHAR)-2, CHARS_IN_GUID);
550 /* note: this overwrites the opening curly bracket of the CLSID string generated above */
551 memcpy(*ppszDisplayName, wszClsidPrefix, sizeof(wszClsidPrefix)-sizeof(WCHAR));
553 /* note: this overwrites the closing curly bracket of the CLSID string generated above */
554 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-2] = ':';
555 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-1] = '\0';
557 TRACE("string is %s\n", debugstr_w(*ppszDisplayName));
558 return S_OK;
561 /******************************************************************************
562 * ClassMoniker_ParseDisplayName
563 ******************************************************************************/
564 static HRESULT WINAPI ClassMoniker_ParseDisplayName(IMoniker* iface,
565 IBindCtx* pbc,
566 IMoniker* pmkToLeft,
567 LPOLESTR pszDisplayName,
568 ULONG* pchEaten,
569 IMoniker** ppmkOut)
571 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
572 return E_NOTIMPL;
575 /******************************************************************************
576 * ClassMoniker_IsSystemMoniker
577 ******************************************************************************/
578 static HRESULT WINAPI ClassMoniker_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
580 TRACE("(%p,%p)\n",iface,pwdMksys);
582 if (!pwdMksys)
583 return E_POINTER;
585 *pwdMksys = MKSYS_CLASSMONIKER;
587 return S_OK;
590 /*******************************************************************************
591 * ClassMonikerIROTData_QueryInterface
592 *******************************************************************************/
593 static HRESULT WINAPI ClassMonikerROTData_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
596 IMoniker *This = impl_from_IROTData(iface);
598 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
600 return ClassMoniker_QueryInterface(This, riid, ppvObject);
603 /***********************************************************************
604 * ClassMonikerIROTData_AddRef
606 static ULONG WINAPI ClassMonikerROTData_AddRef(IROTData *iface)
608 IMoniker *This = impl_from_IROTData(iface);
610 TRACE("(%p)\n",iface);
612 return ClassMoniker_AddRef(This);
615 /***********************************************************************
616 * ClassMonikerIROTData_Release
618 static ULONG WINAPI ClassMonikerROTData_Release(IROTData* iface)
620 IMoniker *This = impl_from_IROTData(iface);
622 TRACE("(%p)\n",iface);
624 return ClassMoniker_Release(This);
627 /******************************************************************************
628 * ClassMonikerIROTData_GetComparisonData
629 ******************************************************************************/
630 static HRESULT WINAPI ClassMonikerROTData_GetComparisonData(IROTData* iface,
631 BYTE* pbData,
632 ULONG cbMax,
633 ULONG* pcbData)
635 ClassMoniker *This = (ClassMoniker *)impl_from_IROTData(iface);
637 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
639 *pcbData = 2*sizeof(CLSID);
640 if (cbMax < *pcbData)
641 return E_OUTOFMEMORY;
643 /* write CLSID of the moniker */
644 memcpy(pbData, &CLSID_ClassMoniker, sizeof(CLSID));
645 /* write CLSID the moniker represents */
646 memcpy(pbData+sizeof(CLSID), &This->clsid, sizeof(CLSID));
648 return S_OK;
651 /********************************************************************************/
652 /* Virtual function table for the ClassMoniker class which include IPersist,*/
653 /* IPersistStream and IMoniker functions. */
654 static const IMonikerVtbl ClassMonikerVtbl =
656 ClassMoniker_QueryInterface,
657 ClassMoniker_AddRef,
658 ClassMoniker_Release,
659 ClassMoniker_GetClassID,
660 ClassMoniker_IsDirty,
661 ClassMoniker_Load,
662 ClassMoniker_Save,
663 ClassMoniker_GetSizeMax,
664 ClassMoniker_BindToObject,
665 ClassMoniker_BindToStorage,
666 ClassMoniker_Reduce,
667 ClassMoniker_ComposeWith,
668 ClassMoniker_Enum,
669 ClassMoniker_IsEqual,
670 ClassMoniker_Hash,
671 ClassMoniker_IsRunning,
672 ClassMoniker_GetTimeOfLastChange,
673 ClassMoniker_Inverse,
674 ClassMoniker_CommonPrefixWith,
675 ClassMoniker_RelativePathTo,
676 ClassMoniker_GetDisplayName,
677 ClassMoniker_ParseDisplayName,
678 ClassMoniker_IsSystemMoniker
681 /********************************************************************************/
682 /* Virtual function table for the IROTData class. */
683 static const IROTDataVtbl ROTDataVtbl =
685 ClassMonikerROTData_QueryInterface,
686 ClassMonikerROTData_AddRef,
687 ClassMonikerROTData_Release,
688 ClassMonikerROTData_GetComparisonData
691 /******************************************************************************
692 * ClassMoniker_Construct (local function)
693 *******************************************************************************/
694 static HRESULT ClassMoniker_Construct(ClassMoniker* This, REFCLSID rclsid)
696 TRACE("(%p,%s)\n",This,debugstr_guid(rclsid));
698 /* Initialize the virtual function table. */
699 This->lpVtbl = &ClassMonikerVtbl;
700 This->lpVtblRotData = &ROTDataVtbl;
701 This->ref = 0;
702 This->clsid = *rclsid;
703 This->pMarshal = NULL;
705 return S_OK;
708 /******************************************************************************
709 * CreateClassMoniker [OLE32.@]
710 ******************************************************************************/
711 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker **ppmk)
713 ClassMoniker* newClassMoniker;
714 HRESULT hr;
716 TRACE("(%s,%p)\n", debugstr_guid(rclsid), ppmk);
718 newClassMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassMoniker));
720 if (!newClassMoniker)
721 return STG_E_INSUFFICIENTMEMORY;
723 hr = ClassMoniker_Construct(newClassMoniker, rclsid);
725 if (FAILED(hr))
727 HeapFree(GetProcessHeap(), 0, newClassMoniker);
728 return hr;
731 return ClassMoniker_QueryInterface((IMoniker *)newClassMoniker, &IID_IMoniker, (void**)ppmk);
734 HRESULT ClassMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
735 LPDWORD pchEaten, LPMONIKER *ppmk)
737 HRESULT hr;
738 LPCWSTR s = strchrW(szDisplayName, ':');
739 LPCWSTR end;
740 CLSID clsid;
741 BYTE table[256];
742 int i;
744 if (!s)
745 return MK_E_SYNTAX;
747 s++;
749 for (end = s; *end && (*end != ':'); end++)
752 TRACE("parsing %s\n", debugstr_wn(s, end - s));
754 /* validate the CLSID string */
755 if (s[0] == '{')
757 if ((end - s != 38) || (s[37] != '}'))
758 return MK_E_SYNTAX;
759 s++;
761 else
763 if (end - s != 36)
764 return MK_E_SYNTAX;
767 for (i=0; i<36; i++)
769 if ((i == 8)||(i == 13)||(i == 18)||(i == 23))
771 if (s[i] != '-')
772 return MK_E_SYNTAX;
773 continue;
775 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
776 ((s[i] >= 'a') && (s[i] <= 'f')) ||
777 ((s[i] >= 'A') && (s[i] <= 'F'))))
778 return MK_E_SYNTAX;
781 /* quick lookup table */
782 memset(table, 0, 256);
784 for (i = 0; i < 10; i++)
785 table['0' + i] = i;
786 for (i = 0; i < 6; i++)
788 table['A' + i] = i+10;
789 table['a' + i] = i+10;
792 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
794 clsid.Data1 = (table[s[0]] << 28 | table[s[1]] << 24 | table[s[2]] << 20 | table[s[3]] << 16 |
795 table[s[4]] << 12 | table[s[5]] << 8 | table[s[6]] << 4 | table[s[7]]);
796 clsid.Data2 = table[s[9]] << 12 | table[s[10]] << 8 | table[s[11]] << 4 | table[s[12]];
797 clsid.Data3 = table[s[14]] << 12 | table[s[15]] << 8 | table[s[16]] << 4 | table[s[17]];
799 /* these are just sequential bytes */
800 clsid.Data4[0] = table[s[19]] << 4 | table[s[20]];
801 clsid.Data4[1] = table[s[21]] << 4 | table[s[22]];
802 clsid.Data4[2] = table[s[24]] << 4 | table[s[25]];
803 clsid.Data4[3] = table[s[26]] << 4 | table[s[27]];
804 clsid.Data4[4] = table[s[28]] << 4 | table[s[29]];
805 clsid.Data4[5] = table[s[30]] << 4 | table[s[31]];
806 clsid.Data4[6] = table[s[32]] << 4 | table[s[33]];
807 clsid.Data4[7] = table[s[34]] << 4 | table[s[35]];
809 hr = CreateClassMoniker(&clsid, ppmk);
810 if (SUCCEEDED(hr))
811 *pchEaten = (*end == ':' ? end + 1 : end) - szDisplayName;
812 return hr;
815 static HRESULT WINAPI ClassMonikerCF_QueryInterface(LPCLASSFACTORY iface,
816 REFIID riid, LPVOID *ppv)
818 *ppv = NULL;
819 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
821 *ppv = iface;
822 IUnknown_AddRef(iface);
823 return S_OK;
825 return E_NOINTERFACE;
828 static ULONG WINAPI ClassMonikerCF_AddRef(LPCLASSFACTORY iface)
830 return 2; /* non-heap based object */
833 static ULONG WINAPI ClassMonikerCF_Release(LPCLASSFACTORY iface)
835 return 1; /* non-heap based object */
838 static HRESULT WINAPI ClassMonikerCF_CreateInstance(LPCLASSFACTORY iface,
839 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
841 HRESULT hr;
842 IMoniker *pmk;
844 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
846 *ppv = NULL;
848 if (pUnk)
849 return CLASS_E_NOAGGREGATION;
851 hr = CreateClassMoniker(&CLSID_NULL, &pmk);
852 if (FAILED(hr)) return hr;
854 hr = IMoniker_QueryInterface(pmk, riid, ppv);
855 IMoniker_Release(pmk);
857 return hr;
860 static HRESULT WINAPI ClassMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
862 FIXME("(%d), stub!\n",fLock);
863 return S_OK;
866 static const IClassFactoryVtbl ClassMonikerCFVtbl =
868 ClassMonikerCF_QueryInterface,
869 ClassMonikerCF_AddRef,
870 ClassMonikerCF_Release,
871 ClassMonikerCF_CreateInstance,
872 ClassMonikerCF_LockServer
874 static const IClassFactoryVtbl *ClassMonikerCF = &ClassMonikerCFVtbl;
876 HRESULT ClassMonikerCF_Create(REFIID riid, LPVOID *ppv)
878 return IClassFactory_QueryInterface((IClassFactory *)&ClassMonikerCF, riid, ppv);