wintrust: Use path in WIN_TRUST_SUBJECT_FILE structure rather than assuming a path...
[wine] / dlls / comcat / information.c
1 /*
2  *      ComCatMgr ICatInformation implementation for comcat.dll
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 "comcat_private.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(ole);
27
28 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid);
29 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
30                                       ULONG buf_wchars);
31
32 struct class_categories {
33     LPCWSTR impl_strings;
34     LPCWSTR req_strings;
35 };
36
37 static struct class_categories *COMCAT_PrepareClassCategories(
38     ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids);
39 static HRESULT COMCAT_IsClassOfCategories(
40     HKEY key, struct class_categories const* class_categories);
41 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(
42     struct class_categories *class_categories);
43 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
44     REFCLSID rclsid, LPCWSTR impl_req);
45
46 /**********************************************************************
47  * COMCAT_ICatInformation_QueryInterface
48  */
49 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
50     LPCATINFORMATION iface,
51     REFIID riid,
52     LPVOID *ppvObj)
53 {
54     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
55     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
56
57     if (This == NULL || ppvObj == NULL) return E_POINTER;
58
59     return IUnknown_QueryInterface((LPUNKNOWN)&This->unkVtbl, riid, ppvObj);
60 }
61
62 /**********************************************************************
63  * COMCAT_ICatInformation_AddRef
64  */
65 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
66 {
67     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
68     TRACE("\n");
69
70     if (This == NULL) return E_POINTER;
71
72     return IUnknown_AddRef((LPUNKNOWN)&This->unkVtbl);
73 }
74
75 /**********************************************************************
76  * COMCAT_ICatInformation_Release
77  */
78 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
79 {
80     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
81     TRACE("\n");
82
83     if (This == NULL) return E_POINTER;
84
85     return IUnknown_Release((LPUNKNOWN)&This->unkVtbl);
86 }
87
88 /**********************************************************************
89  * COMCAT_ICatInformation_EnumCategories
90  */
91 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
92     LPCATINFORMATION iface,
93     LCID lcid,
94     LPENUMCATEGORYINFO *ppenumCatInfo)
95 {
96 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
97     TRACE("\n");
98
99     if (iface == NULL || ppenumCatInfo == NULL) return E_POINTER;
100
101     *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
102     if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
103     IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
104     return S_OK;
105 }
106
107 /**********************************************************************
108  * COMCAT_ICatInformation_GetCategoryDesc
109  */
110 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
111     LPCATINFORMATION iface,
112     REFCATID rcatid,
113     LCID lcid,
114     PWCHAR *ppszDesc)
115 {
116 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
117     WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
118                           't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
119                           'r', 'i', 'e', 's', '\\', 0 };
120     HKEY key;
121     HRESULT res;
122
123     TRACE("\n\tCATID:\t%s\n\tLCID:\t%X\n",debugstr_guid(rcatid), lcid);
124
125     if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
126
127     /* Open the key for this category. */
128     if (!StringFromGUID2(rcatid, keyname + 21, 39)) return E_FAIL;
129     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
130     if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
131
132     /* Allocate a sensible amount of memory for the description. */
133     *ppszDesc = (PWCHAR) CoTaskMemAlloc(128 * sizeof(WCHAR));
134     if (*ppszDesc == NULL) {
135         RegCloseKey(key);
136         return E_OUTOFMEMORY;
137     }
138
139     /* Get the description, and make sure it's null terminated. */
140     res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
141     RegCloseKey(key);
142     if (FAILED(res)) {
143         CoTaskMemFree(*ppszDesc);
144         return res;
145     }
146
147     return S_OK;
148 }
149
150 /**********************************************************************
151  * COMCAT_ICatInformation_EnumClassesOfCategories
152  */
153 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
154     LPCATINFORMATION iface,
155     ULONG cImplemented,
156     CATID *rgcatidImpl,
157     ULONG cRequired,
158     CATID *rgcatidReq,
159     LPENUMCLSID *ppenumCLSID)
160 {
161 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
162     struct class_categories *categories;
163
164     TRACE("\n");
165
166         if (cImplemented == (ULONG)-1) 
167                 cImplemented = 0;
168         if (cRequired == (ULONG)-1) 
169                 cRequired = 0;
170         
171     if (iface == NULL || ppenumCLSID == NULL ||
172         (cImplemented && rgcatidImpl == NULL) ||
173         (cRequired && rgcatidReq == NULL)) return E_POINTER;
174
175     categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
176                                                cRequired, rgcatidReq);
177     if (categories == NULL) return E_OUTOFMEMORY;
178     *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
179     if (*ppenumCLSID == NULL) {
180         HeapFree(GetProcessHeap(), 0, categories);
181         return E_OUTOFMEMORY;
182     }
183     IEnumGUID_AddRef(*ppenumCLSID);
184     return S_OK;
185 }
186
187 /**********************************************************************
188  * COMCAT_ICatInformation_IsClassOfCategories
189  */
190 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
191     LPCATINFORMATION iface,
192     REFCLSID rclsid,
193     ULONG cImplemented,
194     CATID *rgcatidImpl,
195     ULONG cRequired,
196     CATID *rgcatidReq)
197 {
198 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
199     WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
200     HRESULT res;
201     struct class_categories *categories;
202     HKEY key;
203
204     if (WINE_TRACE_ON(ole)) {
205         ULONG count;
206         TRACE("\n\tCLSID:\t%s\n\tImplemented %u\n",debugstr_guid(rclsid),cImplemented);
207         for (count = 0; count < cImplemented; ++count)
208             TRACE("\t\t%s\n",debugstr_guid(&rgcatidImpl[count]));
209         TRACE("\tRequired %u\n",cRequired);
210         for (count = 0; count < cRequired; ++count)
211             TRACE("\t\t%s\n",debugstr_guid(&rgcatidReq[count]));
212     }
213
214     if ((cImplemented && rgcatidImpl == NULL) ||
215         (cRequired && rgcatidReq == NULL)) return E_POINTER;
216
217     res = StringFromGUID2(rclsid, keyname + 6, 39);
218     if (FAILED(res)) return res;
219
220     categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
221                                                cRequired, rgcatidReq);
222     if (categories == NULL) return E_OUTOFMEMORY;
223
224     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
225     if (res == ERROR_SUCCESS) {
226         res = COMCAT_IsClassOfCategories(key, categories);
227         RegCloseKey(key);
228     } else res = S_FALSE;
229
230     HeapFree(GetProcessHeap(), 0, categories);
231
232     return res;
233 }
234
235 /**********************************************************************
236  * COMCAT_ICatInformation_EnumImplCategoriesOfClass
237  */
238 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
239     LPCATINFORMATION iface,
240     REFCLSID rclsid,
241     LPENUMCATID *ppenumCATID)
242 {
243 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
244     static const WCHAR postfix[24] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
245                           'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
246                           'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
247
248     TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
249
250     if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
251         return E_POINTER;
252
253     *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
254     if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
255     return S_OK;
256 }
257
258 /**********************************************************************
259  * COMCAT_ICatInformation_EnumReqCategoriesOfClass
260  */
261 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
262     LPCATINFORMATION iface,
263     REFCLSID rclsid,
264     LPENUMCATID *ppenumCATID)
265 {
266 /*     ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
267     static const WCHAR postfix[21] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
268                           'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
269                           'r', 'i', 'e', 's', 0 };
270
271     TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
272
273     if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
274         return E_POINTER;
275
276     *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
277     if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
278     return S_OK;
279 }
280
281 /**********************************************************************
282  * COMCAT_ICatInformation_Vtbl
283  */
284 const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
285 {
286     COMCAT_ICatInformation_QueryInterface,
287     COMCAT_ICatInformation_AddRef,
288     COMCAT_ICatInformation_Release,
289     COMCAT_ICatInformation_EnumCategories,
290     COMCAT_ICatInformation_GetCategoryDesc,
291     COMCAT_ICatInformation_EnumClassesOfCategories,
292     COMCAT_ICatInformation_IsClassOfCategories,
293     COMCAT_ICatInformation_EnumImplCategoriesOfClass,
294     COMCAT_ICatInformation_EnumReqCategoriesOfClass
295 };
296
297 /**********************************************************************
298  * IEnumCATEGORYINFO implementation
299  *
300  * This implementation is not thread-safe.  The manager itself is, but
301  * I can't imagine a valid use of an enumerator in several threads.
302  */
303 typedef struct
304 {
305     const IEnumCATEGORYINFOVtbl *lpVtbl;
306     LONG  ref;
307     LCID  lcid;
308     HKEY  key;
309     DWORD next_index;
310 } IEnumCATEGORYINFOImpl;
311
312 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
313 {
314     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
315
316     TRACE("\n");
317
318     if (This == NULL) return E_POINTER;
319
320     return InterlockedIncrement(&This->ref);
321 }
322
323 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
324     LPENUMCATEGORYINFO iface,
325     REFIID riid,
326     LPVOID *ppvObj)
327 {
328     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
329     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
330
331     if (This == NULL || ppvObj == NULL) return E_POINTER;
332
333     if (IsEqualGUID(riid, &IID_IUnknown) ||
334         IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
335     {
336         *ppvObj = (LPVOID)iface;
337         COMCAT_IEnumCATEGORYINFO_AddRef(iface);
338         return S_OK;
339     }
340
341     return E_NOINTERFACE;
342 }
343
344 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
345 {
346     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
347     ULONG ref;
348
349     TRACE("\n");
350
351     if (This == NULL) return E_POINTER;
352
353     ref = InterlockedDecrement(&This->ref);
354     if (ref == 0) {
355         if (This->key) RegCloseKey(This->key);
356         HeapFree(GetProcessHeap(), 0, This);
357         return 0;
358     }
359     return ref;
360 }
361
362 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
363     LPENUMCATEGORYINFO iface,
364     ULONG celt,
365     CATEGORYINFO *rgelt,
366     ULONG *pceltFetched)
367 {
368     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
369     ULONG fetched = 0;
370
371     TRACE("\n");
372
373     if (This == NULL || rgelt == NULL) return E_POINTER;
374
375     if (This->key) while (fetched < celt) {
376         HRESULT res;
377         WCHAR catid[39];
378         DWORD cName = 39;
379         HKEY subkey;
380
381         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
382                             NULL, NULL, NULL, NULL);
383         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
384         ++(This->next_index);
385
386         res = CLSIDFromString(catid, &rgelt->catid);
387         if (FAILED(res)) continue;
388
389         res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
390         if (res != ERROR_SUCCESS) continue;
391
392         res = COMCAT_GetCategoryDesc(subkey, This->lcid,
393                                      rgelt->szDescription, 128);
394         RegCloseKey(subkey);
395         if (FAILED(res)) continue;
396
397         rgelt->lcid = This->lcid;
398         ++fetched;
399         ++rgelt;
400     }
401
402     if (pceltFetched) *pceltFetched = fetched;
403     return fetched == celt ? S_OK : S_FALSE;
404 }
405
406 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
407     LPENUMCATEGORYINFO iface,
408     ULONG celt)
409 {
410     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
411
412     TRACE("\n");
413
414     if (This == NULL) return E_POINTER;
415     This->next_index += celt;
416     /* This should return S_FALSE when there aren't celt elems to skip. */
417     return S_OK;
418 }
419
420 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
421 {
422     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
423
424     TRACE("\n");
425
426     if (This == NULL) return E_POINTER;
427     This->next_index = 0;
428     return S_OK;
429 }
430
431 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
432     LPENUMCATEGORYINFO iface,
433     IEnumCATEGORYINFO **ppenum)
434 {
435     IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
436     static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
437                                      't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
438                                      'r', 'i', 'e', 's', 0 };
439     IEnumCATEGORYINFOImpl *new_this;
440
441     TRACE("\n");
442
443     if (This == NULL || ppenum == NULL) return E_POINTER;
444
445     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
446     if (new_this == NULL) return E_OUTOFMEMORY;
447
448     new_this->lpVtbl = This->lpVtbl;
449     new_this->ref = 1;
450     new_this->lcid = This->lcid;
451     /* FIXME: could we more efficiently use DuplicateHandle? */
452     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
453     new_this->next_index = This->next_index;
454
455     *ppenum = (LPENUMCATEGORYINFO)new_this;
456     return S_OK;
457 }
458
459 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
460 {
461     COMCAT_IEnumCATEGORYINFO_QueryInterface,
462     COMCAT_IEnumCATEGORYINFO_AddRef,
463     COMCAT_IEnumCATEGORYINFO_Release,
464     COMCAT_IEnumCATEGORYINFO_Next,
465     COMCAT_IEnumCATEGORYINFO_Skip,
466     COMCAT_IEnumCATEGORYINFO_Reset,
467     COMCAT_IEnumCATEGORYINFO_Clone
468 };
469
470 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
471 {
472     IEnumCATEGORYINFOImpl *This;
473
474     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
475     if (This) {
476         static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
477                                          't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
478                                          'r', 'i', 'e', 's', 0 };
479
480         This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
481         This->lcid = lcid;
482         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
483     }
484     return (LPENUMCATEGORYINFO)This;
485 }
486
487 /**********************************************************************
488  * COMCAT_GetCategoryDesc
489  */
490 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
491                                       ULONG buf_wchars)
492 {
493     static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
494     WCHAR valname[5];
495     HRESULT res;
496     DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
497
498     if (pszDesc == NULL) return E_INVALIDARG;
499
500     /* FIXME: lcid comparisons are more complex than this! */
501     wsprintfW(valname, fmt, lcid);
502     res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
503     if (res != ERROR_SUCCESS || type != REG_SZ) {
504         FIXME("Simplified lcid comparison\n");
505         return CAT_E_NODESCRIPTION;
506     }
507     pszDesc[size / sizeof(WCHAR)] = (WCHAR)0;
508
509     return S_OK;
510 }
511
512 /**********************************************************************
513  * COMCAT_PrepareClassCategories
514  */
515 static struct class_categories *COMCAT_PrepareClassCategories(
516     ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
517 {
518     struct class_categories *categories;
519     WCHAR *strings;
520
521     categories = HeapAlloc(
522         GetProcessHeap(), HEAP_ZERO_MEMORY,
523         sizeof(struct class_categories) +
524         ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
525     if (categories == NULL) return categories;
526
527     strings = (WCHAR *)(categories + 1);
528     categories->impl_strings = strings;
529     while (impl_count--) {
530         StringFromGUID2(impl_catids++, strings, 39);
531         strings += 39;
532     }
533     *strings++ = 0;
534
535     categories->req_strings = strings;
536     while (req_count--) {
537         StringFromGUID2(req_catids++, strings, 39);
538         strings += 39;
539     }
540     *strings++ = 0;
541
542     return categories;
543 }
544
545 /**********************************************************************
546  * COMCAT_IsClassOfCategories
547  */
548 static HRESULT COMCAT_IsClassOfCategories(
549     HKEY key,
550     struct class_categories const* categories)
551 {
552     static const WCHAR impl_keyname[] = { 'I', 'm', 'p', 'l', 'e', 'm', 'e', 'n',
553                                           't', 'e', 'd', ' ', 'C', 'a', 't', 'e',
554                                           'g', 'o', 'r', 'i', 'e', 's', 0 };
555     static const WCHAR req_keyname[]  = { 'R', 'e', 'q', 'u', 'i', 'r', 'e', 'd',
556                                           ' ', 'C', 'a', 't', 'e', 'g', 'o', 'r',
557                                           'i', 'e', 's', 0 };
558     HKEY subkey;
559     HRESULT res;
560     DWORD index;
561     LPCWSTR string;
562
563     /* Check that every given category is implemented by class. */
564     res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
565     if (res != ERROR_SUCCESS) return S_FALSE;
566     for (string = categories->impl_strings; *string; string += 39) {
567         HKEY catkey;
568         res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
569         if (res != ERROR_SUCCESS) {
570             RegCloseKey(subkey);
571             return S_FALSE;
572         }
573         RegCloseKey(catkey);
574     }
575     RegCloseKey(subkey);
576
577     /* Check that all categories required by class are given. */
578     res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
579     if (res == ERROR_SUCCESS) {
580         for (index = 0; ; ++index) {
581             WCHAR keyname[39];
582             DWORD size = 39;
583
584             res = RegEnumKeyExW(subkey, index, keyname, &size,
585                                 NULL, NULL, NULL, NULL);
586             if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
587             if (size != 38) continue; /* bogus catid in registry */
588             for (string = categories->req_strings; *string; string += 39)
589                 if (!strcmpiW(string, keyname)) break;
590             if (!*string) {
591                 RegCloseKey(subkey);
592                 return S_FALSE;
593             }
594         }
595         RegCloseKey(subkey);
596     }
597
598     return S_OK;
599 }
600
601 /**********************************************************************
602  * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
603  *
604  * This implementation is not thread-safe.  The manager itself is, but
605  * I can't imagine a valid use of an enumerator in several threads.
606  */
607 typedef struct
608 {
609     const IEnumGUIDVtbl *lpVtbl;
610     LONG  ref;
611     struct class_categories *categories;
612     HKEY  key;
613     DWORD next_index;
614 } CLSID_IEnumGUIDImpl;
615
616 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
617 {
618     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
619     TRACE("\n");
620
621     if (This == NULL) return E_POINTER;
622
623     return InterlockedIncrement(&This->ref);
624 }
625
626 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
627     LPENUMGUID iface,
628     REFIID riid,
629     LPVOID *ppvObj)
630 {
631     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
632     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
633
634     if (This == NULL || ppvObj == NULL) return E_POINTER;
635
636     if (IsEqualGUID(riid, &IID_IUnknown) ||
637         IsEqualGUID(riid, &IID_IEnumGUID))
638     {
639         *ppvObj = (LPVOID)iface;
640         COMCAT_CLSID_IEnumGUID_AddRef(iface);
641         return S_OK;
642     }
643
644     return E_NOINTERFACE;
645 }
646
647 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
648 {
649     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
650     ULONG ref;
651
652     TRACE("\n");
653
654     if (This == NULL) return E_POINTER;
655
656     ref = InterlockedDecrement(&This->ref);
657     if (ref == 0) {
658         if (This->key) RegCloseKey(This->key);
659         HeapFree(GetProcessHeap(), 0, (LPVOID)This->categories);
660         HeapFree(GetProcessHeap(), 0, This);
661         return 0;
662     }
663     return ref;
664 }
665
666 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
667     LPENUMGUID iface,
668     ULONG celt,
669     GUID *rgelt,
670     ULONG *pceltFetched)
671 {
672     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
673     ULONG fetched = 0;
674
675     TRACE("\n");
676
677     if (This == NULL || rgelt == NULL) return E_POINTER;
678
679     if (This->key) while (fetched < celt) {
680         HRESULT res;
681         WCHAR clsid[39];
682         DWORD cName = 39;
683         HKEY subkey;
684
685         res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
686                             NULL, NULL, NULL, NULL);
687         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
688         ++(This->next_index);
689
690         res = CLSIDFromString(clsid, rgelt);
691         if (FAILED(res)) continue;
692
693         res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
694         if (res != ERROR_SUCCESS) continue;
695
696         res = COMCAT_IsClassOfCategories(subkey, This->categories);
697         RegCloseKey(subkey);
698         if (res != S_OK) continue;
699
700         ++fetched;
701         ++rgelt;
702     }
703
704     if (pceltFetched) *pceltFetched = fetched;
705     return fetched == celt ? S_OK : S_FALSE;
706 }
707
708 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
709     LPENUMGUID iface,
710     ULONG celt)
711 {
712     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
713
714     TRACE("\n");
715
716     if (This == NULL) return E_POINTER;
717     This->next_index += celt;
718     FIXME("Never returns S_FALSE\n");
719     return S_OK;
720 }
721
722 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
723 {
724     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
725
726     TRACE("\n");
727
728     if (This == NULL) return E_POINTER;
729     This->next_index = 0;
730     return S_OK;
731 }
732
733 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
734     LPENUMGUID iface,
735     IEnumGUID **ppenum)
736 {
737     CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
738     static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
739     CLSID_IEnumGUIDImpl *new_this;
740     DWORD size;
741
742     TRACE("\n");
743
744     if (This == NULL || ppenum == NULL) return E_POINTER;
745
746     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
747     if (new_this == NULL) return E_OUTOFMEMORY;
748
749     new_this->lpVtbl = This->lpVtbl;
750     new_this->ref = 1;
751     size = HeapSize(GetProcessHeap(), 0, (LPVOID)This->categories);
752     new_this->categories =
753         HeapAlloc(GetProcessHeap(), 0, size);
754     if (new_this->categories == NULL) {
755         HeapFree(GetProcessHeap(), 0, new_this);
756         return E_OUTOFMEMORY;
757     }
758     memcpy((LPVOID)new_this->categories, This->categories, size);
759     /* FIXME: could we more efficiently use DuplicateHandle? */
760     RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
761     new_this->next_index = This->next_index;
762
763     *ppenum = (LPENUMGUID)new_this;
764     return S_OK;
765 }
766
767 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
768 {
769     COMCAT_CLSID_IEnumGUID_QueryInterface,
770     COMCAT_CLSID_IEnumGUID_AddRef,
771     COMCAT_CLSID_IEnumGUID_Release,
772     COMCAT_CLSID_IEnumGUID_Next,
773     COMCAT_CLSID_IEnumGUID_Skip,
774     COMCAT_CLSID_IEnumGUID_Reset,
775     COMCAT_CLSID_IEnumGUID_Clone
776 };
777
778 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
779 {
780     CLSID_IEnumGUIDImpl *This;
781
782     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
783     if (This) {
784         static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
785
786         This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
787         This->categories = categories;
788         RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
789     }
790     return (LPENUMGUID)This;
791 }
792
793 /**********************************************************************
794  * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
795  *
796  * This implementation is not thread-safe.  The manager itself is, but
797  * I can't imagine a valid use of an enumerator in several threads.
798  */
799 typedef struct
800 {
801     const IEnumGUIDVtbl *lpVtbl;
802     LONG  ref;
803     WCHAR keyname[68];
804     HKEY  key;
805     DWORD next_index;
806 } CATID_IEnumGUIDImpl;
807
808 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
809 {
810     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
811     TRACE("\n");
812
813     if (This == NULL) return E_POINTER;
814
815     return InterlockedIncrement(&This->ref);
816 }
817
818 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
819     LPENUMGUID iface,
820     REFIID riid,
821     LPVOID *ppvObj)
822 {
823     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
824     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
825
826     if (This == NULL || ppvObj == NULL) return E_POINTER;
827
828     if (IsEqualGUID(riid, &IID_IUnknown) ||
829         IsEqualGUID(riid, &IID_IEnumGUID))
830     {
831         *ppvObj = (LPVOID)iface;
832         COMCAT_CATID_IEnumGUID_AddRef(iface);
833         return S_OK;
834     }
835
836     return E_NOINTERFACE;
837 }
838
839 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
840 {
841     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
842     ULONG ref;
843
844     TRACE("\n");
845
846     if (This == NULL) return E_POINTER;
847
848     ref = InterlockedDecrement(&This->ref);
849     if (ref == 0) {
850         if (This->key) RegCloseKey(This->key);
851         HeapFree(GetProcessHeap(), 0, This);
852         return 0;
853     }
854     return ref;
855 }
856
857 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
858     LPENUMGUID iface,
859     ULONG celt,
860     GUID *rgelt,
861     ULONG *pceltFetched)
862 {
863     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
864     ULONG fetched = 0;
865
866     TRACE("\n");
867
868     if (This == NULL || rgelt == NULL) return E_POINTER;
869
870     if (This->key) while (fetched < celt) {
871         HRESULT res;
872         WCHAR catid[39];
873         DWORD cName = 39;
874
875         res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
876                             NULL, NULL, NULL, NULL);
877         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
878         ++(This->next_index);
879
880         res = CLSIDFromString(catid, rgelt);
881         if (FAILED(res)) continue;
882
883         ++fetched;
884         ++rgelt;
885     }
886
887     if (pceltFetched) *pceltFetched = fetched;
888     return fetched == celt ? S_OK : S_FALSE;
889 }
890
891 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
892     LPENUMGUID iface,
893     ULONG celt)
894 {
895     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
896
897     TRACE("\n");
898
899     if (This == NULL) return E_POINTER;
900     This->next_index += celt;
901     FIXME("Never returns S_FALSE\n");
902     return S_OK;
903 }
904
905 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
906 {
907     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
908
909     TRACE("\n");
910
911     if (This == NULL) return E_POINTER;
912     This->next_index = 0;
913     return S_OK;
914 }
915
916 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
917     LPENUMGUID iface,
918     IEnumGUID **ppenum)
919 {
920     CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
921     CATID_IEnumGUIDImpl *new_this;
922
923     TRACE("\n");
924
925     if (This == NULL || ppenum == NULL) return E_POINTER;
926
927     new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
928     if (new_this == NULL) return E_OUTOFMEMORY;
929
930     new_this->lpVtbl = This->lpVtbl;
931     new_this->ref = 1;
932     lstrcpyW(new_this->keyname, This->keyname);
933     /* FIXME: could we more efficiently use DuplicateHandle? */
934     RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
935     new_this->next_index = This->next_index;
936
937     *ppenum = (LPENUMGUID)new_this;
938     return S_OK;
939 }
940
941 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
942 {
943     COMCAT_CATID_IEnumGUID_QueryInterface,
944     COMCAT_CATID_IEnumGUID_AddRef,
945     COMCAT_CATID_IEnumGUID_Release,
946     COMCAT_CATID_IEnumGUID_Next,
947     COMCAT_CATID_IEnumGUID_Skip,
948     COMCAT_CATID_IEnumGUID_Reset,
949     COMCAT_CATID_IEnumGUID_Clone
950 };
951
952 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
953     REFCLSID rclsid, LPCWSTR postfix)
954 {
955     CATID_IEnumGUIDImpl *This;
956
957     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
958     if (This) {
959         WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
960
961         This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
962         memcpy(This->keyname, prefix, sizeof(prefix));
963         StringFromGUID2(rclsid, This->keyname + 6, 39);
964         lstrcpyW(This->keyname + 44, postfix);
965         RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
966     }
967     return (LPENUMGUID)This;
968 }