msxml3: Implement IXMLDOMCDATASection_substringData.
[wine/wine64.git] / dlls / comcat / information.c
blobcd5a9ea9dd9dd4d77efba08ce312310d415ed78c
1 /*
2 * ComCatMgr ICatInformation implementation for comcat.dll
4 * Copyright (C) 2002 John K. Hohm
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.
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.
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
21 #include <string.h>
22 #include "comcat_private.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(ole);
28 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid);
29 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
30 ULONG buf_wchars);
32 struct class_categories {
33 LPCWSTR impl_strings;
34 LPCWSTR req_strings;
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);
46 /**********************************************************************
47 * COMCAT_ICatInformation_QueryInterface
49 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
50 LPCATINFORMATION iface,
51 REFIID riid,
52 LPVOID *ppvObj)
54 ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
55 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
57 if (This == NULL || ppvObj == NULL) return E_POINTER;
59 return IUnknown_QueryInterface((LPUNKNOWN)&This->unkVtbl, riid, ppvObj);
62 /**********************************************************************
63 * COMCAT_ICatInformation_AddRef
65 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
67 ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
68 TRACE("\n");
70 if (This == NULL) return E_POINTER;
72 return IUnknown_AddRef((LPUNKNOWN)&This->unkVtbl);
75 /**********************************************************************
76 * COMCAT_ICatInformation_Release
78 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
80 ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface);
81 TRACE("\n");
83 if (This == NULL) return E_POINTER;
85 return IUnknown_Release((LPUNKNOWN)&This->unkVtbl);
88 /**********************************************************************
89 * COMCAT_ICatInformation_EnumCategories
91 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
92 LPCATINFORMATION iface,
93 LCID lcid,
94 LPENUMCATEGORYINFO *ppenumCatInfo)
96 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
97 TRACE("\n");
99 if (iface == NULL || ppenumCatInfo == NULL) return E_POINTER;
101 *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
102 if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
103 IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
104 return S_OK;
107 /**********************************************************************
108 * COMCAT_ICatInformation_GetCategoryDesc
110 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
111 LPCATINFORMATION iface,
112 REFCATID rcatid,
113 LCID lcid,
114 PWCHAR *ppszDesc)
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;
123 TRACE("\n\tCATID:\t%s\n\tLCID:\t%X\n",debugstr_guid(rcatid), lcid);
125 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
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;
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;
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;
147 return S_OK;
150 /**********************************************************************
151 * COMCAT_ICatInformation_EnumClassesOfCategories
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)
161 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
162 struct class_categories *categories;
164 TRACE("\n");
166 if (cImplemented == (ULONG)-1)
167 cImplemented = 0;
168 if (cRequired == (ULONG)-1)
169 cRequired = 0;
171 if (iface == NULL || ppenumCLSID == NULL ||
172 (cImplemented && rgcatidImpl == NULL) ||
173 (cRequired && rgcatidReq == NULL)) return E_POINTER;
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;
183 IEnumGUID_AddRef(*ppenumCLSID);
184 return S_OK;
187 /**********************************************************************
188 * COMCAT_ICatInformation_IsClassOfCategories
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)
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;
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]));
214 if ((cImplemented && rgcatidImpl == NULL) ||
215 (cRequired && rgcatidReq == NULL)) return E_POINTER;
217 res = StringFromGUID2(rclsid, keyname + 6, 39);
218 if (FAILED(res)) return res;
220 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
221 cRequired, rgcatidReq);
222 if (categories == NULL) return E_OUTOFMEMORY;
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;
230 HeapFree(GetProcessHeap(), 0, categories);
232 return res;
235 /**********************************************************************
236 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
238 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
239 LPCATINFORMATION iface,
240 REFCLSID rclsid,
241 LPENUMCATID *ppenumCATID)
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 };
248 TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
250 if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
251 return E_POINTER;
253 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
254 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
255 return S_OK;
258 /**********************************************************************
259 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
261 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
262 LPCATINFORMATION iface,
263 REFCLSID rclsid,
264 LPENUMCATID *ppenumCATID)
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 };
271 TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
273 if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
274 return E_POINTER;
276 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
277 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
278 return S_OK;
281 /**********************************************************************
282 * COMCAT_ICatInformation_Vtbl
284 const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
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
297 /**********************************************************************
298 * IEnumCATEGORYINFO implementation
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.
303 typedef struct
305 const IEnumCATEGORYINFOVtbl *lpVtbl;
306 LONG ref;
307 LCID lcid;
308 HKEY key;
309 DWORD next_index;
310 } IEnumCATEGORYINFOImpl;
312 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
314 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
316 TRACE("\n");
318 if (This == NULL) return E_POINTER;
320 return InterlockedIncrement(&This->ref);
323 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
324 LPENUMCATEGORYINFO iface,
325 REFIID riid,
326 LPVOID *ppvObj)
328 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
329 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
331 if (This == NULL || ppvObj == NULL) return E_POINTER;
333 if (IsEqualGUID(riid, &IID_IUnknown) ||
334 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
336 *ppvObj = (LPVOID)iface;
337 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
338 return S_OK;
341 return E_NOINTERFACE;
344 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
346 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
347 ULONG ref;
349 TRACE("\n");
351 if (This == NULL) return E_POINTER;
353 ref = InterlockedDecrement(&This->ref);
354 if (ref == 0) {
355 if (This->key) RegCloseKey(This->key);
356 HeapFree(GetProcessHeap(), 0, This);
357 return 0;
359 return ref;
362 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
363 LPENUMCATEGORYINFO iface,
364 ULONG celt,
365 CATEGORYINFO *rgelt,
366 ULONG *pceltFetched)
368 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
369 ULONG fetched = 0;
371 TRACE("\n");
373 if (This == NULL || rgelt == NULL) return E_POINTER;
375 if (This->key) while (fetched < celt) {
376 HRESULT res;
377 WCHAR catid[39];
378 DWORD cName = 39;
379 HKEY subkey;
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);
386 res = CLSIDFromString(catid, &rgelt->catid);
387 if (FAILED(res)) continue;
389 res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
390 if (res != ERROR_SUCCESS) continue;
392 res = COMCAT_GetCategoryDesc(subkey, This->lcid,
393 rgelt->szDescription, 128);
394 RegCloseKey(subkey);
395 if (FAILED(res)) continue;
397 rgelt->lcid = This->lcid;
398 ++fetched;
399 ++rgelt;
402 if (pceltFetched) *pceltFetched = fetched;
403 return fetched == celt ? S_OK : S_FALSE;
406 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
407 LPENUMCATEGORYINFO iface,
408 ULONG celt)
410 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
412 TRACE("\n");
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;
420 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
422 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
424 TRACE("\n");
426 if (This == NULL) return E_POINTER;
427 This->next_index = 0;
428 return S_OK;
431 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
432 LPENUMCATEGORYINFO iface,
433 IEnumCATEGORYINFO **ppenum)
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;
441 TRACE("\n");
443 if (This == NULL || ppenum == NULL) return E_POINTER;
445 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
446 if (new_this == NULL) return E_OUTOFMEMORY;
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;
455 *ppenum = (LPENUMCATEGORYINFO)new_this;
456 return S_OK;
459 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
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
470 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
472 IEnumCATEGORYINFOImpl *This;
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 };
480 This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
481 This->lcid = lcid;
482 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
484 return (LPENUMCATEGORYINFO)This;
487 /**********************************************************************
488 * COMCAT_GetCategoryDesc
490 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
491 ULONG buf_wchars)
493 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
494 WCHAR valname[5];
495 HRESULT res;
496 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
498 if (pszDesc == NULL) return E_INVALIDARG;
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;
507 pszDesc[size / sizeof(WCHAR)] = (WCHAR)0;
509 return S_OK;
512 /**********************************************************************
513 * COMCAT_PrepareClassCategories
515 static struct class_categories *COMCAT_PrepareClassCategories(
516 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
518 struct class_categories *categories;
519 WCHAR *strings;
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;
527 strings = (WCHAR *)(categories + 1);
528 categories->impl_strings = strings;
529 while (impl_count--) {
530 StringFromGUID2(impl_catids++, strings, 39);
531 strings += 39;
533 *strings++ = 0;
535 categories->req_strings = strings;
536 while (req_count--) {
537 StringFromGUID2(req_catids++, strings, 39);
538 strings += 39;
540 *strings++ = 0;
542 return categories;
545 /**********************************************************************
546 * COMCAT_IsClassOfCategories
548 static HRESULT COMCAT_IsClassOfCategories(
549 HKEY key,
550 struct class_categories const* categories)
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;
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;
573 RegCloseKey(catkey);
575 RegCloseKey(subkey);
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;
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;
595 RegCloseKey(subkey);
598 return S_OK;
601 /**********************************************************************
602 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
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.
607 typedef struct
609 const IEnumGUIDVtbl *lpVtbl;
610 LONG ref;
611 struct class_categories *categories;
612 HKEY key;
613 DWORD next_index;
614 } CLSID_IEnumGUIDImpl;
616 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
618 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
619 TRACE("\n");
621 if (This == NULL) return E_POINTER;
623 return InterlockedIncrement(&This->ref);
626 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
627 LPENUMGUID iface,
628 REFIID riid,
629 LPVOID *ppvObj)
631 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
632 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
634 if (This == NULL || ppvObj == NULL) return E_POINTER;
636 if (IsEqualGUID(riid, &IID_IUnknown) ||
637 IsEqualGUID(riid, &IID_IEnumGUID))
639 *ppvObj = (LPVOID)iface;
640 COMCAT_CLSID_IEnumGUID_AddRef(iface);
641 return S_OK;
644 return E_NOINTERFACE;
647 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
649 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
650 ULONG ref;
652 TRACE("\n");
654 if (This == NULL) return E_POINTER;
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;
663 return ref;
666 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
667 LPENUMGUID iface,
668 ULONG celt,
669 GUID *rgelt,
670 ULONG *pceltFetched)
672 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
673 ULONG fetched = 0;
675 TRACE("\n");
677 if (This == NULL || rgelt == NULL) return E_POINTER;
679 if (This->key) while (fetched < celt) {
680 HRESULT res;
681 WCHAR clsid[39];
682 DWORD cName = 39;
683 HKEY subkey;
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);
690 res = CLSIDFromString(clsid, rgelt);
691 if (FAILED(res)) continue;
693 res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
694 if (res != ERROR_SUCCESS) continue;
696 res = COMCAT_IsClassOfCategories(subkey, This->categories);
697 RegCloseKey(subkey);
698 if (res != S_OK) continue;
700 ++fetched;
701 ++rgelt;
704 if (pceltFetched) *pceltFetched = fetched;
705 return fetched == celt ? S_OK : S_FALSE;
708 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
709 LPENUMGUID iface,
710 ULONG celt)
712 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
714 TRACE("\n");
716 if (This == NULL) return E_POINTER;
717 This->next_index += celt;
718 FIXME("Never returns S_FALSE\n");
719 return S_OK;
722 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
724 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
726 TRACE("\n");
728 if (This == NULL) return E_POINTER;
729 This->next_index = 0;
730 return S_OK;
733 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
734 LPENUMGUID iface,
735 IEnumGUID **ppenum)
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;
742 TRACE("\n");
744 if (This == NULL || ppenum == NULL) return E_POINTER;
746 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
747 if (new_this == NULL) return E_OUTOFMEMORY;
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;
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;
763 *ppenum = (LPENUMGUID)new_this;
764 return S_OK;
767 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
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
778 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
780 CLSID_IEnumGUIDImpl *This;
782 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
783 if (This) {
784 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
786 This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
787 This->categories = categories;
788 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
790 return (LPENUMGUID)This;
793 /**********************************************************************
794 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
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.
799 typedef struct
801 const IEnumGUIDVtbl *lpVtbl;
802 LONG ref;
803 WCHAR keyname[68];
804 HKEY key;
805 DWORD next_index;
806 } CATID_IEnumGUIDImpl;
808 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
810 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
811 TRACE("\n");
813 if (This == NULL) return E_POINTER;
815 return InterlockedIncrement(&This->ref);
818 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
819 LPENUMGUID iface,
820 REFIID riid,
821 LPVOID *ppvObj)
823 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
824 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
826 if (This == NULL || ppvObj == NULL) return E_POINTER;
828 if (IsEqualGUID(riid, &IID_IUnknown) ||
829 IsEqualGUID(riid, &IID_IEnumGUID))
831 *ppvObj = (LPVOID)iface;
832 COMCAT_CATID_IEnumGUID_AddRef(iface);
833 return S_OK;
836 return E_NOINTERFACE;
839 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
841 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
842 ULONG ref;
844 TRACE("\n");
846 if (This == NULL) return E_POINTER;
848 ref = InterlockedDecrement(&This->ref);
849 if (ref == 0) {
850 if (This->key) RegCloseKey(This->key);
851 HeapFree(GetProcessHeap(), 0, This);
852 return 0;
854 return ref;
857 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
858 LPENUMGUID iface,
859 ULONG celt,
860 GUID *rgelt,
861 ULONG *pceltFetched)
863 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
864 ULONG fetched = 0;
866 TRACE("\n");
868 if (This == NULL || rgelt == NULL) return E_POINTER;
870 if (This->key) while (fetched < celt) {
871 HRESULT res;
872 WCHAR catid[39];
873 DWORD cName = 39;
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);
880 res = CLSIDFromString(catid, rgelt);
881 if (FAILED(res)) continue;
883 ++fetched;
884 ++rgelt;
887 if (pceltFetched) *pceltFetched = fetched;
888 return fetched == celt ? S_OK : S_FALSE;
891 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
892 LPENUMGUID iface,
893 ULONG celt)
895 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
897 TRACE("\n");
899 if (This == NULL) return E_POINTER;
900 This->next_index += celt;
901 FIXME("Never returns S_FALSE\n");
902 return S_OK;
905 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
907 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
909 TRACE("\n");
911 if (This == NULL) return E_POINTER;
912 This->next_index = 0;
913 return S_OK;
916 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
917 LPENUMGUID iface,
918 IEnumGUID **ppenum)
920 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
921 CATID_IEnumGUIDImpl *new_this;
923 TRACE("\n");
925 if (This == NULL || ppenum == NULL) return E_POINTER;
927 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
928 if (new_this == NULL) return E_OUTOFMEMORY;
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;
937 *ppenum = (LPENUMGUID)new_this;
938 return S_OK;
941 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
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
952 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
953 REFCLSID rclsid, LPCWSTR postfix)
955 CATID_IEnumGUIDImpl *This;
957 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
958 if (This) {
959 WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
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);
967 return (LPENUMGUID)This;