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