winemac: Rename some confusingly-named variables.
[wine.git] / dlls / ole32 / comcat.c
blob925938a57a46b53257e0020f35a8c0e3be01811a
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 {
58 LPCWSTR impl_strings;
59 LPCWSTR req_strings;
62 static IEnumCATEGORYINFO *COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid);
63 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *class_categories);
64 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(REFCLSID rclsid, LPCWSTR impl_req);
66 /**********************************************************************
67 * File-scope string constants
69 static const WCHAR comcat_keyname[] = {
70 'C','o','m','p','o','n','e','n','t',' ','C','a','t','e','g','o','r','i','e','s',0 };
71 static const WCHAR impl_keyname[] = {
72 'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
73 static const WCHAR req_keyname[] = {
74 'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
75 static const WCHAR clsid_keyname[] = { 'C','L','S','I','D',0 };
78 /**********************************************************************
79 * COMCAT_RegisterClassCategories
81 static HRESULT COMCAT_RegisterClassCategories(
82 REFCLSID rclsid,
83 LPCWSTR type,
84 ULONG cCategories,
85 const CATID *rgcatid)
87 WCHAR keyname[39];
88 HRESULT res;
89 HKEY clsid_key, class_key, type_key;
91 if (cCategories && rgcatid == NULL) return E_POINTER;
93 /* Format the class key name. */
94 res = StringFromGUID2(rclsid, keyname, 39);
95 if (FAILED(res)) return res;
97 /* Create (or open) the CLSID key. */
98 res = create_classes_key(HKEY_CLASSES_ROOT, clsid_keyname, KEY_READ|KEY_WRITE, &clsid_key);
99 if (res != ERROR_SUCCESS) return E_FAIL;
101 /* Create (or open) the class key. */
102 res = create_classes_key(clsid_key, keyname, KEY_READ|KEY_WRITE, &class_key);
103 if (res == ERROR_SUCCESS) {
104 /* Create (or open) the category type key. */
105 res = create_classes_key(class_key, type, KEY_READ|KEY_WRITE, &type_key);
106 if (res == ERROR_SUCCESS) {
107 for (; cCategories; --cCategories, ++rgcatid) {
108 HKEY key;
110 /* Format the category key name. */
111 res = StringFromGUID2(rgcatid, keyname, 39);
112 if (FAILED(res)) continue;
114 /* Do the register. */
115 res = create_classes_key(type_key, keyname, KEY_READ|KEY_WRITE, &key);
116 if (res == ERROR_SUCCESS) RegCloseKey(key);
118 res = S_OK;
119 } else res = E_FAIL;
120 RegCloseKey(class_key);
121 } else res = E_FAIL;
122 RegCloseKey(clsid_key);
124 return res;
127 /**********************************************************************
128 * COMCAT_UnRegisterClassCategories
130 static HRESULT COMCAT_UnRegisterClassCategories(
131 REFCLSID rclsid,
132 LPCWSTR type,
133 ULONG cCategories,
134 const CATID *rgcatid)
136 WCHAR keyname[68] = { 'C', 'L', 'S', 'I', 'D', '\\' };
137 HRESULT res;
138 HKEY type_key;
140 if (cCategories && rgcatid == NULL) return E_POINTER;
142 /* Format the class category type key name. */
143 res = StringFromGUID2(rclsid, keyname + 6, 39);
144 if (FAILED(res)) return res;
145 keyname[44] = '\\';
146 lstrcpyW(keyname + 45, type);
148 /* Open the class category type key. */
149 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ|KEY_WRITE, &type_key);
150 if (res != ERROR_SUCCESS) return E_FAIL;
152 for (; cCategories; --cCategories, ++rgcatid) {
153 /* Format the category key name. */
154 res = StringFromGUID2(rgcatid, keyname, 39);
155 if (FAILED(res)) continue;
157 /* Do the unregister. */
158 RegDeleteKeyW(type_key, keyname);
160 RegCloseKey(type_key);
162 return S_OK;
165 /**********************************************************************
166 * COMCAT_GetCategoryDesc
168 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
169 ULONG buf_wchars)
171 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
172 WCHAR valname[5];
173 HRESULT res;
174 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
176 if (pszDesc == NULL) return E_INVALIDARG;
178 /* FIXME: lcid comparisons are more complex than this! */
179 wsprintfW(valname, fmt, lcid);
180 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
181 if (res != ERROR_SUCCESS || type != REG_SZ) {
182 FIXME("Simplified lcid comparison\n");
183 return CAT_E_NODESCRIPTION;
185 pszDesc[size / sizeof(WCHAR)] = 0;
187 return S_OK;
190 /**********************************************************************
191 * COMCAT_PrepareClassCategories
193 static struct class_categories *COMCAT_PrepareClassCategories(
194 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
196 struct class_categories *categories;
197 WCHAR *strings;
199 categories = HeapAlloc(
200 GetProcessHeap(), HEAP_ZERO_MEMORY,
201 sizeof(struct class_categories) +
202 ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
203 if (categories == NULL) return categories;
205 strings = (WCHAR *)(categories + 1);
206 categories->impl_strings = strings;
207 while (impl_count--) {
208 StringFromGUID2(impl_catids++, strings, 39);
209 strings += 39;
211 *strings++ = 0;
213 categories->req_strings = strings;
214 while (req_count--) {
215 StringFromGUID2(req_catids++, strings, 39);
216 strings += 39;
218 *strings++ = 0;
220 return categories;
223 /**********************************************************************
224 * COMCAT_IsClassOfCategories
226 static HRESULT COMCAT_IsClassOfCategories(
227 HKEY key,
228 struct class_categories const* categories)
230 HKEY subkey;
231 HRESULT res;
232 DWORD index;
233 LPCWSTR string;
235 /* Check that every given category is implemented by class. */
236 if (*categories->impl_strings) {
237 res = open_classes_key(key, impl_keyname, KEY_READ, &subkey);
238 if (res != ERROR_SUCCESS) return S_FALSE;
239 for (string = categories->impl_strings; *string; string += 39) {
240 HKEY catkey;
241 res = open_classes_key(subkey, string, 0, &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[39];
256 DWORD size = 39;
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 != 38) continue; /* bogus catid in registry */
262 for (string = categories->req_strings; *string; string += 39)
263 if (!strcmpiW(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) {
339 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
340 WCHAR keyname[39];
341 WCHAR valname[9];
342 HKEY cat_key;
344 /* Create (or open) the key for this category. */
345 if (!StringFromGUID2(&rgci->catid, keyname, 39)) 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, fmt, 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[39];
384 /* Delete the key for this category. */
385 if (!StringFromGUID2(rgcatid, keyname, 39)) 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 *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
493 if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
494 IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
495 return S_OK;
498 /**********************************************************************
499 * COMCAT_ICatInformation_GetCategoryDesc
501 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
502 LPCATINFORMATION iface,
503 REFCATID rcatid,
504 LCID lcid,
505 PWCHAR *ppszDesc)
507 WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
508 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
509 'r', 'i', 'e', 's', '\\', 0 };
510 HKEY key;
511 HRESULT res;
513 TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
515 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
517 /* Open the key for this category. */
518 if (!StringFromGUID2(rcatid, keyname + 21, 39)) return E_FAIL;
519 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
520 if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
522 /* Allocate a sensible amount of memory for the description. */
523 *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
524 if (*ppszDesc == NULL) {
525 RegCloseKey(key);
526 return E_OUTOFMEMORY;
529 /* Get the description, and make sure it's null terminated. */
530 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
531 RegCloseKey(key);
532 if (FAILED(res)) {
533 CoTaskMemFree(*ppszDesc);
534 return res;
537 return S_OK;
540 /**********************************************************************
541 * COMCAT_ICatInformation_EnumClassesOfCategories
543 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
544 LPCATINFORMATION iface,
545 ULONG cImplemented,
546 CATID *rgcatidImpl,
547 ULONG cRequired,
548 CATID *rgcatidReq,
549 LPENUMCLSID *ppenumCLSID)
551 struct class_categories *categories;
553 TRACE("\n");
555 if (cImplemented == (ULONG)-1)
556 cImplemented = 0;
557 if (cRequired == (ULONG)-1)
558 cRequired = 0;
560 if (ppenumCLSID == NULL ||
561 (cImplemented && rgcatidImpl == NULL) ||
562 (cRequired && rgcatidReq == NULL)) return E_POINTER;
564 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
565 cRequired, rgcatidReq);
566 if (categories == NULL) return E_OUTOFMEMORY;
567 *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
568 if (*ppenumCLSID == NULL) {
569 HeapFree(GetProcessHeap(), 0, categories);
570 return E_OUTOFMEMORY;
572 IEnumGUID_AddRef(*ppenumCLSID);
573 return S_OK;
576 /**********************************************************************
577 * COMCAT_ICatInformation_IsClassOfCategories
579 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
580 LPCATINFORMATION iface,
581 REFCLSID rclsid,
582 ULONG cImplemented,
583 CATID *rgcatidImpl,
584 ULONG cRequired,
585 CATID *rgcatidReq)
587 WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
588 HRESULT res;
589 struct class_categories *categories;
590 HKEY key;
592 if (TRACE_ON(ole)) {
593 ULONG count;
594 TRACE("CLSID: %s Implemented %u\n",debugstr_guid(rclsid),cImplemented);
595 for (count = 0; count < cImplemented; ++count)
596 TRACE(" %s\n",debugstr_guid(&rgcatidImpl[count]));
597 TRACE("Required %u\n",cRequired);
598 for (count = 0; count < cRequired; ++count)
599 TRACE(" %s\n",debugstr_guid(&rgcatidReq[count]));
602 if ((cImplemented && rgcatidImpl == NULL) ||
603 (cRequired && rgcatidReq == NULL)) return E_POINTER;
605 res = StringFromGUID2(rclsid, keyname + 6, 39);
606 if (FAILED(res)) return res;
608 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
609 cRequired, rgcatidReq);
610 if (categories == NULL) return E_OUTOFMEMORY;
612 res = open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &key);
613 if (res == ERROR_SUCCESS) {
614 res = COMCAT_IsClassOfCategories(key, categories);
615 RegCloseKey(key);
616 } else res = S_FALSE;
618 HeapFree(GetProcessHeap(), 0, categories);
620 return res;
623 /**********************************************************************
624 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
626 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
627 LPCATINFORMATION iface,
628 REFCLSID rclsid,
629 LPENUMCATID *ppenumCATID)
631 static const WCHAR postfix[] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
632 'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
633 'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
635 TRACE("%s\n",debugstr_guid(rclsid));
637 if (rclsid == NULL || ppenumCATID == NULL)
638 return E_POINTER;
640 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
641 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
642 return S_OK;
645 /**********************************************************************
646 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
648 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
649 LPCATINFORMATION iface,
650 REFCLSID rclsid,
651 LPENUMCATID *ppenumCATID)
653 static const WCHAR postfix[] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
654 'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
655 'r', 'i', 'e', 's', 0 };
657 TRACE("%s\n",debugstr_guid(rclsid));
659 if (rclsid == NULL || ppenumCATID == NULL)
660 return E_POINTER;
662 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
663 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
664 return S_OK;
667 /**********************************************************************
668 * COMCAT_ICatRegister_Vtbl
670 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
672 COMCAT_ICatRegister_QueryInterface,
673 COMCAT_ICatRegister_AddRef,
674 COMCAT_ICatRegister_Release,
675 COMCAT_ICatRegister_RegisterCategories,
676 COMCAT_ICatRegister_UnRegisterCategories,
677 COMCAT_ICatRegister_RegisterClassImplCategories,
678 COMCAT_ICatRegister_UnRegisterClassImplCategories,
679 COMCAT_ICatRegister_RegisterClassReqCategories,
680 COMCAT_ICatRegister_UnRegisterClassReqCategories
684 /**********************************************************************
685 * COMCAT_ICatInformation_Vtbl
687 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
689 COMCAT_ICatInformation_QueryInterface,
690 COMCAT_ICatInformation_AddRef,
691 COMCAT_ICatInformation_Release,
692 COMCAT_ICatInformation_EnumCategories,
693 COMCAT_ICatInformation_GetCategoryDesc,
694 COMCAT_ICatInformation_EnumClassesOfCategories,
695 COMCAT_ICatInformation_IsClassOfCategories,
696 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
697 COMCAT_ICatInformation_EnumReqCategoriesOfClass
700 /**********************************************************************
701 * COMCAT_IClassFactory_QueryInterface (also IUnknown)
703 static HRESULT WINAPI COMCAT_IClassFactory_QueryInterface(
704 LPCLASSFACTORY iface,
705 REFIID riid,
706 LPVOID *ppvObj)
708 TRACE("%s\n",debugstr_guid(riid));
710 if (ppvObj == NULL) return E_POINTER;
712 if (IsEqualGUID(riid, &IID_IUnknown) ||
713 IsEqualGUID(riid, &IID_IClassFactory))
715 *ppvObj = iface;
716 IClassFactory_AddRef(iface);
717 return S_OK;
720 return E_NOINTERFACE;
723 /**********************************************************************
724 * COMCAT_IClassFactory_AddRef (also IUnknown)
726 static ULONG WINAPI COMCAT_IClassFactory_AddRef(LPCLASSFACTORY iface)
728 return 2; /* non-heap based object */
731 /**********************************************************************
732 * COMCAT_IClassFactory_Release (also IUnknown)
734 static ULONG WINAPI COMCAT_IClassFactory_Release(LPCLASSFACTORY iface)
736 return 1; /* non-heap based object */
739 /**********************************************************************
740 * COMCAT_IClassFactory_CreateInstance
742 static HRESULT WINAPI COMCAT_IClassFactory_CreateInstance(
743 LPCLASSFACTORY iface,
744 LPUNKNOWN pUnkOuter,
745 REFIID riid,
746 LPVOID *ppvObj)
748 HRESULT res;
749 TRACE("%s\n",debugstr_guid(riid));
751 if (ppvObj == NULL) return E_POINTER;
753 /* Don't support aggregation (Windows doesn't) */
754 if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
756 res = ICatRegister_QueryInterface(&COMCAT_ComCatMgr.ICatRegister_iface, riid, ppvObj);
757 if (SUCCEEDED(res)) {
758 return res;
761 return CLASS_E_CLASSNOTAVAILABLE;
764 /**********************************************************************
765 * COMCAT_IClassFactory_LockServer
767 static HRESULT WINAPI COMCAT_IClassFactory_LockServer(
768 LPCLASSFACTORY iface,
769 BOOL fLock)
771 FIXME("(%d), stub!\n",fLock);
772 return S_OK;
775 /**********************************************************************
776 * static ClassFactory instance
778 static const IClassFactoryVtbl ComCatCFVtbl =
780 COMCAT_IClassFactory_QueryInterface,
781 COMCAT_IClassFactory_AddRef,
782 COMCAT_IClassFactory_Release,
783 COMCAT_IClassFactory_CreateInstance,
784 COMCAT_IClassFactory_LockServer
787 static const IClassFactoryVtbl *ComCatCF = &ComCatCFVtbl;
789 HRESULT ComCatCF_Create(REFIID riid, LPVOID *ppv)
791 return IClassFactory_QueryInterface((IClassFactory *)&ComCatCF, riid, ppv);
794 /**********************************************************************
795 * IEnumCATEGORYINFO implementation
797 * This implementation is not thread-safe. The manager itself is, but
798 * I can't imagine a valid use of an enumerator in several threads.
800 typedef struct
802 IEnumCATEGORYINFO IEnumCATEGORYINFO_iface;
803 LONG ref;
804 LCID lcid;
805 HKEY key;
806 DWORD next_index;
807 } IEnumCATEGORYINFOImpl;
809 static inline IEnumCATEGORYINFOImpl *impl_from_IEnumCATEGORYINFO(IEnumCATEGORYINFO *iface)
811 return CONTAINING_RECORD(iface, IEnumCATEGORYINFOImpl, IEnumCATEGORYINFO_iface);
814 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(IEnumCATEGORYINFO *iface)
816 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
818 TRACE("\n");
820 return InterlockedIncrement(&This->ref);
823 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
824 IEnumCATEGORYINFO *iface,
825 REFIID riid,
826 LPVOID *ppvObj)
828 TRACE("%s\n",debugstr_guid(riid));
830 if (ppvObj == NULL) return E_POINTER;
832 if (IsEqualGUID(riid, &IID_IUnknown) ||
833 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
835 *ppvObj = iface;
836 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
837 return S_OK;
840 return E_NOINTERFACE;
843 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(IEnumCATEGORYINFO *iface)
845 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
846 ULONG ref;
848 TRACE("\n");
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_IEnumCATEGORYINFO_Next(
860 IEnumCATEGORYINFO *iface,
861 ULONG celt,
862 CATEGORYINFO *rgelt,
863 ULONG *pceltFetched)
865 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
866 ULONG fetched = 0;
868 TRACE("\n");
870 if (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;
877 HKEY subkey;
879 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
880 NULL, NULL, NULL, NULL);
881 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
882 ++(This->next_index);
884 hr = CLSIDFromString(catid, &rgelt->catid);
885 if (FAILED(hr)) continue;
887 res = open_classes_key(This->key, catid, KEY_READ, &subkey);
888 if (res != ERROR_SUCCESS) continue;
890 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
891 rgelt->szDescription, 128);
892 RegCloseKey(subkey);
893 if (FAILED(hr)) continue;
895 rgelt->lcid = This->lcid;
896 ++fetched;
897 ++rgelt;
900 if (pceltFetched) *pceltFetched = fetched;
901 return fetched == celt ? S_OK : S_FALSE;
904 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
905 IEnumCATEGORYINFO *iface,
906 ULONG celt)
908 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
910 TRACE("\n");
912 This->next_index += celt;
913 /* This should return S_FALSE when there aren't celt elems to skip. */
914 return S_OK;
917 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(IEnumCATEGORYINFO *iface)
919 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
921 TRACE("\n");
923 This->next_index = 0;
924 return S_OK;
927 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
928 IEnumCATEGORYINFO *iface,
929 IEnumCATEGORYINFO **ppenum)
931 IEnumCATEGORYINFOImpl *This = impl_from_IEnumCATEGORYINFO(iface);
932 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
933 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
934 'r', 'i', 'e', 's', 0 };
935 IEnumCATEGORYINFOImpl *new_this;
937 TRACE("\n");
939 if (ppenum == NULL) return E_POINTER;
941 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
942 if (new_this == NULL) return E_OUTOFMEMORY;
944 new_this->IEnumCATEGORYINFO_iface = This->IEnumCATEGORYINFO_iface;
945 new_this->ref = 1;
946 new_this->lcid = This->lcid;
947 /* FIXME: could we more efficiently use DuplicateHandle? */
948 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &new_this->key);
949 new_this->next_index = This->next_index;
951 *ppenum = &new_this->IEnumCATEGORYINFO_iface;
952 return S_OK;
955 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
957 COMCAT_IEnumCATEGORYINFO_QueryInterface,
958 COMCAT_IEnumCATEGORYINFO_AddRef,
959 COMCAT_IEnumCATEGORYINFO_Release,
960 COMCAT_IEnumCATEGORYINFO_Next,
961 COMCAT_IEnumCATEGORYINFO_Skip,
962 COMCAT_IEnumCATEGORYINFO_Reset,
963 COMCAT_IEnumCATEGORYINFO_Clone
966 static IEnumCATEGORYINFO *COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
968 IEnumCATEGORYINFOImpl *This;
970 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
971 if (This) {
972 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
973 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
974 'r', 'i', 'e', 's', 0 };
976 This->IEnumCATEGORYINFO_iface.lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
977 This->lcid = lcid;
978 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
980 return &This->IEnumCATEGORYINFO_iface;
983 /**********************************************************************
984 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
986 * This implementation is not thread-safe. The manager itself is, but
987 * I can't imagine a valid use of an enumerator in several threads.
989 typedef struct
991 const IEnumGUIDVtbl *lpVtbl;
992 LONG ref;
993 struct class_categories *categories;
994 HKEY key;
995 DWORD next_index;
996 } CLSID_IEnumGUIDImpl;
998 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
1000 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1001 TRACE("\n");
1003 return InterlockedIncrement(&This->ref);
1006 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
1007 LPENUMGUID iface,
1008 REFIID riid,
1009 LPVOID *ppvObj)
1011 TRACE("%s\n",debugstr_guid(riid));
1013 if (ppvObj == NULL) return E_POINTER;
1015 if (IsEqualGUID(riid, &IID_IUnknown) ||
1016 IsEqualGUID(riid, &IID_IEnumGUID))
1018 *ppvObj = iface;
1019 COMCAT_CLSID_IEnumGUID_AddRef(iface);
1020 return S_OK;
1023 return E_NOINTERFACE;
1026 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
1028 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1029 ULONG ref;
1031 TRACE("\n");
1033 ref = InterlockedDecrement(&This->ref);
1034 if (ref == 0) {
1035 if (This->key) RegCloseKey(This->key);
1036 HeapFree(GetProcessHeap(), 0, This->categories);
1037 HeapFree(GetProcessHeap(), 0, This);
1038 return 0;
1040 return ref;
1043 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
1044 LPENUMGUID iface,
1045 ULONG celt,
1046 GUID *rgelt,
1047 ULONG *pceltFetched)
1049 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1050 ULONG fetched = 0;
1052 TRACE("\n");
1054 if (rgelt == NULL) return E_POINTER;
1056 if (This->key) while (fetched < celt) {
1057 LSTATUS res;
1058 HRESULT hr;
1059 WCHAR clsid[39];
1060 DWORD cName = 39;
1061 HKEY subkey;
1063 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
1064 NULL, NULL, NULL, NULL);
1065 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1066 ++(This->next_index);
1068 hr = CLSIDFromString(clsid, rgelt);
1069 if (FAILED(hr)) continue;
1071 res = open_classes_key(This->key, clsid, KEY_READ, &subkey);
1072 if (res != ERROR_SUCCESS) continue;
1074 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1075 RegCloseKey(subkey);
1076 if (hr != S_OK) continue;
1078 ++fetched;
1079 ++rgelt;
1082 if (pceltFetched) *pceltFetched = fetched;
1083 return fetched == celt ? S_OK : S_FALSE;
1086 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
1087 LPENUMGUID iface,
1088 ULONG celt)
1090 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1092 TRACE("\n");
1094 This->next_index += celt;
1095 FIXME("Never returns S_FALSE\n");
1096 return S_OK;
1099 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
1101 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1103 TRACE("\n");
1105 This->next_index = 0;
1106 return S_OK;
1109 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
1110 LPENUMGUID iface,
1111 IEnumGUID **ppenum)
1113 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1114 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1115 CLSID_IEnumGUIDImpl *new_this;
1116 DWORD size;
1118 TRACE("\n");
1120 if (ppenum == NULL) return E_POINTER;
1122 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1123 if (new_this == NULL) return E_OUTOFMEMORY;
1125 new_this->lpVtbl = This->lpVtbl;
1126 new_this->ref = 1;
1127 size = HeapSize(GetProcessHeap(), 0, This->categories);
1128 new_this->categories =
1129 HeapAlloc(GetProcessHeap(), 0, size);
1130 if (new_this->categories == NULL) {
1131 HeapFree(GetProcessHeap(), 0, new_this);
1132 return E_OUTOFMEMORY;
1134 memcpy(new_this->categories, This->categories, size);
1135 /* FIXME: could we more efficiently use DuplicateHandle? */
1136 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &new_this->key);
1137 new_this->next_index = This->next_index;
1139 *ppenum = (LPENUMGUID)new_this;
1140 return S_OK;
1143 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
1145 COMCAT_CLSID_IEnumGUID_QueryInterface,
1146 COMCAT_CLSID_IEnumGUID_AddRef,
1147 COMCAT_CLSID_IEnumGUID_Release,
1148 COMCAT_CLSID_IEnumGUID_Next,
1149 COMCAT_CLSID_IEnumGUID_Skip,
1150 COMCAT_CLSID_IEnumGUID_Reset,
1151 COMCAT_CLSID_IEnumGUID_Clone
1154 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
1156 CLSID_IEnumGUIDImpl *This;
1158 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1159 if (This) {
1160 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1162 This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
1163 This->categories = categories;
1164 open_classes_key(HKEY_CLASSES_ROOT, keyname, KEY_READ, &This->key);
1166 return (LPENUMGUID)This;
1169 /**********************************************************************
1170 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1172 * This implementation is not thread-safe. The manager itself is, but
1173 * I can't imagine a valid use of an enumerator in several threads.
1175 typedef struct
1177 const IEnumGUIDVtbl *lpVtbl;
1178 LONG ref;
1179 WCHAR keyname[68];
1180 HKEY key;
1181 DWORD next_index;
1182 } CATID_IEnumGUIDImpl;
1184 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
1186 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1187 TRACE("\n");
1189 return InterlockedIncrement(&This->ref);
1192 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
1193 LPENUMGUID iface,
1194 REFIID riid,
1195 LPVOID *ppvObj)
1197 TRACE("%s\n",debugstr_guid(riid));
1199 if (ppvObj == NULL) return E_POINTER;
1201 if (IsEqualGUID(riid, &IID_IUnknown) ||
1202 IsEqualGUID(riid, &IID_IEnumGUID))
1204 *ppvObj = iface;
1205 COMCAT_CATID_IEnumGUID_AddRef(iface);
1206 return S_OK;
1209 return E_NOINTERFACE;
1212 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
1214 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1215 ULONG ref;
1217 TRACE("\n");
1219 ref = InterlockedDecrement(&This->ref);
1220 if (ref == 0) {
1221 if (This->key) RegCloseKey(This->key);
1222 HeapFree(GetProcessHeap(), 0, This);
1223 return 0;
1225 return ref;
1228 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
1229 LPENUMGUID iface,
1230 ULONG celt,
1231 GUID *rgelt,
1232 ULONG *pceltFetched)
1234 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1235 ULONG fetched = 0;
1237 TRACE("\n");
1239 if (rgelt == NULL) return E_POINTER;
1241 if (This->key) while (fetched < celt) {
1242 LSTATUS res;
1243 HRESULT hr;
1244 WCHAR catid[39];
1245 DWORD cName = 39;
1247 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1248 NULL, NULL, NULL, NULL);
1249 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1250 ++(This->next_index);
1252 hr = CLSIDFromString(catid, rgelt);
1253 if (FAILED(hr)) continue;
1255 ++fetched;
1256 ++rgelt;
1259 if (pceltFetched) *pceltFetched = fetched;
1260 return fetched == celt ? S_OK : S_FALSE;
1263 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
1264 LPENUMGUID iface,
1265 ULONG celt)
1267 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1269 TRACE("\n");
1271 This->next_index += celt;
1272 FIXME("Never returns S_FALSE\n");
1273 return S_OK;
1276 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
1278 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1280 TRACE("\n");
1282 This->next_index = 0;
1283 return S_OK;
1286 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
1287 LPENUMGUID iface,
1288 IEnumGUID **ppenum)
1290 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1291 CATID_IEnumGUIDImpl *new_this;
1293 TRACE("\n");
1295 if (ppenum == NULL) return E_POINTER;
1297 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1298 if (new_this == NULL) return E_OUTOFMEMORY;
1300 new_this->lpVtbl = This->lpVtbl;
1301 new_this->ref = 1;
1302 lstrcpyW(new_this->keyname, This->keyname);
1303 /* FIXME: could we more efficiently use DuplicateHandle? */
1304 open_classes_key(HKEY_CLASSES_ROOT, new_this->keyname, KEY_READ, &new_this->key);
1305 new_this->next_index = This->next_index;
1307 *ppenum = (LPENUMGUID)new_this;
1308 return S_OK;
1311 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
1313 COMCAT_CATID_IEnumGUID_QueryInterface,
1314 COMCAT_CATID_IEnumGUID_AddRef,
1315 COMCAT_CATID_IEnumGUID_Release,
1316 COMCAT_CATID_IEnumGUID_Next,
1317 COMCAT_CATID_IEnumGUID_Skip,
1318 COMCAT_CATID_IEnumGUID_Reset,
1319 COMCAT_CATID_IEnumGUID_Clone
1322 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
1323 REFCLSID rclsid, LPCWSTR postfix)
1325 CATID_IEnumGUIDImpl *This;
1327 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1328 if (This) {
1329 WCHAR prefix[] = { 'C', 'L', 'S', 'I', 'D', '\\' };
1331 This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
1332 memcpy(This->keyname, prefix, sizeof(prefix));
1333 StringFromGUID2(rclsid, This->keyname + 6, 39);
1334 lstrcpyW(This->keyname + 44, postfix);
1335 open_classes_key(HKEY_CLASSES_ROOT, This->keyname, KEY_READ, &This->key);
1337 return (LPENUMGUID)This;