ole32/tests: Add a test to show that CoGetPSClsid is not affected by an override...
[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 IEnumCATEGORYINFO *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     IEnumCATEGORYINFO **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 (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[] = { '\\', '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[] = { '\\', '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         IClassFactory_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     IEnumCATEGORYINFO IEnumCATEGORYINFO_iface;
810     LONG  ref;
811     LCID  lcid;
812     HKEY  key;
813     DWORD next_index;
814 } IEnumCATEGORYINFOImpl;
815
816 static inline IEnumCATEGORYINFOImpl *impl_from_IEnumCATEGORYINFO(IEnumCATEGORYINFO *iface)
817 {
818     return CONTAINING_RECORD(iface, IEnumCATEGORYINFOImpl, IEnumCATEGORYINFO_iface);
819 }
820
821 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(IEnumCATEGORYINFO *iface)
822 {
823     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
824
825     TRACE("\n");
826
827     return InterlockedIncrement(&This->ref);
828 }
829
830 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
831     IEnumCATEGORYINFO *iface,
832     REFIID riid,
833     LPVOID *ppvObj)
834 {
835     TRACE("%s\n",debugstr_guid(riid));
836
837     if (ppvObj == NULL) return E_POINTER;
838
839     if (IsEqualGUID(riid, &IID_IUnknown) ||
840         IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
841     {
842         *ppvObj = iface;
843         COMCAT_IEnumCATEGORYINFO_AddRef(iface);
844         return S_OK;
845     }
846
847     return E_NOINTERFACE;
848 }
849
850 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(IEnumCATEGORYINFO *iface)
851 {
852     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
853     ULONG ref;
854
855     TRACE("\n");
856
857     ref = InterlockedDecrement(&This->ref);
858     if (ref == 0) {
859         if (This->key) RegCloseKey(This->key);
860         HeapFree(GetProcessHeap(), 0, This);
861         return 0;
862     }
863     return ref;
864 }
865
866 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
867     IEnumCATEGORYINFO *iface,
868     ULONG celt,
869     CATEGORYINFO *rgelt,
870     ULONG *pceltFetched)
871 {
872     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
873     ULONG fetched = 0;
874
875     TRACE("\n");
876
877     if (rgelt == NULL) return E_POINTER;
878
879     if (This->key) while (fetched < celt) {
880         LSTATUS res;
881         HRESULT hr;
882         WCHAR catid[39];
883         DWORD cName = 39;
884         HKEY subkey;
885
886         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
887                             NULL, NULL, NULL, NULL);
888         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
889         ++(This->next_index);
890
891         hr = CLSIDFromString(catid, &rgelt->catid);
892         if (FAILED(hr)) continue;
893
894         res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
895         if (res != ERROR_SUCCESS) continue;
896
897         hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
898                                     rgelt->szDescription, 128);
899         RegCloseKey(subkey);
900         if (FAILED(hr)) continue;
901
902         rgelt->lcid = This->lcid;
903         ++fetched;
904         ++rgelt;
905     }
906
907     if (pceltFetched) *pceltFetched = fetched;
908     return fetched == celt ? S_OK : S_FALSE;
909 }
910
911 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
912     IEnumCATEGORYINFO *iface,
913     ULONG celt)
914 {
915     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
916
917     TRACE("\n");
918
919     This->next_index += celt;
920     /* This should return S_FALSE when there aren't celt elems to skip. */
921     return S_OK;
922 }
923
924 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(IEnumCATEGORYINFO *iface)
925 {
926     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
927
928     TRACE("\n");
929
930     This->next_index = 0;
931     return S_OK;
932 }
933
934 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
935     IEnumCATEGORYINFO *iface,
936     IEnumCATEGORYINFO **ppenum)
937 {
938     IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
939     static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
940                                      't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
941                                      'r', 'i', 'e', 's', 0 };
942     IEnumCATEGORYINFOImpl *new_this;
943
944     TRACE("\n");
945
946     if (ppenum == NULL) return E_POINTER;
947
948     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
949     if (new_this == NULL) return E_OUTOFMEMORY;
950
951     new_this->IEnumCATEGORYINFO_iface = This->IEnumCATEGORYINFO_iface;
952     new_this->ref = 1;
953     new_this->lcid = This->lcid;
954     /* FIXME: could we more efficiently use DuplicateHandle? */
955     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
956     new_this->next_index = This->next_index;
957
958     *ppenum = &new_this->IEnumCATEGORYINFO_iface;
959     return S_OK;
960 }
961
962 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
963 {
964     COMCAT_IEnumCATEGORYINFO_QueryInterface,
965     COMCAT_IEnumCATEGORYINFO_AddRef,
966     COMCAT_IEnumCATEGORYINFO_Release,
967     COMCAT_IEnumCATEGORYINFO_Next,
968     COMCAT_IEnumCATEGORYINFO_Skip,
969     COMCAT_IEnumCATEGORYINFO_Reset,
970     COMCAT_IEnumCATEGORYINFO_Clone
971 };
972
973 static IEnumCATEGORYINFO *COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
974 {
975     IEnumCATEGORYINFOImpl *This;
976
977     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
978     if (This) {
979         static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
980                                          't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
981                                          'r', 'i', 'e', 's', 0 };
982
983         This->IEnumCATEGORYINFO_iface.lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
984         This->lcid = lcid;
985         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
986     }
987     return &This->IEnumCATEGORYINFO_iface;
988 }
989
990 /**********************************************************************
991  * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
992  *
993  * This implementation is not thread-safe.  The manager itself is, but
994  * I can't imagine a valid use of an enumerator in several threads.
995  */
996 typedef struct
997 {
998     const IEnumGUIDVtbl *lpVtbl;
999     LONG  ref;
1000     struct class_categories *categories;
1001     HKEY  key;
1002     DWORD next_index;
1003 } CLSID_IEnumGUIDImpl;
1004
1005 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
1006 {
1007     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1008     TRACE("\n");
1009
1010     return InterlockedIncrement(&This->ref);
1011 }
1012
1013 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
1014     LPENUMGUID iface,
1015     REFIID riid,
1016     LPVOID *ppvObj)
1017 {
1018     TRACE("%s\n",debugstr_guid(riid));
1019
1020     if (ppvObj == NULL) return E_POINTER;
1021
1022     if (IsEqualGUID(riid, &IID_IUnknown) ||
1023         IsEqualGUID(riid, &IID_IEnumGUID))
1024     {
1025         *ppvObj = iface;
1026         COMCAT_CLSID_IEnumGUID_AddRef(iface);
1027         return S_OK;
1028     }
1029
1030     return E_NOINTERFACE;
1031 }
1032
1033 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
1034 {
1035     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1036     ULONG ref;
1037
1038     TRACE("\n");
1039
1040     ref = InterlockedDecrement(&This->ref);
1041     if (ref == 0) {
1042         if (This->key) RegCloseKey(This->key);
1043         HeapFree(GetProcessHeap(), 0, This->categories);
1044         HeapFree(GetProcessHeap(), 0, This);
1045         return 0;
1046     }
1047     return ref;
1048 }
1049
1050 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
1051     LPENUMGUID iface,
1052     ULONG celt,
1053     GUID *rgelt,
1054     ULONG *pceltFetched)
1055 {
1056     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1057     ULONG fetched = 0;
1058
1059     TRACE("\n");
1060
1061     if (rgelt == NULL) return E_POINTER;
1062
1063     if (This->key) while (fetched < celt) {
1064         LSTATUS res;
1065         HRESULT hr;
1066         WCHAR clsid[39];
1067         DWORD cName = 39;
1068         HKEY subkey;
1069
1070         res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
1071                             NULL, NULL, NULL, NULL);
1072         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1073         ++(This->next_index);
1074
1075         hr = CLSIDFromString(clsid, rgelt);
1076         if (FAILED(hr)) continue;
1077
1078         res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
1079         if (res != ERROR_SUCCESS) continue;
1080
1081         hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1082         RegCloseKey(subkey);
1083         if (hr != S_OK) continue;
1084
1085         ++fetched;
1086         ++rgelt;
1087     }
1088
1089     if (pceltFetched) *pceltFetched = fetched;
1090     return fetched == celt ? S_OK : S_FALSE;
1091 }
1092
1093 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
1094     LPENUMGUID iface,
1095     ULONG celt)
1096 {
1097     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1098
1099     TRACE("\n");
1100
1101     This->next_index += celt;
1102     FIXME("Never returns S_FALSE\n");
1103     return S_OK;
1104 }
1105
1106 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
1107 {
1108     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1109
1110     TRACE("\n");
1111
1112     This->next_index = 0;
1113     return S_OK;
1114 }
1115
1116 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
1117     LPENUMGUID iface,
1118     IEnumGUID **ppenum)
1119 {
1120     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1121     static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1122     CLSID_IEnumGUIDImpl *new_this;
1123     DWORD size;
1124
1125     TRACE("\n");
1126
1127     if (ppenum == NULL) return E_POINTER;
1128
1129     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1130     if (new_this == NULL) return E_OUTOFMEMORY;
1131
1132     new_this->lpVtbl = This->lpVtbl;
1133     new_this->ref = 1;
1134     size = HeapSize(GetProcessHeap(), 0, This->categories);
1135     new_this->categories =
1136         HeapAlloc(GetProcessHeap(), 0, size);
1137     if (new_this->categories == NULL) {
1138         HeapFree(GetProcessHeap(), 0, new_this);
1139         return E_OUTOFMEMORY;
1140     }
1141     memcpy(new_this->categories, This->categories, size);
1142     /* FIXME: could we more efficiently use DuplicateHandle? */
1143     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
1144     new_this->next_index = This->next_index;
1145
1146     *ppenum = (LPENUMGUID)new_this;
1147     return S_OK;
1148 }
1149
1150 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
1151 {
1152     COMCAT_CLSID_IEnumGUID_QueryInterface,
1153     COMCAT_CLSID_IEnumGUID_AddRef,
1154     COMCAT_CLSID_IEnumGUID_Release,
1155     COMCAT_CLSID_IEnumGUID_Next,
1156     COMCAT_CLSID_IEnumGUID_Skip,
1157     COMCAT_CLSID_IEnumGUID_Reset,
1158     COMCAT_CLSID_IEnumGUID_Clone
1159 };
1160
1161 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
1162 {
1163     CLSID_IEnumGUIDImpl *This;
1164
1165     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1166     if (This) {
1167         static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1168
1169         This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
1170         This->categories = categories;
1171         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
1172     }
1173     return (LPENUMGUID)This;
1174 }
1175
1176 /**********************************************************************
1177  * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1178  *
1179  * This implementation is not thread-safe.  The manager itself is, but
1180  * I can't imagine a valid use of an enumerator in several threads.
1181  */
1182 typedef struct
1183 {
1184     const IEnumGUIDVtbl *lpVtbl;
1185     LONG  ref;
1186     WCHAR keyname[68];
1187     HKEY  key;
1188     DWORD next_index;
1189 } CATID_IEnumGUIDImpl;
1190
1191 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
1192 {
1193     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1194     TRACE("\n");
1195
1196     return InterlockedIncrement(&This->ref);
1197 }
1198
1199 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
1200     LPENUMGUID iface,
1201     REFIID riid,
1202     LPVOID *ppvObj)
1203 {
1204     TRACE("%s\n",debugstr_guid(riid));
1205
1206     if (ppvObj == NULL) return E_POINTER;
1207
1208     if (IsEqualGUID(riid, &IID_IUnknown) ||
1209         IsEqualGUID(riid, &IID_IEnumGUID))
1210     {
1211         *ppvObj = iface;
1212         COMCAT_CATID_IEnumGUID_AddRef(iface);
1213         return S_OK;
1214     }
1215
1216     return E_NOINTERFACE;
1217 }
1218
1219 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
1220 {
1221     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1222     ULONG ref;
1223
1224     TRACE("\n");
1225
1226     ref = InterlockedDecrement(&This->ref);
1227     if (ref == 0) {
1228         if (This->key) RegCloseKey(This->key);
1229         HeapFree(GetProcessHeap(), 0, This);
1230         return 0;
1231     }
1232     return ref;
1233 }
1234
1235 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
1236     LPENUMGUID iface,
1237     ULONG celt,
1238     GUID *rgelt,
1239     ULONG *pceltFetched)
1240 {
1241     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1242     ULONG fetched = 0;
1243
1244     TRACE("\n");
1245
1246     if (rgelt == NULL) return E_POINTER;
1247
1248     if (This->key) while (fetched < celt) {
1249         LSTATUS res;
1250         HRESULT hr;
1251         WCHAR catid[39];
1252         DWORD cName = 39;
1253
1254         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1255                             NULL, NULL, NULL, NULL);
1256         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1257         ++(This->next_index);
1258
1259         hr = CLSIDFromString(catid, rgelt);
1260         if (FAILED(hr)) continue;
1261
1262         ++fetched;
1263         ++rgelt;
1264     }
1265
1266     if (pceltFetched) *pceltFetched = fetched;
1267     return fetched == celt ? S_OK : S_FALSE;
1268 }
1269
1270 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
1271     LPENUMGUID iface,
1272     ULONG celt)
1273 {
1274     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1275
1276     TRACE("\n");
1277
1278     This->next_index += celt;
1279     FIXME("Never returns S_FALSE\n");
1280     return S_OK;
1281 }
1282
1283 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
1284 {
1285     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1286
1287     TRACE("\n");
1288
1289     This->next_index = 0;
1290     return S_OK;
1291 }
1292
1293 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
1294     LPENUMGUID iface,
1295     IEnumGUID **ppenum)
1296 {
1297     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1298     CATID_IEnumGUIDImpl *new_this;
1299
1300     TRACE("\n");
1301
1302     if (ppenum == NULL) return E_POINTER;
1303
1304     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1305     if (new_this == NULL) return E_OUTOFMEMORY;
1306
1307     new_this->lpVtbl = This->lpVtbl;
1308     new_this->ref = 1;
1309     lstrcpyW(new_this->keyname, This->keyname);
1310     /* FIXME: could we more efficiently use DuplicateHandle? */
1311     RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
1312     new_this->next_index = This->next_index;
1313
1314     *ppenum = (LPENUMGUID)new_this;
1315     return S_OK;
1316 }
1317
1318 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
1319 {
1320     COMCAT_CATID_IEnumGUID_QueryInterface,
1321     COMCAT_CATID_IEnumGUID_AddRef,
1322     COMCAT_CATID_IEnumGUID_Release,
1323     COMCAT_CATID_IEnumGUID_Next,
1324     COMCAT_CATID_IEnumGUID_Skip,
1325     COMCAT_CATID_IEnumGUID_Reset,
1326     COMCAT_CATID_IEnumGUID_Clone
1327 };
1328
1329 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
1330     REFCLSID rclsid, LPCWSTR postfix)
1331 {
1332     CATID_IEnumGUIDImpl *This;
1333
1334     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1335     if (This) {
1336         WCHAR prefix[] = { 'C', 'L', 'S', 'I', 'D', '\\' };
1337
1338         This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
1339         memcpy(This->keyname, prefix, sizeof(prefix));
1340         StringFromGUID2(rclsid, This->keyname + 6, 39);
1341         lstrcpyW(This->keyname + 44, postfix);
1342         RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
1343     }
1344     return (LPENUMGUID)This;
1345 }