comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / devenum / mediacatenum.c
blob60ba110a7c359aba8f0f65f1a5eae4de7657104f
1 /*
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
20 * NOTES ON THIS FILE:
21 * - Implements IEnumMoniker interface which enumerates through moniker
22 * objects created from HKEY_CLASSES/CLSID/{DEVICE_CLSID}/Instance
25 #include "devenum_private.h"
26 #include "oleauto.h"
27 #include "ocidl.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
33 typedef struct
35 IEnumMoniker IEnumMoniker_iface;
36 LONG ref;
37 DWORD index;
38 HKEY hkey;
39 } EnumMonikerImpl;
41 typedef struct
43 IPropertyBag IPropertyBag_iface;
44 LONG ref;
45 HKEY hkey;
46 } RegPropBagImpl;
49 static inline RegPropBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
51 return CONTAINING_RECORD(iface, RegPropBagImpl, IPropertyBag_iface);
54 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
55 LPPROPERTYBAG iface,
56 REFIID riid,
57 LPVOID *ppvObj)
59 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
61 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObj);
63 if (This == NULL || ppvObj == NULL) return E_POINTER;
65 if (IsEqualGUID(riid, &IID_IUnknown) ||
66 IsEqualGUID(riid, &IID_IPropertyBag))
68 *ppvObj = iface;
69 IPropertyBag_AddRef(iface);
70 return S_OK;
73 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
74 return E_NOINTERFACE;
77 /**********************************************************************
78 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
80 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
82 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
84 TRACE("(%p)->() AddRef from %d\n", iface, This->ref);
86 return InterlockedIncrement(&This->ref);
89 /**********************************************************************
90 * DEVENUM_IPropertyBag_Release (also IUnknown)
92 static ULONG WINAPI DEVENUM_IPropertyBag_Release(LPPROPERTYBAG iface)
94 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
95 ULONG ref;
97 TRACE("(%p)->() ReleaseThis->ref from %d\n", iface, This->ref);
99 ref = InterlockedDecrement(&This->ref);
100 if (ref == 0) {
101 RegCloseKey(This->hkey);
102 CoTaskMemFree(This);
103 DEVENUM_UnlockModule();
105 return ref;
108 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
109 LPPROPERTYBAG iface,
110 LPCOLESTR pszPropName,
111 VARIANT* pVar,
112 IErrorLog* pErrorLog)
114 LPVOID pData = NULL;
115 DWORD received;
116 DWORD type = 0;
117 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
118 HRESULT res = S_OK;
119 LONG reswin32;
121 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
123 if (!pszPropName || !pVar)
124 return E_POINTER;
126 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
127 res = HRESULT_FROM_WIN32(reswin32);
129 if (SUCCEEDED(res))
131 pData = HeapAlloc(GetProcessHeap(), 0, received);
133 /* work around a GCC bug that occurs here unless we use the reswin32 variable as well */
134 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, &type, pData, &received);
135 res = HRESULT_FROM_WIN32(reswin32);
138 if (SUCCEEDED(res))
140 res = E_INVALIDARG; /* assume we cannot coerce into right type */
142 TRACE("Read %d bytes (%s)\n", received, type == REG_SZ ? debugstr_w(pData) : "binary data");
144 switch (type)
146 case REG_SZ:
147 switch (V_VT(pVar))
149 case VT_LPWSTR:
150 V_UNION(pVar, bstrVal) = CoTaskMemAlloc(received);
151 memcpy(V_UNION(pVar, bstrVal), pData, received);
152 res = S_OK;
153 break;
154 case VT_EMPTY:
155 V_VT(pVar) = VT_BSTR;
156 /* fall through */
157 case VT_BSTR:
158 V_UNION(pVar, bstrVal) = SysAllocStringLen(pData, received/sizeof(WCHAR) - 1);
159 res = S_OK;
160 break;
162 break;
163 case REG_DWORD:
164 TRACE("REG_DWORD: %x\n", *(DWORD *)pData);
165 switch (V_VT(pVar))
167 case VT_EMPTY:
168 V_VT(pVar) = VT_I4;
169 /* fall through */
170 case VT_I4:
171 case VT_UI4:
172 V_UNION(pVar, ulVal) = *(DWORD *)pData;
173 res = S_OK;
174 break;
176 break;
177 case REG_BINARY:
179 SAFEARRAYBOUND bound;
180 void * pArrayElements;
181 bound.lLbound = 0;
182 bound.cElements = received;
183 TRACE("REG_BINARY: len = %d\n", received);
184 switch (V_VT(pVar))
186 case VT_EMPTY:
187 V_VT(pVar) = VT_ARRAY | VT_UI1;
188 /* fall through */
189 case VT_ARRAY | VT_UI1:
190 if (!(V_UNION(pVar, parray) = SafeArrayCreate(VT_UI1, 1, &bound)))
191 res = E_OUTOFMEMORY;
192 else
193 res = S_OK;
194 break;
197 if (res == E_INVALIDARG)
198 break;
200 res = SafeArrayAccessData(V_UNION(pVar, parray), &pArrayElements);
201 if (FAILED(res))
202 break;
204 CopyMemory(pArrayElements, pData, received);
205 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
206 break;
209 if (res == E_INVALIDARG)
210 FIXME("Variant type %x not supported for regtype %x\n", V_VT(pVar), type);
213 HeapFree(GetProcessHeap(), 0, pData);
215 TRACE("<- %x\n", res);
216 return res;
219 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
220 LPPROPERTYBAG iface,
221 LPCOLESTR pszPropName,
222 VARIANT* pVar)
224 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
225 LPVOID lpData = NULL;
226 DWORD cbData = 0;
227 DWORD dwType = 0;
228 HRESULT res = S_OK;
230 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
232 switch (V_VT(pVar))
234 case VT_BSTR:
235 case VT_LPWSTR:
236 TRACE("writing %s\n", debugstr_w(V_UNION(pVar, bstrVal)));
237 lpData = V_UNION(pVar, bstrVal);
238 dwType = REG_SZ;
239 cbData = (lstrlenW(V_UNION(pVar, bstrVal)) + 1) * sizeof(WCHAR);
240 break;
241 case VT_I4:
242 case VT_UI4:
243 TRACE("writing %u\n", V_UNION(pVar, ulVal));
244 lpData = &V_UNION(pVar, ulVal);
245 dwType = REG_DWORD;
246 cbData = sizeof(DWORD);
247 break;
248 case VT_ARRAY | VT_UI1:
250 LONG lUbound = 0;
251 LONG lLbound = 0;
252 dwType = REG_BINARY;
253 res = SafeArrayGetLBound(V_UNION(pVar, parray), 1, &lLbound);
254 res = SafeArrayGetUBound(V_UNION(pVar, parray), 1, &lUbound);
255 cbData = (lUbound - lLbound + 1) /* * sizeof(BYTE)*/;
256 TRACE("cbData: %d\n", cbData);
257 res = SafeArrayAccessData(V_UNION(pVar, parray), &lpData);
258 break;
260 default:
261 FIXME("Variant type %d not handled\n", V_VT(pVar));
262 return E_FAIL;
265 if (RegSetValueExW(This->hkey,
266 pszPropName, 0,
267 dwType, lpData, cbData) != ERROR_SUCCESS)
268 res = E_FAIL;
270 if (V_VT(pVar) & VT_ARRAY)
271 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
273 return res;
276 static const IPropertyBagVtbl IPropertyBag_Vtbl =
278 DEVENUM_IPropertyBag_QueryInterface,
279 DEVENUM_IPropertyBag_AddRef,
280 DEVENUM_IPropertyBag_Release,
281 DEVENUM_IPropertyBag_Read,
282 DEVENUM_IPropertyBag_Write
285 static HRESULT DEVENUM_IPropertyBag_Construct(HANDLE hkey, IPropertyBag **ppBag)
287 RegPropBagImpl * rpb = CoTaskMemAlloc(sizeof(RegPropBagImpl));
288 if (!rpb)
289 return E_OUTOFMEMORY;
290 rpb->IPropertyBag_iface.lpVtbl = &IPropertyBag_Vtbl;
291 rpb->ref = 1;
292 rpb->hkey = hkey;
293 *ppBag = &rpb->IPropertyBag_iface;
294 DEVENUM_LockModule();
295 return S_OK;
299 static inline MediaCatMoniker *impl_from_IMoniker(IMoniker *iface)
301 return CONTAINING_RECORD(iface, MediaCatMoniker, IMoniker_iface);
304 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(IMoniker *iface, REFIID riid,
305 void **ppv)
307 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
309 if (!ppv)
310 return E_POINTER;
312 if (IsEqualGUID(riid, &IID_IUnknown) ||
313 IsEqualGUID(riid, &IID_IPersist) ||
314 IsEqualGUID(riid, &IID_IPersistStream) ||
315 IsEqualGUID(riid, &IID_IMoniker))
317 *ppv = iface;
318 IMoniker_AddRef(iface);
319 return S_OK;
322 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
323 *ppv = NULL;
324 return E_NOINTERFACE;
327 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(IMoniker *iface)
329 MediaCatMoniker *This = impl_from_IMoniker(iface);
330 ULONG ref = InterlockedIncrement(&This->ref);
332 TRACE("(%p) ref=%d\n", This, ref);
334 return ref;
337 static ULONG WINAPI DEVENUM_IMediaCatMoniker_Release(IMoniker *iface)
339 MediaCatMoniker *This = impl_from_IMoniker(iface);
340 ULONG ref = InterlockedDecrement(&This->ref);
342 TRACE("(%p) ref=%d\n", This, ref);
344 if (ref == 0) {
345 RegCloseKey(This->hkey);
346 CoTaskMemFree(This);
347 DEVENUM_UnlockModule();
349 return ref;
352 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(IMoniker *iface, CLSID *pClassID)
354 MediaCatMoniker *This = impl_from_IMoniker(iface);
356 FIXME("(%p)->(%p): stub\n", This, pClassID);
358 if (pClassID == NULL)
359 return E_POINTER;
361 return E_NOTIMPL;
364 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(IMoniker *iface)
366 FIXME("(%p)->(): stub\n", iface);
368 return S_FALSE;
371 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(IMoniker *iface, IStream *pStm)
373 FIXME("(%p)->(%p): stub\n", iface, pStm);
375 return E_NOTIMPL;
378 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Save(IMoniker *iface, IStream *pStm, BOOL fClearDirty)
380 FIXME("(%p)->(%p, %s): stub\n", iface, pStm, fClearDirty ? "true" : "false");
382 return STG_E_CANTSAVE;
385 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetSizeMax(IMoniker *iface, ULARGE_INTEGER *pcbSize)
387 FIXME("(%p)->(%p): stub\n", iface, pcbSize);
389 ZeroMemory(pcbSize, sizeof(*pcbSize));
391 return S_OK;
394 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(IMoniker *iface, IBindCtx *pbc,
395 IMoniker *pmkToLeft, REFIID riidResult, void **ppvResult)
397 MediaCatMoniker *This = impl_from_IMoniker(iface);
398 IUnknown * pObj = NULL;
399 IPropertyBag * pProp = NULL;
400 CLSID clsID;
401 VARIANT var;
402 HRESULT res = E_FAIL;
404 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
406 VariantInit(&var);
407 *ppvResult = NULL;
409 if(pmkToLeft==NULL)
411 /* first activation of this class */
412 LPVOID pvptr;
413 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
414 pProp = pvptr;
415 if (SUCCEEDED(res))
417 V_VT(&var) = VT_LPWSTR;
418 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
420 if (SUCCEEDED(res))
422 res = CLSIDFromString(V_UNION(&var,bstrVal), &clsID);
423 CoTaskMemFree(V_UNION(&var, bstrVal));
425 if (SUCCEEDED(res))
427 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
428 pObj = pvptr;
432 if (pObj!=NULL)
434 /* get the requested interface from the loaded class */
435 res = S_OK;
436 if (pProp) {
437 HRESULT res2;
438 LPVOID ppv = NULL;
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);
445 if (SUCCEEDED(res))
446 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
447 IUnknown_Release(pObj);
450 if (pProp)
452 IPropertyBag_Release(pProp);
455 TRACE("<- 0x%x\n", res);
457 return res;
460 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(IMoniker *iface, IBindCtx *pbc,
461 IMoniker *pmkToLeft, REFIID riid, void **ppvObj)
463 MediaCatMoniker *This = impl_from_IMoniker(iface);
465 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
467 *ppvObj = NULL;
469 if (pmkToLeft)
470 return MK_E_NOSTORAGE;
472 if (pbc != NULL)
474 static DWORD reported;
475 if (!reported)
477 FIXME("ignoring IBindCtx %p\n", pbc);
478 reported++;
482 if (IsEqualGUID(riid, &IID_IPropertyBag))
484 HANDLE hkey;
485 DuplicateHandle(GetCurrentProcess(), This->hkey, GetCurrentProcess(), &hkey, 0, 0, DUPLICATE_SAME_ACCESS);
486 return DEVENUM_IPropertyBag_Construct(hkey, (IPropertyBag**)ppvObj);
489 return MK_E_NOSTORAGE;
492 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Reduce(IMoniker *iface, IBindCtx *pbc,
493 DWORD dwReduceHowFar, IMoniker **ppmkToLeft, IMoniker **ppmkReduced)
495 TRACE("(%p)->(%p, %d, %p, %p)\n", iface, pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
497 if (ppmkToLeft)
498 *ppmkToLeft = NULL;
499 *ppmkReduced = iface;
501 return MK_S_REDUCED_TO_SELF;
504 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(IMoniker *iface, IMoniker *pmkRight,
505 BOOL fOnlyIfNotGeneric, IMoniker **ppmkComposite)
507 FIXME("(%p)->(%p, %s, %p): stub\n", iface, pmkRight, fOnlyIfNotGeneric ? "true" : "false", ppmkComposite);
509 /* FIXME: use CreateGenericComposite? */
510 *ppmkComposite = NULL;
512 return E_NOTIMPL;
515 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(IMoniker *iface, BOOL fForward,
516 IEnumMoniker **ppenumMoniker)
518 FIXME("(%p)->(%s, %p): stub\n", iface, fForward ? "true" : "false", ppenumMoniker);
520 *ppenumMoniker = NULL;
522 return S_OK;
525 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(IMoniker *iface, IMoniker *pmkOtherMoniker)
527 FIXME("(%p)->(%p): stub\n", iface, pmkOtherMoniker);
529 return E_NOTIMPL;
532 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(IMoniker *iface, DWORD *pdwHash)
534 TRACE("(%p)->(%p)\n", iface, pdwHash);
536 *pdwHash = 0;
538 return S_OK;
541 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(IMoniker *iface, IBindCtx *pbc,
542 IMoniker *pmkToLeft, IMoniker *pmkNewlyRunning)
544 FIXME("(%p)->(%p, %p, %p): stub\n", iface, pbc, pmkToLeft, pmkNewlyRunning);
546 return S_FALSE;
549 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(IMoniker *iface, IBindCtx *pbc,
550 IMoniker *pmkToLeft, FILETIME *pFileTime)
552 TRACE("(%p)->(%p, %p, %p)\n", iface, pbc, pmkToLeft, pFileTime);
554 pFileTime->dwLowDateTime = 0xFFFFFFFF;
555 pFileTime->dwHighDateTime = 0x7FFFFFFF;
557 return MK_E_UNAVAILABLE;
560 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Inverse(IMoniker *iface, IMoniker **ppmk)
562 TRACE("(%p)->(%p)\n", iface, ppmk);
564 *ppmk = NULL;
566 return MK_E_NOINVERSE;
569 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(IMoniker *iface,
570 IMoniker *pmkOtherMoniker, IMoniker **ppmkPrefix)
572 TRACE("(%p)->(%p, %p)\n", iface, pmkOtherMoniker, ppmkPrefix);
574 *ppmkPrefix = NULL;
576 return MK_E_NOPREFIX;
579 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(IMoniker *iface, IMoniker *pmkOther,
580 IMoniker **ppmkRelPath)
582 TRACE("(%p)->(%p, %p)\n", iface, pmkOther, ppmkRelPath);
584 *ppmkRelPath = pmkOther;
586 return MK_S_HIM;
589 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(IMoniker *iface, IBindCtx *pbc,
590 IMoniker *pmkToLeft, LPOLESTR *ppszDisplayName)
592 MediaCatMoniker *This = impl_from_IMoniker(iface);
593 WCHAR wszBuffer[MAX_PATH];
594 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
595 LONG received = sizeof(wszFriendlyName);
597 TRACE("(%p)->(%p, %p, %p)\n", iface, pbc, pmkToLeft, ppszDisplayName);
599 *ppszDisplayName = NULL;
601 /* FIXME: should this be the weird stuff we have to parse in IParseDisplayName? */
602 if (RegQueryValueW(This->hkey, wszFriendlyName, wszBuffer, &received) == ERROR_SUCCESS)
604 *ppszDisplayName = CoTaskMemAlloc(received);
605 strcpyW(*ppszDisplayName, wszBuffer);
606 return S_OK;
609 return E_FAIL;
612 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(IMoniker *iface, IBindCtx *pbc,
613 IMoniker *pmkToLeft, LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
615 FIXME("(%p)->(%p, %p, %s, %p, %p)\n", iface, pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
617 *pchEaten = 0;
618 *ppmkOut = NULL;
620 return MK_E_SYNTAX;
623 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(IMoniker *iface, DWORD *pdwMksys)
625 TRACE("(%p)->(%p)\n", iface, pdwMksys);
627 return S_FALSE;
630 static const IMonikerVtbl IMoniker_Vtbl =
632 DEVENUM_IMediaCatMoniker_QueryInterface,
633 DEVENUM_IMediaCatMoniker_AddRef,
634 DEVENUM_IMediaCatMoniker_Release,
635 DEVENUM_IMediaCatMoniker_GetClassID,
636 DEVENUM_IMediaCatMoniker_IsDirty,
637 DEVENUM_IMediaCatMoniker_Load,
638 DEVENUM_IMediaCatMoniker_Save,
639 DEVENUM_IMediaCatMoniker_GetSizeMax,
640 DEVENUM_IMediaCatMoniker_BindToObject,
641 DEVENUM_IMediaCatMoniker_BindToStorage,
642 DEVENUM_IMediaCatMoniker_Reduce,
643 DEVENUM_IMediaCatMoniker_ComposeWith,
644 DEVENUM_IMediaCatMoniker_Enum,
645 DEVENUM_IMediaCatMoniker_IsEqual,
646 DEVENUM_IMediaCatMoniker_Hash,
647 DEVENUM_IMediaCatMoniker_IsRunning,
648 DEVENUM_IMediaCatMoniker_GetTimeOfLastChange,
649 DEVENUM_IMediaCatMoniker_Inverse,
650 DEVENUM_IMediaCatMoniker_CommonPrefixWith,
651 DEVENUM_IMediaCatMoniker_RelativePathTo,
652 DEVENUM_IMediaCatMoniker_GetDisplayName,
653 DEVENUM_IMediaCatMoniker_ParseDisplayName,
654 DEVENUM_IMediaCatMoniker_IsSystemMoniker
657 MediaCatMoniker * DEVENUM_IMediaCatMoniker_Construct(void)
659 MediaCatMoniker * pMoniker = NULL;
660 pMoniker = CoTaskMemAlloc(sizeof(MediaCatMoniker));
661 if (!pMoniker)
662 return NULL;
664 pMoniker->IMoniker_iface.lpVtbl = &IMoniker_Vtbl;
665 pMoniker->ref = 0;
666 pMoniker->hkey = NULL;
668 DEVENUM_IMediaCatMoniker_AddRef(&pMoniker->IMoniker_iface);
670 DEVENUM_LockModule();
672 return pMoniker;
675 static inline EnumMonikerImpl *impl_from_IEnumMoniker(IEnumMoniker *iface)
677 return CONTAINING_RECORD(iface, EnumMonikerImpl, IEnumMoniker_iface);
680 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(IEnumMoniker *iface, REFIID riid,
681 void **ppv)
683 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
685 if (!ppv)
686 return E_POINTER;
688 if (IsEqualGUID(riid, &IID_IUnknown) ||
689 IsEqualGUID(riid, &IID_IEnumMoniker))
691 *ppv = iface;
692 IEnumMoniker_AddRef(iface);
693 return S_OK;
696 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
697 *ppv = NULL;
698 return E_NOINTERFACE;
701 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(IEnumMoniker *iface)
703 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
704 ULONG ref = InterlockedIncrement(&This->ref);
706 TRACE("(%p) ref=%d\n", This, ref);
708 return ref;
711 static ULONG WINAPI DEVENUM_IEnumMoniker_Release(IEnumMoniker *iface)
713 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
714 ULONG ref = InterlockedDecrement(&This->ref);
716 TRACE("(%p) ref=%d\n", This, ref);
718 if (!ref)
720 RegCloseKey(This->hkey);
721 CoTaskMemFree(This);
722 DEVENUM_UnlockModule();
723 return 0;
725 return ref;
728 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(IEnumMoniker *iface, ULONG celt, IMoniker **rgelt,
729 ULONG *pceltFetched)
731 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
732 WCHAR buffer[MAX_PATH + 1];
733 LONG res;
734 ULONG fetched = 0;
735 MediaCatMoniker * pMoniker;
737 TRACE("(%p)->(%d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
739 while (fetched < celt)
741 res = RegEnumKeyW(This->hkey, This->index, buffer, sizeof(buffer) / sizeof(WCHAR));
742 if (res != ERROR_SUCCESS)
744 break;
746 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
747 if (!pMoniker)
748 return E_OUTOFMEMORY;
750 if (RegOpenKeyW(This->hkey, buffer, &pMoniker->hkey) != ERROR_SUCCESS)
752 IMoniker_Release(&pMoniker->IMoniker_iface);
753 break;
755 rgelt[fetched] = &pMoniker->IMoniker_iface;
756 fetched++;
759 This->index += fetched;
761 TRACE("-- fetched %d\n", fetched);
763 if (pceltFetched)
764 *pceltFetched = fetched;
766 if (fetched != celt)
767 return S_FALSE;
768 else
769 return S_OK;
772 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(IEnumMoniker *iface, ULONG celt)
774 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
775 DWORD subKeys;
777 TRACE("(%p)->(%d)\n", iface, celt);
779 /* Before incrementing, check if there are any more values to run through.
780 Some programs use the Skip() function to get the number of devices */
781 if(RegQueryInfoKeyW(This->hkey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
783 return S_FALSE;
785 if((This->index + celt) >= subKeys)
787 return S_FALSE;
790 This->index += celt;
792 return S_OK;
795 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(IEnumMoniker *iface)
797 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
799 TRACE("(%p)->()\n", iface);
801 This->index = 0;
803 return S_OK;
806 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(IEnumMoniker *iface, IEnumMoniker **ppenum)
808 FIXME("(%p)->(%p): stub\n", iface, ppenum);
810 return E_NOTIMPL;
813 /**********************************************************************
814 * IEnumMoniker_Vtbl
816 static const IEnumMonikerVtbl IEnumMoniker_Vtbl =
818 DEVENUM_IEnumMoniker_QueryInterface,
819 DEVENUM_IEnumMoniker_AddRef,
820 DEVENUM_IEnumMoniker_Release,
821 DEVENUM_IEnumMoniker_Next,
822 DEVENUM_IEnumMoniker_Skip,
823 DEVENUM_IEnumMoniker_Reset,
824 DEVENUM_IEnumMoniker_Clone
827 HRESULT DEVENUM_IEnumMoniker_Construct(HKEY hkey, IEnumMoniker ** ppEnumMoniker)
829 EnumMonikerImpl * pEnumMoniker = CoTaskMemAlloc(sizeof(EnumMonikerImpl));
830 if (!pEnumMoniker)
831 return E_OUTOFMEMORY;
833 pEnumMoniker->IEnumMoniker_iface.lpVtbl = &IEnumMoniker_Vtbl;
834 pEnumMoniker->ref = 1;
835 pEnumMoniker->index = 0;
836 pEnumMoniker->hkey = hkey;
838 *ppEnumMoniker = &pEnumMoniker->IEnumMoniker_iface;
840 DEVENUM_LockModule();
842 return S_OK;