widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / dlls / ole32 / classmoniker.c
blob662baa3cd39a933ce0d77c8c634531798b3b7d76
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
28 #include "winerror.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "moniker.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38 #define CHARS_IN_GUID 39
40 /* ClassMoniker data structure */
41 typedef struct ClassMoniker
43 IMoniker IMoniker_iface;
44 IROTData IROTData_iface;
45 LONG ref;
46 CLSID clsid; /* clsid identified by this moniker */
47 IUnknown *pMarshal; /* custom marshaler */
48 } ClassMoniker;
50 static inline ClassMoniker *impl_from_IMoniker(IMoniker *iface)
52 return CONTAINING_RECORD(iface, ClassMoniker, IMoniker_iface);
55 static inline ClassMoniker *impl_from_IROTData(IROTData *iface)
57 return CONTAINING_RECORD(iface, ClassMoniker, IROTData_iface);
60 /*******************************************************************************
61 * ClassMoniker_QueryInterface
62 *******************************************************************************/
63 static HRESULT WINAPI ClassMoniker_QueryInterface(IMoniker *iface, REFIID riid, void **ppvObject)
65 ClassMoniker *This = impl_from_IMoniker(iface);
67 TRACE("%p, %s, %p.\n", iface, debugstr_guid(riid), ppvObject);
69 if (!ppvObject)
70 return E_POINTER;
72 *ppvObject = 0;
74 if (IsEqualIID(&IID_IUnknown, riid) ||
75 IsEqualIID(&IID_IPersist, riid) ||
76 IsEqualIID(&IID_IPersistStream, riid) ||
77 IsEqualIID(&IID_IMoniker, riid) ||
78 IsEqualGUID(&CLSID_ClassMoniker, riid))
80 *ppvObject = iface;
82 else if (IsEqualIID(&IID_IROTData, riid))
83 *ppvObject = &This->IROTData_iface;
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 if (!*ppvObject)
95 return E_NOINTERFACE;
97 IMoniker_AddRef(iface);
99 return S_OK;
102 /******************************************************************************
103 * ClassMoniker_AddRef
104 ******************************************************************************/
105 static ULONG WINAPI ClassMoniker_AddRef(IMoniker* iface)
107 ClassMoniker *This = impl_from_IMoniker(iface);
109 TRACE("(%p)\n",This);
111 return InterlockedIncrement(&This->ref);
114 /******************************************************************************
115 * ClassMoniker_Release
116 ******************************************************************************/
117 static ULONG WINAPI ClassMoniker_Release(IMoniker* iface)
119 ClassMoniker *This = impl_from_IMoniker(iface);
120 ULONG ref;
122 TRACE("(%p)\n",This);
124 ref = InterlockedDecrement(&This->ref);
126 /* destroy the object if there are no more references to it */
127 if (ref == 0)
129 if (This->pMarshal) IUnknown_Release(This->pMarshal);
130 HeapFree(GetProcessHeap(),0,This);
133 return ref;
136 /******************************************************************************
137 * ClassMoniker_GetClassID
138 ******************************************************************************/
139 static HRESULT WINAPI ClassMoniker_GetClassID(IMoniker* iface,CLSID *pClassID)
141 TRACE("(%p, %p)\n", iface, pClassID);
143 if (pClassID==NULL)
144 return E_POINTER;
146 *pClassID = CLSID_ClassMoniker;
148 return S_OK;
151 /******************************************************************************
152 * ClassMoniker_IsDirty
153 ******************************************************************************/
154 static HRESULT WINAPI ClassMoniker_IsDirty(IMoniker* iface)
156 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
157 method in the OLE-provided moniker interfaces always return S_FALSE because
158 their internal state never changes. */
160 TRACE("(%p)\n",iface);
162 return S_FALSE;
165 /******************************************************************************
166 * ClassMoniker_Load
167 ******************************************************************************/
168 static HRESULT WINAPI ClassMoniker_Load(IMoniker* iface,IStream* pStm)
170 ClassMoniker *This = impl_from_IMoniker(iface);
171 HRESULT hr;
172 DWORD zero;
174 TRACE("(%p)\n", pStm);
176 hr = IStream_Read(pStm, &This->clsid, sizeof(This->clsid), NULL);
177 if (hr != S_OK) return STG_E_READFAULT;
179 hr = IStream_Read(pStm, &zero, sizeof(zero), NULL);
180 if ((hr != S_OK) || (zero != 0)) return STG_E_READFAULT;
182 return S_OK;
185 /******************************************************************************
186 * ClassMoniker_Save
187 ******************************************************************************/
188 static HRESULT WINAPI ClassMoniker_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
190 ClassMoniker *This = impl_from_IMoniker(iface);
191 HRESULT hr;
192 DWORD zero = 0;
194 TRACE("(%p, %s)\n", pStm, fClearDirty ? "TRUE" : "FALSE");
196 hr = IStream_Write(pStm, &This->clsid, sizeof(This->clsid), NULL);
197 if (FAILED(hr)) return hr;
199 return IStream_Write(pStm, &zero, sizeof(zero), NULL);
202 /******************************************************************************
203 * ClassMoniker_GetSizeMax
204 ******************************************************************************/
205 static HRESULT WINAPI ClassMoniker_GetSizeMax(IMoniker* iface,
206 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
208 TRACE("(%p)\n", pcbSize);
210 pcbSize->QuadPart = sizeof(CLSID) + sizeof(DWORD);
212 return S_OK;
215 /******************************************************************************
216 * ClassMoniker_BindToObject
217 ******************************************************************************/
218 static HRESULT WINAPI ClassMoniker_BindToObject(IMoniker* iface,
219 IBindCtx* pbc,
220 IMoniker* pmkToLeft,
221 REFIID riid,
222 VOID** ppvResult)
224 ClassMoniker *This = impl_from_IMoniker(iface);
225 BIND_OPTS2 bindopts;
226 IClassActivator *pActivator;
227 HRESULT hr;
229 TRACE("(%p, %p, %s, %p)\n", pbc, pmkToLeft, debugstr_guid(riid), ppvResult);
231 bindopts.cbStruct = sizeof(bindopts);
232 IBindCtx_GetBindOptions(pbc, (BIND_OPTS *)&bindopts);
234 if (!pmkToLeft)
235 return CoGetClassObject(&This->clsid, bindopts.dwClassContext, NULL,
236 riid, ppvResult);
237 else
239 hr = IMoniker_BindToObject(pmkToLeft, pbc, NULL, &IID_IClassActivator,
240 (void **)&pActivator);
241 if (FAILED(hr)) return hr;
243 hr = IClassActivator_GetClassObject(pActivator, &This->clsid,
244 bindopts.dwClassContext,
245 bindopts.locale, riid, ppvResult);
247 IClassActivator_Release(pActivator);
249 return hr;
253 /******************************************************************************
254 * ClassMoniker_BindToStorage
255 ******************************************************************************/
256 static HRESULT WINAPI ClassMoniker_BindToStorage(IMoniker* iface,
257 IBindCtx* pbc,
258 IMoniker* pmkToLeft,
259 REFIID riid,
260 VOID** ppvResult)
262 TRACE("(%p, %p, %s, %p)\n", pbc, pmkToLeft, debugstr_guid(riid), ppvResult);
263 return IMoniker_BindToObject(iface, pbc, pmkToLeft, riid, ppvResult);
266 /******************************************************************************
267 * ClassMoniker_Reduce
268 ******************************************************************************/
269 static HRESULT WINAPI ClassMoniker_Reduce(IMoniker* iface,
270 IBindCtx* pbc,
271 DWORD dwReduceHowFar,
272 IMoniker** ppmkToLeft,
273 IMoniker** ppmkReduced)
275 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
277 if (!ppmkReduced)
278 return E_POINTER;
280 IMoniker_AddRef(iface);
282 *ppmkReduced = iface;
284 return MK_S_REDUCED_TO_SELF;
286 /******************************************************************************
287 * ClassMoniker_ComposeWith
288 ******************************************************************************/
289 static HRESULT WINAPI ClassMoniker_ComposeWith(IMoniker* iface,
290 IMoniker* pmkRight,
291 BOOL fOnlyIfNotGeneric,
292 IMoniker** ppmkComposite)
294 HRESULT res=S_OK;
295 DWORD mkSys,mkSys2;
296 IEnumMoniker* penumMk=0;
297 IMoniker *pmostLeftMk=0;
298 IMoniker* tempMkComposite=0;
300 TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
302 if ((ppmkComposite==NULL)||(pmkRight==NULL))
303 return E_POINTER;
305 *ppmkComposite=0;
307 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
309 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
310 if(mkSys==MKSYS_ANTIMONIKER)
311 return res;
313 else
314 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
315 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
317 if(mkSys==MKSYS_GENERICCOMPOSITE){
319 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
321 if (FAILED(res))
322 return res;
324 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
326 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
328 if(mkSys2==MKSYS_ANTIMONIKER){
330 IMoniker_Release(pmostLeftMk);
332 tempMkComposite=iface;
333 IMoniker_AddRef(iface);
335 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
337 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
339 IMoniker_Release(tempMkComposite);
340 IMoniker_Release(pmostLeftMk);
342 tempMkComposite=*ppmkComposite;
343 IMoniker_AddRef(tempMkComposite);
345 return res;
347 else
348 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
350 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
351 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
352 a NULL moniker and a return value of MK_E_NEEDGENERIC */
353 else
354 if (!fOnlyIfNotGeneric)
355 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
357 else
358 return MK_E_NEEDGENERIC;
361 /******************************************************************************
362 * ClassMoniker_Enum
363 ******************************************************************************/
364 static HRESULT WINAPI ClassMoniker_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
366 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
368 if (ppenumMoniker == NULL)
369 return E_POINTER;
371 *ppenumMoniker = NULL;
373 return S_OK;
376 /******************************************************************************
377 * ClassMoniker_IsEqual
378 ******************************************************************************/
379 static HRESULT WINAPI ClassMoniker_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
382 CLSID clsid;
383 LPOLESTR dispName1,dispName2;
384 IBindCtx* bind;
385 HRESULT res = S_FALSE;
387 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
389 if (!pmkOtherMoniker) return S_FALSE;
392 /* check if both are ClassMoniker */
393 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
394 if(!IsEqualCLSID(&clsid,&CLSID_ClassMoniker)) return S_FALSE;
396 /* check if both displaynames are the same */
397 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
398 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
399 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
400 if(wcscmp(dispName1,dispName2)==0) res = S_OK;
401 CoTaskMemFree(dispName2);
403 CoTaskMemFree(dispName1);
406 return res;
409 /******************************************************************************
410 * ClassMoniker_Hash
411 ******************************************************************************/
412 static HRESULT WINAPI ClassMoniker_Hash(IMoniker* iface,DWORD* pdwHash)
414 ClassMoniker *This = impl_from_IMoniker(iface);
416 TRACE("(%p)\n", pdwHash);
418 *pdwHash = This->clsid.Data1;
420 return S_OK;
423 /******************************************************************************
424 * ClassMoniker_IsRunning
425 ******************************************************************************/
426 static HRESULT WINAPI ClassMoniker_IsRunning(IMoniker* iface,
427 IBindCtx* pbc,
428 IMoniker* pmkToLeft,
429 IMoniker* pmkNewlyRunning)
431 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
433 /* as in native */
434 return E_NOTIMPL;
437 /******************************************************************************
438 * ClassMoniker_GetTimeOfLastChange
439 ******************************************************************************/
440 static HRESULT WINAPI ClassMoniker_GetTimeOfLastChange(IMoniker* iface,
441 IBindCtx* pbc,
442 IMoniker* pmkToLeft,
443 FILETIME* pItemTime)
445 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pItemTime);
447 return MK_E_UNAVAILABLE;
450 /******************************************************************************
451 * ClassMoniker_Inverse
452 ******************************************************************************/
453 static HRESULT WINAPI ClassMoniker_Inverse(IMoniker* iface,IMoniker** ppmk)
455 TRACE("(%p)\n",ppmk);
457 if (!ppmk)
458 return E_POINTER;
460 return CreateAntiMoniker(ppmk);
463 /******************************************************************************
464 * ClassMoniker_CommonPrefixWith
465 ******************************************************************************/
466 static HRESULT WINAPI ClassMoniker_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
468 DWORD mkSys;
470 TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
472 *ppmkPrefix = NULL;
474 IMoniker_IsSystemMoniker(pmkOther, &mkSys);
476 /* If the other moniker is an class moniker that is equal to this moniker, this method sets *ppmkPrefix */
477 /* to this moniker and returns MK_S_US */
479 if (mkSys == MKSYS_CLASSMONIKER)
481 if (IMoniker_IsEqual(iface, pmkOther) == S_OK)
483 *ppmkPrefix = iface;
485 IMoniker_AddRef(iface);
487 return MK_S_US;
489 else
490 return MK_E_NOPREFIX;
492 else
493 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
494 /* the case where the other moniker is a generic composite. */
495 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
498 /******************************************************************************
499 * ClassMoniker_RelativePathTo
500 ******************************************************************************/
501 static HRESULT WINAPI ClassMoniker_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
503 TRACE("(%p, %p)\n",pmOther,ppmkRelPath);
505 if (!ppmkRelPath)
506 return E_POINTER;
508 *ppmkRelPath = NULL;
510 return MK_E_NOTBINDABLE;
513 /******************************************************************************
514 * ClassMoniker_GetDisplayName
515 ******************************************************************************/
516 static HRESULT WINAPI ClassMoniker_GetDisplayName(IMoniker* iface,
517 IBindCtx* pbc,
518 IMoniker* pmkToLeft,
519 LPOLESTR *ppszDisplayName)
521 ClassMoniker *This = impl_from_IMoniker(iface);
522 static const WCHAR wszClsidPrefix[] = L"clsid:";
524 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
526 if (!ppszDisplayName)
527 return E_POINTER;
529 if (pmkToLeft)
530 return E_INVALIDARG;
532 *ppszDisplayName = CoTaskMemAlloc(sizeof(wszClsidPrefix) + (CHARS_IN_GUID-2) * sizeof(WCHAR));
534 StringFromGUID2(&This->clsid, *ppszDisplayName+ARRAY_SIZE(wszClsidPrefix)-2, CHARS_IN_GUID);
536 /* note: this overwrites the opening curly bracket of the CLSID string generated above */
537 memcpy(*ppszDisplayName, wszClsidPrefix, sizeof(wszClsidPrefix)-sizeof(WCHAR));
539 /* note: this overwrites the closing curly bracket of the CLSID string generated above */
540 (*ppszDisplayName)[ARRAY_SIZE(wszClsidPrefix)-2+CHARS_IN_GUID-2] = ':';
541 (*ppszDisplayName)[ARRAY_SIZE(wszClsidPrefix)-2+CHARS_IN_GUID-1] = '\0';
543 TRACE("string is %s\n", debugstr_w(*ppszDisplayName));
544 return S_OK;
547 /******************************************************************************
548 * ClassMoniker_ParseDisplayName
549 ******************************************************************************/
550 static HRESULT WINAPI ClassMoniker_ParseDisplayName(IMoniker* iface,
551 IBindCtx* pbc,
552 IMoniker* pmkToLeft,
553 LPOLESTR pszDisplayName,
554 ULONG* pchEaten,
555 IMoniker** ppmkOut)
557 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
558 return E_NOTIMPL;
561 /******************************************************************************
562 * ClassMoniker_IsSystemMoniker
563 ******************************************************************************/
564 static HRESULT WINAPI ClassMoniker_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
566 TRACE("(%p,%p)\n",iface,pwdMksys);
568 if (!pwdMksys)
569 return E_POINTER;
571 *pwdMksys = MKSYS_CLASSMONIKER;
573 return S_OK;
576 /*******************************************************************************
577 * ClassMonikerIROTData_QueryInterface
578 *******************************************************************************/
579 static HRESULT WINAPI ClassMonikerROTData_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
582 ClassMoniker *This = impl_from_IROTData(iface);
584 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
586 return IMoniker_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
589 /***********************************************************************
590 * ClassMonikerIROTData_AddRef
592 static ULONG WINAPI ClassMonikerROTData_AddRef(IROTData *iface)
594 ClassMoniker *This = impl_from_IROTData(iface);
596 TRACE("(%p)\n",iface);
598 return IMoniker_AddRef(&This->IMoniker_iface);
601 /***********************************************************************
602 * ClassMonikerIROTData_Release
604 static ULONG WINAPI ClassMonikerROTData_Release(IROTData* iface)
606 ClassMoniker *This = impl_from_IROTData(iface);
608 TRACE("(%p)\n",iface);
610 return IMoniker_Release(&This->IMoniker_iface);
613 /******************************************************************************
614 * ClassMonikerIROTData_GetComparisonData
615 ******************************************************************************/
616 static HRESULT WINAPI ClassMonikerROTData_GetComparisonData(IROTData* iface,
617 BYTE* pbData,
618 ULONG cbMax,
619 ULONG* pcbData)
621 ClassMoniker *This = impl_from_IROTData(iface);
623 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
625 *pcbData = 2*sizeof(CLSID);
626 if (cbMax < *pcbData)
627 return E_OUTOFMEMORY;
629 /* write CLSID of the moniker */
630 memcpy(pbData, &CLSID_ClassMoniker, sizeof(CLSID));
631 /* write CLSID the moniker represents */
632 memcpy(pbData+sizeof(CLSID), &This->clsid, sizeof(CLSID));
634 return S_OK;
637 /********************************************************************************/
638 /* Virtual function table for the ClassMoniker class which include IPersist,*/
639 /* IPersistStream and IMoniker functions. */
640 static const IMonikerVtbl ClassMonikerVtbl =
642 ClassMoniker_QueryInterface,
643 ClassMoniker_AddRef,
644 ClassMoniker_Release,
645 ClassMoniker_GetClassID,
646 ClassMoniker_IsDirty,
647 ClassMoniker_Load,
648 ClassMoniker_Save,
649 ClassMoniker_GetSizeMax,
650 ClassMoniker_BindToObject,
651 ClassMoniker_BindToStorage,
652 ClassMoniker_Reduce,
653 ClassMoniker_ComposeWith,
654 ClassMoniker_Enum,
655 ClassMoniker_IsEqual,
656 ClassMoniker_Hash,
657 ClassMoniker_IsRunning,
658 ClassMoniker_GetTimeOfLastChange,
659 ClassMoniker_Inverse,
660 ClassMoniker_CommonPrefixWith,
661 ClassMoniker_RelativePathTo,
662 ClassMoniker_GetDisplayName,
663 ClassMoniker_ParseDisplayName,
664 ClassMoniker_IsSystemMoniker
667 /********************************************************************************/
668 /* Virtual function table for the IROTData class. */
669 static const IROTDataVtbl ROTDataVtbl =
671 ClassMonikerROTData_QueryInterface,
672 ClassMonikerROTData_AddRef,
673 ClassMonikerROTData_Release,
674 ClassMonikerROTData_GetComparisonData
677 /******************************************************************************
678 * ClassMoniker_Construct (local function)
679 *******************************************************************************/
680 static HRESULT ClassMoniker_Construct(ClassMoniker* This, REFCLSID rclsid)
682 TRACE("(%p,%s)\n",This,debugstr_guid(rclsid));
684 /* Initialize the virtual function table. */
685 This->IMoniker_iface.lpVtbl = &ClassMonikerVtbl;
686 This->IROTData_iface.lpVtbl = &ROTDataVtbl;
687 This->ref = 0;
688 This->clsid = *rclsid;
689 This->pMarshal = NULL;
691 return S_OK;
694 /******************************************************************************
695 * CreateClassMoniker [OLE32.@]
696 ******************************************************************************/
697 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker **ppmk)
699 ClassMoniker* newClassMoniker;
700 HRESULT hr;
702 TRACE("(%s,%p)\n", debugstr_guid(rclsid), ppmk);
704 newClassMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassMoniker));
706 if (!newClassMoniker)
707 return STG_E_INSUFFICIENTMEMORY;
709 hr = ClassMoniker_Construct(newClassMoniker, rclsid);
711 if (FAILED(hr))
713 HeapFree(GetProcessHeap(), 0, newClassMoniker);
714 return hr;
717 return ClassMoniker_QueryInterface(&newClassMoniker->IMoniker_iface, &IID_IMoniker,
718 (void**)ppmk);
721 HRESULT ClassMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName, LPDWORD pchEaten,
722 IMoniker **ppmk)
724 HRESULT hr;
725 LPCWSTR s = wcschr(szDisplayName, ':');
726 LPCWSTR end;
727 CLSID clsid;
728 BYTE table[256];
729 int i;
731 if (!s)
732 return MK_E_SYNTAX;
734 s++;
736 for (end = s; *end && (*end != ':'); end++)
739 TRACE("parsing %s\n", debugstr_wn(s, end - s));
741 /* validate the CLSID string */
742 if (s[0] == '{')
744 if ((end - s != 38) || (s[37] != '}'))
745 return MK_E_SYNTAX;
746 s++;
748 else
750 if (end - s != 36)
751 return MK_E_SYNTAX;
754 for (i=0; i<36; i++)
756 if ((i == 8)||(i == 13)||(i == 18)||(i == 23))
758 if (s[i] != '-')
759 return MK_E_SYNTAX;
760 continue;
762 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
763 ((s[i] >= 'a') && (s[i] <= 'f')) ||
764 ((s[i] >= 'A') && (s[i] <= 'F'))))
765 return MK_E_SYNTAX;
768 /* quick lookup table */
769 memset(table, 0, 256);
771 for (i = 0; i < 10; i++)
772 table['0' + i] = i;
773 for (i = 0; i < 6; i++)
775 table['A' + i] = i+10;
776 table['a' + i] = i+10;
779 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
781 clsid.Data1 = (table[s[0]] << 28 | table[s[1]] << 24 | table[s[2]] << 20 | table[s[3]] << 16 |
782 table[s[4]] << 12 | table[s[5]] << 8 | table[s[6]] << 4 | table[s[7]]);
783 clsid.Data2 = table[s[9]] << 12 | table[s[10]] << 8 | table[s[11]] << 4 | table[s[12]];
784 clsid.Data3 = table[s[14]] << 12 | table[s[15]] << 8 | table[s[16]] << 4 | table[s[17]];
786 /* these are just sequential bytes */
787 clsid.Data4[0] = table[s[19]] << 4 | table[s[20]];
788 clsid.Data4[1] = table[s[21]] << 4 | table[s[22]];
789 clsid.Data4[2] = table[s[24]] << 4 | table[s[25]];
790 clsid.Data4[3] = table[s[26]] << 4 | table[s[27]];
791 clsid.Data4[4] = table[s[28]] << 4 | table[s[29]];
792 clsid.Data4[5] = table[s[30]] << 4 | table[s[31]];
793 clsid.Data4[6] = table[s[32]] << 4 | table[s[33]];
794 clsid.Data4[7] = table[s[34]] << 4 | table[s[35]];
796 hr = CreateClassMoniker(&clsid, ppmk);
797 if (SUCCEEDED(hr))
798 *pchEaten = (*end == ':' ? end + 1 : end) - szDisplayName;
799 return hr;
802 HRESULT WINAPI ClassMoniker_CreateInstance(IClassFactory *iface,
803 IUnknown *pUnk, REFIID riid, void **ppv)
805 HRESULT hr;
806 IMoniker *pmk;
808 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
810 *ppv = NULL;
812 if (pUnk)
813 return CLASS_E_NOAGGREGATION;
815 hr = CreateClassMoniker(&CLSID_NULL, &pmk);
816 if (FAILED(hr)) return hr;
818 hr = IMoniker_QueryInterface(pmk, riid, ppv);
819 IMoniker_Release(pmk);
821 return hr;