mshtml: Wine Gecko 2.34-beta2 release.
[wine/wine-gecko.git] / dlls / devenum / mediacatenum.c
blob21c9db3d1f06019bcd6d8ebac454a04a9f92d771
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 DWORD subkey_cnt;
39 HKEY hkey;
40 HKEY special_hkey;
41 } EnumMonikerImpl;
43 typedef struct
45 IPropertyBag IPropertyBag_iface;
46 LONG ref;
47 HKEY hkey;
48 } RegPropBagImpl;
51 static inline RegPropBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
53 return CONTAINING_RECORD(iface, RegPropBagImpl, IPropertyBag_iface);
56 static HRESULT WINAPI DEVENUM_IPropertyBag_QueryInterface(
57 LPPROPERTYBAG iface,
58 REFIID riid,
59 LPVOID *ppvObj)
61 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
63 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppvObj);
65 if (This == NULL || ppvObj == NULL) return E_POINTER;
67 if (IsEqualGUID(riid, &IID_IUnknown) ||
68 IsEqualGUID(riid, &IID_IPropertyBag))
70 *ppvObj = iface;
71 IPropertyBag_AddRef(iface);
72 return S_OK;
75 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
76 return E_NOINTERFACE;
79 /**********************************************************************
80 * DEVENUM_IPropertyBag_AddRef (also IUnknown)
82 static ULONG WINAPI DEVENUM_IPropertyBag_AddRef(LPPROPERTYBAG iface)
84 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
86 TRACE("(%p)->() AddRef from %d\n", iface, This->ref);
88 return InterlockedIncrement(&This->ref);
91 /**********************************************************************
92 * DEVENUM_IPropertyBag_Release (also IUnknown)
94 static ULONG WINAPI DEVENUM_IPropertyBag_Release(LPPROPERTYBAG iface)
96 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
97 ULONG ref;
99 TRACE("(%p)->() ReleaseThis->ref from %d\n", iface, This->ref);
101 ref = InterlockedDecrement(&This->ref);
102 if (ref == 0) {
103 RegCloseKey(This->hkey);
104 CoTaskMemFree(This);
105 DEVENUM_UnlockModule();
107 return ref;
110 static HRESULT WINAPI DEVENUM_IPropertyBag_Read(
111 LPPROPERTYBAG iface,
112 LPCOLESTR pszPropName,
113 VARIANT* pVar,
114 IErrorLog* pErrorLog)
116 LPVOID pData = NULL;
117 DWORD received;
118 DWORD type = 0;
119 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
120 HRESULT res = S_OK;
121 LONG reswin32;
123 TRACE("(%p)->(%s, %p, %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
125 if (!pszPropName || !pVar)
126 return E_POINTER;
128 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, NULL, NULL, &received);
129 res = HRESULT_FROM_WIN32(reswin32);
131 if (SUCCEEDED(res))
133 pData = HeapAlloc(GetProcessHeap(), 0, received);
135 /* work around a GCC bug that occurs here unless we use the reswin32 variable as well */
136 reswin32 = RegQueryValueExW(This->hkey, pszPropName, NULL, &type, pData, &received);
137 res = HRESULT_FROM_WIN32(reswin32);
140 if (SUCCEEDED(res))
142 res = E_INVALIDARG; /* assume we cannot coerce into right type */
144 TRACE("Read %d bytes (%s)\n", received, type == REG_SZ ? debugstr_w(pData) : "binary data");
146 switch (type)
148 case REG_SZ:
149 switch (V_VT(pVar))
151 case VT_LPWSTR:
152 V_BSTR(pVar) = CoTaskMemAlloc(received);
153 memcpy(V_BSTR(pVar), pData, received);
154 res = S_OK;
155 break;
156 case VT_EMPTY:
157 V_VT(pVar) = VT_BSTR;
158 /* fall through */
159 case VT_BSTR:
160 V_BSTR(pVar) = SysAllocStringLen(pData, received/sizeof(WCHAR) - 1);
161 res = S_OK;
162 break;
164 break;
165 case REG_DWORD:
166 TRACE("REG_DWORD: %x\n", *(DWORD *)pData);
167 switch (V_VT(pVar))
169 case VT_EMPTY:
170 V_VT(pVar) = VT_I4;
171 /* fall through */
172 case VT_I4:
173 case VT_UI4:
174 V_I4(pVar) = *(DWORD *)pData;
175 res = S_OK;
176 break;
178 break;
179 case REG_BINARY:
181 SAFEARRAYBOUND bound;
182 void * pArrayElements;
183 bound.lLbound = 0;
184 bound.cElements = received;
185 TRACE("REG_BINARY: len = %d\n", received);
186 switch (V_VT(pVar))
188 case VT_EMPTY:
189 V_VT(pVar) = VT_ARRAY | VT_UI1;
190 /* fall through */
191 case VT_ARRAY | VT_UI1:
192 if (!(V_ARRAY(pVar) = SafeArrayCreate(VT_UI1, 1, &bound)))
193 res = E_OUTOFMEMORY;
194 else
195 res = S_OK;
196 break;
199 if (res == E_INVALIDARG)
200 break;
202 res = SafeArrayAccessData(V_ARRAY(pVar), &pArrayElements);
203 if (FAILED(res))
204 break;
206 CopyMemory(pArrayElements, pData, received);
207 res = SafeArrayUnaccessData(V_ARRAY(pVar));
208 break;
211 if (res == E_INVALIDARG)
212 FIXME("Variant type %x not supported for regtype %x\n", V_VT(pVar), type);
215 HeapFree(GetProcessHeap(), 0, pData);
217 TRACE("<- %x\n", res);
218 return res;
221 static HRESULT WINAPI DEVENUM_IPropertyBag_Write(
222 LPPROPERTYBAG iface,
223 LPCOLESTR pszPropName,
224 VARIANT* pVar)
226 RegPropBagImpl *This = impl_from_IPropertyBag(iface);
227 LPVOID lpData = NULL;
228 DWORD cbData = 0;
229 DWORD dwType = 0;
230 HRESULT res = S_OK;
232 TRACE("(%p)->(%s, %p)\n", This, debugstr_w(pszPropName), pVar);
234 switch (V_VT(pVar))
236 case VT_BSTR:
237 case VT_LPWSTR:
238 TRACE("writing %s\n", debugstr_w(V_BSTR(pVar)));
239 lpData = V_BSTR(pVar);
240 dwType = REG_SZ;
241 cbData = (lstrlenW(V_BSTR(pVar)) + 1) * sizeof(WCHAR);
242 break;
243 case VT_I4:
244 case VT_UI4:
245 TRACE("writing %u\n", V_UI4(pVar));
246 lpData = &V_UI4(pVar);
247 dwType = REG_DWORD;
248 cbData = sizeof(DWORD);
249 break;
250 case VT_ARRAY | VT_UI1:
252 LONG lUbound = 0;
253 LONG lLbound = 0;
254 dwType = REG_BINARY;
255 res = SafeArrayGetLBound(V_ARRAY(pVar), 1, &lLbound);
256 res = SafeArrayGetUBound(V_ARRAY(pVar), 1, &lUbound);
257 cbData = (lUbound - lLbound + 1) /* * sizeof(BYTE)*/;
258 TRACE("cbData: %d\n", cbData);
259 res = SafeArrayAccessData(V_ARRAY(pVar), &lpData);
260 break;
262 default:
263 FIXME("Variant type %d not handled\n", V_VT(pVar));
264 return E_FAIL;
267 if (RegSetValueExW(This->hkey,
268 pszPropName, 0,
269 dwType, lpData, cbData) != ERROR_SUCCESS)
270 res = E_FAIL;
272 if (V_VT(pVar) & VT_ARRAY)
273 res = SafeArrayUnaccessData(V_ARRAY(pVar));
275 return res;
278 static const IPropertyBagVtbl IPropertyBag_Vtbl =
280 DEVENUM_IPropertyBag_QueryInterface,
281 DEVENUM_IPropertyBag_AddRef,
282 DEVENUM_IPropertyBag_Release,
283 DEVENUM_IPropertyBag_Read,
284 DEVENUM_IPropertyBag_Write
287 static HRESULT DEVENUM_IPropertyBag_Construct(HANDLE hkey, IPropertyBag **ppBag)
289 RegPropBagImpl * rpb = CoTaskMemAlloc(sizeof(RegPropBagImpl));
290 if (!rpb)
291 return E_OUTOFMEMORY;
292 rpb->IPropertyBag_iface.lpVtbl = &IPropertyBag_Vtbl;
293 rpb->ref = 1;
294 rpb->hkey = hkey;
295 *ppBag = &rpb->IPropertyBag_iface;
296 DEVENUM_LockModule();
297 return S_OK;
301 static inline MediaCatMoniker *impl_from_IMoniker(IMoniker *iface)
303 return CONTAINING_RECORD(iface, MediaCatMoniker, IMoniker_iface);
306 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_QueryInterface(IMoniker *iface, REFIID riid,
307 void **ppv)
309 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
311 if (!ppv)
312 return E_POINTER;
314 if (IsEqualGUID(riid, &IID_IUnknown) ||
315 IsEqualGUID(riid, &IID_IPersist) ||
316 IsEqualGUID(riid, &IID_IPersistStream) ||
317 IsEqualGUID(riid, &IID_IMoniker))
319 *ppv = iface;
320 IMoniker_AddRef(iface);
321 return S_OK;
324 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
325 *ppv = NULL;
326 return E_NOINTERFACE;
329 static ULONG WINAPI DEVENUM_IMediaCatMoniker_AddRef(IMoniker *iface)
331 MediaCatMoniker *This = impl_from_IMoniker(iface);
332 ULONG ref = InterlockedIncrement(&This->ref);
334 TRACE("(%p) ref=%d\n", This, ref);
336 return ref;
339 static ULONG WINAPI DEVENUM_IMediaCatMoniker_Release(IMoniker *iface)
341 MediaCatMoniker *This = impl_from_IMoniker(iface);
342 ULONG ref = InterlockedDecrement(&This->ref);
344 TRACE("(%p) ref=%d\n", This, ref);
346 if (ref == 0) {
347 RegCloseKey(This->hkey);
348 CoTaskMemFree(This);
349 DEVENUM_UnlockModule();
351 return ref;
354 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetClassID(IMoniker *iface, CLSID *pClassID)
356 MediaCatMoniker *This = impl_from_IMoniker(iface);
358 FIXME("(%p)->(%p): stub\n", This, pClassID);
360 if (pClassID == NULL)
361 return E_POINTER;
363 return E_NOTIMPL;
366 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsDirty(IMoniker *iface)
368 FIXME("(%p)->(): stub\n", iface);
370 return S_FALSE;
373 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Load(IMoniker *iface, IStream *pStm)
375 FIXME("(%p)->(%p): stub\n", iface, pStm);
377 return E_NOTIMPL;
380 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Save(IMoniker *iface, IStream *pStm, BOOL fClearDirty)
382 FIXME("(%p)->(%p, %s): stub\n", iface, pStm, fClearDirty ? "true" : "false");
384 return STG_E_CANTSAVE;
387 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetSizeMax(IMoniker *iface, ULARGE_INTEGER *pcbSize)
389 FIXME("(%p)->(%p): stub\n", iface, pcbSize);
391 ZeroMemory(pcbSize, sizeof(*pcbSize));
393 return S_OK;
396 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToObject(IMoniker *iface, IBindCtx *pbc,
397 IMoniker *pmkToLeft, REFIID riidResult, void **ppvResult)
399 MediaCatMoniker *This = impl_from_IMoniker(iface);
400 IUnknown * pObj = NULL;
401 IPropertyBag * pProp = NULL;
402 CLSID clsID;
403 VARIANT var;
404 HRESULT res = E_FAIL;
406 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riidResult), ppvResult);
408 VariantInit(&var);
409 *ppvResult = NULL;
411 if(pmkToLeft==NULL)
413 /* first activation of this class */
414 LPVOID pvptr;
415 res=IMoniker_BindToStorage(iface, NULL, NULL, &IID_IPropertyBag, &pvptr);
416 pProp = pvptr;
417 if (SUCCEEDED(res))
419 V_VT(&var) = VT_LPWSTR;
420 res = IPropertyBag_Read(pProp, clsid_keyname, &var, NULL);
422 if (SUCCEEDED(res))
424 res = CLSIDFromString(V_BSTR(&var), &clsID);
425 CoTaskMemFree(V_BSTR(&var));
427 if (SUCCEEDED(res))
429 res=CoCreateInstance(&clsID,NULL,CLSCTX_ALL,&IID_IUnknown,&pvptr);
430 pObj = pvptr;
434 if (pObj!=NULL)
436 /* get the requested interface from the loaded class */
437 res = S_OK;
438 if (pProp) {
439 HRESULT res2;
440 LPVOID ppv = NULL;
441 res2 = IUnknown_QueryInterface(pObj, &IID_IPersistPropertyBag, &ppv);
442 if (SUCCEEDED(res2)) {
443 res = IPersistPropertyBag_Load((IPersistPropertyBag *) ppv, pProp, NULL);
444 IPersistPropertyBag_Release((IPersistPropertyBag *) ppv);
447 if (SUCCEEDED(res))
448 res= IUnknown_QueryInterface(pObj,riidResult,ppvResult);
449 IUnknown_Release(pObj);
452 if (pProp)
454 IPropertyBag_Release(pProp);
457 TRACE("<- 0x%x\n", res);
459 return res;
462 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_BindToStorage(IMoniker *iface, IBindCtx *pbc,
463 IMoniker *pmkToLeft, REFIID riid, void **ppvObj)
465 MediaCatMoniker *This = impl_from_IMoniker(iface);
467 TRACE("(%p)->(%p, %p, %s, %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObj);
469 *ppvObj = NULL;
471 if (pmkToLeft)
472 return MK_E_NOSTORAGE;
474 if (pbc != NULL)
476 static DWORD reported;
477 if (!reported)
479 FIXME("ignoring IBindCtx %p\n", pbc);
480 reported++;
484 if (IsEqualGUID(riid, &IID_IPropertyBag))
486 HANDLE hkey;
487 DuplicateHandle(GetCurrentProcess(), This->hkey, GetCurrentProcess(), &hkey, 0, 0, DUPLICATE_SAME_ACCESS);
488 return DEVENUM_IPropertyBag_Construct(hkey, (IPropertyBag**)ppvObj);
491 return MK_E_NOSTORAGE;
494 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Reduce(IMoniker *iface, IBindCtx *pbc,
495 DWORD dwReduceHowFar, IMoniker **ppmkToLeft, IMoniker **ppmkReduced)
497 TRACE("(%p)->(%p, %d, %p, %p)\n", iface, pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
499 if (ppmkToLeft)
500 *ppmkToLeft = NULL;
501 *ppmkReduced = iface;
503 return MK_S_REDUCED_TO_SELF;
506 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ComposeWith(IMoniker *iface, IMoniker *pmkRight,
507 BOOL fOnlyIfNotGeneric, IMoniker **ppmkComposite)
509 FIXME("(%p)->(%p, %s, %p): stub\n", iface, pmkRight, fOnlyIfNotGeneric ? "true" : "false", ppmkComposite);
511 /* FIXME: use CreateGenericComposite? */
512 *ppmkComposite = NULL;
514 return E_NOTIMPL;
517 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Enum(IMoniker *iface, BOOL fForward,
518 IEnumMoniker **ppenumMoniker)
520 FIXME("(%p)->(%s, %p): stub\n", iface, fForward ? "true" : "false", ppenumMoniker);
522 *ppenumMoniker = NULL;
524 return S_OK;
527 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsEqual(IMoniker *iface, IMoniker *pmkOtherMoniker)
529 FIXME("(%p)->(%p): stub\n", iface, pmkOtherMoniker);
531 return E_NOTIMPL;
534 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Hash(IMoniker *iface, DWORD *pdwHash)
536 TRACE("(%p)->(%p)\n", iface, pdwHash);
538 *pdwHash = 0;
540 return S_OK;
543 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsRunning(IMoniker *iface, IBindCtx *pbc,
544 IMoniker *pmkToLeft, IMoniker *pmkNewlyRunning)
546 FIXME("(%p)->(%p, %p, %p): stub\n", iface, pbc, pmkToLeft, pmkNewlyRunning);
548 return S_FALSE;
551 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetTimeOfLastChange(IMoniker *iface, IBindCtx *pbc,
552 IMoniker *pmkToLeft, FILETIME *pFileTime)
554 TRACE("(%p)->(%p, %p, %p)\n", iface, pbc, pmkToLeft, pFileTime);
556 pFileTime->dwLowDateTime = 0xFFFFFFFF;
557 pFileTime->dwHighDateTime = 0x7FFFFFFF;
559 return MK_E_UNAVAILABLE;
562 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_Inverse(IMoniker *iface, IMoniker **ppmk)
564 TRACE("(%p)->(%p)\n", iface, ppmk);
566 *ppmk = NULL;
568 return MK_E_NOINVERSE;
571 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_CommonPrefixWith(IMoniker *iface,
572 IMoniker *pmkOtherMoniker, IMoniker **ppmkPrefix)
574 TRACE("(%p)->(%p, %p)\n", iface, pmkOtherMoniker, ppmkPrefix);
576 *ppmkPrefix = NULL;
578 return MK_E_NOPREFIX;
581 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_RelativePathTo(IMoniker *iface, IMoniker *pmkOther,
582 IMoniker **ppmkRelPath)
584 TRACE("(%p)->(%p, %p)\n", iface, pmkOther, ppmkRelPath);
586 *ppmkRelPath = pmkOther;
588 return MK_S_HIM;
591 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_GetDisplayName(IMoniker *iface, IBindCtx *pbc,
592 IMoniker *pmkToLeft, LPOLESTR *ppszDisplayName)
594 MediaCatMoniker *This = impl_from_IMoniker(iface);
595 WCHAR wszBuffer[MAX_PATH];
596 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
597 LONG received = sizeof(wszFriendlyName);
599 TRACE("(%p)->(%p, %p, %p)\n", iface, pbc, pmkToLeft, ppszDisplayName);
601 *ppszDisplayName = NULL;
603 /* FIXME: should this be the weird stuff we have to parse in IParseDisplayName? */
604 if (RegQueryValueW(This->hkey, wszFriendlyName, wszBuffer, &received) == ERROR_SUCCESS)
606 *ppszDisplayName = CoTaskMemAlloc(received);
607 strcpyW(*ppszDisplayName, wszBuffer);
608 return S_OK;
611 return E_FAIL;
614 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_ParseDisplayName(IMoniker *iface, IBindCtx *pbc,
615 IMoniker *pmkToLeft, LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
617 FIXME("(%p)->(%p, %p, %s, %p, %p)\n", iface, pbc, pmkToLeft, debugstr_w(pszDisplayName), pchEaten, ppmkOut);
619 *pchEaten = 0;
620 *ppmkOut = NULL;
622 return MK_E_SYNTAX;
625 static HRESULT WINAPI DEVENUM_IMediaCatMoniker_IsSystemMoniker(IMoniker *iface, DWORD *pdwMksys)
627 TRACE("(%p)->(%p)\n", iface, pdwMksys);
629 return S_FALSE;
632 static const IMonikerVtbl IMoniker_Vtbl =
634 DEVENUM_IMediaCatMoniker_QueryInterface,
635 DEVENUM_IMediaCatMoniker_AddRef,
636 DEVENUM_IMediaCatMoniker_Release,
637 DEVENUM_IMediaCatMoniker_GetClassID,
638 DEVENUM_IMediaCatMoniker_IsDirty,
639 DEVENUM_IMediaCatMoniker_Load,
640 DEVENUM_IMediaCatMoniker_Save,
641 DEVENUM_IMediaCatMoniker_GetSizeMax,
642 DEVENUM_IMediaCatMoniker_BindToObject,
643 DEVENUM_IMediaCatMoniker_BindToStorage,
644 DEVENUM_IMediaCatMoniker_Reduce,
645 DEVENUM_IMediaCatMoniker_ComposeWith,
646 DEVENUM_IMediaCatMoniker_Enum,
647 DEVENUM_IMediaCatMoniker_IsEqual,
648 DEVENUM_IMediaCatMoniker_Hash,
649 DEVENUM_IMediaCatMoniker_IsRunning,
650 DEVENUM_IMediaCatMoniker_GetTimeOfLastChange,
651 DEVENUM_IMediaCatMoniker_Inverse,
652 DEVENUM_IMediaCatMoniker_CommonPrefixWith,
653 DEVENUM_IMediaCatMoniker_RelativePathTo,
654 DEVENUM_IMediaCatMoniker_GetDisplayName,
655 DEVENUM_IMediaCatMoniker_ParseDisplayName,
656 DEVENUM_IMediaCatMoniker_IsSystemMoniker
659 MediaCatMoniker * DEVENUM_IMediaCatMoniker_Construct(void)
661 MediaCatMoniker * pMoniker = NULL;
662 pMoniker = CoTaskMemAlloc(sizeof(MediaCatMoniker));
663 if (!pMoniker)
664 return NULL;
666 pMoniker->IMoniker_iface.lpVtbl = &IMoniker_Vtbl;
667 pMoniker->ref = 0;
668 pMoniker->hkey = NULL;
670 DEVENUM_IMediaCatMoniker_AddRef(&pMoniker->IMoniker_iface);
672 DEVENUM_LockModule();
674 return pMoniker;
677 static inline EnumMonikerImpl *impl_from_IEnumMoniker(IEnumMoniker *iface)
679 return CONTAINING_RECORD(iface, EnumMonikerImpl, IEnumMoniker_iface);
682 static HRESULT WINAPI DEVENUM_IEnumMoniker_QueryInterface(IEnumMoniker *iface, REFIID riid,
683 void **ppv)
685 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
687 if (!ppv)
688 return E_POINTER;
690 if (IsEqualGUID(riid, &IID_IUnknown) ||
691 IsEqualGUID(riid, &IID_IEnumMoniker))
693 *ppv = iface;
694 IEnumMoniker_AddRef(iface);
695 return S_OK;
698 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
699 *ppv = NULL;
700 return E_NOINTERFACE;
703 static ULONG WINAPI DEVENUM_IEnumMoniker_AddRef(IEnumMoniker *iface)
705 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
706 ULONG ref = InterlockedIncrement(&This->ref);
708 TRACE("(%p) ref=%d\n", This, ref);
710 return ref;
713 static ULONG WINAPI DEVENUM_IEnumMoniker_Release(IEnumMoniker *iface)
715 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
716 ULONG ref = InterlockedDecrement(&This->ref);
718 TRACE("(%p) ref=%d\n", This, ref);
720 if (!ref)
722 if(This->special_hkey)
723 RegCloseKey(This->special_hkey);
724 RegCloseKey(This->hkey);
725 CoTaskMemFree(This);
726 DEVENUM_UnlockModule();
727 return 0;
729 return ref;
732 static HRESULT WINAPI DEVENUM_IEnumMoniker_Next(IEnumMoniker *iface, ULONG celt, IMoniker **rgelt,
733 ULONG *pceltFetched)
735 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
736 WCHAR buffer[MAX_PATH + 1];
737 LONG res;
738 ULONG fetched = 0;
739 MediaCatMoniker * pMoniker;
741 TRACE("(%p)->(%d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
743 while (fetched < celt)
745 if(This->index+fetched < This->subkey_cnt)
746 res = RegEnumKeyW(This->hkey, This->index+fetched, buffer, sizeof(buffer) / sizeof(WCHAR));
747 else if(This->special_hkey)
748 res = RegEnumKeyW(This->special_hkey, This->index+fetched-This->subkey_cnt, buffer, sizeof(buffer) / sizeof(WCHAR));
749 else
750 break;
751 if (res != ERROR_SUCCESS)
753 break;
755 pMoniker = DEVENUM_IMediaCatMoniker_Construct();
756 if (!pMoniker)
757 return E_OUTOFMEMORY;
759 if (RegOpenKeyW(This->index+fetched < This->subkey_cnt ? This->hkey : This->special_hkey,
760 buffer, &pMoniker->hkey) != ERROR_SUCCESS)
762 IMoniker_Release(&pMoniker->IMoniker_iface);
763 break;
765 rgelt[fetched] = &pMoniker->IMoniker_iface;
766 fetched++;
769 This->index += fetched;
771 TRACE("-- fetched %d\n", fetched);
773 if (pceltFetched)
774 *pceltFetched = fetched;
776 if (fetched != celt)
777 return S_FALSE;
778 else
779 return S_OK;
782 static HRESULT WINAPI DEVENUM_IEnumMoniker_Skip(IEnumMoniker *iface, ULONG celt)
784 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
785 DWORD special_subkeys = 0;
787 TRACE("(%p)->(%d)\n", iface, celt);
789 /* Before incrementing, check if there are any more values to run through.
790 Some programs use the Skip() function to get the number of devices */
791 if(This->special_hkey)
792 RegQueryInfoKeyW(This->special_hkey, NULL, NULL, NULL, &special_subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
794 if((This->index + celt) >= This->subkey_cnt + special_subkeys)
796 return S_FALSE;
799 This->index += celt;
801 return S_OK;
804 static HRESULT WINAPI DEVENUM_IEnumMoniker_Reset(IEnumMoniker *iface)
806 EnumMonikerImpl *This = impl_from_IEnumMoniker(iface);
808 TRACE("(%p)->()\n", iface);
810 This->index = 0;
812 return S_OK;
815 static HRESULT WINAPI DEVENUM_IEnumMoniker_Clone(IEnumMoniker *iface, IEnumMoniker **ppenum)
817 FIXME("(%p)->(%p): stub\n", iface, ppenum);
819 return E_NOTIMPL;
822 /**********************************************************************
823 * IEnumMoniker_Vtbl
825 static const IEnumMonikerVtbl IEnumMoniker_Vtbl =
827 DEVENUM_IEnumMoniker_QueryInterface,
828 DEVENUM_IEnumMoniker_AddRef,
829 DEVENUM_IEnumMoniker_Release,
830 DEVENUM_IEnumMoniker_Next,
831 DEVENUM_IEnumMoniker_Skip,
832 DEVENUM_IEnumMoniker_Reset,
833 DEVENUM_IEnumMoniker_Clone
836 HRESULT DEVENUM_IEnumMoniker_Construct(HKEY hkey, HKEY special_hkey, IEnumMoniker ** ppEnumMoniker)
838 EnumMonikerImpl * pEnumMoniker = CoTaskMemAlloc(sizeof(EnumMonikerImpl));
839 if (!pEnumMoniker)
840 return E_OUTOFMEMORY;
842 pEnumMoniker->IEnumMoniker_iface.lpVtbl = &IEnumMoniker_Vtbl;
843 pEnumMoniker->ref = 1;
844 pEnumMoniker->index = 0;
845 pEnumMoniker->hkey = hkey;
846 pEnumMoniker->special_hkey = special_hkey;
848 *ppEnumMoniker = &pEnumMoniker->IEnumMoniker_iface;
850 if(RegQueryInfoKeyW(pEnumMoniker->hkey, NULL, NULL, NULL, &pEnumMoniker->subkey_cnt, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
851 pEnumMoniker->subkey_cnt = 0;
854 DEVENUM_LockModule();
856 return S_OK;