Sync with trunk head (part 1 or 2)
[reactos.git] / dll / win32 / urlmon / regsvr.c
1 /*
2 * self-registerable dll functions for urlmon.dll
3 *
4 * Copyright (C) 2003 John K. Hohm
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdio.h>
22
23 #include "urlmon_main.h"
24
25 #include "winreg.h"
26 #include "advpub.h"
27
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
32
33 /*
34 * Near the bottom of this file are the exported DllRegisterServer and
35 * DllUnregisterServer, which make all this worthwhile.
36 */
37
38 /***********************************************************************
39 * interface for self-registering
40 */
41 struct regsvr_interface
42 {
43 IID const *iid; /* NULL for end of list */
44 LPCSTR name; /* can be NULL to omit */
45 IID const *base_iid; /* can be NULL to omit */
46 int num_methods; /* can be <0 to omit */
47 CLSID const *ps_clsid; /* can be NULL to omit */
48 CLSID const *ps_clsid32; /* can be NULL to omit */
49 };
50
51 static HRESULT register_interfaces(struct regsvr_interface const *list);
52 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
53
54 struct regsvr_coclass
55 {
56 CLSID const *clsid; /* NULL for end of list */
57 LPCSTR name; /* can be NULL to omit */
58 LPCSTR ips; /* can be NULL to omit */
59 LPCSTR ips32; /* can be NULL to omit */
60 LPCSTR ips32_tmodel; /* can be NULL to omit */
61 LPCSTR progid; /* can be NULL to omit */
62 LPCSTR viprogid; /* can be NULL to omit */
63 LPCSTR progid_extra; /* can be NULL to omit */
64 };
65
66 static HRESULT register_coclasses(struct regsvr_coclass const *list);
67 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
68
69 /***********************************************************************
70 * static string constants
71 */
72 static WCHAR const interface_keyname[10] = {
73 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
74 static WCHAR const base_ifa_keyname[14] = {
75 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
76 'e', 0 };
77 static WCHAR const num_methods_keyname[11] = {
78 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
79 static WCHAR const ps_clsid_keyname[15] = {
80 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
81 'i', 'd', 0 };
82 static WCHAR const ps_clsid32_keyname[17] = {
83 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
84 'i', 'd', '3', '2', 0 };
85 static WCHAR const clsid_keyname[6] = {
86 'C', 'L', 'S', 'I', 'D', 0 };
87 static WCHAR const curver_keyname[7] = {
88 'C', 'u', 'r', 'V', 'e', 'r', 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 WCHAR const progid_keyname[7] = {
96 'P', 'r', 'o', 'g', 'I', 'D', 0 };
97 static WCHAR const viprogid_keyname[25] = {
98 'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
99 'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
100 0 };
101 static char const tmodel_valuename[] = "ThreadingModel";
102
103 /***********************************************************************
104 * static helper functions
105 */
106 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
107 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
108 WCHAR const *value);
109 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
110 char const *value);
111 static LONG register_progid(WCHAR const *clsid,
112 char const *progid, char const *curver_progid,
113 char const *name, char const *extra);
114
115 /***********************************************************************
116 * register_interfaces
117 */
118 static HRESULT register_interfaces(struct regsvr_interface const *list)
119 {
120 LONG res = ERROR_SUCCESS;
121 HKEY interface_key;
122
123 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
124 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
125 if (res != ERROR_SUCCESS) goto error_return;
126
127 for (; res == ERROR_SUCCESS && list->iid; ++list) {
128 WCHAR buf[39];
129 HKEY iid_key;
130
131 StringFromGUID2(list->iid, buf, 39);
132 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
133 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
134 if (res != ERROR_SUCCESS) goto error_close_interface_key;
135
136 if (list->name) {
137 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
138 (CONST BYTE*)(list->name),
139 strlen(list->name) + 1);
140 if (res != ERROR_SUCCESS) goto error_close_iid_key;
141 }
142
143 if (list->base_iid) {
144 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
145 if (res != ERROR_SUCCESS) goto error_close_iid_key;
146 }
147
148 if (0 <= list->num_methods) {
149 static WCHAR const fmt[3] = { '%', 'd', 0 };
150 HKEY key;
151
152 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
153 KEY_READ | KEY_WRITE, NULL, &key, NULL);
154 if (res != ERROR_SUCCESS) goto error_close_iid_key;
155
156 sprintfW(buf, fmt, list->num_methods);
157 res = RegSetValueExW(key, NULL, 0, REG_SZ,
158 (CONST BYTE*)buf,
159 (lstrlenW(buf) + 1) * sizeof(WCHAR));
160 RegCloseKey(key);
161
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
163 }
164
165 if (list->ps_clsid) {
166 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
167 if (res != ERROR_SUCCESS) goto error_close_iid_key;
168 }
169
170 if (list->ps_clsid32) {
171 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
172 if (res != ERROR_SUCCESS) goto error_close_iid_key;
173 }
174
175 error_close_iid_key:
176 RegCloseKey(iid_key);
177 }
178
179 error_close_interface_key:
180 RegCloseKey(interface_key);
181 error_return:
182 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
183 }
184
185 /***********************************************************************
186 * unregister_interfaces
187 */
188 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
189 {
190 LONG res = ERROR_SUCCESS;
191 HKEY interface_key;
192
193 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
194 KEY_READ | KEY_WRITE, &interface_key);
195 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
196 if (res != ERROR_SUCCESS) goto error_return;
197
198 for (; res == ERROR_SUCCESS && list->iid; ++list) {
199 WCHAR buf[39];
200
201 StringFromGUID2(list->iid, buf, 39);
202 res = RegDeleteTreeW(interface_key, buf);
203 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
204 }
205
206 RegCloseKey(interface_key);
207 error_return:
208 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
209 }
210
211 /***********************************************************************
212 * register_coclasses
213 */
214 static HRESULT register_coclasses(struct regsvr_coclass const *list)
215 {
216 LONG res = ERROR_SUCCESS;
217 HKEY coclass_key;
218
219 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
220 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
221 if (res != ERROR_SUCCESS) goto error_return;
222
223 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
224 WCHAR buf[39];
225 HKEY clsid_key;
226
227 StringFromGUID2(list->clsid, buf, 39);
228 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
229 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
230 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
231
232 if (list->name) {
233 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
234 (CONST BYTE*)(list->name),
235 strlen(list->name) + 1);
236 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
237 }
238
239 if (list->ips) {
240 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
241 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
242 }
243
244 if (list->ips32) {
245 HKEY ips32_key;
246
247 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
248 KEY_READ | KEY_WRITE, NULL,
249 &ips32_key, NULL);
250 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
251
252 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
253 (CONST BYTE*)list->ips32,
254 lstrlenA(list->ips32) + 1);
255 if (res == ERROR_SUCCESS && list->ips32_tmodel)
256 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
257 (CONST BYTE*)list->ips32_tmodel,
258 strlen(list->ips32_tmodel) + 1);
259 RegCloseKey(ips32_key);
260 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
261 }
262
263 if (list->progid) {
264 res = register_key_defvalueA(clsid_key, progid_keyname,
265 list->progid);
266 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
267
268 res = register_progid(buf, list->progid, NULL,
269 list->name, list->progid_extra);
270 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
271 }
272
273 if (list->viprogid) {
274 res = register_key_defvalueA(clsid_key, viprogid_keyname,
275 list->viprogid);
276 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
277
278 res = register_progid(buf, list->viprogid, list->progid,
279 list->name, list->progid_extra);
280 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
281 }
282
283 error_close_clsid_key:
284 RegCloseKey(clsid_key);
285 }
286
287 error_close_coclass_key:
288 RegCloseKey(coclass_key);
289 error_return:
290 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
291 }
292
293 /***********************************************************************
294 * unregister_coclasses
295 */
296 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
297 {
298 LONG res = ERROR_SUCCESS;
299 HKEY coclass_key;
300
301 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
302 KEY_READ | KEY_WRITE, &coclass_key);
303 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
304 if (res != ERROR_SUCCESS) goto error_return;
305
306 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
307 WCHAR buf[39];
308
309 StringFromGUID2(list->clsid, buf, 39);
310 res = RegDeleteTreeW(coclass_key, buf);
311 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
312 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
313
314 if (list->progid) {
315 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
316 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
317 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
318 }
319
320 if (list->viprogid) {
321 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
322 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
323 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
324 }
325 }
326
327 error_close_coclass_key:
328 RegCloseKey(coclass_key);
329 error_return:
330 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
331 }
332
333 /***********************************************************************
334 * regsvr_key_guid
335 */
336 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
337 {
338 WCHAR buf[39];
339
340 StringFromGUID2(guid, buf, 39);
341 return register_key_defvalueW(base, name, buf);
342 }
343
344 /***********************************************************************
345 * regsvr_key_defvalueW
346 */
347 static LONG register_key_defvalueW(
348 HKEY base,
349 WCHAR const *name,
350 WCHAR const *value)
351 {
352 LONG res;
353 HKEY key;
354
355 res = RegCreateKeyExW(base, name, 0, NULL, 0,
356 KEY_READ | KEY_WRITE, NULL, &key, NULL);
357 if (res != ERROR_SUCCESS) return res;
358 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
359 (lstrlenW(value) + 1) * sizeof(WCHAR));
360 RegCloseKey(key);
361 return res;
362 }
363
364 /***********************************************************************
365 * regsvr_key_defvalueA
366 */
367 static LONG register_key_defvalueA(
368 HKEY base,
369 WCHAR const *name,
370 char const *value)
371 {
372 LONG res;
373 HKEY key;
374
375 res = RegCreateKeyExW(base, name, 0, NULL, 0,
376 KEY_READ | KEY_WRITE, NULL, &key, NULL);
377 if (res != ERROR_SUCCESS) return res;
378 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
379 lstrlenA(value) + 1);
380 RegCloseKey(key);
381 return res;
382 }
383
384 /***********************************************************************
385 * regsvr_progid
386 */
387 static LONG register_progid(
388 WCHAR const *clsid,
389 char const *progid,
390 char const *curver_progid,
391 char const *name,
392 char const *extra)
393 {
394 LONG res;
395 HKEY progid_key;
396
397 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
398 NULL, 0, KEY_READ | KEY_WRITE, NULL,
399 &progid_key, NULL);
400 if (res != ERROR_SUCCESS) return res;
401
402 if (name) {
403 res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
404 (CONST BYTE*)name, strlen(name) + 1);
405 if (res != ERROR_SUCCESS) goto error_close_progid_key;
406 }
407
408 if (clsid) {
409 res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
410 if (res != ERROR_SUCCESS) goto error_close_progid_key;
411 }
412
413 if (curver_progid) {
414 res = register_key_defvalueA(progid_key, curver_keyname,
415 curver_progid);
416 if (res != ERROR_SUCCESS) goto error_close_progid_key;
417 }
418
419 if (extra) {
420 HKEY extra_key;
421
422 res = RegCreateKeyExA(progid_key, extra, 0,
423 NULL, 0, KEY_READ | KEY_WRITE, NULL,
424 &extra_key, NULL);
425 if (res == ERROR_SUCCESS)
426 RegCloseKey(extra_key);
427 }
428
429 error_close_progid_key:
430 RegCloseKey(progid_key);
431 return res;
432 }
433
434 /***********************************************************************
435 * coclass list
436 */
437 static struct regsvr_coclass const coclass_list[] = {
438 { &CLSID_StdURLMoniker,
439 "URL Moniker",
440 NULL,
441 "urlmon.dll",
442 "Apartment"
443 },
444 { &CLSID_CdlProtocol,
445 "CDL: Asynchronous Pluggable Protocol Handler",
446 NULL,
447 "urlmon.dll",
448 "Apartment"
449 },
450 { &CLSID_FileProtocol,
451 "file:, local: Asynchronous Pluggable Protocol Handler",
452 NULL,
453 "urlmon.dll",
454 "Apartment"
455 },
456 { &CLSID_FtpProtocol,
457 "ftp: Asynchronous Pluggable Protocol Handler",
458 NULL,
459 "urlmon.dll",
460 "Apartment"
461 },
462 { &CLSID_GopherProtocol,
463 "gopher: Asynchronous Pluggable Protocol Handler",
464 NULL,
465 "urlmon.dll",
466 "Apartment"
467 },
468 { &CLSID_HttpProtocol,
469 "http: Asynchronous Pluggable Protocol Handler",
470 NULL,
471 "urlmon.dll",
472 "Apartment"
473 },
474 { &CLSID_HttpSProtocol,
475 "https: Asynchronous Pluggable Protocol Handler",
476 NULL,
477 "urlmon.dll",
478 "Apartment"
479 },
480 { &CLSID_MkProtocol,
481 "mk: Asynchronous Pluggable Protocol Handler",
482 NULL,
483 "urlmon.dll",
484 "Apartment"
485 },
486 { &CLSID_InternetSecurityManager,
487 "Security Manager",
488 NULL,
489 "urlmon.dll",
490 "Both"
491 },
492 { &CLSID_InternetZoneManager,
493 "URL Zone Manager",
494 NULL,
495 "urlmon.dll",
496 "Both"
497 },
498 { &CLSID_PSFactoryBuffer,
499 "URLMoniker ProxyStub Factory",
500 NULL,
501 "urlmon.dll",
502 "Apartment"
503 },
504 { NULL } /* list terminator */
505 };
506
507 /***********************************************************************
508 * interface list
509 */
510
511 static struct regsvr_interface const interface_list[] = {
512 { NULL } /* list terminator */
513 };
514
515 /***********************************************************************
516 * register_inf
517 */
518
519 #define INF_SET_CLSID(clsid) \
520 do \
521 { \
522 static CHAR name[] = #clsid; \
523 \
524 pse[i].pszName = name; \
525 clsids[i++] = &clsid; \
526 } while (0)
527
528 static HRESULT register_inf(BOOL doregister)
529 {
530 HRESULT hres;
531 HMODULE hAdvpack;
532 HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
533 STRTABLEA strtable;
534 STRENTRYA pse[8];
535 static CLSID const *clsids[8];
536 unsigned int i = 0;
537
538 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
539
540 INF_SET_CLSID(CLSID_CdlProtocol);
541 INF_SET_CLSID(CLSID_FileProtocol);
542 INF_SET_CLSID(CLSID_FtpProtocol);
543 INF_SET_CLSID(CLSID_GopherProtocol);
544 INF_SET_CLSID(CLSID_HttpProtocol);
545 INF_SET_CLSID(CLSID_HttpSProtocol);
546 INF_SET_CLSID(CLSID_MkProtocol);
547 INF_SET_CLSID(CLSID_DeCompMimeFilter);
548
549 for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
550 pse[i].pszValue = heap_alloc(39);
551 sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
552 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
553 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
554 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
555 }
556
557 strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
558 strtable.pse = pse;
559
560 hAdvpack = LoadLibraryW(wszAdvpack);
561 pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
562
563 hres = pRegInstall(URLMON_hInstance, doregister ? "RegisterDll" : "UnregisterDll", &strtable);
564
565 for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
566 heap_free(pse[i].pszValue);
567
568 return hres;
569 }
570
571 #undef INF_SET_CLSID
572
573 /***********************************************************************
574 * DllRegisterServer (URLMON.@)
575 */
576 HRESULT WINAPI DllRegisterServer(void)
577 {
578 HRESULT hr;
579
580 TRACE("\n");
581
582 hr = URLMON_DllRegisterServer();
583 if(SUCCEEDED(hr))
584 hr = register_coclasses(coclass_list);
585 if(SUCCEEDED(hr))
586 hr = register_interfaces(interface_list);
587 if(SUCCEEDED(hr))
588 hr = register_inf(TRUE);
589 return hr;
590 }
591
592 /***********************************************************************
593 * DllUnregisterServer (URLMON.@)
594 */
595 HRESULT WINAPI DllUnregisterServer(void)
596 {
597 HRESULT hr;
598
599 TRACE("\n");
600
601 hr = URLMON_DllUnregisterServer();
602 if(SUCCEEDED(hr))
603 hr = unregister_coclasses(coclass_list);
604 if(SUCCEEDED(hr))
605 hr = unregister_interfaces(interface_list);
606 if(SUCCEEDED(hr))
607 hr = register_inf(FALSE);
608 return hr;
609 }