push b25f14be0dd3305724e1f1ef337bfe2a9ff59832
[wine/hacks.git] / dlls / comcat / information.c
blobd2d4df8adac2a055d89b4f5eb6e04da9d7f092a1
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 LSTATUS res;
377 HRESULT hr;
378 WCHAR catid[39];
379 DWORD cName = 39;
380 HKEY subkey;
382 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
383 NULL, NULL, NULL, NULL);
384 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
385 ++(This->next_index);
387 hr = CLSIDFromString(catid, &rgelt->catid);
388 if (FAILED(hr)) continue;
390 res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
391 if (res != ERROR_SUCCESS) continue;
393 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
394 rgelt->szDescription, 128);
395 RegCloseKey(subkey);
396 if (FAILED(hr)) continue;
398 rgelt->lcid = This->lcid;
399 ++fetched;
400 ++rgelt;
403 if (pceltFetched) *pceltFetched = fetched;
404 return fetched == celt ? S_OK : S_FALSE;
407 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
408 LPENUMCATEGORYINFO iface,
409 ULONG celt)
411 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
413 TRACE("\n");
415 if (This == NULL) return E_POINTER;
416 This->next_index += celt;
417 /* This should return S_FALSE when there aren't celt elems to skip. */
418 return S_OK;
421 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
423 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
425 TRACE("\n");
427 if (This == NULL) return E_POINTER;
428 This->next_index = 0;
429 return S_OK;
432 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
433 LPENUMCATEGORYINFO iface,
434 IEnumCATEGORYINFO **ppenum)
436 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
437 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
438 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
439 'r', 'i', 'e', 's', 0 };
440 IEnumCATEGORYINFOImpl *new_this;
442 TRACE("\n");
444 if (This == NULL || ppenum == NULL) return E_POINTER;
446 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
447 if (new_this == NULL) return E_OUTOFMEMORY;
449 new_this->lpVtbl = This->lpVtbl;
450 new_this->ref = 1;
451 new_this->lcid = This->lcid;
452 /* FIXME: could we more efficiently use DuplicateHandle? */
453 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
454 new_this->next_index = This->next_index;
456 *ppenum = (LPENUMCATEGORYINFO)new_this;
457 return S_OK;
460 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
462 COMCAT_IEnumCATEGORYINFO_QueryInterface,
463 COMCAT_IEnumCATEGORYINFO_AddRef,
464 COMCAT_IEnumCATEGORYINFO_Release,
465 COMCAT_IEnumCATEGORYINFO_Next,
466 COMCAT_IEnumCATEGORYINFO_Skip,
467 COMCAT_IEnumCATEGORYINFO_Reset,
468 COMCAT_IEnumCATEGORYINFO_Clone
471 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
473 IEnumCATEGORYINFOImpl *This;
475 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
476 if (This) {
477 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
478 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
479 'r', 'i', 'e', 's', 0 };
481 This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
482 This->lcid = lcid;
483 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
485 return (LPENUMCATEGORYINFO)This;
488 /**********************************************************************
489 * COMCAT_GetCategoryDesc
491 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
492 ULONG buf_wchars)
494 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
495 WCHAR valname[5];
496 HRESULT res;
497 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
499 if (pszDesc == NULL) return E_INVALIDARG;
501 /* FIXME: lcid comparisons are more complex than this! */
502 wsprintfW(valname, fmt, lcid);
503 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
504 if (res != ERROR_SUCCESS || type != REG_SZ) {
505 FIXME("Simplified lcid comparison\n");
506 return CAT_E_NODESCRIPTION;
508 pszDesc[size / sizeof(WCHAR)] = (WCHAR)0;
510 return S_OK;
513 /**********************************************************************
514 * COMCAT_PrepareClassCategories
516 static struct class_categories *COMCAT_PrepareClassCategories(
517 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
519 struct class_categories *categories;
520 WCHAR *strings;
522 categories = HeapAlloc(
523 GetProcessHeap(), HEAP_ZERO_MEMORY,
524 sizeof(struct class_categories) +
525 ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
526 if (categories == NULL) return categories;
528 strings = (WCHAR *)(categories + 1);
529 categories->impl_strings = strings;
530 while (impl_count--) {
531 StringFromGUID2(impl_catids++, strings, 39);
532 strings += 39;
534 *strings++ = 0;
536 categories->req_strings = strings;
537 while (req_count--) {
538 StringFromGUID2(req_catids++, strings, 39);
539 strings += 39;
541 *strings++ = 0;
543 return categories;
546 /**********************************************************************
547 * COMCAT_IsClassOfCategories
549 static HRESULT COMCAT_IsClassOfCategories(
550 HKEY key,
551 struct class_categories const* categories)
553 static const WCHAR impl_keyname[] = { 'I', 'm', 'p', 'l', 'e', 'm', 'e', 'n',
554 't', 'e', 'd', ' ', 'C', 'a', 't', 'e',
555 'g', 'o', 'r', 'i', 'e', 's', 0 };
556 static const WCHAR req_keyname[] = { 'R', 'e', 'q', 'u', 'i', 'r', 'e', 'd',
557 ' ', 'C', 'a', 't', 'e', 'g', 'o', 'r',
558 'i', 'e', 's', 0 };
559 HKEY subkey;
560 HRESULT res;
561 DWORD index;
562 LPCWSTR string;
564 /* Check that every given category is implemented by class. */
565 res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
566 if (res != ERROR_SUCCESS) return S_FALSE;
567 for (string = categories->impl_strings; *string; string += 39) {
568 HKEY catkey;
569 res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
570 if (res != ERROR_SUCCESS) {
571 RegCloseKey(subkey);
572 return S_FALSE;
574 RegCloseKey(catkey);
576 RegCloseKey(subkey);
578 /* Check that all categories required by class are given. */
579 res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
580 if (res == ERROR_SUCCESS) {
581 for (index = 0; ; ++index) {
582 WCHAR keyname[39];
583 DWORD size = 39;
585 res = RegEnumKeyExW(subkey, index, keyname, &size,
586 NULL, NULL, NULL, NULL);
587 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
588 if (size != 38) continue; /* bogus catid in registry */
589 for (string = categories->req_strings; *string; string += 39)
590 if (!strcmpiW(string, keyname)) break;
591 if (!*string) {
592 RegCloseKey(subkey);
593 return S_FALSE;
596 RegCloseKey(subkey);
599 return S_OK;
602 /**********************************************************************
603 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
605 * This implementation is not thread-safe. The manager itself is, but
606 * I can't imagine a valid use of an enumerator in several threads.
608 typedef struct
610 const IEnumGUIDVtbl *lpVtbl;
611 LONG ref;
612 struct class_categories *categories;
613 HKEY key;
614 DWORD next_index;
615 } CLSID_IEnumGUIDImpl;
617 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
619 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
620 TRACE("\n");
622 if (This == NULL) return E_POINTER;
624 return InterlockedIncrement(&This->ref);
627 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
628 LPENUMGUID iface,
629 REFIID riid,
630 LPVOID *ppvObj)
632 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
633 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
635 if (This == NULL || ppvObj == NULL) return E_POINTER;
637 if (IsEqualGUID(riid, &IID_IUnknown) ||
638 IsEqualGUID(riid, &IID_IEnumGUID))
640 *ppvObj = (LPVOID)iface;
641 COMCAT_CLSID_IEnumGUID_AddRef(iface);
642 return S_OK;
645 return E_NOINTERFACE;
648 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
650 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
651 ULONG ref;
653 TRACE("\n");
655 if (This == NULL) return E_POINTER;
657 ref = InterlockedDecrement(&This->ref);
658 if (ref == 0) {
659 if (This->key) RegCloseKey(This->key);
660 HeapFree(GetProcessHeap(), 0, (LPVOID)This->categories);
661 HeapFree(GetProcessHeap(), 0, This);
662 return 0;
664 return ref;
667 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
668 LPENUMGUID iface,
669 ULONG celt,
670 GUID *rgelt,
671 ULONG *pceltFetched)
673 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
674 ULONG fetched = 0;
676 TRACE("\n");
678 if (This == NULL || rgelt == NULL) return E_POINTER;
680 if (This->key) while (fetched < celt) {
681 LSTATUS res;
682 HRESULT hr;
683 WCHAR clsid[39];
684 DWORD cName = 39;
685 HKEY subkey;
687 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
688 NULL, NULL, NULL, NULL);
689 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
690 ++(This->next_index);
692 hr = CLSIDFromString(clsid, rgelt);
693 if (FAILED(hr)) continue;
695 res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
696 if (res != ERROR_SUCCESS) continue;
698 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
699 RegCloseKey(subkey);
700 if (hr != S_OK) continue;
702 ++fetched;
703 ++rgelt;
706 if (pceltFetched) *pceltFetched = fetched;
707 return fetched == celt ? S_OK : S_FALSE;
710 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
711 LPENUMGUID iface,
712 ULONG celt)
714 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
716 TRACE("\n");
718 if (This == NULL) return E_POINTER;
719 This->next_index += celt;
720 FIXME("Never returns S_FALSE\n");
721 return S_OK;
724 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
726 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
728 TRACE("\n");
730 if (This == NULL) return E_POINTER;
731 This->next_index = 0;
732 return S_OK;
735 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
736 LPENUMGUID iface,
737 IEnumGUID **ppenum)
739 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
740 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
741 CLSID_IEnumGUIDImpl *new_this;
742 DWORD size;
744 TRACE("\n");
746 if (This == NULL || ppenum == NULL) return E_POINTER;
748 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
749 if (new_this == NULL) return E_OUTOFMEMORY;
751 new_this->lpVtbl = This->lpVtbl;
752 new_this->ref = 1;
753 size = HeapSize(GetProcessHeap(), 0, (LPVOID)This->categories);
754 new_this->categories =
755 HeapAlloc(GetProcessHeap(), 0, size);
756 if (new_this->categories == NULL) {
757 HeapFree(GetProcessHeap(), 0, new_this);
758 return E_OUTOFMEMORY;
760 memcpy((LPVOID)new_this->categories, This->categories, size);
761 /* FIXME: could we more efficiently use DuplicateHandle? */
762 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
763 new_this->next_index = This->next_index;
765 *ppenum = (LPENUMGUID)new_this;
766 return S_OK;
769 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
771 COMCAT_CLSID_IEnumGUID_QueryInterface,
772 COMCAT_CLSID_IEnumGUID_AddRef,
773 COMCAT_CLSID_IEnumGUID_Release,
774 COMCAT_CLSID_IEnumGUID_Next,
775 COMCAT_CLSID_IEnumGUID_Skip,
776 COMCAT_CLSID_IEnumGUID_Reset,
777 COMCAT_CLSID_IEnumGUID_Clone
780 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
782 CLSID_IEnumGUIDImpl *This;
784 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
785 if (This) {
786 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
788 This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
789 This->categories = categories;
790 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
792 return (LPENUMGUID)This;
795 /**********************************************************************
796 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
798 * This implementation is not thread-safe. The manager itself is, but
799 * I can't imagine a valid use of an enumerator in several threads.
801 typedef struct
803 const IEnumGUIDVtbl *lpVtbl;
804 LONG ref;
805 WCHAR keyname[68];
806 HKEY key;
807 DWORD next_index;
808 } CATID_IEnumGUIDImpl;
810 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
812 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
813 TRACE("\n");
815 if (This == NULL) return E_POINTER;
817 return InterlockedIncrement(&This->ref);
820 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
821 LPENUMGUID iface,
822 REFIID riid,
823 LPVOID *ppvObj)
825 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
826 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
828 if (This == NULL || ppvObj == NULL) return E_POINTER;
830 if (IsEqualGUID(riid, &IID_IUnknown) ||
831 IsEqualGUID(riid, &IID_IEnumGUID))
833 *ppvObj = (LPVOID)iface;
834 COMCAT_CATID_IEnumGUID_AddRef(iface);
835 return S_OK;
838 return E_NOINTERFACE;
841 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
843 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
844 ULONG ref;
846 TRACE("\n");
848 if (This == NULL) return E_POINTER;
850 ref = InterlockedDecrement(&This->ref);
851 if (ref == 0) {
852 if (This->key) RegCloseKey(This->key);
853 HeapFree(GetProcessHeap(), 0, This);
854 return 0;
856 return ref;
859 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
860 LPENUMGUID iface,
861 ULONG celt,
862 GUID *rgelt,
863 ULONG *pceltFetched)
865 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
866 ULONG fetched = 0;
868 TRACE("\n");
870 if (This == NULL || rgelt == NULL) return E_POINTER;
872 if (This->key) while (fetched < celt) {
873 LSTATUS res;
874 HRESULT hr;
875 WCHAR catid[39];
876 DWORD cName = 39;
878 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
879 NULL, NULL, NULL, NULL);
880 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
881 ++(This->next_index);
883 hr = CLSIDFromString(catid, rgelt);
884 if (FAILED(hr)) continue;
886 ++fetched;
887 ++rgelt;
890 if (pceltFetched) *pceltFetched = fetched;
891 return fetched == celt ? S_OK : S_FALSE;
894 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
895 LPENUMGUID iface,
896 ULONG celt)
898 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
900 TRACE("\n");
902 if (This == NULL) return E_POINTER;
903 This->next_index += celt;
904 FIXME("Never returns S_FALSE\n");
905 return S_OK;
908 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
910 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
912 TRACE("\n");
914 if (This == NULL) return E_POINTER;
915 This->next_index = 0;
916 return S_OK;
919 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
920 LPENUMGUID iface,
921 IEnumGUID **ppenum)
923 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
924 CATID_IEnumGUIDImpl *new_this;
926 TRACE("\n");
928 if (This == NULL || ppenum == NULL) return E_POINTER;
930 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
931 if (new_this == NULL) return E_OUTOFMEMORY;
933 new_this->lpVtbl = This->lpVtbl;
934 new_this->ref = 1;
935 lstrcpyW(new_this->keyname, This->keyname);
936 /* FIXME: could we more efficiently use DuplicateHandle? */
937 RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
938 new_this->next_index = This->next_index;
940 *ppenum = (LPENUMGUID)new_this;
941 return S_OK;
944 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
946 COMCAT_CATID_IEnumGUID_QueryInterface,
947 COMCAT_CATID_IEnumGUID_AddRef,
948 COMCAT_CATID_IEnumGUID_Release,
949 COMCAT_CATID_IEnumGUID_Next,
950 COMCAT_CATID_IEnumGUID_Skip,
951 COMCAT_CATID_IEnumGUID_Reset,
952 COMCAT_CATID_IEnumGUID_Clone
955 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
956 REFCLSID rclsid, LPCWSTR postfix)
958 CATID_IEnumGUIDImpl *This;
960 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
961 if (This) {
962 WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
964 This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
965 memcpy(This->keyname, prefix, sizeof(prefix));
966 StringFromGUID2(rclsid, This->keyname + 6, 39);
967 lstrcpyW(This->keyname + 44, postfix);
968 RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
970 return (LPENUMGUID)This;