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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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,
32 struct class_categories {
37 static struct class_categories *COMCAT_PrepareClassCategories(
38 ULONG impl_count, CATID *impl_catids, ULONG req_count, 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 const* 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,
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);
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);
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,
94 LPENUMCATEGORYINFO *ppenumCatInfo)
96 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
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);
107 /**********************************************************************
108 * COMCAT_ICatInformation_GetCategoryDesc
110 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
111 LPCATINFORMATION iface,
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 };
123 TRACE("\n\tCATID:\t%s\n\tLCID:\t%lX\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) {
136 return E_OUTOFMEMORY;
139 /* Get the description, and make sure it's null terminated. */
140 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
143 CoTaskMemFree(*ppszDesc);
150 /**********************************************************************
151 * COMCAT_ICatInformation_EnumClassesOfCategories
153 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
154 LPCATINFORMATION iface,
159 LPENUMCLSID *ppenumCLSID)
161 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
162 struct class_categories *categories;
166 if (iface == NULL || ppenumCLSID == NULL ||
167 (cImplemented && rgcatidImpl == NULL) ||
168 (cRequired && rgcatidReq == NULL)) return E_POINTER;
170 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
171 cRequired, rgcatidReq);
172 if (categories == NULL) return E_OUTOFMEMORY;
173 *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
174 if (*ppenumCLSID == NULL) {
175 HeapFree(GetProcessHeap(), 0, categories);
176 return E_OUTOFMEMORY;
178 IEnumGUID_AddRef(*ppenumCLSID);
182 /**********************************************************************
183 * COMCAT_ICatInformation_IsClassOfCategories
185 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
186 LPCATINFORMATION iface,
193 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
194 WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
196 struct class_categories *categories;
199 if (WINE_TRACE_ON(ole)) {
201 TRACE("\n\tCLSID:\t%s\n\tImplemented %lu\n",debugstr_guid(rclsid),cImplemented);
202 for (count = 0; count < cImplemented; ++count)
203 TRACE("\t\t%s\n",debugstr_guid(&rgcatidImpl[count]));
204 TRACE("\tRequired %lu\n",cRequired);
205 for (count = 0; count < cRequired; ++count)
206 TRACE("\t\t%s\n",debugstr_guid(&rgcatidReq[count]));
209 if ((cImplemented && rgcatidImpl == NULL) ||
210 (cRequired && rgcatidReq == NULL)) return E_POINTER;
212 res = StringFromGUID2(rclsid, keyname + 6, 39);
213 if (FAILED(res)) return res;
215 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
216 cRequired, rgcatidReq);
217 if (categories == NULL) return E_OUTOFMEMORY;
219 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
220 if (res == ERROR_SUCCESS) {
221 res = COMCAT_IsClassOfCategories(key, categories);
223 } else res = S_FALSE;
225 HeapFree(GetProcessHeap(), 0, categories);
230 /**********************************************************************
231 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
233 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
234 LPCATINFORMATION iface,
236 LPENUMCATID *ppenumCATID)
238 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
239 WCHAR postfix[24] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
240 'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
241 'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
243 TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
245 if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
248 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
249 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
253 /**********************************************************************
254 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
256 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
257 LPCATINFORMATION iface,
259 LPENUMCATID *ppenumCATID)
261 /* ICOM_THIS_MULTI(ComCatMgrImpl, infVtbl, iface); */
262 WCHAR postfix[21] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
263 'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
264 'r', 'i', 'e', 's', 0 };
266 TRACE("\n\tCLSID:\t%s\n",debugstr_guid(rclsid));
268 if (iface == NULL || rclsid == NULL || ppenumCATID == NULL)
271 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
272 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
276 /**********************************************************************
277 * COMCAT_ICatInformation_Vtbl
279 ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
281 COMCAT_ICatInformation_QueryInterface,
282 COMCAT_ICatInformation_AddRef,
283 COMCAT_ICatInformation_Release,
284 COMCAT_ICatInformation_EnumCategories,
285 COMCAT_ICatInformation_GetCategoryDesc,
286 COMCAT_ICatInformation_EnumClassesOfCategories,
287 COMCAT_ICatInformation_IsClassOfCategories,
288 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
289 COMCAT_ICatInformation_EnumReqCategoriesOfClass
292 /**********************************************************************
293 * IEnumCATEGORYINFO implementation
295 * This implementation is not thread-safe. The manager itself is, but
296 * I can't imagine a valid use of an enumerator in several threads.
300 IEnumCATEGORYINFOVtbl *lpVtbl;
305 } IEnumCATEGORYINFOImpl;
307 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
309 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
313 if (This == NULL) return E_POINTER;
315 return InterlockedIncrement(&This->ref);
318 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
319 LPENUMCATEGORYINFO iface,
323 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
324 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
326 if (This == NULL || ppvObj == NULL) return E_POINTER;
328 if (IsEqualGUID(riid, &IID_IUnknown) ||
329 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
331 *ppvObj = (LPVOID)iface;
332 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
336 return E_NOINTERFACE;
339 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
341 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
346 if (This == NULL) return E_POINTER;
348 ref = InterlockedDecrement(&This->ref);
350 if (This->key) RegCloseKey(This->key);
351 HeapFree(GetProcessHeap(), 0, This);
357 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
358 LPENUMCATEGORYINFO iface,
363 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
368 if (This == NULL || rgelt == NULL) return E_POINTER;
370 if (This->key) while (fetched < celt) {
376 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
377 NULL, NULL, NULL, NULL);
378 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
379 ++(This->next_index);
381 res = CLSIDFromString(catid, &rgelt->catid);
382 if (FAILED(res)) continue;
384 res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
385 if (res != ERROR_SUCCESS) continue;
387 res = COMCAT_GetCategoryDesc(subkey, This->lcid,
388 rgelt->szDescription, 128);
390 if (FAILED(res)) continue;
392 rgelt->lcid = This->lcid;
397 if (pceltFetched) *pceltFetched = fetched;
398 return fetched == celt ? S_OK : S_FALSE;
401 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
402 LPENUMCATEGORYINFO iface,
405 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
409 if (This == NULL) return E_POINTER;
410 This->next_index += celt;
411 /* This should return S_FALSE when there aren't celt elems to skip. */
415 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
417 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
421 if (This == NULL) return E_POINTER;
422 This->next_index = 0;
426 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
427 LPENUMCATEGORYINFO iface,
428 IEnumCATEGORYINFO **ppenum)
430 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
431 WCHAR keyname[21] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
432 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
433 'r', 'i', 'e', 's', 0 };
434 IEnumCATEGORYINFOImpl *new_this;
438 if (This == NULL || ppenum == NULL) return E_POINTER;
440 new_this = (IEnumCATEGORYINFOImpl *) HeapAlloc(
441 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
442 if (new_this == NULL) return E_OUTOFMEMORY;
444 new_this->lpVtbl = This->lpVtbl;
446 new_this->lcid = This->lcid;
447 /* FIXME: could we more efficiently use DuplicateHandle? */
448 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
449 new_this->next_index = This->next_index;
451 *ppenum = (LPENUMCATEGORYINFO)new_this;
455 IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
457 COMCAT_IEnumCATEGORYINFO_QueryInterface,
458 COMCAT_IEnumCATEGORYINFO_AddRef,
459 COMCAT_IEnumCATEGORYINFO_Release,
460 COMCAT_IEnumCATEGORYINFO_Next,
461 COMCAT_IEnumCATEGORYINFO_Skip,
462 COMCAT_IEnumCATEGORYINFO_Reset,
463 COMCAT_IEnumCATEGORYINFO_Clone
466 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
468 IEnumCATEGORYINFOImpl *This;
470 This = (IEnumCATEGORYINFOImpl *) HeapAlloc(
471 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
473 WCHAR keyname[21] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
474 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
475 'r', 'i', 'e', 's', 0 };
477 This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
479 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
481 return (LPENUMCATEGORYINFO)This;
484 /**********************************************************************
485 * COMCAT_GetCategoryDesc
487 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
490 WCHAR fmt[4] = { '%', 'l', 'X', 0 };
493 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
495 if (pszDesc == NULL) return E_INVALIDARG;
497 /* FIXME: lcid comparisons are more complex than this! */
498 wsprintfW(valname, fmt, lcid);
499 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
500 if (res != ERROR_SUCCESS || type != REG_SZ) {
501 FIXME("Simplified lcid comparison\n");
502 return CAT_E_NODESCRIPTION;
504 pszDesc[size / sizeof(WCHAR)] = (WCHAR)0;
509 /**********************************************************************
510 * COMCAT_PrepareClassCategories
512 static struct class_categories *COMCAT_PrepareClassCategories(
513 ULONG impl_count, CATID *impl_catids, ULONG req_count, CATID *req_catids)
515 struct class_categories *categories;
518 categories = (struct class_categories *)HeapAlloc(
519 GetProcessHeap(), HEAP_ZERO_MEMORY,
520 sizeof(struct class_categories) +
521 ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
522 if (categories == NULL) return categories;
524 strings = (WCHAR *)(categories + 1);
525 categories->impl_strings = strings;
526 while (impl_count--) {
527 StringFromGUID2(impl_catids++, strings, 39);
532 categories->req_strings = strings;
533 while (req_count--) {
534 StringFromGUID2(req_catids++, strings, 39);
542 /**********************************************************************
543 * COMCAT_IsClassOfCategories
545 static HRESULT COMCAT_IsClassOfCategories(
547 struct class_categories const* categories)
549 WCHAR impl_keyname[23] = { 'I', 'm', 'p', 'l', 'e', 'm', 'e', 'n',
550 't', 'e', 'd', ' ', 'C', 'a', 't', 'e',
551 'g', 'o', 'r', 'i', 'e', 's', 0 };
552 WCHAR req_keyname[20] = { 'R', 'e', 'q', 'u', 'i', 'r', 'e', 'd',
553 ' ', 'C', 'a', 't', 'e', 'g', 'o', 'r',
560 /* Check that every given category is implemented by class. */
561 res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
562 if (res != ERROR_SUCCESS) return S_FALSE;
563 for (string = categories->impl_strings; *string; string += 39) {
565 res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
566 if (res != ERROR_SUCCESS) {
574 /* Check that all categories required by class are given. */
575 res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
576 if (res == ERROR_SUCCESS) {
577 for (index = 0; ; ++index) {
581 res = RegEnumKeyExW(subkey, index, keyname, &size,
582 NULL, NULL, NULL, NULL);
583 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
584 if (size != 38) continue; /* bogus catid in registry */
585 for (string = categories->req_strings; *string; string += 39)
586 if (!strcmpiW(string, keyname)) break;
598 /**********************************************************************
599 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
601 * This implementation is not thread-safe. The manager itself is, but
602 * I can't imagine a valid use of an enumerator in several threads.
606 IEnumGUIDVtbl *lpVtbl;
608 struct class_categories const *categories;
611 } CLSID_IEnumGUIDImpl;
613 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
615 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
618 if (This == NULL) return E_POINTER;
620 return InterlockedIncrement(&This->ref);
623 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
628 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
629 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
631 if (This == NULL || ppvObj == NULL) return E_POINTER;
633 if (IsEqualGUID(riid, &IID_IUnknown) ||
634 IsEqualGUID(riid, &IID_IEnumGUID))
636 *ppvObj = (LPVOID)iface;
637 COMCAT_CLSID_IEnumGUID_AddRef(iface);
641 return E_NOINTERFACE;
644 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
646 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
651 if (This == NULL) return E_POINTER;
653 ref = InterlockedDecrement(&This->ref);
655 if (This->key) RegCloseKey(This->key);
656 HeapFree(GetProcessHeap(), 0, (LPVOID)This->categories);
657 HeapFree(GetProcessHeap(), 0, This);
663 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
669 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
674 if (This == NULL || rgelt == NULL) return E_POINTER;
676 if (This->key) while (fetched < celt) {
682 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
683 NULL, NULL, NULL, NULL);
684 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
685 ++(This->next_index);
687 res = CLSIDFromString(clsid, rgelt);
688 if (FAILED(res)) continue;
690 res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
691 if (res != ERROR_SUCCESS) continue;
693 res = COMCAT_IsClassOfCategories(subkey, This->categories);
695 if (res != S_OK) continue;
701 if (pceltFetched) *pceltFetched = fetched;
702 return fetched == celt ? S_OK : S_FALSE;
705 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
709 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
713 if (This == NULL) return E_POINTER;
714 This->next_index += celt;
715 FIXME("Never returns S_FALSE\n");
719 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
721 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
725 if (This == NULL) return E_POINTER;
726 This->next_index = 0;
730 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
734 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
735 WCHAR keyname[6] = { 'C', 'L', 'S', 'I', 'D', 0 };
736 CLSID_IEnumGUIDImpl *new_this;
741 if (This == NULL || ppenum == NULL) return E_POINTER;
743 new_this = (CLSID_IEnumGUIDImpl *) HeapAlloc(
744 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
745 if (new_this == NULL) return E_OUTOFMEMORY;
747 new_this->lpVtbl = This->lpVtbl;
749 size = HeapSize(GetProcessHeap(), 0, (LPVOID)This->categories);
750 new_this->categories = (struct class_categories *)
751 HeapAlloc(GetProcessHeap(), 0, size);
752 if (new_this->categories == NULL) {
753 HeapFree(GetProcessHeap(), 0, new_this);
754 return E_OUTOFMEMORY;
756 memcpy((LPVOID)new_this->categories, This->categories, size);
757 /* FIXME: could we more efficiently use DuplicateHandle? */
758 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
759 new_this->next_index = This->next_index;
761 *ppenum = (LPENUMGUID)new_this;
765 IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
767 COMCAT_CLSID_IEnumGUID_QueryInterface,
768 COMCAT_CLSID_IEnumGUID_AddRef,
769 COMCAT_CLSID_IEnumGUID_Release,
770 COMCAT_CLSID_IEnumGUID_Next,
771 COMCAT_CLSID_IEnumGUID_Skip,
772 COMCAT_CLSID_IEnumGUID_Reset,
773 COMCAT_CLSID_IEnumGUID_Clone
776 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(
777 struct class_categories const* categories)
779 CLSID_IEnumGUIDImpl *This;
781 This = (CLSID_IEnumGUIDImpl *) HeapAlloc(
782 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
784 WCHAR keyname[6] = { '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.
801 IEnumGUIDVtbl *lpVtbl;
806 } CATID_IEnumGUIDImpl;
808 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
810 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
813 if (This == NULL) return E_POINTER;
815 return InterlockedIncrement(&This->ref);
818 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
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);
836 return E_NOINTERFACE;
839 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
841 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
846 if (This == NULL) return E_POINTER;
848 ref = InterlockedDecrement(&This->ref);
850 if (This->key) RegCloseKey(This->key);
851 HeapFree(GetProcessHeap(), 0, This);
857 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
863 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
868 if (This == NULL || rgelt == NULL) return E_POINTER;
870 if (This->key) while (fetched < celt) {
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;
887 if (pceltFetched) *pceltFetched = fetched;
888 return fetched == celt ? S_OK : S_FALSE;
891 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
895 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
899 if (This == NULL) return E_POINTER;
900 This->next_index += celt;
901 FIXME("Never returns S_FALSE\n");
905 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
907 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
911 if (This == NULL) return E_POINTER;
912 This->next_index = 0;
916 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
920 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
921 CATID_IEnumGUIDImpl *new_this;
925 if (This == NULL || ppenum == NULL) return E_POINTER;
927 new_this = (CATID_IEnumGUIDImpl *) HeapAlloc(
928 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
929 if (new_this == NULL) return E_OUTOFMEMORY;
931 new_this->lpVtbl = This->lpVtbl;
933 lstrcpyW(new_this->keyname, This->keyname);
934 /* FIXME: could we more efficiently use DuplicateHandle? */
935 RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
936 new_this->next_index = This->next_index;
938 *ppenum = (LPENUMGUID)new_this;
942 IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
944 COMCAT_CATID_IEnumGUID_QueryInterface,
945 COMCAT_CATID_IEnumGUID_AddRef,
946 COMCAT_CATID_IEnumGUID_Release,
947 COMCAT_CATID_IEnumGUID_Next,
948 COMCAT_CATID_IEnumGUID_Skip,
949 COMCAT_CATID_IEnumGUID_Reset,
950 COMCAT_CATID_IEnumGUID_Clone
953 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
954 REFCLSID rclsid, LPCWSTR postfix)
956 CATID_IEnumGUIDImpl *This;
958 This = (CATID_IEnumGUIDImpl *) HeapAlloc(
959 GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
961 WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
963 This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
964 memcpy(This->keyname, prefix, sizeof(prefix));
965 StringFromGUID2(rclsid, This->keyname + 6, 39);
966 lstrcpyW(This->keyname + 44, postfix);
967 RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
969 return (LPENUMGUID)This;