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"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
33 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface);
34 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface);
35 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface);
39 const IPropertyBagVtbl *lpVtbl;
45 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
50 RegPropBagImpl *This = (RegPropBagImpl *)iface;
51 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
53 if (This == NULL || ppvObj == NULL) return E_POINTER;
55 if (IsEqualGUID(riid, &IID_IUnknown) ||
56 IsEqualGUID(riid, &IID_IPropertyBag))
58 *ppvObj = (LPVOID)iface;
59 DEVENUM_IPropertyBag_AddRef(iface);
63 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
67 /**********************************************************************
68 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
70 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
72 RegPropBagImpl *This = (RegPropBagImpl *)iface;
75 return InterlockedIncrement(&This->ref);
78 /**********************************************************************
79 * DEVENUM_IPropertyBag_Release (also IUnknown)
81 static ULONG WINAPI DEVENUM_IPropertyBag_Release(LPPROPERTYBAG iface)
83 RegPropBagImpl *This = (RegPropBagImpl *)iface;
88 ref = InterlockedDecrement(&This->ref);
90 RegCloseKey(This->hkey);
92 DEVENUM_UnlockModule();
97 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
99 LPCOLESTR pszPropName,
101 IErrorLog* pErrorLog)
106 RegPropBagImpl *This = (RegPropBagImpl *)iface;
110 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
112 if (!pszPropName || !pVar)
115 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
116 res = HRESULT_FROM_WIN32(reswin32);
120 pData = HeapAlloc(GetProcessHeap(), 0, received);
122 /* work around a GCC bug that occurs here unless we use the reswin32 variable as well */
123 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, &type, (LPBYTE)pData, &received);
124 res = HRESULT_FROM_WIN32(reswin32);
129 res = E_INVALIDARG; /* assume we cannot coerce into right type */
131 TRACE("Read %d bytes (%s)\n", received, type == REG_SZ ? debugstr_w((LPWSTR)pData) : "binary data");
139 V_UNION(pVar, bstrVal) = CoTaskMemAlloc(received);
140 memcpy(V_UNION(pVar, bstrVal), (LPWSTR)pData, received);
144 V_VT(pVar) = VT_BSTR;
147 V_UNION(pVar, bstrVal) = SysAllocStringLen((LPWSTR)pData, received/sizeof(WCHAR) - 1);
153 TRACE("REG_DWORD: %x\n", *(DWORD *)pData);
161 V_UNION(pVar, ulVal) = *(DWORD *)pData;
168 SAFEARRAYBOUND bound;
169 void * pArrayElements;
171 bound.cElements = received;
172 TRACE("REG_BINARY: len = %d\n", received);
176 V_VT(pVar) = VT_ARRAY | VT_UI1;
178 case VT_ARRAY | VT_UI1:
179 if (!(V_UNION(pVar, parray) = SafeArrayCreate(VT_UI1, 1, &bound)))
186 if (res == E_INVALIDARG)
189 res = SafeArrayAccessData(V_UNION(pVar, parray), &pArrayElements);
193 CopyMemory(pArrayElements, pData, received);
194 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
198 if (res == E_INVALIDARG)
199 FIXME("Variant type %x not supported for regtype %x\n", V_VT(pVar), type);
202 HeapFree(GetProcessHeap(), 0, pData);
204 TRACE("<- %x\n", res);
208 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
210 LPCOLESTR pszPropName,
213 RegPropBagImpl *This = (RegPropBagImpl *)iface;
214 LPVOID lpData = NULL;
219 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
224 TRACE("writing %s\n", debugstr_w(V_UNION(pVar, bstrVal)));
225 lpData = (LPVOID)V_UNION(pVar, bstrVal);
227 cbData = (lstrlenW(V_UNION(pVar, bstrVal)) + 1) * sizeof(WCHAR);
231 TRACE("writing %u\n", V_UNION(pVar, ulVal));
232 lpData = (LPVOID)&V_UNION(pVar, ulVal);
234 cbData = sizeof(DWORD);
236 case VT_ARRAY | VT_UI1:
241 res = SafeArrayGetLBound(V_UNION(pVar, parray), 1, &lLbound);
242 res = SafeArrayGetUBound(V_UNION(pVar, parray), 1, &lUbound);
243 cbData = (lUbound - lLbound + 1) /* * sizeof(BYTE)*/;
244 TRACE("cbData: %d\n", cbData);
245 res = SafeArrayAccessData(V_UNION(pVar, parray), &lpData);
249 FIXME("Variant type %d not handled\n", V_VT(pVar));
253 if (RegSetValueExW(This->hkey,
255 dwType, lpData, cbData) != ERROR_SUCCESS)
258 if (V_VT(pVar) & VT_ARRAY)
259 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
264 static const IPropertyBagVtbl IPropertyBag_Vtbl =
266 DEVENUM_IPropertyBag_QueryInterface,
267 DEVENUM_IPropertyBag_AddRef,
268 DEVENUM_IPropertyBag_Release,
269 DEVENUM_IPropertyBag_Read,
270 DEVENUM_IPropertyBag_Write
273 static HRESULT DEVENUM_IPropertyBag_Construct(HANDLE hkey, IPropertyBag **ppBag)
275 RegPropBagImpl * rpb = CoTaskMemAlloc(sizeof(RegPropBagImpl));
277 return E_OUTOFMEMORY;
278 rpb->lpVtbl = &IPropertyBag_Vtbl;
281 *ppBag = (IPropertyBag*)rpb;
282 DEVENUM_LockModule();
287 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(
292 MediaCatMoniker *This = (MediaCatMoniker *)iface;
293 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
295 if (This == NULL || ppvObj == NULL) return E_POINTER;
299 if (IsEqualGUID(riid, &IID_IUnknown) ||
300 IsEqualGUID(riid, &IID_IPersist) ||
301 IsEqualGUID(riid, &IID_IPersistStream) ||
302 IsEqualGUID(riid, &IID_IMoniker))
304 *ppvObj = (LPVOID)iface;
305 DEVENUM_IMediaCatMoniker_AddRef(iface);
309 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
310 return E_NOINTERFACE;
313 /**********************************************************************
314 * DEVENUM_IMediaCatMoniker_AddRef (also IUnknown)
316 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface)
318 MediaCatMoniker *This = (MediaCatMoniker *)iface;
321 return InterlockedIncrement(&This->ref);
324 /**********************************************************************
325 * DEVENUM_IMediaCatMoniker_Release (also IUnknown)
327 static ULONG WINAPI DEVENUM_IMediaCatMoniker_Release(LPMONIKER iface)
329 MediaCatMoniker *This = (MediaCatMoniker *)iface;
333 ref = InterlockedDecrement(&This->ref);
335 RegCloseKey(This->hkey);
337 DEVENUM_UnlockModule();
342 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(
346 MediaCatMoniker *This = (MediaCatMoniker *)iface;
347 FIXME("(%p)->(%p)\n", This, pClassID);
349 if (pClassID == NULL)
355 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(LPMONIKER iface)
362 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(LPMONIKER iface, IStream* pStm)
364 FIXME("(%p)\n", pStm);
369 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Save(LPMONIKER iface, IStream* pStm, BOOL fClearDirty)
371 FIXME("(%p, %s)\n", pStm, fClearDirty ? "true" : "false");
373 return STG_E_CANTSAVE;
376 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetSizeMax(
378 ULARGE_INTEGER* pcbSize)
380 FIXME("(%p)\n", pcbSize);
382 ZeroMemory(pcbSize, sizeof(*pcbSize));
387 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(
394 IUnknown * pObj = NULL;
395 IPropertyBag * pProp = NULL;
398 HRESULT res = E_FAIL;
400 MediaCatMoniker *This = (MediaCatMoniker *)iface;
404 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
410 /* first activation of this class */
412 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
413 pProp = (IPropertyBag*)pvptr;
416 V_VT(&var) = VT_LPWSTR;
417 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
421 res = CLSIDFromString(V_UNION(&var,bstrVal), &clsID);
422 CoTaskMemFree(V_UNION(&var, bstrVal));
426 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
427 pObj = (IUnknown*)pvptr;
433 /* get the requested interface from the loaded class */
438 res2 = IUnknown_QueryInterface(pObj, &IID_IPersistPropertyBag, &ppv);
439 if (SUCCEEDED(res2)) {
440 res = IPersistPropertyBag_Load((IPersistPropertyBag *) ppv, pProp, NULL);
441 IPersistPropertyBag_Release((IPersistPropertyBag *) ppv);
445 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
446 IUnknown_Release(pObj);
451 IPropertyBag_Release(pProp);
454 TRACE("<- 0x%x\n", res);
459 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(
466 MediaCatMoniker *This = (MediaCatMoniker *)iface;
467 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
471 if (pbc || pmkToLeft)
472 return MK_E_NOSTORAGE;
474 if (IsEqualGUID(riid, &IID_IPropertyBag))
477 DuplicateHandle(GetCurrentProcess(), This->hkey, GetCurrentProcess(), &hkey, 0, 0, DUPLICATE_SAME_ACCESS);
478 return DEVENUM_IPropertyBag_Construct(hkey, (IPropertyBag**)ppvObj);
481 return MK_E_NOSTORAGE;
484 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Reduce(
487 DWORD dwReduceHowFar,
488 IMoniker** ppmkToLeft,
489 IMoniker** ppmkReduced)
491 TRACE("(%p, %d, %p, %p)\n", pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
495 *ppmkReduced = iface;
497 return MK_S_REDUCED_TO_SELF;
500 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(
503 BOOL fOnlyIfNotGeneric,
504 IMoniker** ppmkComposite)
506 FIXME("(%p, %s, %p): stub\n", pmkRight, fOnlyIfNotGeneric ? "true" : "false", ppmkComposite);
508 /* FIXME: use CreateGenericComposite? */
509 *ppmkComposite = NULL;
514 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(
517 IEnumMoniker** ppenumMoniker)
519 FIXME("(%s, %p): stub\n", fForward ? "true" : "false", ppenumMoniker);
521 *ppenumMoniker = NULL;
526 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(
528 IMoniker* pmkOtherMoniker)
530 FIXME("(%p)\n", pmkOtherMoniker);
535 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(
539 TRACE("(%p)\n", pdwHash);
546 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(
550 IMoniker* pmkNewlyRunning)
552 FIXME("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
557 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(
563 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pFileTime);
565 pFileTime->dwLowDateTime = 0xFFFFFFFF;
566 pFileTime->dwHighDateTime = 0x7FFFFFFF;
568 return MK_E_UNAVAILABLE;
571 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Inverse(
575 TRACE("(%p)\n", ppmk);
579 return MK_E_NOINVERSE;
582 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(
584 IMoniker* pmkOtherMoniker,
585 IMoniker** ppmkPrefix)
587 TRACE("(%p, %p)\n", pmkOtherMoniker, ppmkPrefix);
591 return MK_E_NOPREFIX;
594 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(
597 IMoniker** ppmkRelPath)
599 TRACE("(%p, %p)\n", pmkOther, ppmkRelPath);
601 *ppmkRelPath = pmkOther;
606 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(
610 LPOLESTR* ppszDisplayName)
612 MediaCatMoniker *This = (MediaCatMoniker *)iface;
613 WCHAR wszBuffer[MAX_PATH];
614 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
615 LONG received = sizeof(wszFriendlyName);
617 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
619 *ppszDisplayName = NULL;
621 /* FIXME: should this be the weird stuff we have to parse in IParseDisplayName? */
622 if (RegQueryValueW(This->hkey, wszFriendlyName, wszBuffer, &received) == ERROR_SUCCESS)
624 *ppszDisplayName = CoTaskMemAlloc(received);
625 strcpyW(*ppszDisplayName, wszBuffer);
632 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(
636 LPOLESTR pszDisplayName,
640 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
648 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(
652 TRACE("(%p)\n", pdwMksys);
657 static const IMonikerVtbl IMoniker_Vtbl =
659 DEVENUM_IMediaCatMoniker_QueryInterface,
660 DEVENUM_IMediaCatMoniker_AddRef,
661 DEVENUM_IMediaCatMoniker_Release,
662 DEVENUM_IMediaCatMoniker_GetClassID,
663 DEVENUM_IMediaCatMoniker_IsDirty,
664 DEVENUM_IMediaCatMoniker_Load,
665 DEVENUM_IMediaCatMoniker_Save,
666 DEVENUM_IMediaCatMoniker_GetSizeMax,
667 DEVENUM_IMediaCatMoniker_BindToObject,
668 DEVENUM_IMediaCatMoniker_BindToStorage,
669 DEVENUM_IMediaCatMoniker_Reduce,
670 DEVENUM_IMediaCatMoniker_ComposeWith,
671 DEVENUM_IMediaCatMoniker_Enum,
672 DEVENUM_IMediaCatMoniker_IsEqual,
673 DEVENUM_IMediaCatMoniker_Hash,
674 DEVENUM_IMediaCatMoniker_IsRunning,
675 DEVENUM_IMediaCatMoniker_GetTimeOfLastChange,
676 DEVENUM_IMediaCatMoniker_Inverse,
677 DEVENUM_IMediaCatMoniker_CommonPrefixWith,
678 DEVENUM_IMediaCatMoniker_RelativePathTo,
679 DEVENUM_IMediaCatMoniker_GetDisplayName,
680 DEVENUM_IMediaCatMoniker_ParseDisplayName,
681 DEVENUM_IMediaCatMoniker_IsSystemMoniker
684 MediaCatMoniker * DEVENUM_IMediaCatMoniker_Construct(void)
686 MediaCatMoniker * pMoniker = NULL;
687 pMoniker = CoTaskMemAlloc(sizeof(MediaCatMoniker));
691 pMoniker->lpVtbl = &IMoniker_Vtbl;
693 pMoniker->hkey = NULL;
695 DEVENUM_IMediaCatMoniker_AddRef((LPMONIKER)pMoniker);
697 DEVENUM_LockModule();
702 /**********************************************************************
703 * DEVENUM_IEnumMoniker_QueryInterface (also IUnknown)
705 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(
710 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
711 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
713 if (This == NULL || ppvObj == NULL) return E_POINTER;
715 if (IsEqualGUID(riid, &IID_IUnknown) ||
716 IsEqualGUID(riid, &IID_IEnumMoniker))
718 *ppvObj = (LPVOID)iface;
719 DEVENUM_IEnumMoniker_AddRef(iface);
723 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
724 return E_NOINTERFACE;
727 /**********************************************************************
728 * DEVENUM_IEnumMoniker_AddRef (also IUnknown)
730 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface)
732 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
733 ULONG ref = InterlockedIncrement(&This->ref);
735 TRACE("(%p)->() AddRef from %d\n", iface, ref - 1);
740 /**********************************************************************
741 * DEVENUM_IEnumMoniker_Release (also IUnknown)
743 static ULONG WINAPI DEVENUM_IEnumMoniker_Release(LPENUMMONIKER iface)
745 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
746 ULONG ref = InterlockedDecrement(&This->ref);
748 TRACE("(%p)->() Release from %d\n", iface, ref + 1);
752 RegCloseKey(This->hkey);
754 DEVENUM_UnlockModule();
760 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(LPENUMMONIKER iface, ULONG celt, IMoniker ** rgelt, ULONG * pceltFetched)
762 WCHAR buffer[MAX_PATH + 1];
765 MediaCatMoniker * pMoniker;
766 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
768 TRACE("(%p)->(%d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
770 while (fetched < celt)
772 res = RegEnumKeyW(This->hkey, This->index, buffer, sizeof(buffer) / sizeof(WCHAR));
773 if (res != ERROR_SUCCESS)
777 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
779 return E_OUTOFMEMORY;
781 if (RegOpenKeyW(This->hkey, buffer, &pMoniker->hkey) != ERROR_SUCCESS)
783 DEVENUM_IMediaCatMoniker_Release((LPMONIKER)pMoniker);
786 rgelt[fetched] = (LPMONIKER)pMoniker;
790 This->index += fetched;
792 TRACE("-- fetched %d\n", fetched);
795 *pceltFetched = fetched;
803 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(LPENUMMONIKER iface, ULONG celt)
805 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
808 TRACE("(%p)->(%d)\n", iface, celt);
810 /* Before incrementing, check if there are any more values to run thru.
811 Some programs use the Skip() function to get the amount of devices */
812 if(RegQueryInfoKeyW(This->hkey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
816 if((This->index + celt) >= subKeys)
826 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(LPENUMMONIKER iface)
828 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
830 TRACE("(%p)->()\n", iface);
837 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(LPENUMMONIKER iface, IEnumMoniker ** ppenum)
839 FIXME("(%p)->(%p): stub\n", iface, ppenum);
844 /**********************************************************************
847 static const IEnumMonikerVtbl IEnumMoniker_Vtbl =
849 DEVENUM_IEnumMoniker_QueryInterface,
850 DEVENUM_IEnumMoniker_AddRef,
851 DEVENUM_IEnumMoniker_Release,
852 DEVENUM_IEnumMoniker_Next,
853 DEVENUM_IEnumMoniker_Skip,
854 DEVENUM_IEnumMoniker_Reset,
855 DEVENUM_IEnumMoniker_Clone
858 HRESULT DEVENUM_IEnumMoniker_Construct(HKEY hkey, IEnumMoniker ** ppEnumMoniker)
860 EnumMonikerImpl * pEnumMoniker = CoTaskMemAlloc(sizeof(EnumMonikerImpl));
862 return E_OUTOFMEMORY;
864 pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
865 pEnumMoniker->ref = 1;
866 pEnumMoniker->index = 0;
867 pEnumMoniker->hkey = hkey;
869 *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
871 DEVENUM_LockModule();