msxml3: Don't crash on null pointer when doing ::Next() on a last child.
[wine] / dlls / ole32 / comcat.c
1 /*
2  * Comcat implementation
3  *
4  * Copyright (C) 2002 John K. Hohm
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <string.h>
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "ole2.h"
33 #include "comcat.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 typedef struct
40 {
41     const ICatRegisterVtbl *lpVtbl;
42     const ICatInformationVtbl *infVtbl;
43 } ComCatMgrImpl;
44
45 struct class_categories {
46     LPCWSTR impl_strings;
47     LPCWSTR req_strings;
48 };
49
50 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid);
51 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *class_categories);
52 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(REFCLSID rclsid, LPCWSTR impl_req);
53
54 /**********************************************************************
55  * File-scope string constants
56  */
57 static const WCHAR comcat_keyname[] = {
58     'C','o','m','p','o','n','e','n','t',' ','C','a','t','e','g','o','r','i','e','s',0 };
59 static const WCHAR impl_keyname[] = {
60     'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
61 static const WCHAR req_keyname[] = {
62     'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
63 static const WCHAR clsid_keyname[] = { 'C','L','S','I','D',0 };
64
65
66 static inline ComCatMgrImpl *impl_from_ICatInformation( ICatInformation *iface )
67 {
68     return (ComCatMgrImpl *)((char*)iface - FIELD_OFFSET(ComCatMgrImpl, infVtbl));
69 }
70
71
72 /**********************************************************************
73  * COMCAT_RegisterClassCategories
74  */
75 static HRESULT COMCAT_RegisterClassCategories(
76     REFCLSID rclsid,
77     LPCWSTR type,
78     ULONG cCategories,
79     const CATID *rgcatid)
80 {
81     WCHAR keyname[39];
82     HRESULT res;
83     HKEY clsid_key, class_key, type_key;
84
85     if (cCategories && rgcatid == NULL) return E_POINTER;
86
87     /* Format the class key name. */
88     res = StringFromGUID2(rclsid, keyname, 39);
89     if (FAILED(res)) return res;
90
91     /* Create (or open) the CLSID key. */
92     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
93                           KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
94     if (res != ERROR_SUCCESS) return E_FAIL;
95
96     /* Create (or open) the class key. */
97     res = RegCreateKeyExW(clsid_key, keyname, 0, NULL, 0,
98                           KEY_READ | KEY_WRITE, NULL, &class_key, NULL);
99     if (res == ERROR_SUCCESS) {
100         /* Create (or open) the category type key. */
101         res = RegCreateKeyExW(class_key, type, 0, NULL, 0,
102                               KEY_READ | KEY_WRITE, NULL, &type_key, NULL);
103         if (res == ERROR_SUCCESS) {
104             for (; cCategories; --cCategories, ++rgcatid) {
105                 HKEY key;
106
107                 /* Format the category key name. */
108                 res = StringFromGUID2(rgcatid, keyname, 39);
109                 if (FAILED(res)) continue;
110
111                 /* Do the register. */
112                 res = RegCreateKeyExW(type_key, keyname, 0, NULL, 0,
113                                       KEY_READ | KEY_WRITE, NULL, &key, NULL);
114                 if (res == ERROR_SUCCESS) RegCloseKey(key);
115             }
116             res = S_OK;
117         } else res = E_FAIL;
118         RegCloseKey(class_key);
119     } else res = E_FAIL;
120     RegCloseKey(clsid_key);
121
122     return res;
123 }
124
125 /**********************************************************************
126  * COMCAT_UnRegisterClassCategories
127  */
128 static HRESULT COMCAT_UnRegisterClassCategories(
129     REFCLSID rclsid,
130     LPCWSTR type,
131     ULONG cCategories,
132     const CATID *rgcatid)
133 {
134     WCHAR keyname[68] = { 'C', 'L', 'S', 'I', 'D', '\\' };
135     HRESULT res;
136     HKEY type_key;
137
138     if (cCategories && rgcatid == NULL) return E_POINTER;
139
140     /* Format the class category type key name. */
141     res = StringFromGUID2(rclsid, keyname + 6, 39);
142     if (FAILED(res)) return res;
143     keyname[44] = '\\';
144     lstrcpyW(keyname + 45, type);
145
146     /* Open the class category type key. */
147     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0,
148                         KEY_READ | KEY_WRITE, &type_key);
149     if (res != ERROR_SUCCESS) return E_FAIL;
150
151     for (; cCategories; --cCategories, ++rgcatid) {
152         /* Format the category key name. */
153         res = StringFromGUID2(rgcatid, keyname, 39);
154         if (FAILED(res)) continue;
155
156         /* Do the unregister. */
157         RegDeleteKeyW(type_key, keyname);
158     }
159     RegCloseKey(type_key);
160
161     return S_OK;
162 }
163
164 /**********************************************************************
165  * COMCAT_GetCategoryDesc
166  */
167 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
168                                       ULONG buf_wchars)
169 {
170     static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
171     WCHAR valname[5];
172     HRESULT res;
173     DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
174
175     if (pszDesc == NULL) return E_INVALIDARG;
176
177     /* FIXME: lcid comparisons are more complex than this! */
178     wsprintfW(valname, fmt, lcid);
179     res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
180     if (res != ERROR_SUCCESS || type != REG_SZ) {
181         FIXME("Simplified lcid comparison\n");
182         return CAT_E_NODESCRIPTION;
183     }
184     pszDesc[size / sizeof(WCHAR)] = 0;
185
186     return S_OK;
187 }
188
189 /**********************************************************************
190  * COMCAT_PrepareClassCategories
191  */
192 static struct class_categories *COMCAT_PrepareClassCategories(
193     ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
194 {
195     struct class_categories *categories;
196     WCHAR *strings;
197
198     categories = HeapAlloc(
199         GetProcessHeap(), HEAP_ZERO_MEMORY,
200         sizeof(struct class_categories) +
201         ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
202     if (categories == NULL) return categories;
203
204     strings = (WCHAR *)(categories + 1);
205     categories->impl_strings = strings;
206     while (impl_count--) {
207         StringFromGUID2(impl_catids++, strings, 39);
208         strings += 39;
209     }
210     *strings++ = 0;
211
212     categories->req_strings = strings;
213     while (req_count--) {
214         StringFromGUID2(req_catids++, strings, 39);
215         strings += 39;
216     }
217     *strings++ = 0;
218
219     return categories;
220 }
221
222 /**********************************************************************
223  * COMCAT_IsClassOfCategories
224  */
225 static HRESULT COMCAT_IsClassOfCategories(
226     HKEY key,
227     struct class_categories const* categories)
228 {
229     HKEY subkey;
230     HRESULT res;
231     DWORD index;
232     LPCWSTR string;
233
234     /* Check that every given category is implemented by class. */
235     if (*categories->impl_strings) {
236         res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
237         if (res != ERROR_SUCCESS) return S_FALSE;
238         for (string = categories->impl_strings; *string; string += 39) {
239             HKEY catkey;
240             res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
241             if (res != ERROR_SUCCESS) {
242                 RegCloseKey(subkey);
243                 return S_FALSE;
244             }
245             RegCloseKey(catkey);
246         }
247         RegCloseKey(subkey);
248     }
249
250     /* Check that all categories required by class are given. */
251     res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
252     if (res == ERROR_SUCCESS) {
253         for (index = 0; ; ++index) {
254             WCHAR keyname[39];
255             DWORD size = 39;
256
257             res = RegEnumKeyExW(subkey, index, keyname, &size,
258                                 NULL, NULL, NULL, NULL);
259             if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
260             if (size != 38) continue; /* bogus catid in registry */
261             for (string = categories->req_strings; *string; string += 39)
262                 if (!strcmpiW(string, keyname)) break;
263             if (!*string) {
264                 RegCloseKey(subkey);
265                 return S_FALSE;
266             }
267         }
268         RegCloseKey(subkey);
269     }
270
271     return S_OK;
272 }
273
274 /**********************************************************************
275  * COMCAT_ICatRegister_QueryInterface
276  */
277 static HRESULT WINAPI COMCAT_ICatRegister_QueryInterface(
278     LPCATREGISTER iface,
279     REFIID riid,
280     LPVOID *ppvObj)
281 {
282     ComCatMgrImpl *This = (ComCatMgrImpl *)iface;
283     TRACE("%s\n",debugstr_guid(riid));
284
285     if (ppvObj == NULL) return E_POINTER;
286
287     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ICatRegister)) {
288         *ppvObj = iface;
289         IUnknown_AddRef(iface);
290         return S_OK;
291     }
292
293     if (IsEqualGUID(riid, &IID_ICatInformation)) {
294         *ppvObj = &This->infVtbl;
295         IUnknown_AddRef(iface);
296         return S_OK;
297     }
298
299     return E_NOINTERFACE;
300 }
301
302 /**********************************************************************
303  * COMCAT_ICatRegister_AddRef
304  */
305 static ULONG WINAPI COMCAT_ICatRegister_AddRef(LPCATREGISTER iface)
306 {
307     return 2; /* non-heap based object */
308 }
309
310 /**********************************************************************
311  * COMCAT_ICatRegister_Release
312  */
313 static ULONG WINAPI COMCAT_ICatRegister_Release(LPCATREGISTER iface)
314 {
315     return 1; /* non-heap based object */
316 }
317
318 /**********************************************************************
319  * COMCAT_ICatRegister_RegisterCategories
320  */
321 static HRESULT WINAPI COMCAT_ICatRegister_RegisterCategories(
322     LPCATREGISTER iface,
323     ULONG cCategories,
324     CATEGORYINFO *rgci)
325 {
326     HKEY comcat_key;
327     HRESULT res;
328
329     TRACE("\n");
330
331     if (cCategories && rgci == NULL)
332         return E_POINTER;
333
334     /* Create (or open) the component categories key. */
335     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0, NULL, 0,
336                           KEY_READ | KEY_WRITE, NULL, &comcat_key, NULL);
337     if (res != ERROR_SUCCESS) return E_FAIL;
338
339     for (; cCategories; --cCategories, ++rgci) {
340         static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
341         WCHAR keyname[39];
342         WCHAR valname[9];
343         HKEY cat_key;
344
345         /* Create (or open) the key for this category. */
346         if (!StringFromGUID2(&rgci->catid, keyname, 39)) continue;
347         res = RegCreateKeyExW(comcat_key, keyname, 0, NULL, 0,
348                               KEY_READ | KEY_WRITE, NULL, &cat_key, NULL);
349         if (res != ERROR_SUCCESS) continue;
350
351         /* Set the value for this locale's description. */
352         wsprintfW(valname, fmt, rgci->lcid);
353         RegSetValueExW(cat_key, valname, 0, REG_SZ,
354                        (CONST BYTE*)(rgci->szDescription),
355                        (lstrlenW(rgci->szDescription) + 1) * sizeof(WCHAR));
356
357         RegCloseKey(cat_key);
358     }
359
360     RegCloseKey(comcat_key);
361     return S_OK;
362 }
363
364 /**********************************************************************
365  * COMCAT_ICatRegister_UnRegisterCategories
366  */
367 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterCategories(
368     LPCATREGISTER iface,
369     ULONG cCategories,
370     CATID *rgcatid)
371 {
372     HKEY comcat_key;
373     HRESULT res;
374
375     TRACE("\n");
376
377     if (cCategories && rgcatid == NULL)
378         return E_POINTER;
379
380     /* Open the component categories key. */
381     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0,
382                         KEY_READ | KEY_WRITE, &comcat_key);
383     if (res != ERROR_SUCCESS) return E_FAIL;
384
385     for (; cCategories; --cCategories, ++rgcatid) {
386         WCHAR keyname[39];
387
388         /* Delete the key for this category. */
389         if (!StringFromGUID2(rgcatid, keyname, 39)) continue;
390         RegDeleteKeyW(comcat_key, keyname);
391     }
392
393     RegCloseKey(comcat_key);
394     return S_OK;
395 }
396
397 /**********************************************************************
398  * COMCAT_ICatRegister_RegisterClassImplCategories
399  */
400 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassImplCategories(
401     LPCATREGISTER iface,
402     REFCLSID rclsid,
403     ULONG cCategories,
404     CATID *rgcatid)
405 {
406     TRACE("\n");
407
408     return COMCAT_RegisterClassCategories(
409         rclsid, impl_keyname, cCategories, rgcatid);
410 }
411
412 /**********************************************************************
413  * COMCAT_ICatRegister_UnRegisterClassImplCategories
414  */
415 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassImplCategories(
416     LPCATREGISTER iface,
417     REFCLSID rclsid,
418     ULONG cCategories,
419     CATID *rgcatid)
420 {
421     TRACE("\n");
422
423     return COMCAT_UnRegisterClassCategories(
424         rclsid, impl_keyname, cCategories, rgcatid);
425 }
426
427 /**********************************************************************
428  * COMCAT_ICatRegister_RegisterClassReqCategories
429  */
430 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassReqCategories(
431     LPCATREGISTER iface,
432     REFCLSID rclsid,
433     ULONG cCategories,
434     CATID *rgcatid)
435 {
436     TRACE("\n");
437
438     return COMCAT_RegisterClassCategories(
439         rclsid, req_keyname, cCategories, rgcatid);
440 }
441
442 /**********************************************************************
443  * COMCAT_ICatRegister_UnRegisterClassReqCategories
444  */
445 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassReqCategories(
446     LPCATREGISTER iface,
447     REFCLSID rclsid,
448     ULONG cCategories,
449     CATID *rgcatid)
450 {
451     TRACE("\n");
452
453     return COMCAT_UnRegisterClassCategories(
454         rclsid, req_keyname, cCategories, rgcatid);
455 }
456
457 /**********************************************************************
458  * COMCAT_ICatInformation_QueryInterface
459  */
460 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
461     LPCATINFORMATION iface,
462     REFIID riid,
463     LPVOID *ppvObj)
464 {
465     ComCatMgrImpl *This = impl_from_ICatInformation( iface );
466     return IUnknown_QueryInterface((LPUNKNOWN)This, riid, ppvObj);
467 }
468
469 /**********************************************************************
470  * COMCAT_ICatInformation_AddRef
471  */
472 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
473 {
474     ComCatMgrImpl *This = impl_from_ICatInformation( iface );
475     return IUnknown_AddRef((LPUNKNOWN)This);
476 }
477
478 /**********************************************************************
479  * COMCAT_ICatInformation_Release
480  */
481 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
482 {
483     ComCatMgrImpl *This = impl_from_ICatInformation( iface );
484     return IUnknown_Release((LPUNKNOWN)This);
485 }
486
487 /**********************************************************************
488  * COMCAT_ICatInformation_EnumCategories
489  */
490 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
491     LPCATINFORMATION iface,
492     LCID lcid,
493     LPENUMCATEGORYINFO *ppenumCatInfo)
494 {
495     TRACE("\n");
496
497     if (ppenumCatInfo == NULL) return E_POINTER;
498
499     *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
500     if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
501     IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
502     return S_OK;
503 }
504
505 /**********************************************************************
506  * COMCAT_ICatInformation_GetCategoryDesc
507  */
508 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
509     LPCATINFORMATION iface,
510     REFCATID rcatid,
511     LCID lcid,
512     PWCHAR *ppszDesc)
513 {
514     WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
515                           't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
516                           'r', 'i', 'e', 's', '\\', 0 };
517     HKEY key;
518     HRESULT res;
519
520     TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
521
522     if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
523
524     /* Open the key for this category. */
525     if (!StringFromGUID2(rcatid, keyname + 21, 39)) return E_FAIL;
526     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
527     if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
528
529     /* Allocate a sensible amount of memory for the description. */
530     *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
531     if (*ppszDesc == NULL) {
532         RegCloseKey(key);
533         return E_OUTOFMEMORY;
534     }
535
536     /* Get the description, and make sure it's null terminated. */
537     res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
538     RegCloseKey(key);
539     if (FAILED(res)) {
540         CoTaskMemFree(*ppszDesc);
541         return res;
542     }
543
544     return S_OK;
545 }
546
547 /**********************************************************************
548  * COMCAT_ICatInformation_EnumClassesOfCategories
549  */
550 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
551     LPCATINFORMATION iface,
552     ULONG cImplemented,
553     CATID *rgcatidImpl,
554     ULONG cRequired,
555     CATID *rgcatidReq,
556     LPENUMCLSID *ppenumCLSID)
557 {
558     struct class_categories *categories;
559
560     TRACE("\n");
561
562         if (cImplemented == (ULONG)-1)
563                 cImplemented = 0;
564         if (cRequired == (ULONG)-1)
565                 cRequired = 0;
566
567     if (ppenumCLSID == NULL ||
568         (cImplemented && rgcatidImpl == NULL) ||
569         (cRequired && rgcatidReq == NULL)) return E_POINTER;
570
571     categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
572                                                cRequired, rgcatidReq);
573     if (categories == NULL) return E_OUTOFMEMORY;
574     *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
575     if (*ppenumCLSID == NULL) {
576         HeapFree(GetProcessHeap(), 0, categories);
577         return E_OUTOFMEMORY;
578     }
579     IEnumGUID_AddRef(*ppenumCLSID);
580     return S_OK;
581 }
582
583 /**********************************************************************
584  * COMCAT_ICatInformation_IsClassOfCategories
585  */
586 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
587     LPCATINFORMATION iface,
588     REFCLSID rclsid,
589     ULONG cImplemented,
590     CATID *rgcatidImpl,
591     ULONG cRequired,
592     CATID *rgcatidReq)
593 {
594     WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
595     HRESULT res;
596     struct class_categories *categories;
597     HKEY key;
598
599     if (WINE_TRACE_ON(ole)) {
600         ULONG count;
601         TRACE("CLSID: %s Implemented %u\n",debugstr_guid(rclsid),cImplemented);
602         for (count = 0; count < cImplemented; ++count)
603             TRACE("    %s\n",debugstr_guid(&rgcatidImpl[count]));
604         TRACE("Required %u\n",cRequired);
605         for (count = 0; count < cRequired; ++count)
606             TRACE("    %s\n",debugstr_guid(&rgcatidReq[count]));
607     }
608
609     if ((cImplemented && rgcatidImpl == NULL) ||
610         (cRequired && rgcatidReq == NULL)) return E_POINTER;
611
612     res = StringFromGUID2(rclsid, keyname + 6, 39);
613     if (FAILED(res)) return res;
614
615     categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
616                                                cRequired, rgcatidReq);
617     if (categories == NULL) return E_OUTOFMEMORY;
618
619     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
620     if (res == ERROR_SUCCESS) {
621         res = COMCAT_IsClassOfCategories(key, categories);
622         RegCloseKey(key);
623     } else res = S_FALSE;
624
625     HeapFree(GetProcessHeap(), 0, categories);
626
627     return res;
628 }
629
630 /**********************************************************************
631  * COMCAT_ICatInformation_EnumImplCategoriesOfClass
632  */
633 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
634     LPCATINFORMATION iface,
635     REFCLSID rclsid,
636     LPENUMCATID *ppenumCATID)
637 {
638     static const WCHAR postfix[24] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
639                           'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
640                           'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
641
642     TRACE("%s\n",debugstr_guid(rclsid));
643
644     if (rclsid == NULL || ppenumCATID == NULL)
645         return E_POINTER;
646
647     *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
648     if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
649     return S_OK;
650 }
651
652 /**********************************************************************
653  * COMCAT_ICatInformation_EnumReqCategoriesOfClass
654  */
655 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
656     LPCATINFORMATION iface,
657     REFCLSID rclsid,
658     LPENUMCATID *ppenumCATID)
659 {
660     static const WCHAR postfix[21] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
661                           'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
662                           'r', 'i', 'e', 's', 0 };
663
664     TRACE("%s\n",debugstr_guid(rclsid));
665
666     if (rclsid == NULL || ppenumCATID == NULL)
667         return E_POINTER;
668
669     *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
670     if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
671     return S_OK;
672 }
673
674 /**********************************************************************
675  * COMCAT_ICatRegister_Vtbl
676  */
677 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
678 {
679     COMCAT_ICatRegister_QueryInterface,
680     COMCAT_ICatRegister_AddRef,
681     COMCAT_ICatRegister_Release,
682     COMCAT_ICatRegister_RegisterCategories,
683     COMCAT_ICatRegister_UnRegisterCategories,
684     COMCAT_ICatRegister_RegisterClassImplCategories,
685     COMCAT_ICatRegister_UnRegisterClassImplCategories,
686     COMCAT_ICatRegister_RegisterClassReqCategories,
687     COMCAT_ICatRegister_UnRegisterClassReqCategories
688 };
689
690
691 /**********************************************************************
692  * COMCAT_ICatInformation_Vtbl
693  */
694 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
695 {
696     COMCAT_ICatInformation_QueryInterface,
697     COMCAT_ICatInformation_AddRef,
698     COMCAT_ICatInformation_Release,
699     COMCAT_ICatInformation_EnumCategories,
700     COMCAT_ICatInformation_GetCategoryDesc,
701     COMCAT_ICatInformation_EnumClassesOfCategories,
702     COMCAT_ICatInformation_IsClassOfCategories,
703     COMCAT_ICatInformation_EnumImplCategoriesOfClass,
704     COMCAT_ICatInformation_EnumReqCategoriesOfClass
705 };
706
707 /**********************************************************************
708  * static ComCatMgr instance
709  */
710 static ComCatMgrImpl COMCAT_ComCatMgr =
711 {
712     &COMCAT_ICatRegister_Vtbl,
713     &COMCAT_ICatInformation_Vtbl
714 };
715
716 /**********************************************************************
717  * COMCAT_IClassFactory_QueryInterface (also IUnknown)
718  */
719 static HRESULT WINAPI COMCAT_IClassFactory_QueryInterface(
720     LPCLASSFACTORY iface,
721     REFIID riid,
722     LPVOID *ppvObj)
723 {
724     TRACE("%s\n",debugstr_guid(riid));
725
726     if (ppvObj == NULL) return E_POINTER;
727
728     if (IsEqualGUID(riid, &IID_IUnknown) ||
729         IsEqualGUID(riid, &IID_IClassFactory))
730     {
731         *ppvObj = iface;
732         IUnknown_AddRef(iface);
733         return S_OK;
734     }
735
736     return E_NOINTERFACE;
737 }
738
739 /**********************************************************************
740  * COMCAT_IClassFactory_AddRef (also IUnknown)
741  */
742 static ULONG WINAPI COMCAT_IClassFactory_AddRef(LPCLASSFACTORY iface)
743 {
744     return 2; /* non-heap based object */
745 }
746
747 /**********************************************************************
748  * COMCAT_IClassFactory_Release (also IUnknown)
749  */
750 static ULONG WINAPI COMCAT_IClassFactory_Release(LPCLASSFACTORY iface)
751 {
752     return 1; /* non-heap based object */
753 }
754
755 /**********************************************************************
756  * COMCAT_IClassFactory_CreateInstance
757  */
758 static HRESULT WINAPI COMCAT_IClassFactory_CreateInstance(
759     LPCLASSFACTORY iface,
760     LPUNKNOWN pUnkOuter,
761     REFIID riid,
762     LPVOID *ppvObj)
763 {
764     HRESULT res;
765     TRACE("%s\n",debugstr_guid(riid));
766
767     if (ppvObj == NULL) return E_POINTER;
768
769     /* Don't support aggregation (Windows doesn't) */
770     if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
771
772     res = IUnknown_QueryInterface((LPUNKNOWN)&COMCAT_ComCatMgr, riid, ppvObj);
773     if (SUCCEEDED(res)) {
774         return res;
775     }
776
777     return CLASS_E_CLASSNOTAVAILABLE;
778 }
779
780 /**********************************************************************
781  * COMCAT_IClassFactory_LockServer
782  */
783 static HRESULT WINAPI COMCAT_IClassFactory_LockServer(
784     LPCLASSFACTORY iface,
785     BOOL fLock)
786 {
787     FIXME("(%d), stub!\n",fLock);
788     return S_OK;
789 }
790
791 /**********************************************************************
792  * static ClassFactory instance
793  */
794 static const IClassFactoryVtbl ComCatCFVtbl =
795 {
796     COMCAT_IClassFactory_QueryInterface,
797     COMCAT_IClassFactory_AddRef,
798     COMCAT_IClassFactory_Release,
799     COMCAT_IClassFactory_CreateInstance,
800     COMCAT_IClassFactory_LockServer
801 };
802
803 static const IClassFactoryVtbl *ComCatCF = &ComCatCFVtbl;
804
805 HRESULT ComCatCF_Create(REFIID riid, LPVOID *ppv)
806 {
807     return IClassFactory_QueryInterface((IClassFactory *)&ComCatCF, riid, ppv);
808 }
809
810 /**********************************************************************
811  * IEnumCATEGORYINFO implementation
812  *
813  * This implementation is not thread-safe.  The manager itself is, but
814  * I can't imagine a valid use of an enumerator in several threads.
815  */
816 typedef struct
817 {
818     const IEnumCATEGORYINFOVtbl *lpVtbl;
819     LONG  ref;
820     LCID  lcid;
821     HKEY  key;
822     DWORD next_index;
823 } IEnumCATEGORYINFOImpl;
824
825 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
826 {
827     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
828
829     TRACE("\n");
830
831     return InterlockedIncrement(&This->ref);
832 }
833
834 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
835     LPENUMCATEGORYINFO iface,
836     REFIID riid,
837     LPVOID *ppvObj)
838 {
839     TRACE("%s\n",debugstr_guid(riid));
840
841     if (ppvObj == NULL) return E_POINTER;
842
843     if (IsEqualGUID(riid, &IID_IUnknown) ||
844         IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
845     {
846         *ppvObj = iface;
847         COMCAT_IEnumCATEGORYINFO_AddRef(iface);
848         return S_OK;
849     }
850
851     return E_NOINTERFACE;
852 }
853
854 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
855 {
856     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
857     ULONG ref;
858
859     TRACE("\n");
860
861     ref = InterlockedDecrement(&This->ref);
862     if (ref == 0) {
863         if (This->key) RegCloseKey(This->key);
864         HeapFree(GetProcessHeap(), 0, This);
865         return 0;
866     }
867     return ref;
868 }
869
870 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
871     LPENUMCATEGORYINFO iface,
872     ULONG celt,
873     CATEGORYINFO *rgelt,
874     ULONG *pceltFetched)
875 {
876     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
877     ULONG fetched = 0;
878
879     TRACE("\n");
880
881     if (rgelt == NULL) return E_POINTER;
882
883     if (This->key) while (fetched < celt) {
884         LSTATUS res;
885         HRESULT hr;
886         WCHAR catid[39];
887         DWORD cName = 39;
888         HKEY subkey;
889
890         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
891                             NULL, NULL, NULL, NULL);
892         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
893         ++(This->next_index);
894
895         hr = CLSIDFromString(catid, &rgelt->catid);
896         if (FAILED(hr)) continue;
897
898         res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
899         if (res != ERROR_SUCCESS) continue;
900
901         hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
902                                     rgelt->szDescription, 128);
903         RegCloseKey(subkey);
904         if (FAILED(hr)) continue;
905
906         rgelt->lcid = This->lcid;
907         ++fetched;
908         ++rgelt;
909     }
910
911     if (pceltFetched) *pceltFetched = fetched;
912     return fetched == celt ? S_OK : S_FALSE;
913 }
914
915 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
916     LPENUMCATEGORYINFO iface,
917     ULONG celt)
918 {
919     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
920
921     TRACE("\n");
922
923     This->next_index += celt;
924     /* This should return S_FALSE when there aren't celt elems to skip. */
925     return S_OK;
926 }
927
928 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
929 {
930     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
931
932     TRACE("\n");
933
934     This->next_index = 0;
935     return S_OK;
936 }
937
938 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
939     LPENUMCATEGORYINFO iface,
940     IEnumCATEGORYINFO **ppenum)
941 {
942     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
943     static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
944                                      't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
945                                      'r', 'i', 'e', 's', 0 };
946     IEnumCATEGORYINFOImpl *new_this;
947
948     TRACE("\n");
949
950     if (ppenum == NULL) return E_POINTER;
951
952     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
953     if (new_this == NULL) return E_OUTOFMEMORY;
954
955     new_this->lpVtbl = This->lpVtbl;
956     new_this->ref = 1;
957     new_this->lcid = This->lcid;
958     /* FIXME: could we more efficiently use DuplicateHandle? */
959     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
960     new_this->next_index = This->next_index;
961
962     *ppenum = (LPENUMCATEGORYINFO)new_this;
963     return S_OK;
964 }
965
966 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
967 {
968     COMCAT_IEnumCATEGORYINFO_QueryInterface,
969     COMCAT_IEnumCATEGORYINFO_AddRef,
970     COMCAT_IEnumCATEGORYINFO_Release,
971     COMCAT_IEnumCATEGORYINFO_Next,
972     COMCAT_IEnumCATEGORYINFO_Skip,
973     COMCAT_IEnumCATEGORYINFO_Reset,
974     COMCAT_IEnumCATEGORYINFO_Clone
975 };
976
977 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
978 {
979     IEnumCATEGORYINFOImpl *This;
980
981     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
982     if (This) {
983         static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
984                                          't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
985                                          'r', 'i', 'e', 's', 0 };
986
987         This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
988         This->lcid = lcid;
989         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
990     }
991     return (LPENUMCATEGORYINFO)This;
992 }
993
994 /**********************************************************************
995  * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
996  *
997  * This implementation is not thread-safe.  The manager itself is, but
998  * I can't imagine a valid use of an enumerator in several threads.
999  */
1000 typedef struct
1001 {
1002     const IEnumGUIDVtbl *lpVtbl;
1003     LONG  ref;
1004     struct class_categories *categories;
1005     HKEY  key;
1006     DWORD next_index;
1007 } CLSID_IEnumGUIDImpl;
1008
1009 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
1010 {
1011     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1012     TRACE("\n");
1013
1014     return InterlockedIncrement(&This->ref);
1015 }
1016
1017 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
1018     LPENUMGUID iface,
1019     REFIID riid,
1020     LPVOID *ppvObj)
1021 {
1022     TRACE("%s\n",debugstr_guid(riid));
1023
1024     if (ppvObj == NULL) return E_POINTER;
1025
1026     if (IsEqualGUID(riid, &IID_IUnknown) ||
1027         IsEqualGUID(riid, &IID_IEnumGUID))
1028     {
1029         *ppvObj = iface;
1030         COMCAT_CLSID_IEnumGUID_AddRef(iface);
1031         return S_OK;
1032     }
1033
1034     return E_NOINTERFACE;
1035 }
1036
1037 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
1038 {
1039     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1040     ULONG ref;
1041
1042     TRACE("\n");
1043
1044     ref = InterlockedDecrement(&This->ref);
1045     if (ref == 0) {
1046         if (This->key) RegCloseKey(This->key);
1047         HeapFree(GetProcessHeap(), 0, This->categories);
1048         HeapFree(GetProcessHeap(), 0, This);
1049         return 0;
1050     }
1051     return ref;
1052 }
1053
1054 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
1055     LPENUMGUID iface,
1056     ULONG celt,
1057     GUID *rgelt,
1058     ULONG *pceltFetched)
1059 {
1060     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1061     ULONG fetched = 0;
1062
1063     TRACE("\n");
1064
1065     if (rgelt == NULL) return E_POINTER;
1066
1067     if (This->key) while (fetched < celt) {
1068         LSTATUS res;
1069         HRESULT hr;
1070         WCHAR clsid[39];
1071         DWORD cName = 39;
1072         HKEY subkey;
1073
1074         res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
1075                             NULL, NULL, NULL, NULL);
1076         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1077         ++(This->next_index);
1078
1079         hr = CLSIDFromString(clsid, rgelt);
1080         if (FAILED(hr)) continue;
1081
1082         res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
1083         if (res != ERROR_SUCCESS) continue;
1084
1085         hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1086         RegCloseKey(subkey);
1087         if (hr != S_OK) continue;
1088
1089         ++fetched;
1090         ++rgelt;
1091     }
1092
1093     if (pceltFetched) *pceltFetched = fetched;
1094     return fetched == celt ? S_OK : S_FALSE;
1095 }
1096
1097 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
1098     LPENUMGUID iface,
1099     ULONG celt)
1100 {
1101     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1102
1103     TRACE("\n");
1104
1105     This->next_index += celt;
1106     FIXME("Never returns S_FALSE\n");
1107     return S_OK;
1108 }
1109
1110 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
1111 {
1112     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1113
1114     TRACE("\n");
1115
1116     This->next_index = 0;
1117     return S_OK;
1118 }
1119
1120 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
1121     LPENUMGUID iface,
1122     IEnumGUID **ppenum)
1123 {
1124     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1125     static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1126     CLSID_IEnumGUIDImpl *new_this;
1127     DWORD size;
1128
1129     TRACE("\n");
1130
1131     if (ppenum == NULL) return E_POINTER;
1132
1133     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1134     if (new_this == NULL) return E_OUTOFMEMORY;
1135
1136     new_this->lpVtbl = This->lpVtbl;
1137     new_this->ref = 1;
1138     size = HeapSize(GetProcessHeap(), 0, This->categories);
1139     new_this->categories =
1140         HeapAlloc(GetProcessHeap(), 0, size);
1141     if (new_this->categories == NULL) {
1142         HeapFree(GetProcessHeap(), 0, new_this);
1143         return E_OUTOFMEMORY;
1144     }
1145     memcpy(new_this->categories, This->categories, size);
1146     /* FIXME: could we more efficiently use DuplicateHandle? */
1147     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
1148     new_this->next_index = This->next_index;
1149
1150     *ppenum = (LPENUMGUID)new_this;
1151     return S_OK;
1152 }
1153
1154 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
1155 {
1156     COMCAT_CLSID_IEnumGUID_QueryInterface,
1157     COMCAT_CLSID_IEnumGUID_AddRef,
1158     COMCAT_CLSID_IEnumGUID_Release,
1159     COMCAT_CLSID_IEnumGUID_Next,
1160     COMCAT_CLSID_IEnumGUID_Skip,
1161     COMCAT_CLSID_IEnumGUID_Reset,
1162     COMCAT_CLSID_IEnumGUID_Clone
1163 };
1164
1165 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
1166 {
1167     CLSID_IEnumGUIDImpl *This;
1168
1169     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1170     if (This) {
1171         static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1172
1173         This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
1174         This->categories = categories;
1175         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
1176     }
1177     return (LPENUMGUID)This;
1178 }
1179
1180 /**********************************************************************
1181  * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1182  *
1183  * This implementation is not thread-safe.  The manager itself is, but
1184  * I can't imagine a valid use of an enumerator in several threads.
1185  */
1186 typedef struct
1187 {
1188     const IEnumGUIDVtbl *lpVtbl;
1189     LONG  ref;
1190     WCHAR keyname[68];
1191     HKEY  key;
1192     DWORD next_index;
1193 } CATID_IEnumGUIDImpl;
1194
1195 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
1196 {
1197     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1198     TRACE("\n");
1199
1200     return InterlockedIncrement(&This->ref);
1201 }
1202
1203 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
1204     LPENUMGUID iface,
1205     REFIID riid,
1206     LPVOID *ppvObj)
1207 {
1208     TRACE("%s\n",debugstr_guid(riid));
1209
1210     if (ppvObj == NULL) return E_POINTER;
1211
1212     if (IsEqualGUID(riid, &IID_IUnknown) ||
1213         IsEqualGUID(riid, &IID_IEnumGUID))
1214     {
1215         *ppvObj = iface;
1216         COMCAT_CATID_IEnumGUID_AddRef(iface);
1217         return S_OK;
1218     }
1219
1220     return E_NOINTERFACE;
1221 }
1222
1223 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
1224 {
1225     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1226     ULONG ref;
1227
1228     TRACE("\n");
1229
1230     ref = InterlockedDecrement(&This->ref);
1231     if (ref == 0) {
1232         if (This->key) RegCloseKey(This->key);
1233         HeapFree(GetProcessHeap(), 0, This);
1234         return 0;
1235     }
1236     return ref;
1237 }
1238
1239 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
1240     LPENUMGUID iface,
1241     ULONG celt,
1242     GUID *rgelt,
1243     ULONG *pceltFetched)
1244 {
1245     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1246     ULONG fetched = 0;
1247
1248     TRACE("\n");
1249
1250     if (rgelt == NULL) return E_POINTER;
1251
1252     if (This->key) while (fetched < celt) {
1253         LSTATUS res;
1254         HRESULT hr;
1255         WCHAR catid[39];
1256         DWORD cName = 39;
1257
1258         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1259                             NULL, NULL, NULL, NULL);
1260         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1261         ++(This->next_index);
1262
1263         hr = CLSIDFromString(catid, rgelt);
1264         if (FAILED(hr)) continue;
1265
1266         ++fetched;
1267         ++rgelt;
1268     }
1269
1270     if (pceltFetched) *pceltFetched = fetched;
1271     return fetched == celt ? S_OK : S_FALSE;
1272 }
1273
1274 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
1275     LPENUMGUID iface,
1276     ULONG celt)
1277 {
1278     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1279
1280     TRACE("\n");
1281
1282     This->next_index += celt;
1283     FIXME("Never returns S_FALSE\n");
1284     return S_OK;
1285 }
1286
1287 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
1288 {
1289     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1290
1291     TRACE("\n");
1292
1293     This->next_index = 0;
1294     return S_OK;
1295 }
1296
1297 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
1298     LPENUMGUID iface,
1299     IEnumGUID **ppenum)
1300 {
1301     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1302     CATID_IEnumGUIDImpl *new_this;
1303
1304     TRACE("\n");
1305
1306     if (ppenum == NULL) return E_POINTER;
1307
1308     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1309     if (new_this == NULL) return E_OUTOFMEMORY;
1310
1311     new_this->lpVtbl = This->lpVtbl;
1312     new_this->ref = 1;
1313     lstrcpyW(new_this->keyname, This->keyname);
1314     /* FIXME: could we more efficiently use DuplicateHandle? */
1315     RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
1316     new_this->next_index = This->next_index;
1317
1318     *ppenum = (LPENUMGUID)new_this;
1319     return S_OK;
1320 }
1321
1322 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
1323 {
1324     COMCAT_CATID_IEnumGUID_QueryInterface,
1325     COMCAT_CATID_IEnumGUID_AddRef,
1326     COMCAT_CATID_IEnumGUID_Release,
1327     COMCAT_CATID_IEnumGUID_Next,
1328     COMCAT_CATID_IEnumGUID_Skip,
1329     COMCAT_CATID_IEnumGUID_Reset,
1330     COMCAT_CATID_IEnumGUID_Clone
1331 };
1332
1333 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
1334     REFCLSID rclsid, LPCWSTR postfix)
1335 {
1336     CATID_IEnumGUIDImpl *This;
1337
1338     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1339     if (This) {
1340         WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
1341
1342         This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
1343         memcpy(This->keyname, prefix, sizeof(prefix));
1344         StringFromGUID2(rclsid, This->keyname + 6, 39);
1345         lstrcpyW(This->keyname + 44, postfix);
1346         RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
1347     }
1348     return (LPENUMGUID)This;
1349 }