appwiz.cpl: Fix compilation on systems that don't support nameless unions.
[wine.git] / dlls / devenum / mediacatenum.c
blob6cd2e61ea222ddca690eb9410e73a590ff0fe364
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 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);
37 typedef struct
39 const IPropertyBagVtbl *lpVtbl;
40 LONG ref;
41 HKEY hkey;
42 } RegPropBagImpl;
45 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
46 LPPROPERTYBAG iface,
47 REFIID riid,
48 LPVOID *ppvObj)
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);
60 return S_OK;
63 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
64 return E_NOINTERFACE;
67 /**********************************************************************
68 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
70 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
72 RegPropBagImpl *This = (RegPropBagImpl *)iface;
73 TRACE("\n");
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;
84 ULONG ref;
86 TRACE("\n");
88 ref = InterlockedDecrement(&This->ref);
89 if (ref == 0) {
90 RegCloseKey(This->hkey);
91 CoTaskMemFree(This);
92 DEVENUM_UnlockModule();
94 return ref;
97 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
98 LPPROPERTYBAG iface,
99 LPCOLESTR pszPropName,
100 VARIANT* pVar,
101 IErrorLog* pErrorLog)
103 LPVOID pData = NULL;
104 DWORD received;
105 DWORD type = 0;
106 RegPropBagImpl *This = (RegPropBagImpl *)iface;
107 HRESULT res = S_OK;
108 LONG reswin32;
110 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
112 if (!pszPropName || !pVar)
113 return E_POINTER;
115 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
116 res = HRESULT_FROM_WIN32(reswin32);
118 if (SUCCEEDED(res))
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);
127 if (SUCCEEDED(res))
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");
133 switch (type)
135 case REG_SZ:
136 switch (V_VT(pVar))
138 case VT_LPWSTR:
139 V_UNION(pVar, bstrVal) = CoTaskMemAlloc(received);
140 memcpy(V_UNION(pVar, bstrVal), (LPWSTR)pData, received);
141 res = S_OK;
142 break;
143 case VT_EMPTY:
144 V_VT(pVar) = VT_BSTR;
145 /* fall through */
146 case VT_BSTR:
147 V_UNION(pVar, bstrVal) = SysAllocStringLen((LPWSTR)pData, received/sizeof(WCHAR) - 1);
148 res = S_OK;
149 break;
151 break;
152 case REG_DWORD:
153 TRACE("REG_DWORD: %x\n", *(DWORD *)pData);
154 switch (V_VT(pVar))
156 case VT_EMPTY:
157 V_VT(pVar) = VT_I4;
158 /* fall through */
159 case VT_I4:
160 case VT_UI4:
161 V_UNION(pVar, ulVal) = *(DWORD *)pData;
162 res = S_OK;
163 break;
165 break;
166 case REG_BINARY:
168 SAFEARRAYBOUND bound;
169 void * pArrayElements;
170 bound.lLbound = 0;
171 bound.cElements = received;
172 TRACE("REG_BINARY: len = %d\n", received);
173 switch (V_VT(pVar))
175 case VT_EMPTY:
176 V_VT(pVar) = VT_ARRAY | VT_UI1;
177 /* fall through */
178 case VT_ARRAY | VT_UI1:
179 if (!(V_UNION(pVar, parray) = SafeArrayCreate(VT_UI1, 1, &bound)))
180 res = E_OUTOFMEMORY;
181 else
182 res = S_OK;
183 break;
186 if (res == E_INVALIDARG)
187 break;
189 res = SafeArrayAccessData(V_UNION(pVar, parray), &pArrayElements);
190 if (FAILED(res))
191 break;
193 CopyMemory(pArrayElements, pData, received);
194 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
195 break;
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);
205 return res;
208 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
209 LPPROPERTYBAG iface,
210 LPCOLESTR pszPropName,
211 VARIANT* pVar)
213 RegPropBagImpl *This = (RegPropBagImpl *)iface;
214 LPVOID lpData = NULL;
215 DWORD cbData = 0;
216 DWORD dwType = 0;
217 HRESULT res = S_OK;
219 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
221 switch (V_VT(pVar))
223 case VT_BSTR:
224 TRACE("writing %s\n", debugstr_w(V_UNION(pVar, bstrVal)));
225 lpData = (LPVOID)V_UNION(pVar, bstrVal);
226 dwType = REG_SZ;
227 cbData = (lstrlenW(V_UNION(pVar, bstrVal)) + 1) * sizeof(WCHAR);
228 break;
229 case VT_I4:
230 case VT_UI4:
231 TRACE("writing %u\n", V_UNION(pVar, ulVal));
232 lpData = (LPVOID)&V_UNION(pVar, ulVal);
233 dwType = REG_DWORD;
234 cbData = sizeof(DWORD);
235 break;
236 case VT_ARRAY | VT_UI1:
238 LONG lUbound = 0;
239 LONG lLbound = 0;
240 dwType = REG_BINARY;
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);
246 break;
248 default:
249 FIXME("Variant type %d not handled\n", V_VT(pVar));
250 return E_FAIL;
253 if (RegSetValueExW(This->hkey,
254 pszPropName, 0,
255 dwType, lpData, cbData) != ERROR_SUCCESS)
256 res = E_FAIL;
258 if (V_VT(pVar) & VT_ARRAY)
259 res = SafeArrayUnaccessData(V_UNION(pVar, parray));
261 return res;
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));
276 if (!rpb)
277 return E_OUTOFMEMORY;
278 rpb->lpVtbl = &IPropertyBag_Vtbl;
279 rpb->ref = 1;
280 rpb->hkey = hkey;
281 *ppBag = (IPropertyBag*)rpb;
282 DEVENUM_LockModule();
283 return S_OK;
287 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(
288 LPMONIKER iface,
289 REFIID riid,
290 LPVOID *ppvObj)
292 MediaCatMoniker *This = (MediaCatMoniker *)iface;
293 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
295 if (This == NULL || ppvObj == NULL) return E_POINTER;
297 *ppvObj = NULL;
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);
306 return S_OK;
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;
319 TRACE("\n");
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;
330 ULONG ref;
331 TRACE("\n");
333 ref = InterlockedDecrement(&This->ref);
334 if (ref == 0) {
335 RegCloseKey(This->hkey);
336 CoTaskMemFree(This);
337 DEVENUM_UnlockModule();
339 return ref;
342 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(
343 LPMONIKER iface,
344 CLSID* pClassID)
346 MediaCatMoniker *This = (MediaCatMoniker *)iface;
347 FIXME("(%p)->(%p)\n", This, pClassID);
349 if (pClassID == NULL)
350 return E_POINTER;
352 return E_NOTIMPL;
355 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(LPMONIKER iface)
357 FIXME("()\n");
359 return S_FALSE;
362 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(LPMONIKER iface, IStream* pStm)
364 FIXME("(%p)\n", pStm);
366 return E_NOTIMPL;
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(
377 LPMONIKER iface,
378 ULARGE_INTEGER* pcbSize)
380 FIXME("(%p)\n", pcbSize);
382 ZeroMemory(pcbSize, sizeof(*pcbSize));
384 return S_OK;
387 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(
388 LPMONIKER iface,
389 IBindCtx* pbc,
390 IMoniker* pmkToLeft,
391 REFIID riidResult,
392 void** ppvResult)
394 IUnknown * pObj = NULL;
395 IPropertyBag * pProp = NULL;
396 CLSID clsID;
397 VARIANT var;
398 HRESULT res = E_FAIL;
400 MediaCatMoniker *This = (MediaCatMoniker *)iface;
402 VariantInit(&var);
404 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
406 *ppvResult = NULL;
408 if(pmkToLeft==NULL)
410 /* first activation of this class */
411 LPVOID pvptr;
412 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
413 pProp = (IPropertyBag*)pvptr;
414 if (SUCCEEDED(res))
416 V_VT(&var) = VT_LPWSTR;
417 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
419 if (SUCCEEDED(res))
421 res = CLSIDFromString(V_UNION(&var,bstrVal), &clsID);
422 CoTaskMemFree(V_UNION(&var, bstrVal));
424 if (SUCCEEDED(res))
426 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
427 pObj = (IUnknown*)pvptr;
431 if (pObj!=NULL)
433 /* get the requested interface from the loaded class */
434 res = S_OK;
435 if (pProp) {
436 HRESULT res2;
437 LPVOID ppv = NULL;
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);
444 if (SUCCEEDED(res))
445 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
446 IUnknown_Release(pObj);
449 if (pProp)
451 IPropertyBag_Release(pProp);
454 TRACE("<- 0x%x\n", res);
456 return res;
459 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(
460 LPMONIKER iface,
461 IBindCtx* pbc,
462 IMoniker* pmkToLeft,
463 REFIID riid,
464 void** ppvObj)
466 MediaCatMoniker *This = (MediaCatMoniker *)iface;
467 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
469 *ppvObj = NULL;
471 if (pbc || pmkToLeft)
472 return MK_E_NOSTORAGE;
474 if (IsEqualGUID(riid, &IID_IPropertyBag))
476 HANDLE hkey;
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(
485 LPMONIKER iface,
486 IBindCtx* pbc,
487 DWORD dwReduceHowFar,
488 IMoniker** ppmkToLeft,
489 IMoniker** ppmkReduced)
491 TRACE("(%p, %d, %p, %p)\n", pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
493 if (ppmkToLeft)
494 *ppmkToLeft = NULL;
495 *ppmkReduced = iface;
497 return MK_S_REDUCED_TO_SELF;
500 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(
501 LPMONIKER iface,
502 IMoniker* pmkRight,
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;
511 return E_NOTIMPL;
514 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(
515 LPMONIKER iface,
516 BOOL fForward,
517 IEnumMoniker** ppenumMoniker)
519 FIXME("(%s, %p): stub\n", fForward ? "true" : "false", ppenumMoniker);
521 *ppenumMoniker = NULL;
523 return S_OK;
526 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(
527 LPMONIKER iface,
528 IMoniker* pmkOtherMoniker)
530 FIXME("(%p)\n", pmkOtherMoniker);
532 return E_NOTIMPL;
535 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(
536 LPMONIKER iface,
537 DWORD* pdwHash)
539 TRACE("(%p)\n", pdwHash);
541 *pdwHash = 0;
543 return S_OK;
546 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(
547 LPMONIKER iface,
548 IBindCtx* pbc,
549 IMoniker* pmkToLeft,
550 IMoniker* pmkNewlyRunning)
552 FIXME("(%p, %p, %p)\n", pbc, pmkToLeft, pmkNewlyRunning);
554 return S_FALSE;
557 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(
558 LPMONIKER iface,
559 IBindCtx* pbc,
560 IMoniker* pmkToLeft,
561 FILETIME* pFileTime)
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(
572 LPMONIKER iface,
573 IMoniker** ppmk)
575 TRACE("(%p)\n", ppmk);
577 *ppmk = NULL;
579 return MK_E_NOINVERSE;
582 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(
583 LPMONIKER iface,
584 IMoniker* pmkOtherMoniker,
585 IMoniker** ppmkPrefix)
587 TRACE("(%p, %p)\n", pmkOtherMoniker, ppmkPrefix);
589 *ppmkPrefix = NULL;
591 return MK_E_NOPREFIX;
594 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(
595 LPMONIKER iface,
596 IMoniker* pmkOther,
597 IMoniker** ppmkRelPath)
599 TRACE("(%p, %p)\n", pmkOther, ppmkRelPath);
601 *ppmkRelPath = pmkOther;
603 return MK_S_HIM;
606 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(
607 LPMONIKER iface,
608 IBindCtx* pbc,
609 IMoniker* pmkToLeft,
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);
626 return S_OK;
629 return E_FAIL;
632 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(
633 LPMONIKER iface,
634 IBindCtx* pbc,
635 IMoniker* pmkToLeft,
636 LPOLESTR pszDisplayName,
637 ULONG* pchEaten,
638 IMoniker** ppmkOut)
640 FIXME("(%p, %p, %s, %p, %p)\n", pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
642 *pchEaten = 0;
643 *ppmkOut = NULL;
645 return MK_E_SYNTAX;
648 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(
649 LPMONIKER iface,
650 DWORD* pdwMksys)
652 TRACE("(%p)\n", pdwMksys);
654 return S_FALSE;
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));
688 if (!pMoniker)
689 return NULL;
691 pMoniker->lpVtbl = &IMoniker_Vtbl;
692 pMoniker->ref = 0;
693 pMoniker->hkey = NULL;
695 DEVENUM_IMediaCatMoniker_AddRef((LPMONIKER)pMoniker);
697 DEVENUM_LockModule();
699 return pMoniker;
702 /**********************************************************************
703 * DEVENUM_IEnumMoniker_QueryInterface (also IUnknown)
705 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(
706 LPENUMMONIKER iface,
707 REFIID riid,
708 LPVOID *ppvObj)
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);
720 return S_OK;
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);
737 return ref;
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);
750 if (!ref)
752 RegCloseKey(This->hkey);
753 CoTaskMemFree(This);
754 DEVENUM_UnlockModule();
755 return 0;
757 return ref;
760 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(LPENUMMONIKER iface, ULONG celt, IMoniker ** rgelt, ULONG * pceltFetched)
762 WCHAR buffer[MAX_PATH + 1];
763 LONG res;
764 ULONG fetched = 0;
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)
775 break;
777 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
778 if (!pMoniker)
779 return E_OUTOFMEMORY;
781 if (RegOpenKeyW(This->hkey, buffer, &pMoniker->hkey) != ERROR_SUCCESS)
783 DEVENUM_IMediaCatMoniker_Release((LPMONIKER)pMoniker);
784 break;
786 rgelt[fetched] = (LPMONIKER)pMoniker;
787 fetched++;
790 This->index += fetched;
792 TRACE("-- fetched %d\n", fetched);
794 if (pceltFetched)
795 *pceltFetched = fetched;
797 if (fetched != celt)
798 return S_FALSE;
799 else
800 return S_OK;
803 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(LPENUMMONIKER iface, ULONG celt)
805 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
806 DWORD subKeys;
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)
814 return S_FALSE;
816 if((This->index + celt) >= subKeys)
818 return S_FALSE;
821 This->index += celt;
823 return S_OK;
826 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(LPENUMMONIKER iface)
828 EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
830 TRACE("(%p)->()\n", iface);
832 This->index = 0;
834 return S_OK;
837 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(LPENUMMONIKER iface, IEnumMoniker ** ppenum)
839 FIXME("(%p)->(%p): stub\n", iface, ppenum);
841 return E_NOTIMPL;
844 /**********************************************************************
845 * IEnumMoniker_Vtbl
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));
861 if (!pEnumMoniker)
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();
873 return S_OK;