2 * IEnumMoniker implementation for DEVENUM.dll
4 * Copyright (C) 2002 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * - Implements IEnumMoniker interface which enumerates through moniker
22 * objects created from HKEY_CLASSES/CLSID/{DEVICE_CLSID}/Instance
25 #include "devenum_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
34 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface);
35 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface);
36 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface);
40 const IPropertyBagVtbl *lpVtbl;
46 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
51 RegPropBagImpl *This = (RegPropBagImpl *)iface;
52 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
54 if (This == NULL || ppvObj == NULL) return E_POINTER;
56 if (IsEqualGUID(riid, &IID_IUnknown) ||
57 IsEqualGUID(riid, &IID_IPropertyBag))
59 *ppvObj = (LPVOID)iface;
60 DEVENUM_IPropertyBag_AddRef(iface);
64 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
68 /**********************************************************************
69 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
71 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
73 RegPropBagImpl *This = (RegPropBagImpl *)iface;
76 return InterlockedIncrement(&This->ref);
79 /**********************************************************************
80 * DEVENUM_IPropertyBag_Release (also IUnknown)
82 static ULONG WINAPI DEVENUM_IPropertyBag_Release(LPPROPERTYBAG iface)
84 RegPropBagImpl *This = (RegPropBagImpl *)iface;
89 ref = InterlockedDecrement(&This->ref);
91 RegCloseKey(This->hkey);
93 DEVENUM_UnlockModule();
98 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
100 LPCOLESTR pszPropName,
102 IErrorLog* pErrorLog)
107 RegPropBagImpl *This = (RegPropBagImpl *)iface;
111 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
113 if (!pszPropName || !pVar)
116 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
117 res = HRESULT_FROM_WIN32(reswin32);
121 pData = HeapAlloc(GetProcessHeap(), 0, received);
123 /* work around a GCC bug that occurs here unless we use the reswin32 variable as well */
124 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, &type, (LPBYTE)pData, &received);
125 res = HRESULT_FROM_WIN32(reswin32);
130 res = E_INVALIDARG; /* assume we cannot coerce into right type */
132 TRACE("Read %d bytes (%s)\n", received, type == REG_SZ ? debugstr_w((LPWSTR)pData) : "binary data");
140 V_UNION(pVar, bstrVal) = CoTaskMemAlloc(received);
141 memcpy(V_UNION(pVar, bstrVal), (LPWSTR)pData, received);
145 V_VT(pVar) = VT_BSTR;
148 V_UNION(pVar, bstrVal) = SysAllocStringLen((LPWSTR)pData, received/sizeof(WCHAR) - 1);
154 TRACE("REG_DWORD: %x\n", *(DWORD *)pData);
162 V_UNION(pVar, ulVal) = *(DWORD *)pData;
169 SAFEARRAYBOUND bound;
170 void * pArrayElements;
172 bound.cElements = received;
173 TRACE("REG_BINARY: len = %d\n", received);
177 case VT_ARRAY | VT_UI1:
178 if (!(V_UNION(pVar, parray) = SafeArrayCreate(VT_UI1, 1, &bound)))
184 if (res == E_INVALIDARG)
187 res = SafeArrayAccessData(V_UNION(pVar, parray), &pArrayElements);
191 CopyMemory(pArrayElements, pData, received);
192 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
196 if (res == E_INVALIDARG)
197 FIXME("Variant type %x not supported for regtype %x\n", V_VT(pVar), type);
200 HeapFree(GetProcessHeap(), 0, pData);
202 TRACE("<- %x\n", res);
206 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
208 LPCOLESTR pszPropName,
211 RegPropBagImpl *This = (RegPropBagImpl *)iface;
212 LPVOID lpData = NULL;
217 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
222 TRACE("writing %s\n", debugstr_w(V_UNION(pVar, bstrVal)));
223 lpData = (LPVOID)V_UNION(pVar, bstrVal);
225 cbData = (lstrlenW(V_UNION(pVar, bstrVal)) + 1) * sizeof(WCHAR);
229 TRACE("writing %u\n", V_UNION(pVar, ulVal));
230 lpData = (LPVOID)&V_UNION(pVar, ulVal);
232 cbData = sizeof(DWORD);
234 case VT_ARRAY | VT_UI1:
239 res = SafeArrayGetLBound(V_UNION(pVar, parray), 1, &lLbound);
240 res = SafeArrayGetUBound(V_UNION(pVar, parray), 1, &lUbound);
241 cbData = (lUbound - lLbound + 1) /* * sizeof(BYTE)*/;
242 TRACE("cbData: %d\n", cbData);
243 res = SafeArrayAccessData(V_UNION(pVar, parray), &lpData);
247 FIXME("Variant type %d not handled\n", V_VT(pVar));
251 if (RegSetValueExW(This->hkey,
253 dwType, lpData, cbData) != ERROR_SUCCESS)
256 if (V_VT(pVar) & VT_ARRAY)
257 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
262 static const IPropertyBagVtbl IPropertyBag_Vtbl =
264 DEVENUM_IPropertyBag_QueryInterface,
265 DEVENUM_IPropertyBag_AddRef,
266 DEVENUM_IPropertyBag_Release,
267 DEVENUM_IPropertyBag_Read,
268 DEVENUM_IPropertyBag_Write
271 static HRESULT DEVENUM_IPropertyBag_Construct(HANDLE hkey, IPropertyBag **ppBag)
273 RegPropBagImpl * rpb = CoTaskMemAlloc(sizeof(RegPropBagImpl));
275 return E_OUTOFMEMORY;
276 rpb->lpVtbl = &IPropertyBag_Vtbl;
279 *ppBag = (IPropertyBag*)rpb;
280 DEVENUM_LockModule();
285 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(
290 MediaCatMoniker *This = (MediaCatMoniker *)iface;
291 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
293 if (This == NULL || ppvObj == NULL) return E_POINTER;
297 if (IsEqualGUID(riid, &IID_IUnknown) ||
298 IsEqualGUID(riid, &IID_IPersist) ||
299 IsEqualGUID(riid, &IID_IPersistStream) ||
300 IsEqualGUID(riid, &IID_IMoniker))
302 *ppvObj = (LPVOID)iface;
303 DEVENUM_IMediaCatMoniker_AddRef(iface);
307 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
308 return E_NOINTERFACE;
311 /**********************************************************************
312 * DEVENUM_IMediaCatMoniker_AddRef (also IUnknown)
314 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface)
316 MediaCatMoniker *This = (MediaCatMoniker *)iface;
319 return InterlockedIncrement(&This->ref);
322 /**********************************************************************
323 * DEVENUM_IMediaCatMoniker_Release (also IUnknown)
325 static ULONG WINAPI DEVENUM_IMediaCatMoniker_Release(LPMONIKER iface)
327 MediaCatMoniker *This = (MediaCatMoniker *)iface;
331 ref = InterlockedDecrement(&This->ref);
333 RegCloseKey(This->hkey);
335 DEVENUM_UnlockModule();
340 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(
344 MediaCatMoniker *This = (MediaCatMoniker *)iface;
345 FIXME("(%p)->(%p)\n", This, pClassID);
347 if (pClassID == NULL)
353 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(LPMONIKER iface)
360 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(LPMONIKER iface, IStream* pStm)
362 FIXME("(%p)\n", pStm);
367 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Save(LPMONIKER iface, IStream* pStm, BOOL fClearDirty)
369 FIXME("(%p, %s)\n", pStm, fClearDirty ? "true" : "false");
371 return STG_E_CANTSAVE;
374 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetSizeMax(
376 ULARGE_INTEGER* pcbSize)
378 FIXME("(%p)\n", pcbSize);
380 ZeroMemory(pcbSize, sizeof(*pcbSize));
385 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(
392 IUnknown * pObj = NULL;
393 IPropertyBag * pProp = NULL;
396 HRESULT res = E_FAIL;
398 MediaCatMoniker *This = (MediaCatMoniker *)iface;
402 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
408 /* first activation of this class */
410 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
411 pProp = (IPropertyBag*)pvptr;
414 V_VT(&var) = VT_LPWSTR;
415 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
419 res = CLSIDFromString(V_UNION(&var,bstrVal), &clsID);
420 CoTaskMemFree(V_UNION(&var, bstrVal));
424 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
425 pObj = (IUnknown*)pvptr;
431 /* get the requested interface from the loaded class */
436 res2 = IUnknown_QueryInterface(pObj, &IID_IPersistPropertyBag, &ppv);
437 if (SUCCEEDED(res2)) {
438 res = IPersistPropertyBag_Load((IPersistPropertyBag *) ppv, pProp, NULL);
439 IPersistPropertyBag_Release((IPersistPropertyBag *) ppv);
443 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
444 IUnknown_Release(pObj);
449 IPropertyBag_Release(pProp);
452 TRACE("<- 0x%x\n", res);
457 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(
464 MediaCatMoniker *This = (MediaCatMoniker *)iface;
465 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
469 if (pbc || pmkToLeft)
470 return MK_E_NOSTORAGE;
472 if (IsEqualGUID(riid, &IID_IPropertyBag))
475 DuplicateHandle(GetCurrentProcess(), This->hkey, GetCurrentProcess(), &hkey, 0, 0, DUPLICATE_SAME_ACCESS);
476 return DEVENUM_IPropertyBag_Construct(hkey, (IPropertyBag**)ppvObj);
479 return MK_E_NOSTORAGE;
482 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Reduce(
485 DWORD dwReduceHowFar,
486 IMoniker** ppmkToLeft,
487 IMoniker** ppmkReduced)
489 TRACE("(%p, %d, %p, %p)\n", pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
493 *ppmkReduced = iface;
495 return MK_S_REDUCED_TO_SELF;
498 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(
501 BOOL fOnlyIfNotGeneric,
502 IMoniker** ppmkComposite)
504 FIXME("(%p, %s, %p): stub\n", pmkRight, fOnlyIfNotGeneric ? "true" : "false", ppmkComposite);
506 /* FIXME: use CreateGenericComposite? */
507 *ppmkComposite = NULL;
512 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(
515 IEnumMoniker** ppenumMoniker)
517 FIXME("(%s, %p): stub\n", fForward ? "true" : "false", ppenumMoniker);
519 *ppenumMoniker = NULL;
524 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(
526 IMoniker* pmkOtherMoniker)
528 FIXME("(%p)\n", pmkOtherMoniker);
533 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(
537 TRACE("(%p)\n", pdwHash);
544 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(
548 IMoniker* pmkNewlyRunning)
550 FIXME("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
555 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(
561 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pFileTime);
563 pFileTime->dwLowDateTime = 0xFFFFFFFF;
564 pFileTime->dwHighDateTime = 0x7FFFFFFF;
566 return MK_E_UNAVAILABLE;
569 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Inverse(
573 TRACE("(%p)\n", ppmk);
577 return MK_E_NOINVERSE;
580 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(
582 IMoniker* pmkOtherMoniker,
583 IMoniker** ppmkPrefix)
585 TRACE("(%p, %p)\n", pmkOtherMoniker, ppmkPrefix);
589 return MK_E_NOPREFIX;
592 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(
595 IMoniker** ppmkRelPath)
597 TRACE("(%p, %p)\n", pmkOther, ppmkRelPath);
599 *ppmkRelPath = pmkOther;
604 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(
608 LPOLESTR* ppszDisplayName)
610 MediaCatMoniker *This = (MediaCatMoniker *)iface;
611 WCHAR wszBuffer[MAX_PATH];
612 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
613 LONG received = sizeof(wszFriendlyName);
615 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
617 *ppszDisplayName = NULL;
619 /* FIXME: should this be the weird stuff we have to parse in IParseDisplayName? */
620 if (RegQueryValueW(This->hkey, wszFriendlyName, wszBuffer, &received) == ERROR_SUCCESS)
622 *ppszDisplayName = CoTaskMemAlloc(received);
623 strcpyW(*ppszDisplayName, wszBuffer);
630 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(
634 LPOLESTR pszDisplayName,
638 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
646 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(
650 TRACE("(%p)\n", pdwMksys);
655 static const IMonikerVtbl IMoniker_Vtbl =
657 DEVENUM_IMediaCatMoniker_QueryInterface,
658 DEVENUM_IMediaCatMoniker_AddRef,
659 DEVENUM_IMediaCatMoniker_Release,
660 DEVENUM_IMediaCatMoniker_GetClassID,
661 DEVENUM_IMediaCatMoniker_IsDirty,
662 DEVENUM_IMediaCatMoniker_Load,
663 DEVENUM_IMediaCatMoniker_Save,
664 DEVENUM_IMediaCatMoniker_GetSizeMax,
665 DEVENUM_IMediaCatMoniker_BindToObject,
666 DEVENUM_IMediaCatMoniker_BindToStorage,
667 DEVENUM_IMediaCatMoniker_Reduce,
668 DEVENUM_IMediaCatMoniker_ComposeWith,
669 DEVENUM_IMediaCatMoniker_Enum,
670 DEVENUM_IMediaCatMoniker_IsEqual,
671 DEVENUM_IMediaCatMoniker_Hash,
672 DEVENUM_IMediaCatMoniker_IsRunning,
673 DEVENUM_IMediaCatMoniker_GetTimeOfLastChange,
674 DEVENUM_IMediaCatMoniker_Inverse,
675 DEVENUM_IMediaCatMoniker_CommonPrefixWith,
676 DEVENUM_IMediaCatMoniker_RelativePathTo,
677 DEVENUM_IMediaCatMoniker_GetDisplayName,
678 DEVENUM_IMediaCatMoniker_ParseDisplayName,
679 DEVENUM_IMediaCatMoniker_IsSystemMoniker
682 MediaCatMoniker * DEVENUM_IMediaCatMoniker_Construct(void)
684 MediaCatMoniker * pMoniker = NULL;
685 pMoniker = CoTaskMemAlloc(sizeof(MediaCatMoniker));
689 pMoniker->lpVtbl = &IMoniker_Vtbl;
691 pMoniker->hkey = NULL;
693 DEVENUM_IMediaCatMoniker_AddRef((LPMONIKER)pMoniker);
695 DEVENUM_LockModule();
700 /**********************************************************************
701 * DEVENUM_IEnumMoniker_QueryInterface (also IUnknown)
703 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(
708 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
709 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
711 if (This == NULL || ppvObj == NULL) return E_POINTER;
713 if (IsEqualGUID(riid, &IID_IUnknown) ||
714 IsEqualGUID(riid, &IID_IEnumMoniker))
716 *ppvObj = (LPVOID)iface;
717 DEVENUM_IEnumMoniker_AddRef(iface);
721 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
722 return E_NOINTERFACE;
725 /**********************************************************************
726 * DEVENUM_IEnumMoniker_AddRef (also IUnknown)
728 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface)
730 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
731 ULONG ref = InterlockedIncrement(&This->ref);
733 TRACE("(%p)->() AddRef from %d\n", iface, ref - 1);
738 /**********************************************************************
739 * DEVENUM_IEnumMoniker_Release (also IUnknown)
741 static ULONG WINAPI DEVENUM_IEnumMoniker_Release(LPENUMMONIKER iface)
743 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
744 ULONG ref = InterlockedDecrement(&This->ref);
746 TRACE("(%p)->() Release from %d\n", iface, ref + 1);
750 RegCloseKey(This->hkey);
752 DEVENUM_UnlockModule();
758 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(LPENUMMONIKER iface, ULONG celt, IMoniker ** rgelt, ULONG * pceltFetched)
760 WCHAR buffer[MAX_PATH + 1];
763 MediaCatMoniker * pMoniker;
764 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
766 TRACE("(%p)->(%d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
768 while (fetched < celt)
770 res = RegEnumKeyW(This->hkey, This->index, buffer, sizeof(buffer) / sizeof(WCHAR));
771 if (res != ERROR_SUCCESS)
775 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
777 return E_OUTOFMEMORY;
779 if (RegOpenKeyW(This->hkey, buffer, &pMoniker->hkey) != ERROR_SUCCESS)
781 DEVENUM_IMediaCatMoniker_Release((LPMONIKER)pMoniker);
784 rgelt[fetched] = (LPMONIKER)pMoniker;
788 This->index += fetched;
790 TRACE("-- fetched %d\n", fetched);
793 *pceltFetched = fetched;
801 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(LPENUMMONIKER iface, ULONG celt)
803 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
805 TRACE("(%p)->(%d)\n", iface, celt);
812 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(LPENUMMONIKER iface)
814 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
816 TRACE("(%p)->()\n", iface);
823 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(LPENUMMONIKER iface, IEnumMoniker ** ppenum)
825 FIXME("(%p)->(%p): stub\n", iface, ppenum);
830 /**********************************************************************
833 static const IEnumMonikerVtbl IEnumMoniker_Vtbl =
835 DEVENUM_IEnumMoniker_QueryInterface,
836 DEVENUM_IEnumMoniker_AddRef,
837 DEVENUM_IEnumMoniker_Release,
838 DEVENUM_IEnumMoniker_Next,
839 DEVENUM_IEnumMoniker_Skip,
840 DEVENUM_IEnumMoniker_Reset,
841 DEVENUM_IEnumMoniker_Clone
844 HRESULT DEVENUM_IEnumMoniker_Construct(HKEY hkey, IEnumMoniker ** ppEnumMoniker)
846 EnumMonikerImpl * pEnumMoniker = CoTaskMemAlloc(sizeof(EnumMonikerImpl));
848 return E_OUTOFMEMORY;
850 pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
851 pEnumMoniker->ref = 1;
852 pEnumMoniker->index = 0;
853 pEnumMoniker->hkey = hkey;
855 *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
857 DEVENUM_LockModule();