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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
33 /* #define ICOM_THIS_From_IROTData(class, name) class* This = (class*)(((char*)name)-sizeof(void*)) */
35 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
37 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface);
38 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface);
39 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface);
43 const IPropertyBagVtbl *lpVtbl;
49 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
54 RegPropBagImpl *This = (RegPropBagImpl *)iface;
55 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
57 if (This == NULL || ppvObj == NULL) return E_POINTER;
59 if (IsEqualGUID(riid, &IID_IUnknown) ||
60 IsEqualGUID(riid, &IID_IPropertyBag))
62 *ppvObj = (LPVOID)iface;
63 DEVENUM_IPropertyBag_AddRef(iface);
67 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
71 /**********************************************************************
72 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
74 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
76 RegPropBagImpl *This = (RegPropBagImpl *)iface;
79 return InterlockedIncrement(&This->ref);
82 /**********************************************************************
83 * DEVENUM_IPropertyBag_Release (also IUnknown)
85 static ULONG WINAPI DEVENUM_IPropertyBag_Release(LPPROPERTYBAG iface)
87 RegPropBagImpl *This = (RegPropBagImpl *)iface;
92 ref = InterlockedDecrement(&This->ref);
94 RegCloseKey(This->hkey);
96 DEVENUM_UnlockModule();
101 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
103 LPCOLESTR pszPropName,
105 IErrorLog* pErrorLog)
110 RegPropBagImpl *This = (RegPropBagImpl *)iface;
114 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
116 if (!pszPropName || !pVar)
119 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
120 res = HRESULT_FROM_WIN32(reswin32);
124 pData = HeapAlloc(GetProcessHeap(), 0, received);
126 /* work around a GCC bug that occurs here unless we use the reswin32 variable as well */
127 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, &type, pData, &received);
128 res = HRESULT_FROM_WIN32(reswin32);
133 res = E_INVALIDARG; /* assume we cannot coerce into right type */
135 TRACE("Read %ld bytes (%s)\n", received, type == REG_SZ ? debugstr_w((LPWSTR)pData) : "binary data");
143 V_UNION(pVar, bstrVal) = CoTaskMemAlloc(received);
144 memcpy(V_UNION(pVar, bstrVal), (LPWSTR)pData, received);
148 V_VT(pVar) = VT_BSTR;
151 V_UNION(pVar, bstrVal) = SysAllocStringLen((LPWSTR)pData, received/sizeof(WCHAR) - 1);
157 TRACE("REG_DWORD: %lx\n", *(DWORD *)pData);
165 V_UNION(pVar, ulVal) = *(DWORD *)pData;
172 SAFEARRAYBOUND bound;
173 void * pArrayElements;
175 bound.cElements = received;
176 TRACE("REG_BINARY: len = %ld\n", received);
180 case VT_ARRAY | VT_UI1:
181 if (!(V_UNION(pVar, parray) = SafeArrayCreate(VT_UI1, 1, &bound)))
187 if (res == E_INVALIDARG)
190 res = SafeArrayAccessData(V_UNION(pVar, parray), &pArrayElements);
194 CopyMemory(pArrayElements, pData, received);
195 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
199 if (res == E_INVALIDARG)
200 FIXME("Variant type %x not supported for regtype %lx\n", V_VT(pVar), type);
203 HeapFree(GetProcessHeap(), 0, pData);
205 TRACE("<- %lx\n", res);
209 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
211 LPCOLESTR pszPropName,
214 RegPropBagImpl *This = (RegPropBagImpl *)iface;
215 LPVOID lpData = NULL;
220 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
225 TRACE("writing %s\n", debugstr_w(V_UNION(pVar, bstrVal)));
226 lpData = (LPVOID)V_UNION(pVar, bstrVal);
228 cbData = (lstrlenW(V_UNION(pVar, bstrVal)) + 1) * sizeof(WCHAR);
232 TRACE("writing %lu\n", V_UNION(pVar, ulVal));
233 lpData = (LPVOID)&V_UNION(pVar, ulVal);
235 cbData = sizeof(DWORD);
237 case VT_ARRAY | VT_UI1:
242 res = SafeArrayGetLBound(V_UNION(pVar, parray), 1, &lLbound);
243 res = SafeArrayGetUBound(V_UNION(pVar, parray), 1, &lUbound);
244 cbData = (lUbound - lLbound + 1) /* * sizeof(BYTE)*/;
245 TRACE("cbData: %ld\n", cbData);
246 res = SafeArrayAccessData(V_UNION(pVar, parray), &lpData);
250 FIXME("Variant type %d not handled\n", V_VT(pVar));
254 if (RegSetValueExW(This->hkey,
256 dwType, lpData, cbData) != ERROR_SUCCESS)
259 if (V_VT(pVar) & VT_ARRAY)
260 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
265 static const IPropertyBagVtbl IPropertyBag_Vtbl =
267 DEVENUM_IPropertyBag_QueryInterface,
268 DEVENUM_IPropertyBag_AddRef,
269 DEVENUM_IPropertyBag_Release,
270 DEVENUM_IPropertyBag_Read,
271 DEVENUM_IPropertyBag_Write
274 static HRESULT DEVENUM_IPropertyBag_Construct(HANDLE hkey, IPropertyBag **ppBag)
276 RegPropBagImpl * rpb = CoTaskMemAlloc(sizeof(RegPropBagImpl));
278 return E_OUTOFMEMORY;
279 rpb->lpVtbl = &IPropertyBag_Vtbl;
282 *ppBag = (IPropertyBag*)rpb;
283 DEVENUM_LockModule();
288 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(
293 MediaCatMoniker *This = (MediaCatMoniker *)iface;
294 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
296 if (This == NULL || ppvObj == NULL) return E_POINTER;
300 if (IsEqualGUID(riid, &IID_IUnknown) ||
301 IsEqualGUID(riid, &IID_IPersist) ||
302 IsEqualGUID(riid, &IID_IPersistStream) ||
303 IsEqualGUID(riid, &IID_IMoniker))
305 *ppvObj = (LPVOID)iface;
306 DEVENUM_IMediaCatMoniker_AddRef(iface);
310 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
311 return E_NOINTERFACE;
314 /**********************************************************************
315 * DEVENUM_IMediaCatMoniker_AddRef (also IUnknown)
317 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(LPMONIKER iface)
319 MediaCatMoniker *This = (MediaCatMoniker *)iface;
322 return InterlockedIncrement(&This->ref);
325 /**********************************************************************
326 * DEVENUM_IMediaCatMoniker_Release (also IUnknown)
328 static ULONG WINAPI DEVENUM_IMediaCatMoniker_Release(LPMONIKER iface)
330 MediaCatMoniker *This = (MediaCatMoniker *)iface;
334 ref = InterlockedDecrement(&This->ref);
336 RegCloseKey(This->hkey);
338 DEVENUM_UnlockModule();
343 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(
347 MediaCatMoniker *This = (MediaCatMoniker *)iface;
348 FIXME("(%p)->(%p)\n", This, pClassID);
350 if (pClassID == NULL)
356 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(LPMONIKER iface)
363 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(LPMONIKER iface, IStream* pStm)
365 FIXME("(%p)\n", pStm);
370 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Save(LPMONIKER iface, IStream* pStm, BOOL fClearDirty)
372 FIXME("(%p, %s)\n", pStm, fClearDirty ? "true" : "false");
374 return STG_E_CANTSAVE;
377 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetSizeMax(
379 ULARGE_INTEGER* pcbSize)
381 FIXME("(%p)\n", pcbSize);
383 ZeroMemory(pcbSize, sizeof(*pcbSize));
388 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(
395 IUnknown * pObj = NULL;
396 IPropertyBag * pProp = NULL;
399 HRESULT res = E_FAIL;
401 MediaCatMoniker *This = (MediaCatMoniker *)iface;
405 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
411 /* first activation of this class */
413 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
414 pProp = (IPropertyBag*)pvptr;
417 V_VT(&var) = VT_LPWSTR;
418 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
422 res = CLSIDFromString(V_UNION(&var,bstrVal), &clsID);
423 CoTaskMemFree(V_UNION(&var, bstrVal));
427 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
428 pObj = (IUnknown*)pvptr;
434 /* get the requested interface from the loaded class */
439 res2 = IUnknown_QueryInterface(pObj, &IID_IPersistPropertyBag, &ppv);
440 if (SUCCEEDED(res2)) {
441 res = IPersistPropertyBag_Load((IPersistPropertyBag *) ppv, pProp, NULL);
442 IPersistPropertyBag_Release((IPersistPropertyBag *) ppv);
446 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
447 IUnknown_Release(pObj);
452 IPropertyBag_Release(pProp);
455 TRACE("<- 0x%lx\n", res);
460 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(
467 MediaCatMoniker *This = (MediaCatMoniker *)iface;
468 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
472 if (pbc || pmkToLeft)
473 return MK_E_NOSTORAGE;
475 if (IsEqualGUID(riid, &IID_IPropertyBag))
478 DuplicateHandle(GetCurrentProcess(), This->hkey, GetCurrentProcess(), &hkey, 0, 0, DUPLICATE_SAME_ACCESS);
479 return DEVENUM_IPropertyBag_Construct(hkey, (IPropertyBag**)ppvObj);
482 return MK_E_NOSTORAGE;
485 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Reduce(
488 DWORD dwReduceHowFar,
489 IMoniker** ppmkToLeft,
490 IMoniker** ppmkReduced)
492 TRACE("(%p, %ld, %p, %p)\n", pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
496 *ppmkReduced = iface;
498 return MK_S_REDUCED_TO_SELF;
501 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(
504 BOOL fOnlyIfNotGeneric,
505 IMoniker** ppmkComposite)
507 FIXME("(%p, %s, %p): stub\n", pmkRight, fOnlyIfNotGeneric ? "true" : "false", ppmkComposite);
509 /* FIXME: use CreateGenericComposite? */
510 *ppmkComposite = NULL;
515 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(
518 IEnumMoniker** ppenumMoniker)
520 FIXME("(%s, %p): stub\n", fForward ? "true" : "false", ppenumMoniker);
522 *ppenumMoniker = NULL;
527 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(
529 IMoniker* pmkOtherMoniker)
531 FIXME("(%p)\n", pmkOtherMoniker);
536 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(
540 TRACE("(%p)\n", pdwHash);
547 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(
551 IMoniker* pmkNewlyRunning)
553 FIXME("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
558 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(
564 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, pFileTime);
566 pFileTime->dwLowDateTime = 0xFFFFFFFF;
567 pFileTime->dwHighDateTime = 0x7FFFFFFF;
569 return MK_E_UNAVAILABLE;
572 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Inverse(
576 TRACE("(%p)\n", ppmk);
580 return MK_E_NOINVERSE;
583 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(
585 IMoniker* pmkOtherMoniker,
586 IMoniker** ppmkPrefix)
588 TRACE("(%p, %p)\n", pmkOtherMoniker, ppmkPrefix);
592 return MK_E_NOPREFIX;
595 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(
598 IMoniker** ppmkRelPath)
600 TRACE("(%p, %p)\n", pmkOther, ppmkRelPath);
602 *ppmkRelPath = pmkOther;
607 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(
611 LPOLESTR* ppszDisplayName)
613 MediaCatMoniker *This = (MediaCatMoniker *)iface;
614 WCHAR wszBuffer[MAX_PATH];
615 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
616 LONG received = sizeof(wszFriendlyName);
618 TRACE("(%p, %p, %p)\n", pbc, pmkToLeft, ppszDisplayName);
620 *ppszDisplayName = NULL;
622 /* FIXME: should this be the weird stuff we have to parse in IParseDisplayName? */
623 if (RegQueryValueW(This->hkey, wszFriendlyName, wszBuffer, &received) == ERROR_SUCCESS)
625 *ppszDisplayName = CoTaskMemAlloc(received);
626 strcpyW(*ppszDisplayName, wszBuffer);
633 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(
637 LPOLESTR pszDisplayName,
641 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
649 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(
653 TRACE("(%p)\n", pdwMksys);
658 static const IMonikerVtbl IMoniker_Vtbl =
660 DEVENUM_IMediaCatMoniker_QueryInterface,
661 DEVENUM_IMediaCatMoniker_AddRef,
662 DEVENUM_IMediaCatMoniker_Release,
663 DEVENUM_IMediaCatMoniker_GetClassID,
664 DEVENUM_IMediaCatMoniker_IsDirty,
665 DEVENUM_IMediaCatMoniker_Load,
666 DEVENUM_IMediaCatMoniker_Save,
667 DEVENUM_IMediaCatMoniker_GetSizeMax,
668 DEVENUM_IMediaCatMoniker_BindToObject,
669 DEVENUM_IMediaCatMoniker_BindToStorage,
670 DEVENUM_IMediaCatMoniker_Reduce,
671 DEVENUM_IMediaCatMoniker_ComposeWith,
672 DEVENUM_IMediaCatMoniker_Enum,
673 DEVENUM_IMediaCatMoniker_IsEqual,
674 DEVENUM_IMediaCatMoniker_Hash,
675 DEVENUM_IMediaCatMoniker_IsRunning,
676 DEVENUM_IMediaCatMoniker_GetTimeOfLastChange,
677 DEVENUM_IMediaCatMoniker_Inverse,
678 DEVENUM_IMediaCatMoniker_CommonPrefixWith,
679 DEVENUM_IMediaCatMoniker_RelativePathTo,
680 DEVENUM_IMediaCatMoniker_GetDisplayName,
681 DEVENUM_IMediaCatMoniker_ParseDisplayName,
682 DEVENUM_IMediaCatMoniker_IsSystemMoniker
685 MediaCatMoniker * DEVENUM_IMediaCatMoniker_Construct()
687 MediaCatMoniker * pMoniker = NULL;
688 pMoniker = CoTaskMemAlloc(sizeof(MediaCatMoniker));
692 pMoniker->lpVtbl = &IMoniker_Vtbl;
694 pMoniker->hkey = NULL;
696 DEVENUM_IMediaCatMoniker_AddRef((LPMONIKER)pMoniker);
698 DEVENUM_LockModule();
703 /**********************************************************************
704 * DEVENUM_IEnumMoniker_QueryInterface (also IUnknown)
706 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(
711 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
712 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
714 if (This == NULL || ppvObj == NULL) return E_POINTER;
716 if (IsEqualGUID(riid, &IID_IUnknown) ||
717 IsEqualGUID(riid, &IID_IEnumMoniker))
719 *ppvObj = (LPVOID)iface;
720 DEVENUM_IEnumMoniker_AddRef(iface);
724 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
725 return E_NOINTERFACE;
728 /**********************************************************************
729 * DEVENUM_IEnumMoniker_AddRef (also IUnknown)
731 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(LPENUMMONIKER iface)
733 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
734 ULONG ref = InterlockedIncrement(&This->ref);
736 TRACE("(%p)->() AddRef from %ld\n", iface, ref - 1);
741 /**********************************************************************
742 * DEVENUM_IEnumMoniker_Release (also IUnknown)
744 static ULONG WINAPI DEVENUM_IEnumMoniker_Release(LPENUMMONIKER iface)
746 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
747 ULONG ref = InterlockedDecrement(&This->ref);
749 TRACE("(%p)->() Release from %ld\n", iface, ref + 1);
753 RegCloseKey(This->hkey);
755 DEVENUM_UnlockModule();
761 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(LPENUMMONIKER iface, ULONG celt, IMoniker ** rgelt, ULONG * pceltFetched)
763 WCHAR buffer[MAX_PATH + 1];
766 MediaCatMoniker * pMoniker;
767 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
769 TRACE("(%p)->(%ld, %p, %p)\n", iface, celt, rgelt, pceltFetched);
771 while (fetched < celt)
773 res = RegEnumKeyW(This->hkey, This->index, buffer, sizeof(buffer) / sizeof(WCHAR));
774 if (res != ERROR_SUCCESS)
778 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
780 return E_OUTOFMEMORY;
782 if (RegOpenKeyW(This->hkey, buffer, &pMoniker->hkey) != ERROR_SUCCESS)
784 DEVENUM_IMediaCatMoniker_Release((LPMONIKER)pMoniker);
787 rgelt[fetched] = (LPMONIKER)pMoniker;
791 This->index += fetched;
793 TRACE("-- fetched %ld\n", fetched);
796 *pceltFetched = fetched;
804 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(LPENUMMONIKER iface, ULONG celt)
806 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
808 TRACE("(%p)->(%ld)\n", iface, celt);
815 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(LPENUMMONIKER iface)
817 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
819 TRACE("(%p)->()\n", iface);
826 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(LPENUMMONIKER iface, IEnumMoniker ** ppenum)
828 FIXME("(%p)->(%p): stub\n", iface, ppenum);
833 /**********************************************************************
836 static const IEnumMonikerVtbl IEnumMoniker_Vtbl =
838 DEVENUM_IEnumMoniker_QueryInterface,
839 DEVENUM_IEnumMoniker_AddRef,
840 DEVENUM_IEnumMoniker_Release,
841 DEVENUM_IEnumMoniker_Next,
842 DEVENUM_IEnumMoniker_Skip,
843 DEVENUM_IEnumMoniker_Reset,
844 DEVENUM_IEnumMoniker_Clone
847 HRESULT DEVENUM_IEnumMoniker_Construct(HKEY hkey, IEnumMoniker ** ppEnumMoniker)
849 EnumMonikerImpl * pEnumMoniker = CoTaskMemAlloc(sizeof(EnumMonikerImpl));
851 return E_OUTOFMEMORY;
853 pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
854 pEnumMoniker->ref = 1;
855 pEnumMoniker->index = 0;
856 pEnumMoniker->hkey = hkey;
858 *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
860 DEVENUM_LockModule();