Fixed some issues found by winapi_check.
[wine/multimedia.git] / dlls / comcat / regsvr.c
blobe2f1b2029ea8efdd3b249f271364ca9bec68c7db
1 /*
2 * self-registerable dll functions for comcat.dll
4 * Copyright (C) 2002-2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define COM_NO_WINDOWS_H
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winerror.h"
31 #include "ole2.h"
32 #include "comcat.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 * Near the bottom of this file are the exported DllRegisterServer and
40 * DllUnregisterServer, which make all this worthwhile.
43 /***********************************************************************
44 * interface for self-registering
46 struct regsvr_interface
48 IID const *iid; /* NULL for end of list */
49 LPCSTR name; /* can be NULL to omit */
50 IID const *base_iid; /* can be NULL to omit */
51 int num_methods; /* can be <0 to omit */
52 CLSID const *ps_clsid; /* can be NULL to omit */
53 CLSID const *ps_clsid32; /* can be NULL to omit */
56 static HRESULT register_interfaces(struct regsvr_interface const *list);
57 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
59 struct regsvr_coclass
61 CLSID const *clsid; /* NULL for end of list */
62 LPCSTR name; /* can be NULL to omit */
63 LPCSTR ips; /* can be NULL to omit */
64 LPCSTR ips32; /* can be NULL to omit */
65 LPCSTR ips32_tmodel; /* can be NULL to omit */
68 static HRESULT register_coclasses(struct regsvr_coclass const *list);
69 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
71 /***********************************************************************
72 * static string constants
74 static WCHAR const interface_keyname[10] = {
75 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
76 static WCHAR const base_ifa_keyname[14] = {
77 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
78 'e', 0 };
79 static WCHAR const num_methods_keyname[11] = {
80 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
81 static WCHAR const ps_clsid_keyname[15] = {
82 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
83 'i', 'd', 0 };
84 static WCHAR const ps_clsid32_keyname[17] = {
85 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
86 'i', 'd', '3', '2', 0 };
87 static WCHAR const clsid_keyname[6] = {
88 'C', 'L', 'S', 'I', 'D', 0 };
89 static WCHAR const ips_keyname[13] = {
90 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
91 0 };
92 static WCHAR const ips32_keyname[15] = {
93 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
94 '3', '2', 0 };
95 static char const tmodel_valuename[] = "ThreadingModel";
97 /***********************************************************************
98 * static helper functions
100 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
101 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
102 WCHAR const *value);
103 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
104 char const *value);
105 static LONG recursive_delete_key(HKEY key);
108 /***********************************************************************
109 * register_interfaces
111 static HRESULT register_interfaces(struct regsvr_interface const *list)
113 LONG res = ERROR_SUCCESS;
114 HKEY interface_key;
116 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
117 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
118 if (res != ERROR_SUCCESS) goto error_return;
120 for (; res == ERROR_SUCCESS && list->iid; ++list) {
121 WCHAR buf[39];
122 HKEY iid_key;
124 StringFromGUID2(list->iid, buf, 39);
125 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
126 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
127 if (res != ERROR_SUCCESS) goto error_close_interface_key;
129 if (list->name) {
130 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
131 (CONST BYTE*)(list->name),
132 strlen(list->name) + 1);
133 if (res != ERROR_SUCCESS) goto error_close_iid_key;
136 if (list->base_iid) {
137 register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
138 if (res != ERROR_SUCCESS) goto error_close_iid_key;
141 if (0 <= list->num_methods) {
142 static WCHAR const fmt[3] = { '%', 'd', 0 };
143 HKEY key;
145 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
146 KEY_READ | KEY_WRITE, NULL, &key, NULL);
147 if (res != ERROR_SUCCESS) goto error_close_iid_key;
149 wsprintfW(buf, fmt, list->num_methods);
150 res = RegSetValueExW(key, NULL, 0, REG_SZ,
151 (CONST BYTE*)buf,
152 (lstrlenW(buf) + 1) * sizeof(WCHAR));
153 RegCloseKey(key);
155 if (res != ERROR_SUCCESS) goto error_close_iid_key;
158 if (list->ps_clsid) {
159 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
160 if (res != ERROR_SUCCESS) goto error_close_iid_key;
163 if (list->ps_clsid32) {
164 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
165 if (res != ERROR_SUCCESS) goto error_close_iid_key;
168 error_close_iid_key:
169 RegCloseKey(iid_key);
172 error_close_interface_key:
173 RegCloseKey(interface_key);
174 error_return:
175 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
178 /***********************************************************************
179 * unregister_interfaces
181 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
183 LONG res = ERROR_SUCCESS;
184 HKEY interface_key;
186 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
187 KEY_READ | KEY_WRITE, &interface_key);
188 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
189 if (res != ERROR_SUCCESS) goto error_return;
191 for (; res == ERROR_SUCCESS && list->iid; ++list) {
192 WCHAR buf[39];
193 HKEY iid_key;
195 StringFromGUID2(list->iid, buf, 39);
196 res = RegOpenKeyExW(interface_key, buf, 0,
197 KEY_READ | KEY_WRITE, &iid_key);
198 if (res == ERROR_FILE_NOT_FOUND) {
199 res = ERROR_SUCCESS;
200 continue;
202 if (res != ERROR_SUCCESS) goto error_close_interface_key;
203 res = recursive_delete_key(iid_key);
204 RegCloseKey(iid_key);
205 if (res != ERROR_SUCCESS) goto error_close_interface_key;
208 error_close_interface_key:
209 RegCloseKey(interface_key);
210 error_return:
211 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
214 /***********************************************************************
215 * register_coclasses
217 static HRESULT register_coclasses(struct regsvr_coclass const *list)
219 LONG res = ERROR_SUCCESS;
220 HKEY coclass_key;
222 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
223 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
224 if (res != ERROR_SUCCESS) goto error_return;
226 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
227 WCHAR buf[39];
228 HKEY clsid_key;
230 StringFromGUID2(list->clsid, buf, 39);
231 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
232 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
233 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
235 if (list->name) {
236 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
237 (CONST BYTE*)(list->name),
238 strlen(list->name) + 1);
239 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
242 if (list->ips) {
243 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
244 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
247 if (list->ips32) {
248 HKEY ips32_key;
250 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
251 KEY_READ | KEY_WRITE, NULL,
252 &ips32_key, NULL);
253 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
255 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
256 (CONST BYTE*)list->ips32,
257 lstrlenA(list->ips32) + 1);
258 if (res == ERROR_SUCCESS && list->ips32_tmodel)
259 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
260 (CONST BYTE*)list->ips32_tmodel,
261 strlen(list->ips32_tmodel) + 1);
262 RegCloseKey(ips32_key);
263 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
266 error_close_clsid_key:
267 RegCloseKey(clsid_key);
270 error_close_coclass_key:
271 RegCloseKey(coclass_key);
272 error_return:
273 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
276 /***********************************************************************
277 * unregister_coclasses
279 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
281 LONG res = ERROR_SUCCESS;
282 HKEY coclass_key;
284 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
285 KEY_READ | KEY_WRITE, &coclass_key);
286 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
287 if (res != ERROR_SUCCESS) goto error_return;
289 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
290 WCHAR buf[39];
291 HKEY clsid_key;
293 StringFromGUID2(list->clsid, buf, 39);
294 res = RegOpenKeyExW(coclass_key, buf, 0,
295 KEY_READ | KEY_WRITE, &clsid_key);
296 if (res == ERROR_FILE_NOT_FOUND) {
297 res = ERROR_SUCCESS;
298 continue;
300 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
301 res = recursive_delete_key(clsid_key);
302 RegCloseKey(clsid_key);
303 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
306 error_close_coclass_key:
307 RegCloseKey(coclass_key);
308 error_return:
309 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
312 /***********************************************************************
313 * regsvr_key_guid
315 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
317 WCHAR buf[39];
319 StringFromGUID2(guid, buf, 39);
320 return register_key_defvalueW(base, name, buf);
323 /***********************************************************************
324 * regsvr_key_defvalueW
326 static LONG register_key_defvalueW(
327 HKEY base,
328 WCHAR const *name,
329 WCHAR const *value)
331 LONG res;
332 HKEY key;
334 res = RegCreateKeyExW(base, name, 0, NULL, 0,
335 KEY_READ | KEY_WRITE, NULL, &key, NULL);
336 if (res != ERROR_SUCCESS) return res;
337 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
338 (lstrlenW(value) + 1) * sizeof(WCHAR));
339 RegCloseKey(key);
340 return res;
343 /***********************************************************************
344 * regsvr_key_defvalueA
346 static LONG register_key_defvalueA(
347 HKEY base,
348 WCHAR const *name,
349 char const *value)
351 LONG res;
352 HKEY key;
354 res = RegCreateKeyExW(base, name, 0, NULL, 0,
355 KEY_READ | KEY_WRITE, NULL, &key, NULL);
356 if (res != ERROR_SUCCESS) return res;
357 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
358 lstrlenA(value) + 1);
359 RegCloseKey(key);
360 return res;
363 /***********************************************************************
364 * recursive_delete_key
366 static LONG recursive_delete_key(HKEY key)
368 LONG res;
369 WCHAR subkey_name[MAX_PATH];
370 DWORD cName;
371 HKEY subkey;
373 for (;;) {
374 cName = sizeof(subkey_name) / sizeof(WCHAR);
375 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
376 NULL, NULL, NULL, NULL);
377 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
378 res = ERROR_SUCCESS; /* presumably we're done enumerating */
379 break;
381 res = RegOpenKeyExW(key, subkey_name, 0,
382 KEY_READ | KEY_WRITE, &subkey);
383 if (res == ERROR_FILE_NOT_FOUND) continue;
384 if (res != ERROR_SUCCESS) break;
386 res = recursive_delete_key(subkey);
387 RegCloseKey(subkey);
388 if (res != ERROR_SUCCESS) break;
391 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
392 return res;
395 /***********************************************************************
396 * coclass list
398 static struct regsvr_coclass const coclass_list[] = {
399 { &CLSID_StdComponentCategoriesMgr,
400 "StdComponentCategoriesMgr",
401 NULL,
402 "comcat.dll",
403 "Both"
405 { NULL } /* list terminator */
408 /***********************************************************************
409 * interface list
411 static GUID const CLSID_PSFactoryBuffer_actxprxy = {
412 0xB8DA6310, 0xE19B, 0x11D0, {0x93,0x3C,0x00,0xA0,0xC9,0x0D,0xCA,0xA9} };
414 static struct regsvr_interface const interface_list[] = {
415 { &IID_IEnumGUID,
416 "IEnumGUID",
417 NULL,
419 NULL,
420 &CLSID_PSFactoryBuffer_actxprxy
422 { &IID_IEnumCATEGORYINFO,
423 "IEnumCATEGORYINFO",
424 NULL,
426 NULL,
427 &CLSID_PSFactoryBuffer_actxprxy
429 { &IID_ICatRegister,
430 "ICatRegister",
431 NULL,
433 NULL,
434 &CLSID_PSFactoryBuffer_actxprxy
436 { &IID_ICatInformation,
437 "ICatInformation",
438 NULL,
440 NULL,
441 &CLSID_PSFactoryBuffer_actxprxy
443 { NULL } /* list terminator */
446 /***********************************************************************
447 * DllRegisterServer (COMCAT.@)
449 HRESULT WINAPI COMCAT_DllRegisterServer()
451 HRESULT hr;
453 TRACE("\n");
455 hr = register_coclasses(coclass_list);
456 if (SUCCEEDED(hr))
457 hr = register_interfaces(interface_list);
458 return hr;
461 /***********************************************************************
462 * DllUnregisterServer (COMCAT.@)
464 HRESULT WINAPI COMCAT_DllUnregisterServer()
466 HRESULT hr;
468 TRACE("\n");
470 hr = unregister_coclasses(coclass_list);
471 if (SUCCEEDED(hr))
472 hr = unregister_interfaces(interface_list);
473 return hr;