ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / ole32 / comcat.c
blobd43c3afc0d45b030557a3c32c460531e14e8a274
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/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl;
42 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl;
44 typedef struct
46 ICatRegister ICatRegister_iface;
47 ICatInformation ICatInformation_iface;
48 } ComCatMgrImpl;
50 /* static ComCatMgr instance */
51 static ComCatMgrImpl COMCAT_ComCatMgr =
53 { &COMCAT_ICatRegister_Vtbl },
54 { &COMCAT_ICatInformation_Vtbl }
57 struct class_categories
59 ULONG size; /* total length, including structure itself */
60 ULONG impl_offset;
61 ULONG req_offset;
64 static HRESULT EnumCATEGORYINFO_Construct(LCID lcid, IEnumCATEGORYINFO **ret);
65 static HRESULT CLSIDEnumGUID_Construct(struct class_categories *class_categories, IEnumCLSID **ret);
66 static HRESULT CATIDEnumGUID_Construct(REFCLSID rclsid, LPCWSTR impl_req, IEnumCATID **ret);
68 /**********************************************************************
69 * File-scope string constants
71 static const WCHAR comcat_keyname[] = {
72 'C','o','m','p','o','n','e','n','t',' ','C','a','t','e','g','o','r','i','e','s',0 };
73 static const WCHAR impl_keyname[] = {
74 'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
75 static const WCHAR req_keyname[] = {
76 'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
77 static const WCHAR clsid_keyname[] = { 'C','L','S','I','D',0 };
80 /**********************************************************************
81 * COMCAT_RegisterClassCategories
83 static HRESULT COMCAT_RegisterClassCategories(
84 REFCLSID rclsid,
85 LPCWSTR type,
86 ULONG cCategories,
87 const CATID *rgcatid)
89 WCHAR keyname[CHARS_IN_GUID];
90 HRESULT res;
91 HKEY clsid_key, class_key, type_key;
93 if (cCategories && rgcatid == NULL) return E_POINTER;
95 /* Format the class key name. */
96 res = StringFromGUID2(rclsid, keyname, CHARS_IN_GUID);
97 if (FAILED(res)) return res;
99 /* Create (or open) the CLSID key. */
100 res = create_classes_key(HKEY_CLASSES_ROOT, clsid_keyname, KEY_READ|KEY_WRITE, &clsid_key);
101 if (res != ERROR_SUCCESS) return E_FAIL;
103 /* Create (or open) the class key. */
104 res = create_classes_key(clsid_key, keyname, KEY_READ|KEY_WRITE, &class_key);
105 if (res == ERROR_SUCCESS) {
106 /* Create (or open) the category type key. */
107 res = create_classes_key(class_key, type, KEY_READ|KEY_WRITE, &type_key);
108 if (res == ERROR_SUCCESS) {
109 for (; cCategories; --cCategories, ++rgcatid) {
110 HKEY key;
112 /* Format the category key name. */
113 res = StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID);
114 if (FAILED(res)) continue;
116 /* Do the register. */
117 res = create_classes_key(type_key, keyname, KEY_READ|KEY_WRITE, &key);
118 if (res == ERROR_SUCCESS) RegCloseKey(key);
120 res = S_OK;
121 } else res = E_FAIL;
122 RegCloseKey(class_key);
123 } else res = E_FAIL;
124 RegCloseKey(clsid_key);
126 return res;
129 /**********************************************************************
130 * COMCAT_UnRegisterClassCategories
132 static HRESULT COMCAT_UnRegisterClassCategories(
133 REFCLSID rclsid,
134 LPCWSTR type,
135 ULONG cCategories,
136 const CATID *rgcatid)
138 WCHAR keyname[68] = { 'C', 'L', 'S', 'I', 'D', '\\' };
139 HRESULT res;
140 HKEY type_key;
142 if (cCategories && rgcatid == NULL) return E_POINTER;
144 /* Format the class category type key name. */
145 res = StringFromGUID2(rclsid, keyname + 6, CHARS_IN_GUID);
146 if (FAILED(res)) return res;
147 keyname[44] = '\\';
148 lstrcpyW(keyname + 45, type);
150 /* Open the class category type key. */
151 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ|KEY_WRITE, &type_key);
152 if (res != ERROR_SUCCESS) return E_FAIL;
154 for (; cCategories; --cCategories, ++rgcatid) {
155 /* Format the category key name. */
156 res = StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID);
157 if (FAILED(res)) continue;
159 /* Do the unregister. */
160 RegDeleteKeyW(type_key, keyname);
162 RegCloseKey(type_key);
164 return S_OK;
167 /**********************************************************************
168 * COMCAT_GetCategoryDesc
170 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
171 ULONG buf_wchars)
173 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
174 WCHAR valname[5];
175 HRESULT res;
176 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
178 if (pszDesc == NULL) return E_INVALIDARG;
180 /* FIXME: lcid comparisons are more complex than this! */
181 wsprintfW(valname, fmt, lcid);
182 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
183 if (res != ERROR_SUCCESS || type != REG_SZ) {
184 FIXME("Simplified lcid comparison\n");
185 return CAT_E_NODESCRIPTION;
187 pszDesc[size / sizeof(WCHAR)] = 0;
189 return S_OK;
192 /**********************************************************************
193 * COMCAT_PrepareClassCategories
195 static struct class_categories *COMCAT_PrepareClassCategories(
196 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
198 struct class_categories *categories;
199 WCHAR *strings;
200 ULONG size;
202 size = sizeof(struct class_categories) + ((impl_count + req_count)*CHARS_IN_GUID + 2)*sizeof(WCHAR);
203 categories = HeapAlloc(GetProcessHeap(), 0, size);
204 if (categories == NULL) return categories;
206 categories->size = size;
207 categories->impl_offset = sizeof(struct class_categories);
208 categories->req_offset = categories->impl_offset + (impl_count*CHARS_IN_GUID + 1)*sizeof(WCHAR);
210 strings = (WCHAR *)(categories + 1);
211 while (impl_count--) {
212 StringFromGUID2(impl_catids++, strings, CHARS_IN_GUID);
213 strings += CHARS_IN_GUID;
215 *strings++ = 0;
217 while (req_count--) {
218 StringFromGUID2(req_catids++, strings, CHARS_IN_GUID);
219 strings += CHARS_IN_GUID;
221 *strings++ = 0;
223 return categories;
226 /**********************************************************************
227 * COMCAT_IsClassOfCategories
229 static HRESULT COMCAT_IsClassOfCategories(
230 HKEY key,
231 struct class_categories const* categories)
233 const WCHAR *impl_strings, *req_strings;
234 HKEY subkey;
235 HRESULT res;
236 DWORD index;
237 LPCWSTR string;
239 impl_strings = (WCHAR*)((BYTE*)categories + categories->impl_offset);
240 req_strings = (WCHAR*)((BYTE*)categories + categories->req_offset);
242 /* Check that every given category is implemented by class. */
243 if (*impl_strings) {
244 res = open_classes_key(key, impl_keyname, KEY_READ, &subkey);
245 if (res != ERROR_SUCCESS) return S_FALSE;
246 for (string = impl_strings; *string; string += CHARS_IN_GUID) {
247 HKEY catkey;
248 res = open_classes_key(subkey, string, READ_CONTROL, &catkey);
249 if (res != ERROR_SUCCESS) {
250 RegCloseKey(subkey);
251 return S_FALSE;
253 RegCloseKey(catkey);
255 RegCloseKey(subkey);
258 /* Check that all categories required by class are given. */
259 res = open_classes_key(key, req_keyname, KEY_READ, &subkey);
260 if (res == ERROR_SUCCESS) {
261 for (index = 0; ; ++index) {
262 WCHAR keyname[CHARS_IN_GUID];
263 DWORD size = CHARS_IN_GUID;
265 res = RegEnumKeyExW(subkey, index, keyname, &size,
266 NULL, NULL, NULL, NULL);
267 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
268 if (size != CHARS_IN_GUID-1) continue; /* bogus catid in registry */
269 for (string = req_strings; *string; string += CHARS_IN_GUID)
270 if (!strcmpiW(string, keyname)) break;
271 if (!*string) {
272 RegCloseKey(subkey);
273 return S_FALSE;
276 RegCloseKey(subkey);
279 return S_OK;
282 /**********************************************************************
283 * COMCAT_ICatRegister_QueryInterface
285 static HRESULT WINAPI COMCAT_ICatRegister_QueryInterface(
286 LPCATREGISTER iface,
287 REFIID riid,
288 LPVOID *ppvObj)
290 TRACE("%s\n",debugstr_guid(riid));
292 if (ppvObj == NULL) return E_POINTER;
294 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ICatRegister)) {
295 *ppvObj = iface;
296 ICatRegister_AddRef(iface);
297 return S_OK;
300 if (IsEqualGUID(riid, &IID_ICatInformation)) {
301 *ppvObj = &COMCAT_ComCatMgr.ICatInformation_iface;
302 ICatRegister_AddRef(iface);
303 return S_OK;
306 return E_NOINTERFACE;
309 /**********************************************************************
310 * COMCAT_ICatRegister_AddRef
312 static ULONG WINAPI COMCAT_ICatRegister_AddRef(LPCATREGISTER iface)
314 return 2; /* non-heap based object */
317 /**********************************************************************
318 * COMCAT_ICatRegister_Release
320 static ULONG WINAPI COMCAT_ICatRegister_Release(LPCATREGISTER iface)
322 return 1; /* non-heap based object */
325 /**********************************************************************
326 * COMCAT_ICatRegister_RegisterCategories
328 static HRESULT WINAPI COMCAT_ICatRegister_RegisterCategories(
329 LPCATREGISTER iface,
330 ULONG cCategories,
331 CATEGORYINFO *rgci)
333 HKEY comcat_key;
334 HRESULT res;
336 TRACE("\n");
338 if (cCategories && rgci == NULL)
339 return E_POINTER;
341 /* Create (or open) the component categories key. */
342 res = create_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ|KEY_WRITE, &comcat_key);
343 if (res != ERROR_SUCCESS) return E_FAIL;
345 for (; cCategories; --cCategories, ++rgci) {
346 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
347 WCHAR keyname[CHARS_IN_GUID];
348 WCHAR valname[9];
349 HKEY cat_key;
351 /* Create (or open) the key for this category. */
352 if (!StringFromGUID2(&rgci->catid, keyname, CHARS_IN_GUID)) continue;
353 res = create_classes_key(comcat_key, keyname, KEY_READ|KEY_WRITE, &cat_key);
354 if (res != ERROR_SUCCESS) continue;
356 /* Set the value for this locale's description. */
357 wsprintfW(valname, fmt, rgci->lcid);
358 RegSetValueExW(cat_key, valname, 0, REG_SZ, (const BYTE*)rgci->szDescription,
359 (lstrlenW(rgci->szDescription) + 1) * sizeof(WCHAR));
361 RegCloseKey(cat_key);
364 RegCloseKey(comcat_key);
365 return S_OK;
368 /**********************************************************************
369 * COMCAT_ICatRegister_UnRegisterCategories
371 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterCategories(
372 LPCATREGISTER iface,
373 ULONG cCategories,
374 CATID *rgcatid)
376 HKEY comcat_key;
377 HRESULT res;
379 TRACE("\n");
381 if (cCategories && rgcatid == NULL)
382 return E_POINTER;
384 /* Open the component categories key. */
385 res = open_classes_key(HKEY_CLASSES_ROOT, comcat_keyname, KEY_READ|KEY_WRITE, &comcat_key);
386 if (res != ERROR_SUCCESS) return E_FAIL;
388 for (; cCategories; --cCategories, ++rgcatid) {
389 WCHAR keyname[CHARS_IN_GUID];
391 /* Delete the key for this category. */
392 if (!StringFromGUID2(rgcatid, keyname, CHARS_IN_GUID)) continue;
393 RegDeleteKeyW(comcat_key, keyname);
396 RegCloseKey(comcat_key);
397 return S_OK;
400 /**********************************************************************
401 * COMCAT_ICatRegister_RegisterClassImplCategories
403 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassImplCategories(
404 LPCATREGISTER iface,
405 REFCLSID rclsid,
406 ULONG cCategories,
407 CATID *rgcatid)
409 TRACE("\n");
411 return COMCAT_RegisterClassCategories(
412 rclsid, impl_keyname, cCategories, rgcatid);
415 /**********************************************************************
416 * COMCAT_ICatRegister_UnRegisterClassImplCategories
418 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassImplCategories(
419 LPCATREGISTER iface,
420 REFCLSID rclsid,
421 ULONG cCategories,
422 CATID *rgcatid)
424 TRACE("\n");
426 return COMCAT_UnRegisterClassCategories(
427 rclsid, impl_keyname, cCategories, rgcatid);
430 /**********************************************************************
431 * COMCAT_ICatRegister_RegisterClassReqCategories
433 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassReqCategories(
434 LPCATREGISTER iface,
435 REFCLSID rclsid,
436 ULONG cCategories,
437 CATID *rgcatid)
439 TRACE("\n");
441 return COMCAT_RegisterClassCategories(
442 rclsid, req_keyname, cCategories, rgcatid);
445 /**********************************************************************
446 * COMCAT_ICatRegister_UnRegisterClassReqCategories
448 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassReqCategories(
449 LPCATREGISTER iface,
450 REFCLSID rclsid,
451 ULONG cCategories,
452 CATID *rgcatid)
454 TRACE("\n");
456 return COMCAT_UnRegisterClassCategories(
457 rclsid, req_keyname, cCategories, rgcatid);
460 /**********************************************************************
461 * COMCAT_ICatInformation_QueryInterface
463 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
464 LPCATINFORMATION iface,
465 REFIID riid,
466 LPVOID *ppvObj)
468 return ICatRegister_QueryInterface(&COMCAT_ComCatMgr.ICatRegister_iface, riid, ppvObj);
471 /**********************************************************************
472 * COMCAT_ICatInformation_AddRef
474 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
476 return ICatRegister_AddRef(&COMCAT_ComCatMgr.ICatRegister_iface);
479 /**********************************************************************
480 * COMCAT_ICatInformation_Release
482 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
484 return ICatRegister_Release(&COMCAT_ComCatMgr.ICatRegister_iface);
487 /**********************************************************************
488 * COMCAT_ICatInformation_EnumCategories
490 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
491 LPCATINFORMATION iface,
492 LCID lcid,
493 IEnumCATEGORYINFO **ppenumCatInfo)
495 TRACE("\n");
497 if (ppenumCatInfo == NULL) return E_POINTER;
499 return EnumCATEGORYINFO_Construct(lcid, ppenumCatInfo);
502 /**********************************************************************
503 * COMCAT_ICatInformation_GetCategoryDesc
505 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
506 LPCATINFORMATION iface,
507 REFCATID rcatid,
508 LCID lcid,
509 PWCHAR *ppszDesc)
511 WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
512 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
513 'r', 'i', 'e', 's', '\\', 0 };
514 HKEY key;
515 HRESULT res;
517 TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
519 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
521 /* Open the key for this category. */
522 if (!StringFromGUID2(rcatid, keyname + 21, CHARS_IN_GUID)) return E_FAIL;
523 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
524 if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
526 /* Allocate a sensible amount of memory for the description. */
527 *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
528 if (*ppszDesc == NULL) {
529 RegCloseKey(key);
530 return E_OUTOFMEMORY;
533 /* Get the description, and make sure it's null terminated. */
534 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
535 RegCloseKey(key);
536 if (FAILED(res)) {
537 CoTaskMemFree(*ppszDesc);
538 return res;
541 return S_OK;
544 /**********************************************************************
545 * COMCAT_ICatInformation_EnumClassesOfCategories
547 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
548 LPCATINFORMATION iface,
549 ULONG cImplemented,
550 CATID *rgcatidImpl,
551 ULONG cRequired,
552 CATID *rgcatidReq,
553 LPENUMCLSID *ppenumCLSID)
555 struct class_categories *categories;
556 HRESULT hr;
558 TRACE("\n");
560 if (cImplemented == (ULONG)-1)
561 cImplemented = 0;
562 if (cRequired == (ULONG)-1)
563 cRequired = 0;
565 if (ppenumCLSID == NULL ||
566 (cImplemented && rgcatidImpl == NULL) ||
567 (cRequired && rgcatidReq == NULL)) return E_POINTER;
569 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
570 cRequired, rgcatidReq);
571 if (categories == NULL) return E_OUTOFMEMORY;
573 hr = CLSIDEnumGUID_Construct(categories, ppenumCLSID);
574 if (FAILED(hr))
576 HeapFree(GetProcessHeap(), 0, categories);
577 return hr;
580 return hr;
583 /**********************************************************************
584 * COMCAT_ICatInformation_IsClassOfCategories
586 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
587 LPCATINFORMATION iface,
588 REFCLSID rclsid,
589 ULONG cImplemented,
590 CATID *rgcatidImpl,
591 ULONG cRequired,
592 CATID *rgcatidReq)
594 WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
595 HRESULT res;
596 struct class_categories *categories;
597 HKEY key;
599 if (TRACE_ON(ole)) {
600 ULONG count;
601 TRACE("CLSID: %s Implemented %u\n",debugstr_guid(rclsid),cImplemented);
602 for (count = 0; count < cImplemented; ++count)
603 TRACE(" %s\n",debugstr_guid(&rgcatidImpl[count]));
604 TRACE("Required %u\n",cRequired);
605 for (count = 0; count < cRequired; ++count)
606 TRACE(" %s\n",debugstr_guid(&rgcatidReq[count]));
609 if ((cImplemented && rgcatidImpl == NULL) ||
610 (cRequired && rgcatidReq == NULL)) return E_POINTER;
612 res = StringFromGUID2(rclsid, keyname + 6, CHARS_IN_GUID);
613 if (FAILED(res)) return res;
615 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
616 cRequired, rgcatidReq);
617 if (categories == NULL) return E_OUTOFMEMORY;
619 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
620 if (res == ERROR_SUCCESS) {
621 res = COMCAT_IsClassOfCategories(key, categories);
622 RegCloseKey(key);
623 } else res = S_FALSE;
625 HeapFree(GetProcessHeap(), 0, categories);
627 return res;
630 /**********************************************************************
631 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
633 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
634 LPCATINFORMATION iface,
635 REFCLSID rclsid,
636 LPENUMCATID *ppenumCATID)
638 static const WCHAR postfix[] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
639 'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
640 'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
642 TRACE("%s\n",debugstr_guid(rclsid));
644 if (rclsid == NULL || ppenumCATID == NULL)
645 return E_POINTER;
647 return CATIDEnumGUID_Construct(rclsid, postfix, ppenumCATID);
650 /**********************************************************************
651 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
653 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
654 LPCATINFORMATION iface,
655 REFCLSID rclsid,
656 LPENUMCATID *ppenumCATID)
658 static const WCHAR postfix[] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
659 'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
660 'r', 'i', 'e', 's', 0 };
662 TRACE("%s\n",debugstr_guid(rclsid));
664 if (rclsid == NULL || ppenumCATID == NULL)
665 return E_POINTER;
667 return CATIDEnumGUID_Construct(rclsid, postfix, ppenumCATID);
670 /**********************************************************************
671 * COMCAT_ICatRegister_Vtbl
673 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
675 COMCAT_ICatRegister_QueryInterface,
676 COMCAT_ICatRegister_AddRef,
677 COMCAT_ICatRegister_Release,
678 COMCAT_ICatRegister_RegisterCategories,
679 COMCAT_ICatRegister_UnRegisterCategories,
680 COMCAT_ICatRegister_RegisterClassImplCategories,
681 COMCAT_ICatRegister_UnRegisterClassImplCategories,
682 COMCAT_ICatRegister_RegisterClassReqCategories,
683 COMCAT_ICatRegister_UnRegisterClassReqCategories
687 /**********************************************************************
688 * COMCAT_ICatInformation_Vtbl
690 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
692 COMCAT_ICatInformation_QueryInterface,
693 COMCAT_ICatInformation_AddRef,
694 COMCAT_ICatInformation_Release,
695 COMCAT_ICatInformation_EnumCategories,
696 COMCAT_ICatInformation_GetCategoryDesc,
697 COMCAT_ICatInformation_EnumClassesOfCategories,
698 COMCAT_ICatInformation_IsClassOfCategories,
699 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
700 COMCAT_ICatInformation_EnumReqCategoriesOfClass
703 HRESULT WINAPI ComCat_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter, REFIID riid, void **ppvObj)
705 HRESULT res;
706 TRACE("%s\n",debugstr_guid(riid));
708 if (ppvObj == NULL) return E_POINTER;
710 /* Don't support aggregation (Windows doesn't) */
711 if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
713 res = ICatRegister_QueryInterface(&COMCAT_ComCatMgr.ICatRegister_iface, riid, ppvObj);
714 if (SUCCEEDED(res)) {
715 return res;
718 return CLASS_E_CLASSNOTAVAILABLE;
721 /**********************************************************************
722 * IEnumCATEGORYINFO implementation
724 * This implementation is not thread-safe. The manager itself is, but
725 * I can't imagine a valid use of an enumerator in several threads.
727 typedef struct
729 IEnumCATEGORYINFO IEnumCATEGORYINFO_iface;
730 LONG ref;
731 LCID lcid;
732 HKEY key;
733 DWORD next_index;
734 } IEnumCATEGORYINFOImpl;
736 static inline IEnumCATEGORYINFOImpl *impl_from_IEnumCATEGORYINFO(IEnumCATEGORYINFO *iface)
738 return CONTAINING_RECORD(iface, IEnumCATEGORYINFOImpl, IEnumCATEGORYINFO_iface);
741 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(IEnumCATEGORYINFO *iface)
743 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
745 TRACE("\n");
747 return InterlockedIncrement(&This->ref);
750 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
751 IEnumCATEGORYINFO *iface,
752 REFIID riid,
753 LPVOID *ppvObj)
755 TRACE("%s\n",debugstr_guid(riid));
757 if (ppvObj == NULL) return E_POINTER;
759 if (IsEqualGUID(riid, &IID_IUnknown) ||
760 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
762 *ppvObj = iface;
763 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
764 return S_OK;
767 return E_NOINTERFACE;
770 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(IEnumCATEGORYINFO *iface)
772 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
773 ULONG ref;
775 TRACE("\n");
777 ref = InterlockedDecrement(&This->ref);
778 if (ref == 0) {
779 if (This->key) RegCloseKey(This->key);
780 HeapFree(GetProcessHeap(), 0, This);
781 return 0;
783 return ref;
786 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
787 IEnumCATEGORYINFO *iface,
788 ULONG celt,
789 CATEGORYINFO *rgelt,
790 ULONG *pceltFetched)
792 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
793 ULONG fetched = 0;
795 TRACE("\n");
797 if (rgelt == NULL) return E_POINTER;
799 if (This->key) while (fetched < celt) {
800 LSTATUS res;
801 HRESULT hr;
802 WCHAR catid[CHARS_IN_GUID];
803 DWORD cName = CHARS_IN_GUID;
804 HKEY subkey;
806 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
807 NULL, NULL, NULL, NULL);
808 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
809 ++(This->next_index);
811 hr = CLSIDFromString(catid, &rgelt->catid);
812 if (FAILED(hr)) continue;
814 res = open_classes_key(This->key, catid, KEY_READ, &subkey);
815 if (res != ERROR_SUCCESS) continue;
817 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
818 rgelt->szDescription, 128);
819 RegCloseKey(subkey);
820 if (FAILED(hr)) continue;
822 rgelt->lcid = This->lcid;
823 ++fetched;
824 ++rgelt;
827 if (pceltFetched) *pceltFetched = fetched;
828 return fetched == celt ? S_OK : S_FALSE;
831 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
832 IEnumCATEGORYINFO *iface,
833 ULONG celt)
835 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
837 TRACE("\n");
839 This->next_index += celt;
840 /* This should return S_FALSE when there aren't celt elems to skip. */
841 return S_OK;
844 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(IEnumCATEGORYINFO *iface)
846 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
848 TRACE("\n");
850 This->next_index = 0;
851 return S_OK;
854 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
855 IEnumCATEGORYINFO *iface,
856 IEnumCATEGORYINFO **ppenum)
858 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
859 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
860 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
861 'r', 'i', 'e', 's', 0 };
862 IEnumCATEGORYINFOImpl *new_this;
864 TRACE("\n");
866 if (ppenum == NULL) return E_POINTER;
868 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
869 if (new_this == NULL) return E_OUTOFMEMORY;
871 new_this->IEnumCATEGORYINFO_iface = This->IEnumCATEGORYINFO_iface;
872 new_this->ref = 1;
873 new_this->lcid = This->lcid;
874 /* FIXME: could we more efficiently use DuplicateHandle? */
875 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &new_this->key);
876 new_this->next_index = This->next_index;
878 *ppenum = &new_this->IEnumCATEGORYINFO_iface;
879 return S_OK;
882 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
884 COMCAT_IEnumCATEGORYINFO_QueryInterface,
885 COMCAT_IEnumCATEGORYINFO_AddRef,
886 COMCAT_IEnumCATEGORYINFO_Release,
887 COMCAT_IEnumCATEGORYINFO_Next,
888 COMCAT_IEnumCATEGORYINFO_Skip,
889 COMCAT_IEnumCATEGORYINFO_Reset,
890 COMCAT_IEnumCATEGORYINFO_Clone
893 static HRESULT EnumCATEGORYINFO_Construct(LCID lcid, IEnumCATEGORYINFO **ret)
895 static const WCHAR keyname[] = {'C','o','m','p','o','n','e','n','t',' ','C','a','t','e','g','o','r','i','e','s',0};
896 IEnumCATEGORYINFOImpl *This;
898 *ret = NULL;
900 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
901 if (!This) return E_OUTOFMEMORY;
903 This->IEnumCATEGORYINFO_iface.lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
904 This->ref = 1;
905 This->lcid = lcid;
906 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
908 *ret = &This->IEnumCATEGORYINFO_iface;
909 return S_OK;
912 /**********************************************************************
913 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
915 * This implementation is not thread-safe. The manager itself is, but
916 * I can't imagine a valid use of an enumerator in several threads.
918 typedef struct
920 IEnumGUID IEnumGUID_iface;
921 LONG ref;
922 struct class_categories *categories;
923 HKEY key;
924 DWORD next_index;
925 } CLSID_IEnumGUIDImpl;
927 static inline CLSID_IEnumGUIDImpl *impl_from_IEnumCLSID(IEnumGUID *iface)
929 return CONTAINING_RECORD(iface, CLSID_IEnumGUIDImpl, IEnumGUID_iface);
932 static HRESULT WINAPI CLSIDEnumGUID_QueryInterface(
933 IEnumGUID *iface,
934 REFIID riid,
935 LPVOID *ppvObj)
937 TRACE("%s\n",debugstr_guid(riid));
939 if (ppvObj == NULL) return E_POINTER;
941 if (IsEqualGUID(riid, &IID_IUnknown) ||
942 IsEqualGUID(riid, &IID_IEnumGUID))
944 *ppvObj = iface;
945 IEnumGUID_AddRef(iface);
946 return S_OK;
949 return E_NOINTERFACE;
952 static ULONG WINAPI CLSIDEnumGUID_AddRef(IEnumGUID *iface)
954 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
955 TRACE("\n");
957 return InterlockedIncrement(&This->ref);
960 static ULONG WINAPI CLSIDEnumGUID_Release(IEnumGUID *iface)
962 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
963 ULONG ref;
965 TRACE("\n");
967 ref = InterlockedDecrement(&This->ref);
968 if (ref == 0) {
969 if (This->key) RegCloseKey(This->key);
970 HeapFree(GetProcessHeap(), 0, This->categories);
971 HeapFree(GetProcessHeap(), 0, This);
972 return 0;
974 return ref;
977 static HRESULT WINAPI CLSIDEnumGUID_Next(
978 IEnumGUID *iface,
979 ULONG celt,
980 GUID *rgelt,
981 ULONG *pceltFetched)
983 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
984 ULONG fetched = 0;
986 TRACE("\n");
988 if (rgelt == NULL) return E_POINTER;
990 if (This->key) while (fetched < celt) {
991 LSTATUS res;
992 HRESULT hr;
993 WCHAR clsid[CHARS_IN_GUID];
994 DWORD cName = CHARS_IN_GUID;
995 HKEY subkey;
997 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
998 NULL, NULL, NULL, NULL);
999 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1000 ++(This->next_index);
1002 hr = CLSIDFromString(clsid, rgelt);
1003 if (FAILED(hr)) continue;
1005 res = open_classes_key(This->key, clsid, KEY_READ, &subkey);
1006 if (res != ERROR_SUCCESS) continue;
1008 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1009 RegCloseKey(subkey);
1010 if (hr != S_OK) continue;
1012 ++fetched;
1013 ++rgelt;
1016 if (pceltFetched) *pceltFetched = fetched;
1017 return fetched == celt ? S_OK : S_FALSE;
1020 static HRESULT WINAPI CLSIDEnumGUID_Skip(
1021 IEnumGUID *iface,
1022 ULONG celt)
1024 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1026 TRACE("\n");
1028 This->next_index += celt;
1029 FIXME("Never returns S_FALSE\n");
1030 return S_OK;
1033 static HRESULT WINAPI CLSIDEnumGUID_Reset(IEnumGUID *iface)
1035 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1037 TRACE("\n");
1039 This->next_index = 0;
1040 return S_OK;
1043 static HRESULT WINAPI CLSIDEnumGUID_Clone(
1044 IEnumGUID *iface,
1045 IEnumGUID **ppenum)
1047 static const WCHAR keynameW[] = {'C','L','S','I','D',0};
1048 CLSID_IEnumGUIDImpl *This = impl_from_IEnumCLSID(iface);
1049 CLSID_IEnumGUIDImpl *cloned;
1051 TRACE("(%p)->(%p)\n", This, ppenum);
1053 if (ppenum == NULL) return E_POINTER;
1055 *ppenum = NULL;
1057 cloned = HeapAlloc(GetProcessHeap(), 0, sizeof(CLSID_IEnumGUIDImpl));
1058 if (cloned == NULL) return E_OUTOFMEMORY;
1060 cloned->IEnumGUID_iface.lpVtbl = This->IEnumGUID_iface.lpVtbl;
1061 cloned->ref = 1;
1063 cloned->categories = HeapAlloc(GetProcessHeap(), 0, This->categories->size);
1064 if (cloned->categories == NULL) {
1065 HeapFree(GetProcessHeap(), 0, cloned);
1066 return E_OUTOFMEMORY;
1068 memcpy(cloned->categories, This->categories, This->categories->size);
1070 cloned->key = NULL;
1071 open_classes_key(HKEY_CLASSES_ROOT, keynameW, KEY_READ, &cloned->key);
1072 cloned->next_index = This->next_index;
1074 *ppenum = &cloned->IEnumGUID_iface;
1075 return S_OK;
1078 static const IEnumGUIDVtbl CLSIDEnumGUIDVtbl =
1080 CLSIDEnumGUID_QueryInterface,
1081 CLSIDEnumGUID_AddRef,
1082 CLSIDEnumGUID_Release,
1083 CLSIDEnumGUID_Next,
1084 CLSIDEnumGUID_Skip,
1085 CLSIDEnumGUID_Reset,
1086 CLSIDEnumGUID_Clone
1089 static HRESULT CLSIDEnumGUID_Construct(struct class_categories *categories, IEnumCLSID **ret)
1091 static const WCHAR keyname[] = {'C','L','S','I','D',0};
1092 CLSID_IEnumGUIDImpl *This;
1094 *ret = NULL;
1096 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1097 if (!This) return E_OUTOFMEMORY;
1099 This->IEnumGUID_iface.lpVtbl = &CLSIDEnumGUIDVtbl;
1100 This->ref = 1;
1101 This->categories = categories;
1102 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
1104 *ret = &This->IEnumGUID_iface;
1106 return S_OK;
1109 /**********************************************************************
1110 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1112 * This implementation is not thread-safe. The manager itself is, but
1113 * I can't imagine a valid use of an enumerator in several threads.
1115 typedef struct
1117 IEnumGUID IEnumGUID_iface;
1118 LONG ref;
1119 WCHAR keyname[68];
1120 HKEY key;
1121 DWORD next_index;
1122 } CATID_IEnumGUIDImpl;
1124 static inline CATID_IEnumGUIDImpl *impl_from_IEnumCATID(IEnumGUID *iface)
1126 return CONTAINING_RECORD(iface, CATID_IEnumGUIDImpl, IEnumGUID_iface);
1129 static HRESULT WINAPI CATIDEnumGUID_QueryInterface(
1130 IEnumGUID *iface,
1131 REFIID riid,
1132 LPVOID *ppvObj)
1134 TRACE("%s\n",debugstr_guid(riid));
1136 if (ppvObj == NULL) return E_POINTER;
1138 if (IsEqualGUID(riid, &IID_IUnknown) ||
1139 IsEqualGUID(riid, &IID_IEnumGUID))
1141 *ppvObj = iface;
1142 IEnumGUID_AddRef(iface);
1143 return S_OK;
1146 return E_NOINTERFACE;
1149 static ULONG WINAPI CATIDEnumGUID_AddRef(IEnumGUID *iface)
1151 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1152 TRACE("\n");
1154 return InterlockedIncrement(&This->ref);
1157 static ULONG WINAPI CATIDEnumGUID_Release(IEnumGUID *iface)
1159 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1160 ULONG ref;
1162 TRACE("\n");
1164 ref = InterlockedDecrement(&This->ref);
1165 if (ref == 0) {
1166 if (This->key) RegCloseKey(This->key);
1167 HeapFree(GetProcessHeap(), 0, This);
1168 return 0;
1170 return ref;
1173 static HRESULT WINAPI CATIDEnumGUID_Next(
1174 IEnumGUID *iface,
1175 ULONG celt,
1176 GUID *rgelt,
1177 ULONG *pceltFetched)
1179 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1180 ULONG fetched = 0;
1182 TRACE("\n");
1184 if (rgelt == NULL) return E_POINTER;
1186 if (This->key) while (fetched < celt) {
1187 LSTATUS res;
1188 HRESULT hr;
1189 WCHAR catid[CHARS_IN_GUID];
1190 DWORD cName = CHARS_IN_GUID;
1192 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1193 NULL, NULL, NULL, NULL);
1194 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1195 ++(This->next_index);
1197 hr = CLSIDFromString(catid, rgelt);
1198 if (FAILED(hr)) continue;
1200 ++fetched;
1201 ++rgelt;
1204 if (pceltFetched) *pceltFetched = fetched;
1205 return fetched == celt ? S_OK : S_FALSE;
1208 static HRESULT WINAPI CATIDEnumGUID_Skip(
1209 IEnumGUID *iface,
1210 ULONG celt)
1212 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1214 TRACE("\n");
1216 This->next_index += celt;
1217 FIXME("Never returns S_FALSE\n");
1218 return S_OK;
1221 static HRESULT WINAPI CATIDEnumGUID_Reset(IEnumGUID *iface)
1223 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1225 TRACE("\n");
1227 This->next_index = 0;
1228 return S_OK;
1231 static HRESULT WINAPI CATIDEnumGUID_Clone(
1232 IEnumGUID *iface,
1233 IEnumGUID **ppenum)
1235 CATID_IEnumGUIDImpl *This = impl_from_IEnumCATID(iface);
1236 CATID_IEnumGUIDImpl *new_this;
1238 TRACE("\n");
1240 if (ppenum == NULL) return E_POINTER;
1242 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1243 if (new_this == NULL) return E_OUTOFMEMORY;
1245 new_this->IEnumGUID_iface.lpVtbl = This->IEnumGUID_iface.lpVtbl;
1246 new_this->ref = 1;
1247 lstrcpyW(new_this->keyname, This->keyname);
1248 /* FIXME: could we more efficiently use DuplicateHandle? */
1249 open_classes_key(HKEY_CLASSES_ROOT, new_this->keyname, KEY_READ, &new_this->key);
1250 new_this->next_index = This->next_index;
1252 *ppenum = &new_this->IEnumGUID_iface;
1253 return S_OK;
1256 static const IEnumGUIDVtbl CATIDEnumGUIDVtbl =
1258 CATIDEnumGUID_QueryInterface,
1259 CATIDEnumGUID_AddRef,
1260 CATIDEnumGUID_Release,
1261 CATIDEnumGUID_Next,
1262 CATIDEnumGUID_Skip,
1263 CATIDEnumGUID_Reset,
1264 CATIDEnumGUID_Clone
1267 static HRESULT CATIDEnumGUID_Construct(REFCLSID rclsid, LPCWSTR postfix, IEnumGUID **ret)
1269 static const WCHAR prefixW[] = {'C','L','S','I','D','\\',0};
1270 WCHAR keyname[100], clsidW[CHARS_IN_GUID];
1271 CATID_IEnumGUIDImpl *This;
1273 *ret = NULL;
1275 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1276 if (!This) return E_OUTOFMEMORY;
1278 StringFromGUID2(rclsid, clsidW, CHARS_IN_GUID);
1280 This->IEnumGUID_iface.lpVtbl = &CATIDEnumGUIDVtbl;
1281 This->ref = 1;
1282 strcpyW(keyname, prefixW);
1283 strcatW(keyname, clsidW);
1284 strcatW(keyname, postfix);
1286 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
1288 *ret = &This->IEnumGUID_iface;
1289 return S_OK;