msctf: Make ThreadFocusHookProc() static.
[wine/multimedia.git] / dlls / ole32 / comcat.c
blobb8060bdefca71094687a4195aeec724d6f05b3fc
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 "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 typedef struct
41 const ICatRegisterVtbl *lpVtbl;
42 const ICatInformationVtbl *infVtbl;
43 } ComCatMgrImpl;
45 struct class_categories {
46 LPCWSTR impl_strings;
47 LPCWSTR req_strings;
50 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid);
51 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *class_categories);
52 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(REFCLSID rclsid, LPCWSTR impl_req);
54 /**********************************************************************
55 * File-scope string constants
57 static const WCHAR comcat_keyname[] = {
58 'C','o','m','p','o','n','e','n','t',' ','C','a','t','e','g','o','r','i','e','s',0 };
59 static const WCHAR impl_keyname[] = {
60 'I','m','p','l','e','m','e','n','t','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
61 static const WCHAR req_keyname[] = {
62 'R','e','q','u','i','r','e','d',' ','C','a','t','e','g','o','r','i','e','s',0 };
63 static const WCHAR clsid_keyname[] = { 'C','L','S','I','D',0 };
66 static inline ComCatMgrImpl *impl_from_ICatInformation( ICatInformation *iface )
68 return (ComCatMgrImpl *)((char*)iface - FIELD_OFFSET(ComCatMgrImpl, infVtbl));
72 /**********************************************************************
73 * COMCAT_RegisterClassCategories
75 static HRESULT COMCAT_RegisterClassCategories(
76 REFCLSID rclsid,
77 LPCWSTR type,
78 ULONG cCategories,
79 const CATID *rgcatid)
81 WCHAR keyname[39];
82 HRESULT res;
83 HKEY clsid_key, class_key, type_key;
85 if (cCategories && rgcatid == NULL) return E_POINTER;
87 /* Format the class key name. */
88 res = StringFromGUID2(rclsid, keyname, 39);
89 if (FAILED(res)) return res;
91 /* Create (or open) the CLSID key. */
92 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
93 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
94 if (res != ERROR_SUCCESS) return E_FAIL;
96 /* Create (or open) the class key. */
97 res = RegCreateKeyExW(clsid_key, keyname, 0, NULL, 0,
98 KEY_READ | KEY_WRITE, NULL, &class_key, NULL);
99 if (res == ERROR_SUCCESS) {
100 /* Create (or open) the category type key. */
101 res = RegCreateKeyExW(class_key, type, 0, NULL, 0,
102 KEY_READ | KEY_WRITE, NULL, &type_key, NULL);
103 if (res == ERROR_SUCCESS) {
104 for (; cCategories; --cCategories, ++rgcatid) {
105 HKEY key;
107 /* Format the category key name. */
108 res = StringFromGUID2(rgcatid, keyname, 39);
109 if (FAILED(res)) continue;
111 /* Do the register. */
112 res = RegCreateKeyExW(type_key, keyname, 0, NULL, 0,
113 KEY_READ | KEY_WRITE, NULL, &key, NULL);
114 if (res == ERROR_SUCCESS) RegCloseKey(key);
116 res = S_OK;
117 } else res = E_FAIL;
118 RegCloseKey(class_key);
119 } else res = E_FAIL;
120 RegCloseKey(clsid_key);
122 return res;
125 /**********************************************************************
126 * COMCAT_UnRegisterClassCategories
128 static HRESULT COMCAT_UnRegisterClassCategories(
129 REFCLSID rclsid,
130 LPCWSTR type,
131 ULONG cCategories,
132 const CATID *rgcatid)
134 WCHAR keyname[68] = { 'C', 'L', 'S', 'I', 'D', '\\' };
135 HRESULT res;
136 HKEY type_key;
138 if (cCategories && rgcatid == NULL) return E_POINTER;
140 /* Format the class category type key name. */
141 res = StringFromGUID2(rclsid, keyname + 6, 39);
142 if (FAILED(res)) return res;
143 keyname[44] = '\\';
144 lstrcpyW(keyname + 45, type);
146 /* Open the class category type key. */
147 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0,
148 KEY_READ | KEY_WRITE, &type_key);
149 if (res != ERROR_SUCCESS) return E_FAIL;
151 for (; cCategories; --cCategories, ++rgcatid) {
152 /* Format the category key name. */
153 res = StringFromGUID2(rgcatid, keyname, 39);
154 if (FAILED(res)) continue;
156 /* Do the unregister. */
157 RegDeleteKeyW(type_key, keyname);
159 RegCloseKey(type_key);
161 return S_OK;
164 /**********************************************************************
165 * COMCAT_GetCategoryDesc
167 static HRESULT COMCAT_GetCategoryDesc(HKEY key, LCID lcid, PWCHAR pszDesc,
168 ULONG buf_wchars)
170 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
171 WCHAR valname[5];
172 HRESULT res;
173 DWORD type, size = (buf_wchars - 1) * sizeof(WCHAR);
175 if (pszDesc == NULL) return E_INVALIDARG;
177 /* FIXME: lcid comparisons are more complex than this! */
178 wsprintfW(valname, fmt, lcid);
179 res = RegQueryValueExW(key, valname, 0, &type, (LPBYTE)pszDesc, &size);
180 if (res != ERROR_SUCCESS || type != REG_SZ) {
181 FIXME("Simplified lcid comparison\n");
182 return CAT_E_NODESCRIPTION;
184 pszDesc[size / sizeof(WCHAR)] = 0;
186 return S_OK;
189 /**********************************************************************
190 * COMCAT_PrepareClassCategories
192 static struct class_categories *COMCAT_PrepareClassCategories(
193 ULONG impl_count, const CATID *impl_catids, ULONG req_count, const CATID *req_catids)
195 struct class_categories *categories;
196 WCHAR *strings;
198 categories = HeapAlloc(
199 GetProcessHeap(), HEAP_ZERO_MEMORY,
200 sizeof(struct class_categories) +
201 ((impl_count + req_count) * 39 + 2) * sizeof(WCHAR));
202 if (categories == NULL) return categories;
204 strings = (WCHAR *)(categories + 1);
205 categories->impl_strings = strings;
206 while (impl_count--) {
207 StringFromGUID2(impl_catids++, strings, 39);
208 strings += 39;
210 *strings++ = 0;
212 categories->req_strings = strings;
213 while (req_count--) {
214 StringFromGUID2(req_catids++, strings, 39);
215 strings += 39;
217 *strings++ = 0;
219 return categories;
222 /**********************************************************************
223 * COMCAT_IsClassOfCategories
225 static HRESULT COMCAT_IsClassOfCategories(
226 HKEY key,
227 struct class_categories const* categories)
229 HKEY subkey;
230 HRESULT res;
231 DWORD index;
232 LPCWSTR string;
234 /* Check that every given category is implemented by class. */
235 res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
236 if (res != ERROR_SUCCESS) return S_FALSE;
237 for (string = categories->impl_strings; *string; string += 39) {
238 HKEY catkey;
239 res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
240 if (res != ERROR_SUCCESS) {
241 RegCloseKey(subkey);
242 return S_FALSE;
244 RegCloseKey(catkey);
246 RegCloseKey(subkey);
248 /* Check that all categories required by class are given. */
249 res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
250 if (res == ERROR_SUCCESS) {
251 for (index = 0; ; ++index) {
252 WCHAR keyname[39];
253 DWORD size = 39;
255 res = RegEnumKeyExW(subkey, index, keyname, &size,
256 NULL, NULL, NULL, NULL);
257 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
258 if (size != 38) continue; /* bogus catid in registry */
259 for (string = categories->req_strings; *string; string += 39)
260 if (!strcmpiW(string, keyname)) break;
261 if (!*string) {
262 RegCloseKey(subkey);
263 return S_FALSE;
266 RegCloseKey(subkey);
269 return S_OK;
272 /**********************************************************************
273 * COMCAT_ICatRegister_QueryInterface
275 static HRESULT WINAPI COMCAT_ICatRegister_QueryInterface(
276 LPCATREGISTER iface,
277 REFIID riid,
278 LPVOID *ppvObj)
280 ComCatMgrImpl *This = (ComCatMgrImpl *)iface;
281 TRACE("%s\n",debugstr_guid(riid));
283 if (ppvObj == NULL) return E_POINTER;
285 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ICatRegister)) {
286 *ppvObj = iface;
287 IUnknown_AddRef(iface);
288 return S_OK;
291 if (IsEqualGUID(riid, &IID_ICatInformation)) {
292 *ppvObj = &This->infVtbl;
293 IUnknown_AddRef(iface);
294 return S_OK;
297 return E_NOINTERFACE;
300 /**********************************************************************
301 * COMCAT_ICatRegister_AddRef
303 static ULONG WINAPI COMCAT_ICatRegister_AddRef(LPCATREGISTER iface)
305 return 2; /* non-heap based object */
308 /**********************************************************************
309 * COMCAT_ICatRegister_Release
311 static ULONG WINAPI COMCAT_ICatRegister_Release(LPCATREGISTER iface)
313 return 1; /* non-heap based object */
316 /**********************************************************************
317 * COMCAT_ICatRegister_RegisterCategories
319 static HRESULT WINAPI COMCAT_ICatRegister_RegisterCategories(
320 LPCATREGISTER iface,
321 ULONG cCategories,
322 CATEGORYINFO *rgci)
324 HKEY comcat_key;
325 HRESULT res;
327 TRACE("\n");
329 if (cCategories && rgci == NULL)
330 return E_POINTER;
332 /* Create (or open) the component categories key. */
333 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0, NULL, 0,
334 KEY_READ | KEY_WRITE, NULL, &comcat_key, NULL);
335 if (res != ERROR_SUCCESS) return E_FAIL;
337 for (; cCategories; --cCategories, ++rgci) {
338 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
339 WCHAR keyname[39];
340 WCHAR valname[9];
341 HKEY cat_key;
343 /* Create (or open) the key for this category. */
344 if (!StringFromGUID2(&rgci->catid, keyname, 39)) continue;
345 res = RegCreateKeyExW(comcat_key, keyname, 0, NULL, 0,
346 KEY_READ | KEY_WRITE, NULL, &cat_key, NULL);
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,
352 (CONST BYTE*)(rgci->szDescription),
353 (lstrlenW(rgci->szDescription) + 1) * sizeof(WCHAR));
355 RegCloseKey(cat_key);
358 RegCloseKey(comcat_key);
359 return S_OK;
362 /**********************************************************************
363 * COMCAT_ICatRegister_UnRegisterCategories
365 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterCategories(
366 LPCATREGISTER iface,
367 ULONG cCategories,
368 CATID *rgcatid)
370 HKEY comcat_key;
371 HRESULT res;
373 TRACE("\n");
375 if (cCategories && rgcatid == NULL)
376 return E_POINTER;
378 /* Open the component categories key. */
379 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0,
380 KEY_READ | KEY_WRITE, &comcat_key);
381 if (res != ERROR_SUCCESS) return E_FAIL;
383 for (; cCategories; --cCategories, ++rgcatid) {
384 WCHAR keyname[39];
386 /* Delete the key for this category. */
387 if (!StringFromGUID2(rgcatid, keyname, 39)) continue;
388 RegDeleteKeyW(comcat_key, keyname);
391 RegCloseKey(comcat_key);
392 return S_OK;
395 /**********************************************************************
396 * COMCAT_ICatRegister_RegisterClassImplCategories
398 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassImplCategories(
399 LPCATREGISTER iface,
400 REFCLSID rclsid,
401 ULONG cCategories,
402 CATID *rgcatid)
404 TRACE("\n");
406 return COMCAT_RegisterClassCategories(
407 rclsid, impl_keyname, cCategories, rgcatid);
410 /**********************************************************************
411 * COMCAT_ICatRegister_UnRegisterClassImplCategories
413 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassImplCategories(
414 LPCATREGISTER iface,
415 REFCLSID rclsid,
416 ULONG cCategories,
417 CATID *rgcatid)
419 TRACE("\n");
421 return COMCAT_UnRegisterClassCategories(
422 rclsid, impl_keyname, cCategories, rgcatid);
425 /**********************************************************************
426 * COMCAT_ICatRegister_RegisterClassReqCategories
428 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassReqCategories(
429 LPCATREGISTER iface,
430 REFCLSID rclsid,
431 ULONG cCategories,
432 CATID *rgcatid)
434 TRACE("\n");
436 return COMCAT_RegisterClassCategories(
437 rclsid, req_keyname, cCategories, rgcatid);
440 /**********************************************************************
441 * COMCAT_ICatRegister_UnRegisterClassReqCategories
443 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassReqCategories(
444 LPCATREGISTER iface,
445 REFCLSID rclsid,
446 ULONG cCategories,
447 CATID *rgcatid)
449 TRACE("\n");
451 return COMCAT_UnRegisterClassCategories(
452 rclsid, req_keyname, cCategories, rgcatid);
455 /**********************************************************************
456 * COMCAT_ICatInformation_QueryInterface
458 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
459 LPCATINFORMATION iface,
460 REFIID riid,
461 LPVOID *ppvObj)
463 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
464 return IUnknown_QueryInterface((LPUNKNOWN)This, riid, ppvObj);
467 /**********************************************************************
468 * COMCAT_ICatInformation_AddRef
470 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
472 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
473 return IUnknown_AddRef((LPUNKNOWN)This);
476 /**********************************************************************
477 * COMCAT_ICatInformation_Release
479 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
481 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
482 return IUnknown_Release((LPUNKNOWN)This);
485 /**********************************************************************
486 * COMCAT_ICatInformation_EnumCategories
488 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
489 LPCATINFORMATION iface,
490 LCID lcid,
491 LPENUMCATEGORYINFO *ppenumCatInfo)
493 TRACE("\n");
495 if (ppenumCatInfo == NULL) return E_POINTER;
497 *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
498 if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
499 IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
500 return S_OK;
503 /**********************************************************************
504 * COMCAT_ICatInformation_GetCategoryDesc
506 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
507 LPCATINFORMATION iface,
508 REFCATID rcatid,
509 LCID lcid,
510 PWCHAR *ppszDesc)
512 WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
513 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
514 'r', 'i', 'e', 's', '\\', 0 };
515 HKEY key;
516 HRESULT res;
518 TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
520 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
522 /* Open the key for this category. */
523 if (!StringFromGUID2(rcatid, keyname + 21, 39)) return E_FAIL;
524 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
525 if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
527 /* Allocate a sensible amount of memory for the description. */
528 *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
529 if (*ppszDesc == NULL) {
530 RegCloseKey(key);
531 return E_OUTOFMEMORY;
534 /* Get the description, and make sure it's null terminated. */
535 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
536 RegCloseKey(key);
537 if (FAILED(res)) {
538 CoTaskMemFree(*ppszDesc);
539 return res;
542 return S_OK;
545 /**********************************************************************
546 * COMCAT_ICatInformation_EnumClassesOfCategories
548 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
549 LPCATINFORMATION iface,
550 ULONG cImplemented,
551 CATID *rgcatidImpl,
552 ULONG cRequired,
553 CATID *rgcatidReq,
554 LPENUMCLSID *ppenumCLSID)
556 struct class_categories *categories;
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;
572 *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
573 if (*ppenumCLSID == NULL) {
574 HeapFree(GetProcessHeap(), 0, categories);
575 return E_OUTOFMEMORY;
577 IEnumGUID_AddRef(*ppenumCLSID);
578 return S_OK;
581 /**********************************************************************
582 * COMCAT_ICatInformation_IsClassOfCategories
584 static HRESULT WINAPI COMCAT_ICatInformation_IsClassOfCategories(
585 LPCATINFORMATION iface,
586 REFCLSID rclsid,
587 ULONG cImplemented,
588 CATID *rgcatidImpl,
589 ULONG cRequired,
590 CATID *rgcatidReq)
592 WCHAR keyname[45] = { 'C', 'L', 'S', 'I', 'D', '\\', 0 };
593 HRESULT res;
594 struct class_categories *categories;
595 HKEY key;
597 if (WINE_TRACE_ON(ole)) {
598 ULONG count;
599 TRACE("CLSID: %s Implemented %u\n",debugstr_guid(rclsid),cImplemented);
600 for (count = 0; count < cImplemented; ++count)
601 TRACE(" %s\n",debugstr_guid(&rgcatidImpl[count]));
602 TRACE("Required %u\n",cRequired);
603 for (count = 0; count < cRequired; ++count)
604 TRACE(" %s\n",debugstr_guid(&rgcatidReq[count]));
607 if ((cImplemented && rgcatidImpl == NULL) ||
608 (cRequired && rgcatidReq == NULL)) return E_POINTER;
610 res = StringFromGUID2(rclsid, keyname + 6, 39);
611 if (FAILED(res)) return res;
613 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
614 cRequired, rgcatidReq);
615 if (categories == NULL) return E_OUTOFMEMORY;
617 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
618 if (res == ERROR_SUCCESS) {
619 res = COMCAT_IsClassOfCategories(key, categories);
620 RegCloseKey(key);
621 } else res = S_FALSE;
623 HeapFree(GetProcessHeap(), 0, categories);
625 return res;
628 /**********************************************************************
629 * COMCAT_ICatInformation_EnumImplCategoriesOfClass
631 static HRESULT WINAPI COMCAT_ICatInformation_EnumImplCategoriesOfClass(
632 LPCATINFORMATION iface,
633 REFCLSID rclsid,
634 LPENUMCATID *ppenumCATID)
636 static const WCHAR postfix[24] = { '\\', 'I', 'm', 'p', 'l', 'e', 'm', 'e',
637 'n', 't', 'e', 'd', ' ', 'C', 'a', 't',
638 'e', 'g', 'o', 'r', 'i', 'e', 's', 0 };
640 TRACE("%s\n",debugstr_guid(rclsid));
642 if (rclsid == NULL || ppenumCATID == NULL)
643 return E_POINTER;
645 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
646 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
647 return S_OK;
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[21] = { '\\', '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 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
668 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
669 return S_OK;
672 /**********************************************************************
673 * COMCAT_ICatRegister_Vtbl
675 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
677 COMCAT_ICatRegister_QueryInterface,
678 COMCAT_ICatRegister_AddRef,
679 COMCAT_ICatRegister_Release,
680 COMCAT_ICatRegister_RegisterCategories,
681 COMCAT_ICatRegister_UnRegisterCategories,
682 COMCAT_ICatRegister_RegisterClassImplCategories,
683 COMCAT_ICatRegister_UnRegisterClassImplCategories,
684 COMCAT_ICatRegister_RegisterClassReqCategories,
685 COMCAT_ICatRegister_UnRegisterClassReqCategories
689 /**********************************************************************
690 * COMCAT_ICatInformation_Vtbl
692 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
694 COMCAT_ICatInformation_QueryInterface,
695 COMCAT_ICatInformation_AddRef,
696 COMCAT_ICatInformation_Release,
697 COMCAT_ICatInformation_EnumCategories,
698 COMCAT_ICatInformation_GetCategoryDesc,
699 COMCAT_ICatInformation_EnumClassesOfCategories,
700 COMCAT_ICatInformation_IsClassOfCategories,
701 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
702 COMCAT_ICatInformation_EnumReqCategoriesOfClass
705 /**********************************************************************
706 * static ComCatMgr instance
708 static ComCatMgrImpl COMCAT_ComCatMgr =
710 &COMCAT_ICatRegister_Vtbl,
711 &COMCAT_ICatInformation_Vtbl
714 /**********************************************************************
715 * COMCAT_IClassFactory_QueryInterface (also IUnknown)
717 static HRESULT WINAPI COMCAT_IClassFactory_QueryInterface(
718 LPCLASSFACTORY iface,
719 REFIID riid,
720 LPVOID *ppvObj)
722 TRACE("%s\n",debugstr_guid(riid));
724 if (ppvObj == NULL) return E_POINTER;
726 if (IsEqualGUID(riid, &IID_IUnknown) ||
727 IsEqualGUID(riid, &IID_IClassFactory))
729 *ppvObj = iface;
730 IUnknown_AddRef(iface);
731 return S_OK;
734 return E_NOINTERFACE;
737 /**********************************************************************
738 * COMCAT_IClassFactory_AddRef (also IUnknown)
740 static ULONG WINAPI COMCAT_IClassFactory_AddRef(LPCLASSFACTORY iface)
742 return 2; /* non-heap based object */
745 /**********************************************************************
746 * COMCAT_IClassFactory_Release (also IUnknown)
748 static ULONG WINAPI COMCAT_IClassFactory_Release(LPCLASSFACTORY iface)
750 return 1; /* non-heap based object */
753 /**********************************************************************
754 * COMCAT_IClassFactory_CreateInstance
756 static HRESULT WINAPI COMCAT_IClassFactory_CreateInstance(
757 LPCLASSFACTORY iface,
758 LPUNKNOWN pUnkOuter,
759 REFIID riid,
760 LPVOID *ppvObj)
762 HRESULT res;
763 TRACE("%s\n",debugstr_guid(riid));
765 if (ppvObj == NULL) return E_POINTER;
767 /* Don't support aggregation (Windows doesn't) */
768 if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
770 res = IUnknown_QueryInterface((LPUNKNOWN)&COMCAT_ComCatMgr, riid, ppvObj);
771 if (SUCCEEDED(res)) {
772 return res;
775 return CLASS_E_CLASSNOTAVAILABLE;
778 /**********************************************************************
779 * COMCAT_IClassFactory_LockServer
781 static HRESULT WINAPI COMCAT_IClassFactory_LockServer(
782 LPCLASSFACTORY iface,
783 BOOL fLock)
785 FIXME("(%d), stub!\n",fLock);
786 return S_OK;
789 /**********************************************************************
790 * static ClassFactory instance
792 static const IClassFactoryVtbl ComCatCFVtbl =
794 COMCAT_IClassFactory_QueryInterface,
795 COMCAT_IClassFactory_AddRef,
796 COMCAT_IClassFactory_Release,
797 COMCAT_IClassFactory_CreateInstance,
798 COMCAT_IClassFactory_LockServer
801 static const IClassFactoryVtbl *ComCatCF = &ComCatCFVtbl;
803 HRESULT ComCatCF_Create(REFIID riid, LPVOID *ppv)
805 return IClassFactory_QueryInterface((IClassFactory *)&ComCatCF, riid, ppv);
808 /**********************************************************************
809 * IEnumCATEGORYINFO implementation
811 * This implementation is not thread-safe. The manager itself is, but
812 * I can't imagine a valid use of an enumerator in several threads.
814 typedef struct
816 const IEnumCATEGORYINFOVtbl *lpVtbl;
817 LONG ref;
818 LCID lcid;
819 HKEY key;
820 DWORD next_index;
821 } IEnumCATEGORYINFOImpl;
823 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
825 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
827 TRACE("\n");
829 return InterlockedIncrement(&This->ref);
832 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
833 LPENUMCATEGORYINFO iface,
834 REFIID riid,
835 LPVOID *ppvObj)
837 TRACE("%s\n",debugstr_guid(riid));
839 if (ppvObj == NULL) return E_POINTER;
841 if (IsEqualGUID(riid, &IID_IUnknown) ||
842 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
844 *ppvObj = iface;
845 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
846 return S_OK;
849 return E_NOINTERFACE;
852 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
854 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
855 ULONG ref;
857 TRACE("\n");
859 ref = InterlockedDecrement(&This->ref);
860 if (ref == 0) {
861 if (This->key) RegCloseKey(This->key);
862 HeapFree(GetProcessHeap(), 0, This);
863 return 0;
865 return ref;
868 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
869 LPENUMCATEGORYINFO iface,
870 ULONG celt,
871 CATEGORYINFO *rgelt,
872 ULONG *pceltFetched)
874 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
875 ULONG fetched = 0;
877 TRACE("\n");
879 if (rgelt == NULL) return E_POINTER;
881 if (This->key) while (fetched < celt) {
882 LSTATUS res;
883 HRESULT hr;
884 WCHAR catid[39];
885 DWORD cName = 39;
886 HKEY subkey;
888 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
889 NULL, NULL, NULL, NULL);
890 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
891 ++(This->next_index);
893 hr = CLSIDFromString(catid, &rgelt->catid);
894 if (FAILED(hr)) continue;
896 res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
897 if (res != ERROR_SUCCESS) continue;
899 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
900 rgelt->szDescription, 128);
901 RegCloseKey(subkey);
902 if (FAILED(hr)) continue;
904 rgelt->lcid = This->lcid;
905 ++fetched;
906 ++rgelt;
909 if (pceltFetched) *pceltFetched = fetched;
910 return fetched == celt ? S_OK : S_FALSE;
913 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
914 LPENUMCATEGORYINFO iface,
915 ULONG celt)
917 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
919 TRACE("\n");
921 This->next_index += celt;
922 /* This should return S_FALSE when there aren't celt elems to skip. */
923 return S_OK;
926 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
928 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
930 TRACE("\n");
932 This->next_index = 0;
933 return S_OK;
936 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
937 LPENUMCATEGORYINFO iface,
938 IEnumCATEGORYINFO **ppenum)
940 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
941 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
942 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
943 'r', 'i', 'e', 's', 0 };
944 IEnumCATEGORYINFOImpl *new_this;
946 TRACE("\n");
948 if (ppenum == NULL) return E_POINTER;
950 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
951 if (new_this == NULL) return E_OUTOFMEMORY;
953 new_this->lpVtbl = This->lpVtbl;
954 new_this->ref = 1;
955 new_this->lcid = This->lcid;
956 /* FIXME: could we more efficiently use DuplicateHandle? */
957 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
958 new_this->next_index = This->next_index;
960 *ppenum = (LPENUMCATEGORYINFO)new_this;
961 return S_OK;
964 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
966 COMCAT_IEnumCATEGORYINFO_QueryInterface,
967 COMCAT_IEnumCATEGORYINFO_AddRef,
968 COMCAT_IEnumCATEGORYINFO_Release,
969 COMCAT_IEnumCATEGORYINFO_Next,
970 COMCAT_IEnumCATEGORYINFO_Skip,
971 COMCAT_IEnumCATEGORYINFO_Reset,
972 COMCAT_IEnumCATEGORYINFO_Clone
975 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
977 IEnumCATEGORYINFOImpl *This;
979 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
980 if (This) {
981 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
982 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
983 'r', 'i', 'e', 's', 0 };
985 This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
986 This->lcid = lcid;
987 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
989 return (LPENUMCATEGORYINFO)This;
992 /**********************************************************************
993 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
995 * This implementation is not thread-safe. The manager itself is, but
996 * I can't imagine a valid use of an enumerator in several threads.
998 typedef struct
1000 const IEnumGUIDVtbl *lpVtbl;
1001 LONG ref;
1002 struct class_categories *categories;
1003 HKEY key;
1004 DWORD next_index;
1005 } CLSID_IEnumGUIDImpl;
1007 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
1009 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1010 TRACE("\n");
1012 return InterlockedIncrement(&This->ref);
1015 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
1016 LPENUMGUID iface,
1017 REFIID riid,
1018 LPVOID *ppvObj)
1020 TRACE("%s\n",debugstr_guid(riid));
1022 if (ppvObj == NULL) return E_POINTER;
1024 if (IsEqualGUID(riid, &IID_IUnknown) ||
1025 IsEqualGUID(riid, &IID_IEnumGUID))
1027 *ppvObj = iface;
1028 COMCAT_CLSID_IEnumGUID_AddRef(iface);
1029 return S_OK;
1032 return E_NOINTERFACE;
1035 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
1037 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1038 ULONG ref;
1040 TRACE("\n");
1042 ref = InterlockedDecrement(&This->ref);
1043 if (ref == 0) {
1044 if (This->key) RegCloseKey(This->key);
1045 HeapFree(GetProcessHeap(), 0, This->categories);
1046 HeapFree(GetProcessHeap(), 0, This);
1047 return 0;
1049 return ref;
1052 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
1053 LPENUMGUID iface,
1054 ULONG celt,
1055 GUID *rgelt,
1056 ULONG *pceltFetched)
1058 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1059 ULONG fetched = 0;
1061 TRACE("\n");
1063 if (rgelt == NULL) return E_POINTER;
1065 if (This->key) while (fetched < celt) {
1066 LSTATUS res;
1067 HRESULT hr;
1068 WCHAR clsid[39];
1069 DWORD cName = 39;
1070 HKEY subkey;
1072 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
1073 NULL, NULL, NULL, NULL);
1074 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1075 ++(This->next_index);
1077 hr = CLSIDFromString(clsid, rgelt);
1078 if (FAILED(hr)) continue;
1080 res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
1081 if (res != ERROR_SUCCESS) continue;
1083 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1084 RegCloseKey(subkey);
1085 if (hr != S_OK) continue;
1087 ++fetched;
1088 ++rgelt;
1091 if (pceltFetched) *pceltFetched = fetched;
1092 return fetched == celt ? S_OK : S_FALSE;
1095 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
1096 LPENUMGUID iface,
1097 ULONG celt)
1099 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1101 TRACE("\n");
1103 This->next_index += celt;
1104 FIXME("Never returns S_FALSE\n");
1105 return S_OK;
1108 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
1110 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1112 TRACE("\n");
1114 This->next_index = 0;
1115 return S_OK;
1118 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
1119 LPENUMGUID iface,
1120 IEnumGUID **ppenum)
1122 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1123 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1124 CLSID_IEnumGUIDImpl *new_this;
1125 DWORD size;
1127 TRACE("\n");
1129 if (ppenum == NULL) return E_POINTER;
1131 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1132 if (new_this == NULL) return E_OUTOFMEMORY;
1134 new_this->lpVtbl = This->lpVtbl;
1135 new_this->ref = 1;
1136 size = HeapSize(GetProcessHeap(), 0, This->categories);
1137 new_this->categories =
1138 HeapAlloc(GetProcessHeap(), 0, size);
1139 if (new_this->categories == NULL) {
1140 HeapFree(GetProcessHeap(), 0, new_this);
1141 return E_OUTOFMEMORY;
1143 memcpy(new_this->categories, This->categories, size);
1144 /* FIXME: could we more efficiently use DuplicateHandle? */
1145 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
1146 new_this->next_index = This->next_index;
1148 *ppenum = (LPENUMGUID)new_this;
1149 return S_OK;
1152 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
1154 COMCAT_CLSID_IEnumGUID_QueryInterface,
1155 COMCAT_CLSID_IEnumGUID_AddRef,
1156 COMCAT_CLSID_IEnumGUID_Release,
1157 COMCAT_CLSID_IEnumGUID_Next,
1158 COMCAT_CLSID_IEnumGUID_Skip,
1159 COMCAT_CLSID_IEnumGUID_Reset,
1160 COMCAT_CLSID_IEnumGUID_Clone
1163 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
1165 CLSID_IEnumGUIDImpl *This;
1167 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1168 if (This) {
1169 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1171 This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
1172 This->categories = categories;
1173 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
1175 return (LPENUMGUID)This;
1178 /**********************************************************************
1179 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1181 * This implementation is not thread-safe. The manager itself is, but
1182 * I can't imagine a valid use of an enumerator in several threads.
1184 typedef struct
1186 const IEnumGUIDVtbl *lpVtbl;
1187 LONG ref;
1188 WCHAR keyname[68];
1189 HKEY key;
1190 DWORD next_index;
1191 } CATID_IEnumGUIDImpl;
1193 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
1195 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1196 TRACE("\n");
1198 return InterlockedIncrement(&This->ref);
1201 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
1202 LPENUMGUID iface,
1203 REFIID riid,
1204 LPVOID *ppvObj)
1206 TRACE("%s\n",debugstr_guid(riid));
1208 if (ppvObj == NULL) return E_POINTER;
1210 if (IsEqualGUID(riid, &IID_IUnknown) ||
1211 IsEqualGUID(riid, &IID_IEnumGUID))
1213 *ppvObj = iface;
1214 COMCAT_CATID_IEnumGUID_AddRef(iface);
1215 return S_OK;
1218 return E_NOINTERFACE;
1221 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
1223 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1224 ULONG ref;
1226 TRACE("\n");
1228 ref = InterlockedDecrement(&This->ref);
1229 if (ref == 0) {
1230 if (This->key) RegCloseKey(This->key);
1231 HeapFree(GetProcessHeap(), 0, This);
1232 return 0;
1234 return ref;
1237 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
1238 LPENUMGUID iface,
1239 ULONG celt,
1240 GUID *rgelt,
1241 ULONG *pceltFetched)
1243 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1244 ULONG fetched = 0;
1246 TRACE("\n");
1248 if (rgelt == NULL) return E_POINTER;
1250 if (This->key) while (fetched < celt) {
1251 LSTATUS res;
1252 HRESULT hr;
1253 WCHAR catid[39];
1254 DWORD cName = 39;
1256 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1257 NULL, NULL, NULL, NULL);
1258 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1259 ++(This->next_index);
1261 hr = CLSIDFromString(catid, rgelt);
1262 if (FAILED(hr)) continue;
1264 ++fetched;
1265 ++rgelt;
1268 if (pceltFetched) *pceltFetched = fetched;
1269 return fetched == celt ? S_OK : S_FALSE;
1272 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
1273 LPENUMGUID iface,
1274 ULONG celt)
1276 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1278 TRACE("\n");
1280 This->next_index += celt;
1281 FIXME("Never returns S_FALSE\n");
1282 return S_OK;
1285 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
1287 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1289 TRACE("\n");
1291 This->next_index = 0;
1292 return S_OK;
1295 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
1296 LPENUMGUID iface,
1297 IEnumGUID **ppenum)
1299 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1300 CATID_IEnumGUIDImpl *new_this;
1302 TRACE("\n");
1304 if (ppenum == NULL) return E_POINTER;
1306 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1307 if (new_this == NULL) return E_OUTOFMEMORY;
1309 new_this->lpVtbl = This->lpVtbl;
1310 new_this->ref = 1;
1311 lstrcpyW(new_this->keyname, This->keyname);
1312 /* FIXME: could we more efficiently use DuplicateHandle? */
1313 RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
1314 new_this->next_index = This->next_index;
1316 *ppenum = (LPENUMGUID)new_this;
1317 return S_OK;
1320 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
1322 COMCAT_CATID_IEnumGUID_QueryInterface,
1323 COMCAT_CATID_IEnumGUID_AddRef,
1324 COMCAT_CATID_IEnumGUID_Release,
1325 COMCAT_CATID_IEnumGUID_Next,
1326 COMCAT_CATID_IEnumGUID_Skip,
1327 COMCAT_CATID_IEnumGUID_Reset,
1328 COMCAT_CATID_IEnumGUID_Clone
1331 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
1332 REFCLSID rclsid, LPCWSTR postfix)
1334 CATID_IEnumGUIDImpl *This;
1336 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1337 if (This) {
1338 WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
1340 This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
1341 memcpy(This->keyname, prefix, sizeof(prefix));
1342 StringFromGUID2(rclsid, This->keyname + 6, 39);
1343 lstrcpyW(This->keyname + 44, postfix);
1344 RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
1346 return (LPENUMGUID)This;