evr/mixer: Use hex format for message type.
[wine.git] / dlls / ole32 / comcat.c
blob0b97b00d9087fbdbd30956a5073d6e6ca3db52d2
1 /*
2 * Comcat implementation
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 <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
32 #include "ole2.h"
33 #include "comcat.h"
34 #include "compobj_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl;
41 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl;
43 typedef struct
45 ICatRegister ICatRegister_iface;
46 ICatInformation ICatInformation_iface;
47 } ComCatMgrImpl;
49 /* static ComCatMgr instance */
50 static ComCatMgrImpl COMCAT_ComCatMgr =
52 { &COMCAT_ICatRegister_Vtbl },
53 { &COMCAT_ICatInformation_Vtbl }
56 struct class_categories
58 ULONG size; /* total length, including structure itself */
59 ULONG impl_offset;
60 ULONG req_offset;
63 static HRESULT EnumCATEGORYINFO_Construct(LCID lcid, IEnumCATEGORYINFO **ret);
64 static HRESULT CLSIDEnumGUID_Construct(struct class_categories *class_categories, IEnumCLSID **ret);
65 static HRESULT CATIDEnumGUID_Construct(REFCLSID rclsid, LPCWSTR impl_req, IEnumCATID **ret);
67 /**********************************************************************
68 * File-scope string constants
70 static const WCHAR comcat_keyname[] = L"Component Categories";
71 static const WCHAR impl_keyname[] = L"Implemented Categories";
72 static const WCHAR req_keyname[] = L"Required Categories";
74 /**********************************************************************
75 * COMCAT_RegisterClassCategories
77 static HRESULT COMCAT_RegisterClassCategories(
78 REFCLSID rclsid,
79 LPCWSTR type,
80 ULONG cCategories,
81 const CATID *rgcatid)
83 WCHAR keyname[CHARS_IN_GUID];
84 HRESULT res;
85 HKEY clsid_key, class_key, type_key;
87 if (cCategories && rgcatid == NULL) return E_POINTER;
89 /* Format the class key name. */
90 res = StringFromGUID2(rclsid, keyname, CHARS_IN_GUID);
91 if (FAILED(res)) return res;
93 /* Create (or open) the CLSID key. */
94 res = create_classes_key(HKEY_CLASSES_ROOT, L"CLSID", KEY_READ|KEY_WRITE, &clsid_key);
95 if (res != ERROR_SUCCESS) return E_FAIL;
97 /* Create (or open) the class key. */
98 res = create_classes_key(clsid_key, keyname, KEY_READ|KEY_WRITE, &class_key);
99 if (res == ERROR_SUCCESS) {
100 /* Create (or open) the category type key. */
101 res = create_classes_key(class_key, type, KEY_READ|KEY_WRITE, &type_key);
102 if (res == ERROR_SUCCESS) {
103 for (; cCategories; --cCategories, ++rgcatid) {
104 HKEY key;
106 /* Format the category key name. */
107 res = StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID);
108 if (FAILED(res)) continue;
110 /* Do the register. */
111 res = create_classes_key(type_key, keyname, KEY_READ|KEY_WRITE, &key);
112 if (res == ERROR_SUCCESS) RegCloseKey(key);
114 res = S_OK;
115 } else res = E_FAIL;
116 RegCloseKey(class_key);
117 } else res = E_FAIL;
118 RegCloseKey(clsid_key);
120 return res;
123 /**********************************************************************
124 * COMCAT_UnRegisterClassCategories
126 static HRESULT COMCAT_UnRegisterClassCategories(
127 REFCLSID rclsid,
128 LPCWSTR type,
129 ULONG cCategories,
130 const CATID *rgcatid)
132 WCHAR keyname[68] = L"CLSID\\";
133 HRESULT res;
134 HKEY type_key;
136 if (cCategories && rgcatid == NULL) return E_POINTER;
138 /* Format the class category type key name. */
139 res = StringFromGUID2(rclsid, keyname + 6, CHARS_IN_GUID);
140 if (FAILED(res)) return res;
141 keyname[44] = '\\';
142 lstrcpyW(keyname + 45, type);
144 /* Open the class category type key. */
145 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ|KEY_WRITE, &type_key);
146 if (res != ERROR_SUCCESS) return E_FAIL;
148 for (; cCategories; --cCategories, ++rgcatid) {
149 /* Format the category key name. */
150 res = StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID);
151 if (FAILED(res)) continue;
153 /* Do the unregister. */
154 RegDeleteKeyW(type_key, keyname);
156 RegCloseKey(type_key);
158 return S_OK;
161 /**********************************************************************
162 * COMCAT_GetCategoryDesc
164 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
165 ULONG buf_wchars)
167 WCHAR valname[5];
168 HRESULT res;
169 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
171 if (pszDesc == NULL) return E_INVALIDARG;
173 /* FIXME: lcid comparisons are more complex than this! */
174 wsprintfW(valname, L"%lX", lcid);
175 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
176 if (res != ERROR_SUCCESS || type != REG_SZ) {
177 FIXME("Simplified lcid comparison\n");
178 return CAT_E_NODESCRIPTION;
180 pszDesc[size / sizeof(WCHAR)] = 0;
182 return S_OK;
185 /**********************************************************************
186 * COMCAT_PrepareClassCategories
188 static struct class_categories *COMCAT_PrepareClassCategories(
189 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
191 struct class_categories *categories;
192 WCHAR *strings;
193 ULONG size;
195 size = sizeof(struct class_categories) + ((impl_count + req_count)*CHARS_IN_GUID + 2)*sizeof(WCHAR);
196 categories = HeapAlloc(GetProcessHeap(), 0, size);
197 if (categories == NULL) return categories;
199 categories->size = size;
200 categories->impl_offset = sizeof(struct class_categories);
201 categories->req_offset = categories->impl_offset + (impl_count*CHARS_IN_GUID + 1)*sizeof(WCHAR);
203 strings = (WCHAR *)(categories + 1);
204 while (impl_count--) {
205 StringFromGUID2(impl_catids++, strings, CHARS_IN_GUID);
206 strings += CHARS_IN_GUID;
208 *strings++ = 0;
210 while (req_count--) {
211 StringFromGUID2(req_catids++, strings, CHARS_IN_GUID);
212 strings += CHARS_IN_GUID;
214 *strings++ = 0;
216 return categories;
219 /**********************************************************************
220 * COMCAT_IsClassOfCategories
222 static HRESULT COMCAT_IsClassOfCategories(
223 HKEY key,
224 struct class_categories const* categories)
226 const WCHAR *impl_strings, *req_strings;
227 HKEY subkey;
228 HRESULT res;
229 DWORD index;
230 LPCWSTR string;
232 impl_strings = (WCHAR*)((BYTE*)categories + categories->impl_offset);
233 req_strings = (WCHAR*)((BYTE*)categories + categories->req_offset);
235 /* Check that every given category is implemented by class. */
236 if (*impl_strings) {
237 res = open_classes_key(key, impl_keyname, KEY_READ, &subkey);
238 if (res != ERROR_SUCCESS) return S_FALSE;
239 for (string = impl_strings; *string; string += CHARS_IN_GUID) {
240 HKEY catkey;
241 res = open_classes_key(subkey, string, READ_CONTROL, &catkey);
242 if (res != ERROR_SUCCESS) {
243 RegCloseKey(subkey);
244 return S_FALSE;
246 RegCloseKey(catkey);
248 RegCloseKey(subkey);
251 /* Check that all categories required by class are given. */
252 res = open_classes_key(key, req_keyname, KEY_READ, &subkey);
253 if (res == ERROR_SUCCESS) {
254 for (index = 0; ; ++index) {
255 WCHAR keyname[CHARS_IN_GUID];
256 DWORD size = CHARS_IN_GUID;
258 res = RegEnumKeyExW(subkey, index, keyname, &size,
259 NULL, NULL, NULL, NULL);
260 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
261 if (size != CHARS_IN_GUID-1) continue; /* bogus catid in registry */
262 for (string = req_strings; *string; string += CHARS_IN_GUID)
263 if (!wcsicmp(string, keyname)) break;
264 if (!*string) {
265 RegCloseKey(subkey);
266 return S_FALSE;
269 RegCloseKey(subkey);
272 return S_OK;
275 /**********************************************************************
276 * COMCAT_ICatRegister_QueryInterface
278 static HRESULT WINAPI COMCAT_ICatRegister_QueryInterface(
279 LPCATREGISTER iface,
280 REFIID riid,
281 LPVOID *ppvObj)
283 TRACE("%s\n",debugstr_guid(riid));
285 if (ppvObj == NULL) return E_POINTER;
287 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ICatRegister)) {
288 *ppvObj = iface;
289 ICatRegister_AddRef(iface);
290 return S_OK;
293 if (IsEqualGUID(riid, &IID_ICatInformation)) {
294 *ppvObj = &COMCAT_ComCatMgr.ICatInformation_iface;
295 ICatRegister_AddRef(iface);
296 return S_OK;
299 return E_NOINTERFACE;
302 /**********************************************************************
303 * COMCAT_ICatRegister_AddRef
305 static ULONG WINAPI COMCAT_ICatRegister_AddRef(LPCATREGISTER iface)
307 return 2; /* non-heap based object */
310 /**********************************************************************
311 * COMCAT_ICatRegister_Release
313 static ULONG WINAPI COMCAT_ICatRegister_Release(LPCATREGISTER iface)
315 return 1; /* non-heap based object */
318 /**********************************************************************
319 * COMCAT_ICatRegister_RegisterCategories
321 static HRESULT WINAPI COMCAT_ICatRegister_RegisterCategories(
322 LPCATREGISTER iface,
323 ULONG cCategories,
324 CATEGORYINFO *rgci)
326 HKEY comcat_key;
327 HRESULT res;
329 TRACE("\n");
331 if (cCategories && rgci == NULL)
332 return E_POINTER;
334 /* Create (or open) the component categories key. */
335 res = create_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ|KEY_WRITE, &comcat_key);
336 if (res != ERROR_SUCCESS) return E_FAIL;
338 for (; cCategories; --cCategories, ++rgci)
340 WCHAR keyname[CHARS_IN_GUID];
341 WCHAR valname[9];
342 HKEY cat_key;
344 /* Create (or open) the key for this category. */
345 if (!StringFromGUID2(&rgci->catid, keyname, CHARS_IN_GUID)) continue;
346 res = create_classes_key(comcat_key, keyname, KEY_READ|KEY_WRITE, &cat_key);
347 if (res != ERROR_SUCCESS) continue;
349 /* Set the value for this locale's description. */
350 wsprintfW(valname, L"%lX", rgci->lcid);
351 RegSetValueExW(cat_key, valname, 0, REG_SZ, (const BYTE*)rgci->szDescription,
352 (lstrlenW(rgci->szDescription) + 1) * sizeof(WCHAR));
354 RegCloseKey(cat_key);
357 RegCloseKey(comcat_key);
358 return S_OK;
361 /**********************************************************************
362 * COMCAT_ICatRegister_UnRegisterCategories
364 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterCategories(
365 LPCATREGISTER iface,
366 ULONG cCategories,
367 CATID *rgcatid)
369 HKEY comcat_key;
370 HRESULT res;
372 TRACE("\n");
374 if (cCategories && rgcatid == NULL)
375 return E_POINTER;
377 /* Open the component categories key. */
378 res = open_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ|KEY_WRITE, &comcat_key);
379 if (res != ERROR_SUCCESS) return E_FAIL;
381 for (; cCategories; --cCategories, ++rgcatid) {
382 WCHAR keyname[CHARS_IN_GUID];
384 /* Delete the key for this category. */
385 if (!StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID)) continue;
386 RegDeleteKeyW(comcat_key, keyname);
389 RegCloseKey(comcat_key);
390 return S_OK;
393 /**********************************************************************
394 * COMCAT_ICatRegister_RegisterClassImplCategories
396 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassImplCategories(
397 LPCATREGISTER iface,
398 REFCLSID rclsid,
399 ULONG cCategories,
400 CATID *rgcatid)
402 TRACE("\n");
404 return COMCAT_RegisterClassCategories(
405 rclsid, impl_keyname, cCategories, rgcatid);
408 /**********************************************************************
409 * COMCAT_ICatRegister_UnRegisterClassImplCategories
411 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassImplCategories(
412 LPCATREGISTER iface,
413 REFCLSID rclsid,
414 ULONG cCategories,
415 CATID *rgcatid)
417 TRACE("\n");
419 return COMCAT_UnRegisterClassCategories(
420 rclsid, impl_keyname, cCategories, rgcatid);
423 /**********************************************************************
424 * COMCAT_ICatRegister_RegisterClassReqCategories
426 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassReqCategories(
427 LPCATREGISTER iface,
428 REFCLSID rclsid,
429 ULONG cCategories,
430 CATID *rgcatid)
432 TRACE("\n");
434 return COMCAT_RegisterClassCategories(
435 rclsid, req_keyname, cCategories, rgcatid);
438 /**********************************************************************
439 * COMCAT_ICatRegister_UnRegisterClassReqCategories
441 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassReqCategories(
442 LPCATREGISTER iface,
443 REFCLSID rclsid,
444 ULONG cCategories,
445 CATID *rgcatid)
447 TRACE("\n");
449 return COMCAT_UnRegisterClassCategories(
450 rclsid, req_keyname, cCategories, rgcatid);
453 /**********************************************************************
454 * COMCAT_ICatInformation_QueryInterface
456 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
457 LPCATINFORMATION iface,
458 REFIID riid,
459 LPVOID *ppvObj)
461 return ICatRegister_QueryInterface(&COMCAT_ComCatMgr.ICatRegister_iface, riid, ppvObj);
464 /**********************************************************************
465 * COMCAT_ICatInformation_AddRef
467 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
469 return ICatRegister_AddRef(&COMCAT_ComCatMgr.ICatRegister_iface);
472 /**********************************************************************
473 * COMCAT_ICatInformation_Release
475 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
477 return ICatRegister_Release(&COMCAT_ComCatMgr.ICatRegister_iface);
480 /**********************************************************************
481 * COMCAT_ICatInformation_EnumCategories
483 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
484 LPCATINFORMATION iface,
485 LCID lcid,
486 IEnumCATEGORYINFO **ppenumCatInfo)
488 TRACE("\n");
490 if (ppenumCatInfo == NULL) return E_POINTER;
492 return EnumCATEGORYINFO_Construct(lcid, ppenumCatInfo);
495 /**********************************************************************
496 * COMCAT_ICatInformation_GetCategoryDesc
498 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
499 LPCATINFORMATION iface,
500 REFCATID rcatid,
501 LCID lcid,
502 PWCHAR *ppszDesc)
504 WCHAR keyname[60] = L"Component Categories\\";
505 HKEY key;
506 HRESULT res;
508 TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
510 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
512 /* Open the key for this category. */
513 if (!StringFromGUID2(rcatid, keyname + 21, CHARS_IN_GUID)) return E_FAIL;
514 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
515 if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
517 /* Allocate a sensible amount of memory for the description. */
518 *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
519 if (*ppszDesc == NULL) {
520 RegCloseKey(key);
521 return E_OUTOFMEMORY;
524 /* Get the description, and make sure it's null terminated. */
525 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
526 RegCloseKey(key);
527 if (FAILED(res)) {
528 CoTaskMemFree(*ppszDesc);
529 return res;
532 return S_OK;
535 /**********************************************************************
536 * COMCAT_ICatInformation_EnumClassesOfCategories
538 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
539 LPCATINFORMATION iface,
540 ULONG cImplemented,
541 CATID *rgcatidImpl,
542 ULONG cRequired,
543 CATID *rgcatidReq,
544 LPENUMCLSID *ppenumCLSID)
546 struct class_categories *categories;
547 HRESULT hr;
549 TRACE("\n");
551 if (cImplemented == (ULONG)-1)
552 cImplemented = 0;
553 if (cRequired == (ULONG)-1)
554 cRequired = 0;
556 if (ppenumCLSID == NULL ||
557 (cImplemented && rgcatidImpl == NULL) ||
558 (cRequired && rgcatidReq == NULL)) return E_POINTER;
560 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
561 cRequired, rgcatidReq);
562 if (categories == NULL) return E_OUTOFMEMORY;
564 hr = CLSIDEnumGUID_Construct(categories, ppenumCLSID);
565 if (FAILED(hr))
567 HeapFree(GetProcessHeap(), 0, categories);
568 return hr;
571 return hr;
574 /**********************************************************************
575 * COMCAT_ICatInformation_IsClassOfCategories
577 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
578 LPCATINFORMATION iface,
579 REFCLSID rclsid,
580 ULONG cImplemented,
581 CATID *rgcatidImpl,
582 ULONG cRequired,
583 CATID *rgcatidReq)
585 WCHAR keyname[45] = L"CLSID\\";
586 HRESULT res;
587 struct class_categories *categories;
588 HKEY key;
590 if (TRACE_ON(ole)) {
591 ULONG count;
592 TRACE("CLSID: %s Implemented %u\n",debugstr_guid(rclsid),cImplemented);
593 for (count = 0; count < cImplemented; ++count)
594 TRACE(" %s\n",debugstr_guid(&rgcatidImpl[count]));
595 TRACE("Required %u\n",cRequired);
596 for (count = 0; count < cRequired; ++count)
597 TRACE(" %s\n",debugstr_guid(&rgcatidReq[count]));
600 if ((cImplemented && rgcatidImpl == NULL) ||
601 (cRequired && rgcatidReq == NULL)) return E_POINTER;
603 res = StringFromGUID2(rclsid, keyname + 6, CHARS_IN_GUID);
604 if (FAILED(res)) return res;
606 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
607 cRequired, rgcatidReq);
608 if (categories == NULL) return E_OUTOFMEMORY;
610 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
611 if (res == ERROR_SUCCESS) {
612 res = COMCAT_IsClassOfCategories(key, categories);
613 RegCloseKey(key);
614 } else res = S_FALSE;
616 HeapFree(GetProcessHeap(), 0, categories);
618 return res;
621 /**********************************************************************
622 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
624 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
625 LPCATINFORMATION iface,
626 REFCLSID rclsid,
627 LPENUMCATID *ppenumCATID)
629 TRACE("%s\n",debugstr_guid(rclsid));
631 if (rclsid == NULL || ppenumCATID == NULL)
632 return E_POINTER;
634 return CATIDEnumGUID_Construct(rclsid, L"\\Implemented Categories", ppenumCATID);
637 /**********************************************************************
638 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
640 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
641 LPCATINFORMATION iface,
642 REFCLSID rclsid,
643 LPENUMCATID *ppenumCATID)
645 TRACE("%s\n",debugstr_guid(rclsid));
647 if (rclsid == NULL || ppenumCATID == NULL)
648 return E_POINTER;
650 return CATIDEnumGUID_Construct(rclsid, L"\\Required Categories", ppenumCATID);
653 /**********************************************************************
654 * COMCAT_ICatRegister_Vtbl
656 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
658 COMCAT_ICatRegister_QueryInterface,
659 COMCAT_ICatRegister_AddRef,
660 COMCAT_ICatRegister_Release,
661 COMCAT_ICatRegister_RegisterCategories,
662 COMCAT_ICatRegister_UnRegisterCategories,
663 COMCAT_ICatRegister_RegisterClassImplCategories,
664 COMCAT_ICatRegister_UnRegisterClassImplCategories,
665 COMCAT_ICatRegister_RegisterClassReqCategories,
666 COMCAT_ICatRegister_UnRegisterClassReqCategories
670 /**********************************************************************
671 * COMCAT_ICatInformation_Vtbl
673 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
675 COMCAT_ICatInformation_QueryInterface,
676 COMCAT_ICatInformation_AddRef,
677 COMCAT_ICatInformation_Release,
678 COMCAT_ICatInformation_EnumCategories,
679 COMCAT_ICatInformation_GetCategoryDesc,
680 COMCAT_ICatInformation_EnumClassesOfCategories,
681 COMCAT_ICatInformation_IsClassOfCategories,
682 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
683 COMCAT_ICatInformation_EnumReqCategoriesOfClass
686 HRESULT WINAPI ComCat_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppvObj)
688 HRESULT res;
689 TRACE("%s\n",debugstr_guid(riid));
691 if (ppvObj == NULL) return E_POINTER;
693 /* Don't support aggregation (Windows doesn't) */
694 if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
696 res = ICatRegister_QueryInterface(&COMCAT_ComCatMgr.ICatRegister_iface, riid, ppvObj);
697 if (SUCCEEDED(res)) {
698 return res;
701 return CLASS_E_CLASSNOTAVAILABLE;
704 /**********************************************************************
705 * IEnumCATEGORYINFO implementation
707 * This implementation is not thread-safe. The manager itself is, but
708 * I can't imagine a valid use of an enumerator in several threads.
710 typedef struct
712 IEnumCATEGORYINFO IEnumCATEGORYINFO_iface;
713 LONG ref;
714 LCID lcid;
715 HKEY key;
716 DWORD next_index;
717 } IEnumCATEGORYINFOImpl;
719 static inline IEnumCATEGORYINFOImpl *impl_from_IEnumCATEGORYINFO(IEnumCATEGORYINFO *iface)
721 return CONTAINING_RECORD(iface, IEnumCATEGORYINFOImpl, IEnumCATEGORYINFO_iface);
724 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(IEnumCATEGORYINFO *iface)
726 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
728 TRACE("\n");
730 return InterlockedIncrement(&This->ref);
733 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
734 IEnumCATEGORYINFO *iface,
735 REFIID riid,
736 LPVOID *ppvObj)
738 TRACE("%s\n",debugstr_guid(riid));
740 if (ppvObj == NULL) return E_POINTER;
742 if (IsEqualGUID(riid, &IID_IUnknown) ||
743 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
745 *ppvObj = iface;
746 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
747 return S_OK;
750 return E_NOINTERFACE;
753 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(IEnumCATEGORYINFO *iface)
755 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
756 ULONG ref;
758 TRACE("\n");
760 ref = InterlockedDecrement(&This->ref);
761 if (ref == 0) {
762 if (This->key) RegCloseKey(This->key);
763 HeapFree(GetProcessHeap(), 0, This);
764 return 0;
766 return ref;
769 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
770 IEnumCATEGORYINFO *iface,
771 ULONG celt,
772 CATEGORYINFO *rgelt,
773 ULONG *pceltFetched)
775 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
776 ULONG fetched = 0;
778 TRACE("\n");
780 if (rgelt == NULL) return E_POINTER;
782 if (This->key) while (fetched < celt) {
783 LSTATUS res;
784 HRESULT hr;
785 WCHAR catid[CHARS_IN_GUID];
786 DWORD cName = CHARS_IN_GUID;
787 HKEY subkey;
789 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
790 NULL, NULL, NULL, NULL);
791 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
792 ++(This->next_index);
794 hr = CLSIDFromString(catid, &rgelt->catid);
795 if (FAILED(hr)) continue;
797 res = open_classes_key(This->key, catid, KEY_READ, &subkey);
798 if (res != ERROR_SUCCESS) continue;
800 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
801 rgelt->szDescription, 128);
802 RegCloseKey(subkey);
803 if (FAILED(hr)) continue;
805 rgelt->lcid = This->lcid;
806 ++fetched;
807 ++rgelt;
810 if (pceltFetched) *pceltFetched = fetched;
811 return fetched == celt ? S_OK : S_FALSE;
814 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
815 IEnumCATEGORYINFO *iface,
816 ULONG celt)
818 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
820 TRACE("\n");
822 This->next_index += celt;
823 /* This should return S_FALSE when there aren't celt elems to skip. */
824 return S_OK;
827 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(IEnumCATEGORYINFO *iface)
829 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
831 TRACE("\n");
833 This->next_index = 0;
834 return S_OK;
837 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
838 IEnumCATEGORYINFO *iface,
839 IEnumCATEGORYINFO **ppenum)
841 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
842 IEnumCATEGORYINFOImpl *new_this;
844 TRACE("\n");
846 if (ppenum == NULL) return E_POINTER;
848 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
849 if (new_this == NULL) return E_OUTOFMEMORY;
851 new_this->IEnumCATEGORYINFO_iface = This->IEnumCATEGORYINFO_iface;
852 new_this->ref = 1;
853 new_this->lcid = This->lcid;
854 /* FIXME: could we more efficiently use DuplicateHandle? */
855 open_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ, &new_this->key);
856 new_this->next_index = This->next_index;
858 *ppenum = &new_this->IEnumCATEGORYINFO_iface;
859 return S_OK;
862 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
864 COMCAT_IEnumCATEGORYINFO_QueryInterface,
865 COMCAT_IEnumCATEGORYINFO_AddRef,
866 COMCAT_IEnumCATEGORYINFO_Release,
867 COMCAT_IEnumCATEGORYINFO_Next,
868 COMCAT_IEnumCATEGORYINFO_Skip,
869 COMCAT_IEnumCATEGORYINFO_Reset,
870 COMCAT_IEnumCATEGORYINFO_Clone
873 static HRESULT EnumCATEGORYINFO_Construct(LCID lcid, IEnumCATEGORYINFO **ret)
875 IEnumCATEGORYINFOImpl *This;
877 *ret = NULL;
879 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
880 if (!This) return E_OUTOFMEMORY;
882 This->IEnumCATEGORYINFO_iface.lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
883 This->ref = 1;
884 This->lcid = lcid;
885 open_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ, &This->key);
887 *ret = &This->IEnumCATEGORYINFO_iface;
888 return S_OK;
891 /**********************************************************************
892 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
894 * This implementation is not thread-safe. The manager itself is, but
895 * I can't imagine a valid use of an enumerator in several threads.
897 typedef struct
899 IEnumGUID IEnumGUID_iface;
900 LONG ref;
901 struct class_categories *categories;
902 HKEY key;
903 DWORD next_index;
904 } CLSID_IEnumGUIDImpl;
906 static inline CLSID_IEnumGUIDImpl *impl_from_IEnumCLSID(IEnumGUID *iface)
908 return CONTAINING_RECORD(iface, CLSID_IEnumGUIDImpl, IEnumGUID_iface);
911 static HRESULT WINAPI CLSIDEnumGUID_QueryInterface(
912 IEnumGUID *iface,
913 REFIID riid,
914 LPVOID *ppvObj)
916 TRACE("%s\n",debugstr_guid(riid));
918 if (ppvObj == NULL) return E_POINTER;
920 if (IsEqualGUID(riid, &IID_IUnknown) ||
921 IsEqualGUID(riid, &IID_IEnumGUID))
923 *ppvObj = iface;
924 IEnumGUID_AddRef(iface);
925 return S_OK;
928 return E_NOINTERFACE;
931 static ULONG WINAPI CLSIDEnumGUID_AddRef(IEnumGUID *iface)
933 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
934 TRACE("\n");
936 return InterlockedIncrement(&This->ref);
939 static ULONG WINAPI CLSIDEnumGUID_Release(IEnumGUID *iface)
941 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
942 ULONG ref;
944 TRACE("\n");
946 ref = InterlockedDecrement(&This->ref);
947 if (ref == 0) {
948 if (This->key) RegCloseKey(This->key);
949 HeapFree(GetProcessHeap(), 0, This->categories);
950 HeapFree(GetProcessHeap(), 0, This);
951 return 0;
953 return ref;
956 static HRESULT WINAPI CLSIDEnumGUID_Next(
957 IEnumGUID *iface,
958 ULONG celt,
959 GUID *rgelt,
960 ULONG *pceltFetched)
962 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
963 ULONG fetched = 0;
965 TRACE("\n");
967 if (rgelt == NULL) return E_POINTER;
969 if (This->key) while (fetched < celt) {
970 LSTATUS res;
971 HRESULT hr;
972 WCHAR clsid[CHARS_IN_GUID];
973 DWORD cName = CHARS_IN_GUID;
974 HKEY subkey;
976 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
977 NULL, NULL, NULL, NULL);
978 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
979 ++(This->next_index);
981 hr = CLSIDFromString(clsid, rgelt);
982 if (FAILED(hr)) continue;
984 res = open_classes_key(This->key, clsid, KEY_READ, &subkey);
985 if (res != ERROR_SUCCESS) continue;
987 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
988 RegCloseKey(subkey);
989 if (hr != S_OK) continue;
991 ++fetched;
992 ++rgelt;
995 if (pceltFetched) *pceltFetched = fetched;
996 return fetched == celt ? S_OK : S_FALSE;
999 static HRESULT WINAPI CLSIDEnumGUID_Skip(
1000 IEnumGUID *iface,
1001 ULONG celt)
1003 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1005 TRACE("\n");
1007 This->next_index += celt;
1008 FIXME("Never returns S_FALSE\n");
1009 return S_OK;
1012 static HRESULT WINAPI CLSIDEnumGUID_Reset(IEnumGUID *iface)
1014 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1016 TRACE("\n");
1018 This->next_index = 0;
1019 return S_OK;
1022 static HRESULT WINAPI CLSIDEnumGUID_Clone(
1023 IEnumGUID *iface,
1024 IEnumGUID **ppenum)
1026 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1027 CLSID_IEnumGUIDImpl *cloned;
1029 TRACE("(%p)->(%p)\n", This, ppenum);
1031 if (ppenum == NULL) return E_POINTER;
1033 *ppenum = NULL;
1035 cloned = HeapAlloc(GetProcessHeap(), 0, sizeof(CLSID_IEnumGUIDImpl));
1036 if (cloned == NULL) return E_OUTOFMEMORY;
1038 cloned->IEnumGUID_iface.lpVtbl = This->IEnumGUID_iface.lpVtbl;
1039 cloned->ref = 1;
1041 cloned->categories = HeapAlloc(GetProcessHeap(), 0, This->categories->size);
1042 if (cloned->categories == NULL) {
1043 HeapFree(GetProcessHeap(), 0, cloned);
1044 return E_OUTOFMEMORY;
1046 memcpy(cloned->categories, This->categories, This->categories->size);
1048 cloned->key = NULL;
1049 open_classes_key(HKEY_CLASSES_ROOT, L"CLSID", KEY_READ, &cloned->key);
1050 cloned->next_index = This->next_index;
1052 *ppenum = &cloned->IEnumGUID_iface;
1053 return S_OK;
1056 static const IEnumGUIDVtbl CLSIDEnumGUIDVtbl =
1058 CLSIDEnumGUID_QueryInterface,
1059 CLSIDEnumGUID_AddRef,
1060 CLSIDEnumGUID_Release,
1061 CLSIDEnumGUID_Next,
1062 CLSIDEnumGUID_Skip,
1063 CLSIDEnumGUID_Reset,
1064 CLSIDEnumGUID_Clone
1067 static HRESULT CLSIDEnumGUID_Construct(struct class_categories *categories, IEnumCLSID **ret)
1069 CLSID_IEnumGUIDImpl *This;
1071 *ret = NULL;
1073 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1074 if (!This) return E_OUTOFMEMORY;
1076 This->IEnumGUID_iface.lpVtbl = &CLSIDEnumGUIDVtbl;
1077 This->ref = 1;
1078 This->categories = categories;
1079 open_classes_key(HKEY_CLASSES_ROOT, L"CLSID", KEY_READ, &This->key);
1081 *ret = &This->IEnumGUID_iface;
1083 return S_OK;
1086 /**********************************************************************
1087 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1089 * This implementation is not thread-safe. The manager itself is, but
1090 * I can't imagine a valid use of an enumerator in several threads.
1092 typedef struct
1094 IEnumGUID IEnumGUID_iface;
1095 LONG ref;
1096 WCHAR keyname[68];
1097 HKEY key;
1098 DWORD next_index;
1099 } CATID_IEnumGUIDImpl;
1101 static inline CATID_IEnumGUIDImpl *impl_from_IEnumCATID(IEnumGUID *iface)
1103 return CONTAINING_RECORD(iface, CATID_IEnumGUIDImpl, IEnumGUID_iface);
1106 static HRESULT WINAPI CATIDEnumGUID_QueryInterface(
1107 IEnumGUID *iface,
1108 REFIID riid,
1109 LPVOID *ppvObj)
1111 TRACE("%s\n",debugstr_guid(riid));
1113 if (ppvObj == NULL) return E_POINTER;
1115 if (IsEqualGUID(riid, &IID_IUnknown) ||
1116 IsEqualGUID(riid, &IID_IEnumGUID))
1118 *ppvObj = iface;
1119 IEnumGUID_AddRef(iface);
1120 return S_OK;
1123 return E_NOINTERFACE;
1126 static ULONG WINAPI CATIDEnumGUID_AddRef(IEnumGUID *iface)
1128 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1129 TRACE("\n");
1131 return InterlockedIncrement(&This->ref);
1134 static ULONG WINAPI CATIDEnumGUID_Release(IEnumGUID *iface)
1136 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1137 ULONG ref;
1139 TRACE("\n");
1141 ref = InterlockedDecrement(&This->ref);
1142 if (ref == 0) {
1143 if (This->key) RegCloseKey(This->key);
1144 HeapFree(GetProcessHeap(), 0, This);
1145 return 0;
1147 return ref;
1150 static HRESULT WINAPI CATIDEnumGUID_Next(
1151 IEnumGUID *iface,
1152 ULONG celt,
1153 GUID *rgelt,
1154 ULONG *pceltFetched)
1156 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1157 ULONG fetched = 0;
1159 TRACE("\n");
1161 if (rgelt == NULL) return E_POINTER;
1163 if (This->key) while (fetched < celt) {
1164 LSTATUS res;
1165 HRESULT hr;
1166 WCHAR catid[CHARS_IN_GUID];
1167 DWORD cName = CHARS_IN_GUID;
1169 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1170 NULL, NULL, NULL, NULL);
1171 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1172 ++(This->next_index);
1174 hr = CLSIDFromString(catid, rgelt);
1175 if (FAILED(hr)) continue;
1177 ++fetched;
1178 ++rgelt;
1181 if (pceltFetched) *pceltFetched = fetched;
1182 return fetched == celt ? S_OK : S_FALSE;
1185 static HRESULT WINAPI CATIDEnumGUID_Skip(
1186 IEnumGUID *iface,
1187 ULONG celt)
1189 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1191 TRACE("\n");
1193 This->next_index += celt;
1194 FIXME("Never returns S_FALSE\n");
1195 return S_OK;
1198 static HRESULT WINAPI CATIDEnumGUID_Reset(IEnumGUID *iface)
1200 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1202 TRACE("\n");
1204 This->next_index = 0;
1205 return S_OK;
1208 static HRESULT WINAPI CATIDEnumGUID_Clone(
1209 IEnumGUID *iface,
1210 IEnumGUID **ppenum)
1212 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1213 CATID_IEnumGUIDImpl *new_this;
1215 TRACE("\n");
1217 if (ppenum == NULL) return E_POINTER;
1219 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1220 if (new_this == NULL) return E_OUTOFMEMORY;
1222 new_this->IEnumGUID_iface.lpVtbl = This->IEnumGUID_iface.lpVtbl;
1223 new_this->ref = 1;
1224 lstrcpyW(new_this->keyname, This->keyname);
1225 /* FIXME: could we more efficiently use DuplicateHandle? */
1226 open_classes_key(HKEY_CLASSES_ROOT, new_this->keyname, KEY_READ, &new_this->key);
1227 new_this->next_index = This->next_index;
1229 *ppenum = &new_this->IEnumGUID_iface;
1230 return S_OK;
1233 static const IEnumGUIDVtbl CATIDEnumGUIDVtbl =
1235 CATIDEnumGUID_QueryInterface,
1236 CATIDEnumGUID_AddRef,
1237 CATIDEnumGUID_Release,
1238 CATIDEnumGUID_Next,
1239 CATIDEnumGUID_Skip,
1240 CATIDEnumGUID_Reset,
1241 CATIDEnumGUID_Clone
1244 static HRESULT CATIDEnumGUID_Construct(REFCLSID rclsid, LPCWSTR postfix, IEnumGUID **ret)
1246 WCHAR keyname[100], clsidW[CHARS_IN_GUID];
1247 CATID_IEnumGUIDImpl *This;
1249 *ret = NULL;
1251 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1252 if (!This) return E_OUTOFMEMORY;
1254 StringFromGUID2(rclsid, clsidW, CHARS_IN_GUID);
1256 This->IEnumGUID_iface.lpVtbl = &CATIDEnumGUIDVtbl;
1257 This->ref = 1;
1258 lstrcpyW(keyname, L"CLSID\\");
1259 lstrcatW(keyname, clsidW);
1260 lstrcatW(keyname, postfix);
1262 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
1264 *ret = &This->IEnumGUID_iface;
1265 return S_OK;