msvcrt: _filbuf should not lock any file.
[wine.git] / dlls / ole32 / classmoniker.c
blob89c7297c4d2e362e823b4bf78f3443db8449bcd5
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 IMoniker IMoniker_iface;
47 IROTData IROTData_iface;
48 LONG ref;
49 CLSID clsid; /* clsid identified by this moniker */
50 IUnknown *pMarshal; /* custom marshaler */
51 } ClassMoniker;
53 static inline ClassMoniker *impl_from_IMoniker(IMoniker *iface)
55 return CONTAINING_RECORD(iface, ClassMoniker, IMoniker_iface);
58 static inline ClassMoniker *impl_from_IROTData(IROTData *iface)
60 return CONTAINING_RECORD(iface, ClassMoniker, IROTData_iface);
63 /*******************************************************************************
64 * ClassMoniker_QueryInterface
65 *******************************************************************************/
66 static HRESULT WINAPI ClassMoniker_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
68 ClassMoniker *This = impl_from_IMoniker(iface);
70 TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
72 /* Perform a sanity check on the parameters.*/
73 if (!ppvObject)
74 return E_POINTER;
76 /* Initialize the return parameter */
77 *ppvObject = 0;
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))
85 *ppvObject = iface;
87 else if (IsEqualIID(&IID_IROTData, riid))
88 *ppvObject = &This->IROTData_iface;
89 else if (IsEqualIID(&IID_IMarshal, riid))
91 HRESULT hr = S_OK;
92 if (!This->pMarshal)
93 hr = MonikerMarshal_Create(iface, &This->pMarshal);
94 if (hr != S_OK)
95 return hr;
96 return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
99 /* Check that we obtained an interface.*/
100 if (!*ppvObject)
101 return E_NOINTERFACE;
103 /* Query Interface always increases the reference count by one when it is successful */
104 IMoniker_AddRef(iface);
106 return S_OK;
109 /******************************************************************************
110 * ClassMoniker_AddRef
111 ******************************************************************************/
112 static ULONG WINAPI ClassMoniker_AddRef(IMoniker* iface)
114 ClassMoniker *This = impl_from_IMoniker(iface);
116 TRACE("(%p)\n",This);
118 return InterlockedIncrement(&This->ref);
121 /******************************************************************************
122 * ClassMoniker_Release
123 ******************************************************************************/
124 static ULONG WINAPI ClassMoniker_Release(IMoniker* iface)
126 ClassMoniker *This = impl_from_IMoniker(iface);
127 ULONG ref;
129 TRACE("(%p)\n",This);
131 ref = InterlockedDecrement(&This->ref);
133 /* destroy the object if there are no more references to it */
134 if (ref == 0)
136 if (This->pMarshal) IUnknown_Release(This->pMarshal);
137 HeapFree(GetProcessHeap(),0,This);
140 return ref;
143 /******************************************************************************
144 * ClassMoniker_GetClassID
145 ******************************************************************************/
146 static HRESULT WINAPI ClassMoniker_GetClassID(IMoniker* iface,CLSID *pClassID)
148 TRACE("(%p,%p),stub!\n",iface,pClassID);
150 if (pClassID==NULL)
151 return E_POINTER;
153 *pClassID = CLSID_ClassMoniker;
155 return S_OK;
158 /******************************************************************************
159 * ClassMoniker_IsDirty
160 ******************************************************************************/
161 static HRESULT WINAPI ClassMoniker_IsDirty(IMoniker* iface)
163 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
164 method in the OLE-provided moniker interfaces always return S_FALSE because
165 their internal state never changes. */
167 TRACE("(%p)\n",iface);
169 return S_FALSE;
172 /******************************************************************************
173 * ClassMoniker_Load
174 ******************************************************************************/
175 static HRESULT WINAPI ClassMoniker_Load(IMoniker* iface,IStream* pStm)
177 ClassMoniker *This = impl_from_IMoniker(iface);
178 HRESULT hr;
179 DWORD zero;
181 TRACE("(%p)\n", pStm);
183 hr = IStream_Read(pStm, &This->clsid, sizeof(This->clsid), NULL);
184 if (hr != S_OK) return STG_E_READFAULT;
186 hr = IStream_Read(pStm, &zero, sizeof(zero), NULL);
187 if ((hr != S_OK) || (zero != 0)) return STG_E_READFAULT;
189 return S_OK;
192 /******************************************************************************
193 * ClassMoniker_Save
194 ******************************************************************************/
195 static HRESULT WINAPI ClassMoniker_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
197 ClassMoniker *This = impl_from_IMoniker(iface);
198 HRESULT hr;
199 DWORD zero = 0;
201 TRACE("(%p, %s)\n", pStm, fClearDirty ? "TRUE" : "FALSE");
203 hr = IStream_Write(pStm, &This->clsid, sizeof(This->clsid), NULL);
204 if (FAILED(hr)) return hr;
206 return IStream_Write(pStm, &zero, sizeof(zero), NULL);
209 /******************************************************************************
210 * ClassMoniker_GetSizeMax
211 ******************************************************************************/
212 static HRESULT WINAPI ClassMoniker_GetSizeMax(IMoniker* iface,
213 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
215 TRACE("(%p)\n", pcbSize);
217 pcbSize->QuadPart = sizeof(CLSID) + sizeof(DWORD);
219 return S_OK;
222 /******************************************************************************
223 * ClassMoniker_BindToObject
224 ******************************************************************************/
225 static HRESULT WINAPI ClassMoniker_BindToObject(IMoniker* iface,
226 IBindCtx* pbc,
227 IMoniker* pmkToLeft,
228 REFIID riid,
229 VOID** ppvResult)
231 ClassMoniker *This = impl_from_IMoniker(iface);
232 BIND_OPTS2 bindopts;
233 IClassActivator *pActivator;
234 HRESULT hr;
236 TRACE("(%p,%p,%p,%p)\n", pbc, pmkToLeft, riid, ppvResult);
238 bindopts.cbStruct = sizeof(bindopts);
239 IBindCtx_GetBindOptions(pbc, (BIND_OPTS *)&bindopts);
241 if (!pmkToLeft)
242 return CoGetClassObject(&This->clsid, bindopts.dwClassContext, NULL,
243 riid, ppvResult);
244 else
246 hr = IMoniker_BindToObject(pmkToLeft, pbc, NULL, &IID_IClassActivator,
247 (void **)&pActivator);
248 if (FAILED(hr)) return hr;
250 hr = IClassActivator_GetClassObject(pActivator, &This->clsid,
251 bindopts.dwClassContext,
252 bindopts.locale, riid, ppvResult);
254 IClassActivator_Release(pActivator);
256 return hr;
260 /******************************************************************************
261 * ClassMoniker_BindToStorage
262 ******************************************************************************/
263 static HRESULT WINAPI ClassMoniker_BindToStorage(IMoniker* iface,
264 IBindCtx* pbc,
265 IMoniker* pmkToLeft,
266 REFIID riid,
267 VOID** ppvResult)
269 TRACE("(%p,%p,%p,%p)\n",pbc, pmkToLeft, riid, ppvResult);
270 return IMoniker_BindToObject(iface, pbc, pmkToLeft, riid, ppvResult);
273 /******************************************************************************
274 * ClassMoniker_Reduce
275 ******************************************************************************/
276 static HRESULT WINAPI ClassMoniker_Reduce(IMoniker* iface,
277 IBindCtx* pbc,
278 DWORD dwReduceHowFar,
279 IMoniker** ppmkToLeft,
280 IMoniker** ppmkReduced)
282 TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
284 if (!ppmkReduced)
285 return E_POINTER;
287 IMoniker_AddRef(iface);
289 *ppmkReduced = iface;
291 return MK_S_REDUCED_TO_SELF;
293 /******************************************************************************
294 * ClassMoniker_ComposeWith
295 ******************************************************************************/
296 static HRESULT WINAPI ClassMoniker_ComposeWith(IMoniker* iface,
297 IMoniker* pmkRight,
298 BOOL fOnlyIfNotGeneric,
299 IMoniker** ppmkComposite)
301 HRESULT res=S_OK;
302 DWORD mkSys,mkSys2;
303 IEnumMoniker* penumMk=0;
304 IMoniker *pmostLeftMk=0;
305 IMoniker* tempMkComposite=0;
307 TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
309 if ((ppmkComposite==NULL)||(pmkRight==NULL))
310 return E_POINTER;
312 *ppmkComposite=0;
314 IMoniker_IsSystemMoniker(pmkRight,&mkSys);
316 /* If pmkRight is an anti-moniker, the returned moniker is NULL */
317 if(mkSys==MKSYS_ANTIMONIKER)
318 return res;
320 else
321 /* if pmkRight is a composite whose leftmost component is an anti-moniker, */
322 /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
324 if(mkSys==MKSYS_GENERICCOMPOSITE){
326 res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
328 if (FAILED(res))
329 return res;
331 res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
333 IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
335 if(mkSys2==MKSYS_ANTIMONIKER){
337 IMoniker_Release(pmostLeftMk);
339 tempMkComposite=iface;
340 IMoniker_AddRef(iface);
342 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
344 res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
346 IMoniker_Release(tempMkComposite);
347 IMoniker_Release(pmostLeftMk);
349 tempMkComposite=*ppmkComposite;
350 IMoniker_AddRef(tempMkComposite);
352 return res;
354 else
355 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
357 /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
358 composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
359 a NULL moniker and a return value of MK_E_NEEDGENERIC */
360 else
361 if (!fOnlyIfNotGeneric)
362 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
364 else
365 return MK_E_NEEDGENERIC;
368 /******************************************************************************
369 * ClassMoniker_Enum
370 ******************************************************************************/
371 static HRESULT WINAPI ClassMoniker_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
373 TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
375 if (ppenumMoniker == NULL)
376 return E_POINTER;
378 *ppenumMoniker = NULL;
380 return S_OK;
383 /******************************************************************************
384 * ClassMoniker_IsEqual
385 ******************************************************************************/
386 static HRESULT WINAPI ClassMoniker_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
389 CLSID clsid;
390 LPOLESTR dispName1,dispName2;
391 IBindCtx* bind;
392 HRESULT res = S_FALSE;
394 TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
396 if (!pmkOtherMoniker) return S_FALSE;
399 /* check if both are ClassMoniker */
400 if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
401 if(!IsEqualCLSID(&clsid,&CLSID_ClassMoniker)) return S_FALSE;
403 /* check if both displaynames are the same */
404 if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
405 if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
406 if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
407 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
408 CoTaskMemFree(dispName2);
410 CoTaskMemFree(dispName1);
413 return res;
416 /******************************************************************************
417 * ClassMoniker_Hash
418 ******************************************************************************/
419 static HRESULT WINAPI ClassMoniker_Hash(IMoniker* iface,DWORD* pdwHash)
421 ClassMoniker *This = impl_from_IMoniker(iface);
423 TRACE("(%p)\n", pdwHash);
425 *pdwHash = This->clsid.Data1;
427 return S_OK;
430 /******************************************************************************
431 * ClassMoniker_IsRunning
432 ******************************************************************************/
433 static HRESULT WINAPI ClassMoniker_IsRunning(IMoniker* iface,
434 IBindCtx* pbc,
435 IMoniker* pmkToLeft,
436 IMoniker* pmkNewlyRunning)
438 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
440 /* as in native */
441 return E_NOTIMPL;
444 /******************************************************************************
445 * ClassMoniker_GetTimeOfLastChange
446 ******************************************************************************/
447 static HRESULT WINAPI ClassMoniker_GetTimeOfLastChange(IMoniker* iface,
448 IBindCtx* pbc,
449 IMoniker* pmkToLeft,
450 FILETIME* pItemTime)
452 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pItemTime);
454 return MK_E_UNAVAILABLE;
457 /******************************************************************************
458 * ClassMoniker_Inverse
459 ******************************************************************************/
460 static HRESULT WINAPI ClassMoniker_Inverse(IMoniker* iface,IMoniker** ppmk)
462 TRACE("(%p)\n",ppmk);
464 if (!ppmk)
465 return E_POINTER;
467 return CreateAntiMoniker(ppmk);
470 /******************************************************************************
471 * ClassMoniker_CommonPrefixWith
472 ******************************************************************************/
473 static HRESULT WINAPI ClassMoniker_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
475 DWORD mkSys;
477 TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
479 *ppmkPrefix = NULL;
481 IMoniker_IsSystemMoniker(pmkOther, &mkSys);
483 /* If the other moniker is an class moniker that is equal to this moniker, this method sets *ppmkPrefix */
484 /* to this moniker and returns MK_S_US */
486 if (mkSys == MKSYS_CLASSMONIKER)
488 if (IMoniker_IsEqual(iface, pmkOther) == S_OK)
490 *ppmkPrefix = iface;
492 IMoniker_AddRef(iface);
494 return MK_S_US;
496 else
497 return MK_E_NOPREFIX;
499 else
500 /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
501 /* the case where the other moniker is a generic composite. */
502 return MonikerCommonPrefixWith(iface, pmkOther, ppmkPrefix);
505 /******************************************************************************
506 * ClassMoniker_RelativePathTo
507 ******************************************************************************/
508 static HRESULT WINAPI ClassMoniker_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
510 TRACE("(%p, %p)\n",pmOther,ppmkRelPath);
512 if (!ppmkRelPath)
513 return E_POINTER;
515 *ppmkRelPath = NULL;
517 return MK_E_NOTBINDABLE;
520 /******************************************************************************
521 * ClassMoniker_GetDisplayName
522 ******************************************************************************/
523 static HRESULT WINAPI ClassMoniker_GetDisplayName(IMoniker* iface,
524 IBindCtx* pbc,
525 IMoniker* pmkToLeft,
526 LPOLESTR *ppszDisplayName)
528 ClassMoniker *This = impl_from_IMoniker(iface);
529 static const WCHAR wszClsidPrefix[] = {'c','l','s','i','d',':',0};
531 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
533 if (!ppszDisplayName)
534 return E_POINTER;
536 if (pmkToLeft)
537 return E_INVALIDARG;
539 *ppszDisplayName = CoTaskMemAlloc(sizeof(wszClsidPrefix) + (CHARS_IN_GUID-2) * sizeof(WCHAR));
541 StringFromGUID2(&This->clsid, *ppszDisplayName+sizeof(wszClsidPrefix)/sizeof(WCHAR)-2, CHARS_IN_GUID);
543 /* note: this overwrites the opening curly bracket of the CLSID string generated above */
544 memcpy(*ppszDisplayName, wszClsidPrefix, sizeof(wszClsidPrefix)-sizeof(WCHAR));
546 /* note: this overwrites the closing curly bracket of the CLSID string generated above */
547 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-2] = ':';
548 (*ppszDisplayName)[sizeof(wszClsidPrefix)/sizeof(WCHAR)-2+CHARS_IN_GUID-1] = '\0';
550 TRACE("string is %s\n", debugstr_w(*ppszDisplayName));
551 return S_OK;
554 /******************************************************************************
555 * ClassMoniker_ParseDisplayName
556 ******************************************************************************/
557 static HRESULT WINAPI ClassMoniker_ParseDisplayName(IMoniker* iface,
558 IBindCtx* pbc,
559 IMoniker* pmkToLeft,
560 LPOLESTR pszDisplayName,
561 ULONG* pchEaten,
562 IMoniker** ppmkOut)
564 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
565 return E_NOTIMPL;
568 /******************************************************************************
569 * ClassMoniker_IsSystemMoniker
570 ******************************************************************************/
571 static HRESULT WINAPI ClassMoniker_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
573 TRACE("(%p,%p)\n",iface,pwdMksys);
575 if (!pwdMksys)
576 return E_POINTER;
578 *pwdMksys = MKSYS_CLASSMONIKER;
580 return S_OK;
583 /*******************************************************************************
584 * ClassMonikerIROTData_QueryInterface
585 *******************************************************************************/
586 static HRESULT WINAPI ClassMonikerROTData_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
589 ClassMoniker *This = impl_from_IROTData(iface);
591 TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
593 return IMoniker_QueryInterface(&This->IMoniker_iface, riid, ppvObject);
596 /***********************************************************************
597 * ClassMonikerIROTData_AddRef
599 static ULONG WINAPI ClassMonikerROTData_AddRef(IROTData *iface)
601 ClassMoniker *This = impl_from_IROTData(iface);
603 TRACE("(%p)\n",iface);
605 return IMoniker_AddRef(&This->IMoniker_iface);
608 /***********************************************************************
609 * ClassMonikerIROTData_Release
611 static ULONG WINAPI ClassMonikerROTData_Release(IROTData* iface)
613 ClassMoniker *This = impl_from_IROTData(iface);
615 TRACE("(%p)\n",iface);
617 return IMoniker_Release(&This->IMoniker_iface);
620 /******************************************************************************
621 * ClassMonikerIROTData_GetComparisonData
622 ******************************************************************************/
623 static HRESULT WINAPI ClassMonikerROTData_GetComparisonData(IROTData* iface,
624 BYTE* pbData,
625 ULONG cbMax,
626 ULONG* pcbData)
628 ClassMoniker *This = impl_from_IROTData(iface);
630 TRACE("(%p, %u, %p)\n", pbData, cbMax, pcbData);
632 *pcbData = 2*sizeof(CLSID);
633 if (cbMax < *pcbData)
634 return E_OUTOFMEMORY;
636 /* write CLSID of the moniker */
637 memcpy(pbData, &CLSID_ClassMoniker, sizeof(CLSID));
638 /* write CLSID the moniker represents */
639 memcpy(pbData+sizeof(CLSID), &This->clsid, sizeof(CLSID));
641 return S_OK;
644 /********************************************************************************/
645 /* Virtual function table for the ClassMoniker class which include IPersist,*/
646 /* IPersistStream and IMoniker functions. */
647 static const IMonikerVtbl ClassMonikerVtbl =
649 ClassMoniker_QueryInterface,
650 ClassMoniker_AddRef,
651 ClassMoniker_Release,
652 ClassMoniker_GetClassID,
653 ClassMoniker_IsDirty,
654 ClassMoniker_Load,
655 ClassMoniker_Save,
656 ClassMoniker_GetSizeMax,
657 ClassMoniker_BindToObject,
658 ClassMoniker_BindToStorage,
659 ClassMoniker_Reduce,
660 ClassMoniker_ComposeWith,
661 ClassMoniker_Enum,
662 ClassMoniker_IsEqual,
663 ClassMoniker_Hash,
664 ClassMoniker_IsRunning,
665 ClassMoniker_GetTimeOfLastChange,
666 ClassMoniker_Inverse,
667 ClassMoniker_CommonPrefixWith,
668 ClassMoniker_RelativePathTo,
669 ClassMoniker_GetDisplayName,
670 ClassMoniker_ParseDisplayName,
671 ClassMoniker_IsSystemMoniker
674 /********************************************************************************/
675 /* Virtual function table for the IROTData class. */
676 static const IROTDataVtbl ROTDataVtbl =
678 ClassMonikerROTData_QueryInterface,
679 ClassMonikerROTData_AddRef,
680 ClassMonikerROTData_Release,
681 ClassMonikerROTData_GetComparisonData
684 /******************************************************************************
685 * ClassMoniker_Construct (local function)
686 *******************************************************************************/
687 static HRESULT ClassMoniker_Construct(ClassMoniker* This, REFCLSID rclsid)
689 TRACE("(%p,%s)\n",This,debugstr_guid(rclsid));
691 /* Initialize the virtual function table. */
692 This->IMoniker_iface.lpVtbl = &ClassMonikerVtbl;
693 This->IROTData_iface.lpVtbl = &ROTDataVtbl;
694 This->ref = 0;
695 This->clsid = *rclsid;
696 This->pMarshal = NULL;
698 return S_OK;
701 /******************************************************************************
702 * CreateClassMoniker [OLE32.@]
703 ******************************************************************************/
704 HRESULT WINAPI CreateClassMoniker(REFCLSID rclsid, IMoniker **ppmk)
706 ClassMoniker* newClassMoniker;
707 HRESULT hr;
709 TRACE("(%s,%p)\n", debugstr_guid(rclsid), ppmk);
711 newClassMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassMoniker));
713 if (!newClassMoniker)
714 return STG_E_INSUFFICIENTMEMORY;
716 hr = ClassMoniker_Construct(newClassMoniker, rclsid);
718 if (FAILED(hr))
720 HeapFree(GetProcessHeap(), 0, newClassMoniker);
721 return hr;
724 return ClassMoniker_QueryInterface(&newClassMoniker->IMoniker_iface, &IID_IMoniker,
725 (void**)ppmk);
728 HRESULT ClassMoniker_CreateFromDisplayName(LPBC pbc, LPCOLESTR szDisplayName, LPDWORD pchEaten,
729 IMoniker **ppmk)
731 HRESULT hr;
732 LPCWSTR s = strchrW(szDisplayName, ':');
733 LPCWSTR end;
734 CLSID clsid;
735 BYTE table[256];
736 int i;
738 if (!s)
739 return MK_E_SYNTAX;
741 s++;
743 for (end = s; *end && (*end != ':'); end++)
746 TRACE("parsing %s\n", debugstr_wn(s, end - s));
748 /* validate the CLSID string */
749 if (s[0] == '{')
751 if ((end - s != 38) || (s[37] != '}'))
752 return MK_E_SYNTAX;
753 s++;
755 else
757 if (end - s != 36)
758 return MK_E_SYNTAX;
761 for (i=0; i<36; i++)
763 if ((i == 8)||(i == 13)||(i == 18)||(i == 23))
765 if (s[i] != '-')
766 return MK_E_SYNTAX;
767 continue;
769 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
770 ((s[i] >= 'a') && (s[i] <= 'f')) ||
771 ((s[i] >= 'A') && (s[i] <= 'F'))))
772 return MK_E_SYNTAX;
775 /* quick lookup table */
776 memset(table, 0, 256);
778 for (i = 0; i < 10; i++)
779 table['0' + i] = i;
780 for (i = 0; i < 6; i++)
782 table['A' + i] = i+10;
783 table['a' + i] = i+10;
786 /* in form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX */
788 clsid.Data1 = (table[s[0]] << 28 | table[s[1]] << 24 | table[s[2]] << 20 | table[s[3]] << 16 |
789 table[s[4]] << 12 | table[s[5]] << 8 | table[s[6]] << 4 | table[s[7]]);
790 clsid.Data2 = table[s[9]] << 12 | table[s[10]] << 8 | table[s[11]] << 4 | table[s[12]];
791 clsid.Data3 = table[s[14]] << 12 | table[s[15]] << 8 | table[s[16]] << 4 | table[s[17]];
793 /* these are just sequential bytes */
794 clsid.Data4[0] = table[s[19]] << 4 | table[s[20]];
795 clsid.Data4[1] = table[s[21]] << 4 | table[s[22]];
796 clsid.Data4[2] = table[s[24]] << 4 | table[s[25]];
797 clsid.Data4[3] = table[s[26]] << 4 | table[s[27]];
798 clsid.Data4[4] = table[s[28]] << 4 | table[s[29]];
799 clsid.Data4[5] = table[s[30]] << 4 | table[s[31]];
800 clsid.Data4[6] = table[s[32]] << 4 | table[s[33]];
801 clsid.Data4[7] = table[s[34]] << 4 | table[s[35]];
803 hr = CreateClassMoniker(&clsid, ppmk);
804 if (SUCCEEDED(hr))
805 *pchEaten = (*end == ':' ? end + 1 : end) - szDisplayName;
806 return hr;
809 static HRESULT WINAPI ClassMonikerCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
811 *ppv = NULL;
812 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
814 *ppv = iface;
815 IClassFactory_AddRef(iface);
816 return S_OK;
818 return E_NOINTERFACE;
821 static ULONG WINAPI ClassMonikerCF_AddRef(LPCLASSFACTORY iface)
823 return 2; /* non-heap based object */
826 static ULONG WINAPI ClassMonikerCF_Release(LPCLASSFACTORY iface)
828 return 1; /* non-heap based object */
831 static HRESULT WINAPI ClassMonikerCF_CreateInstance(LPCLASSFACTORY iface,
832 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
834 HRESULT hr;
835 IMoniker *pmk;
837 TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
839 *ppv = NULL;
841 if (pUnk)
842 return CLASS_E_NOAGGREGATION;
844 hr = CreateClassMoniker(&CLSID_NULL, &pmk);
845 if (FAILED(hr)) return hr;
847 hr = IMoniker_QueryInterface(pmk, riid, ppv);
848 IMoniker_Release(pmk);
850 return hr;
853 static HRESULT WINAPI ClassMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
855 FIXME("(%d), stub!\n",fLock);
856 return S_OK;
859 static const IClassFactoryVtbl ClassMonikerCFVtbl =
861 ClassMonikerCF_QueryInterface,
862 ClassMonikerCF_AddRef,
863 ClassMonikerCF_Release,
864 ClassMonikerCF_CreateInstance,
865 ClassMonikerCF_LockServer
868 static IClassFactory ClassMonikerCF = { &ClassMonikerCFVtbl };
870 HRESULT ClassMonikerCF_Create(REFIID riid, LPVOID *ppv)
872 return IClassFactory_QueryInterface(&ClassMonikerCF, riid, ppv);