shell32: Fix the errors in two Chinese (Simplified) resources.
[wine/hacks.git] / dlls / ole32 / comcat.c
blobfe0f7e5962ecddcf9829d9d8186929f6dd3dfc4f
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 if (*categories->impl_strings) {
236 res = RegOpenKeyExW(key, impl_keyname, 0, KEY_READ, &subkey);
237 if (res != ERROR_SUCCESS) return S_FALSE;
238 for (string = categories->impl_strings; *string; string += 39) {
239 HKEY catkey;
240 res = RegOpenKeyExW(subkey, string, 0, 0, &catkey);
241 if (res != ERROR_SUCCESS) {
242 RegCloseKey(subkey);
243 return S_FALSE;
245 RegCloseKey(catkey);
247 RegCloseKey(subkey);
250 /* Check that all categories required by class are given. */
251 res = RegOpenKeyExW(key, req_keyname, 0, KEY_READ, &subkey);
252 if (res == ERROR_SUCCESS) {
253 for (index = 0; ; ++index) {
254 WCHAR keyname[39];
255 DWORD size = 39;
257 res = RegEnumKeyExW(subkey, index, keyname, &size,
258 NULL, NULL, NULL, NULL);
259 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
260 if (size != 38) continue; /* bogus catid in registry */
261 for (string = categories->req_strings; *string; string += 39)
262 if (!strcmpiW(string, keyname)) break;
263 if (!*string) {
264 RegCloseKey(subkey);
265 return S_FALSE;
268 RegCloseKey(subkey);
271 return S_OK;
274 /**********************************************************************
275 * COMCAT_ICatRegister_QueryInterface
277 static HRESULT WINAPI COMCAT_ICatRegister_QueryInterface(
278 LPCATREGISTER iface,
279 REFIID riid,
280 LPVOID *ppvObj)
282 ComCatMgrImpl *This = (ComCatMgrImpl *)iface;
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 IUnknown_AddRef(iface);
290 return S_OK;
293 if (IsEqualGUID(riid, &IID_ICatInformation)) {
294 *ppvObj = &This->infVtbl;
295 IUnknown_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 = RegCreateKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0, NULL, 0,
336 KEY_READ | KEY_WRITE, NULL, &comcat_key, NULL);
337 if (res != ERROR_SUCCESS) return E_FAIL;
339 for (; cCategories; --cCategories, ++rgci) {
340 static const WCHAR fmt[] = { '%', 'l', 'X', 0 };
341 WCHAR keyname[39];
342 WCHAR valname[9];
343 HKEY cat_key;
345 /* Create (or open) the key for this category. */
346 if (!StringFromGUID2(&rgci->catid, keyname, 39)) continue;
347 res = RegCreateKeyExW(comcat_key, keyname, 0, NULL, 0,
348 KEY_READ | KEY_WRITE, NULL, &cat_key, NULL);
349 if (res != ERROR_SUCCESS) continue;
351 /* Set the value for this locale's description. */
352 wsprintfW(valname, fmt, rgci->lcid);
353 RegSetValueExW(cat_key, valname, 0, REG_SZ,
354 (CONST BYTE*)(rgci->szDescription),
355 (lstrlenW(rgci->szDescription) + 1) * sizeof(WCHAR));
357 RegCloseKey(cat_key);
360 RegCloseKey(comcat_key);
361 return S_OK;
364 /**********************************************************************
365 * COMCAT_ICatRegister_UnRegisterCategories
367 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterCategories(
368 LPCATREGISTER iface,
369 ULONG cCategories,
370 CATID *rgcatid)
372 HKEY comcat_key;
373 HRESULT res;
375 TRACE("\n");
377 if (cCategories && rgcatid == NULL)
378 return E_POINTER;
380 /* Open the component categories key. */
381 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, comcat_keyname, 0,
382 KEY_READ | KEY_WRITE, &comcat_key);
383 if (res != ERROR_SUCCESS) return E_FAIL;
385 for (; cCategories; --cCategories, ++rgcatid) {
386 WCHAR keyname[39];
388 /* Delete the key for this category. */
389 if (!StringFromGUID2(rgcatid, keyname, 39)) continue;
390 RegDeleteKeyW(comcat_key, keyname);
393 RegCloseKey(comcat_key);
394 return S_OK;
397 /**********************************************************************
398 * COMCAT_ICatRegister_RegisterClassImplCategories
400 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassImplCategories(
401 LPCATREGISTER iface,
402 REFCLSID rclsid,
403 ULONG cCategories,
404 CATID *rgcatid)
406 TRACE("\n");
408 return COMCAT_RegisterClassCategories(
409 rclsid, impl_keyname, cCategories, rgcatid);
412 /**********************************************************************
413 * COMCAT_ICatRegister_UnRegisterClassImplCategories
415 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassImplCategories(
416 LPCATREGISTER iface,
417 REFCLSID rclsid,
418 ULONG cCategories,
419 CATID *rgcatid)
421 TRACE("\n");
423 return COMCAT_UnRegisterClassCategories(
424 rclsid, impl_keyname, cCategories, rgcatid);
427 /**********************************************************************
428 * COMCAT_ICatRegister_RegisterClassReqCategories
430 static HRESULT WINAPI COMCAT_ICatRegister_RegisterClassReqCategories(
431 LPCATREGISTER iface,
432 REFCLSID rclsid,
433 ULONG cCategories,
434 CATID *rgcatid)
436 TRACE("\n");
438 return COMCAT_RegisterClassCategories(
439 rclsid, req_keyname, cCategories, rgcatid);
442 /**********************************************************************
443 * COMCAT_ICatRegister_UnRegisterClassReqCategories
445 static HRESULT WINAPI COMCAT_ICatRegister_UnRegisterClassReqCategories(
446 LPCATREGISTER iface,
447 REFCLSID rclsid,
448 ULONG cCategories,
449 CATID *rgcatid)
451 TRACE("\n");
453 return COMCAT_UnRegisterClassCategories(
454 rclsid, req_keyname, cCategories, rgcatid);
457 /**********************************************************************
458 * COMCAT_ICatInformation_QueryInterface
460 static HRESULT WINAPI COMCAT_ICatInformation_QueryInterface(
461 LPCATINFORMATION iface,
462 REFIID riid,
463 LPVOID *ppvObj)
465 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
466 return IUnknown_QueryInterface((LPUNKNOWN)This, riid, ppvObj);
469 /**********************************************************************
470 * COMCAT_ICatInformation_AddRef
472 static ULONG WINAPI COMCAT_ICatInformation_AddRef(LPCATINFORMATION iface)
474 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
475 return IUnknown_AddRef((LPUNKNOWN)This);
478 /**********************************************************************
479 * COMCAT_ICatInformation_Release
481 static ULONG WINAPI COMCAT_ICatInformation_Release(LPCATINFORMATION iface)
483 ComCatMgrImpl *This = impl_from_ICatInformation( iface );
484 return IUnknown_Release((LPUNKNOWN)This);
487 /**********************************************************************
488 * COMCAT_ICatInformation_EnumCategories
490 static HRESULT WINAPI COMCAT_ICatInformation_EnumCategories(
491 LPCATINFORMATION iface,
492 LCID lcid,
493 LPENUMCATEGORYINFO *ppenumCatInfo)
495 TRACE("\n");
497 if (ppenumCatInfo == NULL) return E_POINTER;
499 *ppenumCatInfo = COMCAT_IEnumCATEGORYINFO_Construct(lcid);
500 if (*ppenumCatInfo == NULL) return E_OUTOFMEMORY;
501 IEnumCATEGORYINFO_AddRef(*ppenumCatInfo);
502 return S_OK;
505 /**********************************************************************
506 * COMCAT_ICatInformation_GetCategoryDesc
508 static HRESULT WINAPI COMCAT_ICatInformation_GetCategoryDesc(
509 LPCATINFORMATION iface,
510 REFCATID rcatid,
511 LCID lcid,
512 PWCHAR *ppszDesc)
514 WCHAR keyname[60] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
515 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
516 'r', 'i', 'e', 's', '\\', 0 };
517 HKEY key;
518 HRESULT res;
520 TRACE("CATID: %s LCID: %x\n",debugstr_guid(rcatid), lcid);
522 if (rcatid == NULL || ppszDesc == NULL) return E_INVALIDARG;
524 /* Open the key for this category. */
525 if (!StringFromGUID2(rcatid, keyname + 21, 39)) return E_FAIL;
526 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &key);
527 if (res != ERROR_SUCCESS) return CAT_E_CATIDNOEXIST;
529 /* Allocate a sensible amount of memory for the description. */
530 *ppszDesc = CoTaskMemAlloc(128 * sizeof(WCHAR));
531 if (*ppszDesc == NULL) {
532 RegCloseKey(key);
533 return E_OUTOFMEMORY;
536 /* Get the description, and make sure it's null terminated. */
537 res = COMCAT_GetCategoryDesc(key, lcid, *ppszDesc, 128);
538 RegCloseKey(key);
539 if (FAILED(res)) {
540 CoTaskMemFree(*ppszDesc);
541 return res;
544 return S_OK;
547 /**********************************************************************
548 * COMCAT_ICatInformation_EnumClassesOfCategories
550 static HRESULT WINAPI COMCAT_ICatInformation_EnumClassesOfCategories(
551 LPCATINFORMATION iface,
552 ULONG cImplemented,
553 CATID *rgcatidImpl,
554 ULONG cRequired,
555 CATID *rgcatidReq,
556 LPENUMCLSID *ppenumCLSID)
558 struct class_categories *categories;
560 TRACE("\n");
562 if (cImplemented == (ULONG)-1)
563 cImplemented = 0;
564 if (cRequired == (ULONG)-1)
565 cRequired = 0;
567 if (ppenumCLSID == NULL ||
568 (cImplemented && rgcatidImpl == NULL) ||
569 (cRequired && rgcatidReq == NULL)) return E_POINTER;
571 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
572 cRequired, rgcatidReq);
573 if (categories == NULL) return E_OUTOFMEMORY;
574 *ppenumCLSID = COMCAT_CLSID_IEnumGUID_Construct(categories);
575 if (*ppenumCLSID == NULL) {
576 HeapFree(GetProcessHeap(), 0, categories);
577 return E_OUTOFMEMORY;
579 IEnumGUID_AddRef(*ppenumCLSID);
580 return S_OK;
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 (WINE_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, 39);
613 if (FAILED(res)) return res;
615 categories = COMCAT_PrepareClassCategories(cImplemented, rgcatidImpl,
616 cRequired, rgcatidReq);
617 if (categories == NULL) return E_OUTOFMEMORY;
619 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, 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[24] = { '\\', '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 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
648 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
649 return S_OK;
652 /**********************************************************************
653 * COMCAT_ICatInformation_EnumReqCategoriesOfClass
655 static HRESULT WINAPI COMCAT_ICatInformation_EnumReqCategoriesOfClass(
656 LPCATINFORMATION iface,
657 REFCLSID rclsid,
658 LPENUMCATID *ppenumCATID)
660 static const WCHAR postfix[21] = { '\\', 'R', 'e', 'q', 'u', 'i', 'r', 'e',
661 'd', ' ', 'C', 'a', 't', 'e', 'g', 'o',
662 'r', 'i', 'e', 's', 0 };
664 TRACE("%s\n",debugstr_guid(rclsid));
666 if (rclsid == NULL || ppenumCATID == NULL)
667 return E_POINTER;
669 *ppenumCATID = COMCAT_CATID_IEnumGUID_Construct(rclsid, postfix);
670 if (*ppenumCATID == NULL) return E_OUTOFMEMORY;
671 return S_OK;
674 /**********************************************************************
675 * COMCAT_ICatRegister_Vtbl
677 static const ICatRegisterVtbl COMCAT_ICatRegister_Vtbl =
679 COMCAT_ICatRegister_QueryInterface,
680 COMCAT_ICatRegister_AddRef,
681 COMCAT_ICatRegister_Release,
682 COMCAT_ICatRegister_RegisterCategories,
683 COMCAT_ICatRegister_UnRegisterCategories,
684 COMCAT_ICatRegister_RegisterClassImplCategories,
685 COMCAT_ICatRegister_UnRegisterClassImplCategories,
686 COMCAT_ICatRegister_RegisterClassReqCategories,
687 COMCAT_ICatRegister_UnRegisterClassReqCategories
691 /**********************************************************************
692 * COMCAT_ICatInformation_Vtbl
694 static const ICatInformationVtbl COMCAT_ICatInformation_Vtbl =
696 COMCAT_ICatInformation_QueryInterface,
697 COMCAT_ICatInformation_AddRef,
698 COMCAT_ICatInformation_Release,
699 COMCAT_ICatInformation_EnumCategories,
700 COMCAT_ICatInformation_GetCategoryDesc,
701 COMCAT_ICatInformation_EnumClassesOfCategories,
702 COMCAT_ICatInformation_IsClassOfCategories,
703 COMCAT_ICatInformation_EnumImplCategoriesOfClass,
704 COMCAT_ICatInformation_EnumReqCategoriesOfClass
707 /**********************************************************************
708 * static ComCatMgr instance
710 static ComCatMgrImpl COMCAT_ComCatMgr =
712 &COMCAT_ICatRegister_Vtbl,
713 &COMCAT_ICatInformation_Vtbl
716 /**********************************************************************
717 * COMCAT_IClassFactory_QueryInterface (also IUnknown)
719 static HRESULT WINAPI COMCAT_IClassFactory_QueryInterface(
720 LPCLASSFACTORY iface,
721 REFIID riid,
722 LPVOID *ppvObj)
724 TRACE("%s\n",debugstr_guid(riid));
726 if (ppvObj == NULL) return E_POINTER;
728 if (IsEqualGUID(riid, &IID_IUnknown) ||
729 IsEqualGUID(riid, &IID_IClassFactory))
731 *ppvObj = iface;
732 IUnknown_AddRef(iface);
733 return S_OK;
736 return E_NOINTERFACE;
739 /**********************************************************************
740 * COMCAT_IClassFactory_AddRef (also IUnknown)
742 static ULONG WINAPI COMCAT_IClassFactory_AddRef(LPCLASSFACTORY iface)
744 return 2; /* non-heap based object */
747 /**********************************************************************
748 * COMCAT_IClassFactory_Release (also IUnknown)
750 static ULONG WINAPI COMCAT_IClassFactory_Release(LPCLASSFACTORY iface)
752 return 1; /* non-heap based object */
755 /**********************************************************************
756 * COMCAT_IClassFactory_CreateInstance
758 static HRESULT WINAPI COMCAT_IClassFactory_CreateInstance(
759 LPCLASSFACTORY iface,
760 LPUNKNOWN pUnkOuter,
761 REFIID riid,
762 LPVOID *ppvObj)
764 HRESULT res;
765 TRACE("%s\n",debugstr_guid(riid));
767 if (ppvObj == NULL) return E_POINTER;
769 /* Don't support aggregation (Windows doesn't) */
770 if (pUnkOuter != NULL) return CLASS_E_NOAGGREGATION;
772 res = IUnknown_QueryInterface((LPUNKNOWN)&COMCAT_ComCatMgr, riid, ppvObj);
773 if (SUCCEEDED(res)) {
774 return res;
777 return CLASS_E_CLASSNOTAVAILABLE;
780 /**********************************************************************
781 * COMCAT_IClassFactory_LockServer
783 static HRESULT WINAPI COMCAT_IClassFactory_LockServer(
784 LPCLASSFACTORY iface,
785 BOOL fLock)
787 FIXME("(%d), stub!\n",fLock);
788 return S_OK;
791 /**********************************************************************
792 * static ClassFactory instance
794 static const IClassFactoryVtbl ComCatCFVtbl =
796 COMCAT_IClassFactory_QueryInterface,
797 COMCAT_IClassFactory_AddRef,
798 COMCAT_IClassFactory_Release,
799 COMCAT_IClassFactory_CreateInstance,
800 COMCAT_IClassFactory_LockServer
803 static const IClassFactoryVtbl *ComCatCF = &ComCatCFVtbl;
805 HRESULT ComCatCF_Create(REFIID riid, LPVOID *ppv)
807 return IClassFactory_QueryInterface((IClassFactory *)&ComCatCF, riid, ppv);
810 /**********************************************************************
811 * IEnumCATEGORYINFO implementation
813 * This implementation is not thread-safe. The manager itself is, but
814 * I can't imagine a valid use of an enumerator in several threads.
816 typedef struct
818 const IEnumCATEGORYINFOVtbl *lpVtbl;
819 LONG ref;
820 LCID lcid;
821 HKEY key;
822 DWORD next_index;
823 } IEnumCATEGORYINFOImpl;
825 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_AddRef(LPENUMCATEGORYINFO iface)
827 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
829 TRACE("\n");
831 return InterlockedIncrement(&This->ref);
834 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_QueryInterface(
835 LPENUMCATEGORYINFO iface,
836 REFIID riid,
837 LPVOID *ppvObj)
839 TRACE("%s\n",debugstr_guid(riid));
841 if (ppvObj == NULL) return E_POINTER;
843 if (IsEqualGUID(riid, &IID_IUnknown) ||
844 IsEqualGUID(riid, &IID_IEnumCATEGORYINFO))
846 *ppvObj = iface;
847 COMCAT_IEnumCATEGORYINFO_AddRef(iface);
848 return S_OK;
851 return E_NOINTERFACE;
854 static ULONG WINAPI COMCAT_IEnumCATEGORYINFO_Release(LPENUMCATEGORYINFO iface)
856 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
857 ULONG ref;
859 TRACE("\n");
861 ref = InterlockedDecrement(&This->ref);
862 if (ref == 0) {
863 if (This->key) RegCloseKey(This->key);
864 HeapFree(GetProcessHeap(), 0, This);
865 return 0;
867 return ref;
870 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Next(
871 LPENUMCATEGORYINFO iface,
872 ULONG celt,
873 CATEGORYINFO *rgelt,
874 ULONG *pceltFetched)
876 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
877 ULONG fetched = 0;
879 TRACE("\n");
881 if (rgelt == NULL) return E_POINTER;
883 if (This->key) while (fetched < celt) {
884 LSTATUS res;
885 HRESULT hr;
886 WCHAR catid[39];
887 DWORD cName = 39;
888 HKEY subkey;
890 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
891 NULL, NULL, NULL, NULL);
892 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
893 ++(This->next_index);
895 hr = CLSIDFromString(catid, &rgelt->catid);
896 if (FAILED(hr)) continue;
898 res = RegOpenKeyExW(This->key, catid, 0, KEY_READ, &subkey);
899 if (res != ERROR_SUCCESS) continue;
901 hr = COMCAT_GetCategoryDesc(subkey, This->lcid,
902 rgelt->szDescription, 128);
903 RegCloseKey(subkey);
904 if (FAILED(hr)) continue;
906 rgelt->lcid = This->lcid;
907 ++fetched;
908 ++rgelt;
911 if (pceltFetched) *pceltFetched = fetched;
912 return fetched == celt ? S_OK : S_FALSE;
915 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Skip(
916 LPENUMCATEGORYINFO iface,
917 ULONG celt)
919 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
921 TRACE("\n");
923 This->next_index += celt;
924 /* This should return S_FALSE when there aren't celt elems to skip. */
925 return S_OK;
928 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Reset(LPENUMCATEGORYINFO iface)
930 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
932 TRACE("\n");
934 This->next_index = 0;
935 return S_OK;
938 static HRESULT WINAPI COMCAT_IEnumCATEGORYINFO_Clone(
939 LPENUMCATEGORYINFO iface,
940 IEnumCATEGORYINFO **ppenum)
942 IEnumCATEGORYINFOImpl *This = (IEnumCATEGORYINFOImpl *)iface;
943 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
944 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
945 'r', 'i', 'e', 's', 0 };
946 IEnumCATEGORYINFOImpl *new_this;
948 TRACE("\n");
950 if (ppenum == NULL) return E_POINTER;
952 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
953 if (new_this == NULL) return E_OUTOFMEMORY;
955 new_this->lpVtbl = This->lpVtbl;
956 new_this->ref = 1;
957 new_this->lcid = This->lcid;
958 /* FIXME: could we more efficiently use DuplicateHandle? */
959 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
960 new_this->next_index = This->next_index;
962 *ppenum = (LPENUMCATEGORYINFO)new_this;
963 return S_OK;
966 static const IEnumCATEGORYINFOVtbl COMCAT_IEnumCATEGORYINFO_Vtbl =
968 COMCAT_IEnumCATEGORYINFO_QueryInterface,
969 COMCAT_IEnumCATEGORYINFO_AddRef,
970 COMCAT_IEnumCATEGORYINFO_Release,
971 COMCAT_IEnumCATEGORYINFO_Next,
972 COMCAT_IEnumCATEGORYINFO_Skip,
973 COMCAT_IEnumCATEGORYINFO_Reset,
974 COMCAT_IEnumCATEGORYINFO_Clone
977 static LPENUMCATEGORYINFO COMCAT_IEnumCATEGORYINFO_Construct(LCID lcid)
979 IEnumCATEGORYINFOImpl *This;
981 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumCATEGORYINFOImpl));
982 if (This) {
983 static const WCHAR keyname[] = { 'C', 'o', 'm', 'p', 'o', 'n', 'e', 'n',
984 't', ' ', 'C', 'a', 't', 'e', 'g', 'o',
985 'r', 'i', 'e', 's', 0 };
987 This->lpVtbl = &COMCAT_IEnumCATEGORYINFO_Vtbl;
988 This->lcid = lcid;
989 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
991 return (LPENUMCATEGORYINFO)This;
994 /**********************************************************************
995 * ClassesOfCategories IEnumCLSID (IEnumGUID) implementation
997 * This implementation is not thread-safe. The manager itself is, but
998 * I can't imagine a valid use of an enumerator in several threads.
1000 typedef struct
1002 const IEnumGUIDVtbl *lpVtbl;
1003 LONG ref;
1004 struct class_categories *categories;
1005 HKEY key;
1006 DWORD next_index;
1007 } CLSID_IEnumGUIDImpl;
1009 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_AddRef(LPENUMGUID iface)
1011 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1012 TRACE("\n");
1014 return InterlockedIncrement(&This->ref);
1017 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_QueryInterface(
1018 LPENUMGUID iface,
1019 REFIID riid,
1020 LPVOID *ppvObj)
1022 TRACE("%s\n",debugstr_guid(riid));
1024 if (ppvObj == NULL) return E_POINTER;
1026 if (IsEqualGUID(riid, &IID_IUnknown) ||
1027 IsEqualGUID(riid, &IID_IEnumGUID))
1029 *ppvObj = iface;
1030 COMCAT_CLSID_IEnumGUID_AddRef(iface);
1031 return S_OK;
1034 return E_NOINTERFACE;
1037 static ULONG WINAPI COMCAT_CLSID_IEnumGUID_Release(LPENUMGUID iface)
1039 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1040 ULONG ref;
1042 TRACE("\n");
1044 ref = InterlockedDecrement(&This->ref);
1045 if (ref == 0) {
1046 if (This->key) RegCloseKey(This->key);
1047 HeapFree(GetProcessHeap(), 0, This->categories);
1048 HeapFree(GetProcessHeap(), 0, This);
1049 return 0;
1051 return ref;
1054 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Next(
1055 LPENUMGUID iface,
1056 ULONG celt,
1057 GUID *rgelt,
1058 ULONG *pceltFetched)
1060 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1061 ULONG fetched = 0;
1063 TRACE("\n");
1065 if (rgelt == NULL) return E_POINTER;
1067 if (This->key) while (fetched < celt) {
1068 LSTATUS res;
1069 HRESULT hr;
1070 WCHAR clsid[39];
1071 DWORD cName = 39;
1072 HKEY subkey;
1074 res = RegEnumKeyExW(This->key, This->next_index, clsid, &cName,
1075 NULL, NULL, NULL, NULL);
1076 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1077 ++(This->next_index);
1079 hr = CLSIDFromString(clsid, rgelt);
1080 if (FAILED(hr)) continue;
1082 res = RegOpenKeyExW(This->key, clsid, 0, KEY_READ, &subkey);
1083 if (res != ERROR_SUCCESS) continue;
1085 hr = COMCAT_IsClassOfCategories(subkey, This->categories);
1086 RegCloseKey(subkey);
1087 if (hr != S_OK) continue;
1089 ++fetched;
1090 ++rgelt;
1093 if (pceltFetched) *pceltFetched = fetched;
1094 return fetched == celt ? S_OK : S_FALSE;
1097 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Skip(
1098 LPENUMGUID iface,
1099 ULONG celt)
1101 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1103 TRACE("\n");
1105 This->next_index += celt;
1106 FIXME("Never returns S_FALSE\n");
1107 return S_OK;
1110 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Reset(LPENUMGUID iface)
1112 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1114 TRACE("\n");
1116 This->next_index = 0;
1117 return S_OK;
1120 static HRESULT WINAPI COMCAT_CLSID_IEnumGUID_Clone(
1121 LPENUMGUID iface,
1122 IEnumGUID **ppenum)
1124 CLSID_IEnumGUIDImpl *This = (CLSID_IEnumGUIDImpl *)iface;
1125 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1126 CLSID_IEnumGUIDImpl *new_this;
1127 DWORD size;
1129 TRACE("\n");
1131 if (ppenum == NULL) return E_POINTER;
1133 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1134 if (new_this == NULL) return E_OUTOFMEMORY;
1136 new_this->lpVtbl = This->lpVtbl;
1137 new_this->ref = 1;
1138 size = HeapSize(GetProcessHeap(), 0, This->categories);
1139 new_this->categories =
1140 HeapAlloc(GetProcessHeap(), 0, size);
1141 if (new_this->categories == NULL) {
1142 HeapFree(GetProcessHeap(), 0, new_this);
1143 return E_OUTOFMEMORY;
1145 memcpy(new_this->categories, This->categories, size);
1146 /* FIXME: could we more efficiently use DuplicateHandle? */
1147 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &new_this->key);
1148 new_this->next_index = This->next_index;
1150 *ppenum = (LPENUMGUID)new_this;
1151 return S_OK;
1154 static const IEnumGUIDVtbl COMCAT_CLSID_IEnumGUID_Vtbl =
1156 COMCAT_CLSID_IEnumGUID_QueryInterface,
1157 COMCAT_CLSID_IEnumGUID_AddRef,
1158 COMCAT_CLSID_IEnumGUID_Release,
1159 COMCAT_CLSID_IEnumGUID_Next,
1160 COMCAT_CLSID_IEnumGUID_Skip,
1161 COMCAT_CLSID_IEnumGUID_Reset,
1162 COMCAT_CLSID_IEnumGUID_Clone
1165 static LPENUMGUID COMCAT_CLSID_IEnumGUID_Construct(struct class_categories *categories)
1167 CLSID_IEnumGUIDImpl *This;
1169 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CLSID_IEnumGUIDImpl));
1170 if (This) {
1171 static const WCHAR keyname[] = { 'C', 'L', 'S', 'I', 'D', 0 };
1173 This->lpVtbl = &COMCAT_CLSID_IEnumGUID_Vtbl;
1174 This->categories = categories;
1175 RegOpenKeyExW(HKEY_CLASSES_ROOT, keyname, 0, KEY_READ, &This->key);
1177 return (LPENUMGUID)This;
1180 /**********************************************************************
1181 * CategoriesOfClass IEnumCATID (IEnumGUID) implementation
1183 * This implementation is not thread-safe. The manager itself is, but
1184 * I can't imagine a valid use of an enumerator in several threads.
1186 typedef struct
1188 const IEnumGUIDVtbl *lpVtbl;
1189 LONG ref;
1190 WCHAR keyname[68];
1191 HKEY key;
1192 DWORD next_index;
1193 } CATID_IEnumGUIDImpl;
1195 static ULONG WINAPI COMCAT_CATID_IEnumGUID_AddRef(LPENUMGUID iface)
1197 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1198 TRACE("\n");
1200 return InterlockedIncrement(&This->ref);
1203 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_QueryInterface(
1204 LPENUMGUID iface,
1205 REFIID riid,
1206 LPVOID *ppvObj)
1208 TRACE("%s\n",debugstr_guid(riid));
1210 if (ppvObj == NULL) return E_POINTER;
1212 if (IsEqualGUID(riid, &IID_IUnknown) ||
1213 IsEqualGUID(riid, &IID_IEnumGUID))
1215 *ppvObj = iface;
1216 COMCAT_CATID_IEnumGUID_AddRef(iface);
1217 return S_OK;
1220 return E_NOINTERFACE;
1223 static ULONG WINAPI COMCAT_CATID_IEnumGUID_Release(LPENUMGUID iface)
1225 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1226 ULONG ref;
1228 TRACE("\n");
1230 ref = InterlockedDecrement(&This->ref);
1231 if (ref == 0) {
1232 if (This->key) RegCloseKey(This->key);
1233 HeapFree(GetProcessHeap(), 0, This);
1234 return 0;
1236 return ref;
1239 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Next(
1240 LPENUMGUID iface,
1241 ULONG celt,
1242 GUID *rgelt,
1243 ULONG *pceltFetched)
1245 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1246 ULONG fetched = 0;
1248 TRACE("\n");
1250 if (rgelt == NULL) return E_POINTER;
1252 if (This->key) while (fetched < celt) {
1253 LSTATUS res;
1254 HRESULT hr;
1255 WCHAR catid[39];
1256 DWORD cName = 39;
1258 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
1259 NULL, NULL, NULL, NULL);
1260 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1261 ++(This->next_index);
1263 hr = CLSIDFromString(catid, rgelt);
1264 if (FAILED(hr)) continue;
1266 ++fetched;
1267 ++rgelt;
1270 if (pceltFetched) *pceltFetched = fetched;
1271 return fetched == celt ? S_OK : S_FALSE;
1274 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Skip(
1275 LPENUMGUID iface,
1276 ULONG celt)
1278 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1280 TRACE("\n");
1282 This->next_index += celt;
1283 FIXME("Never returns S_FALSE\n");
1284 return S_OK;
1287 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Reset(LPENUMGUID iface)
1289 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1291 TRACE("\n");
1293 This->next_index = 0;
1294 return S_OK;
1297 static HRESULT WINAPI COMCAT_CATID_IEnumGUID_Clone(
1298 LPENUMGUID iface,
1299 IEnumGUID **ppenum)
1301 CATID_IEnumGUIDImpl *This = (CATID_IEnumGUIDImpl *)iface;
1302 CATID_IEnumGUIDImpl *new_this;
1304 TRACE("\n");
1306 if (ppenum == NULL) return E_POINTER;
1308 new_this = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1309 if (new_this == NULL) return E_OUTOFMEMORY;
1311 new_this->lpVtbl = This->lpVtbl;
1312 new_this->ref = 1;
1313 lstrcpyW(new_this->keyname, This->keyname);
1314 /* FIXME: could we more efficiently use DuplicateHandle? */
1315 RegOpenKeyExW(HKEY_CLASSES_ROOT, new_this->keyname, 0, KEY_READ, &new_this->key);
1316 new_this->next_index = This->next_index;
1318 *ppenum = (LPENUMGUID)new_this;
1319 return S_OK;
1322 static const IEnumGUIDVtbl COMCAT_CATID_IEnumGUID_Vtbl =
1324 COMCAT_CATID_IEnumGUID_QueryInterface,
1325 COMCAT_CATID_IEnumGUID_AddRef,
1326 COMCAT_CATID_IEnumGUID_Release,
1327 COMCAT_CATID_IEnumGUID_Next,
1328 COMCAT_CATID_IEnumGUID_Skip,
1329 COMCAT_CATID_IEnumGUID_Reset,
1330 COMCAT_CATID_IEnumGUID_Clone
1333 static LPENUMGUID COMCAT_CATID_IEnumGUID_Construct(
1334 REFCLSID rclsid, LPCWSTR postfix)
1336 CATID_IEnumGUIDImpl *This;
1338 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CATID_IEnumGUIDImpl));
1339 if (This) {
1340 WCHAR prefix[6] = { 'C', 'L', 'S', 'I', 'D', '\\' };
1342 This->lpVtbl = &COMCAT_CATID_IEnumGUID_Vtbl;
1343 memcpy(This->keyname, prefix, sizeof(prefix));
1344 StringFromGUID2(rclsid, This->keyname + 6, 39);
1345 lstrcpyW(This->keyname + 44, postfix);
1346 RegOpenKeyExW(HKEY_CLASSES_ROOT, This->keyname, 0, KEY_READ, &This->key);
1348 return (LPENUMGUID)This;