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