2 * self-registerable dll functions for urlmon.dll
4 * Copyright (C) 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "urlmon_main.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
34 * Near the bottom of this file are the exported DllRegisterServer and
35 * DllUnregisterServer, which make all this worthwhile.
38 /***********************************************************************
39 * interface for self-registering
41 struct regsvr_interface
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 */
51 static HRESULT register_interfaces(struct regsvr_interface const *list);
52 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
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 */
66 static HRESULT register_coclasses(struct regsvr_coclass const *list);
67 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
69 /***********************************************************************
70 * static string constants
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',
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',
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',
92 static WCHAR const ips32_keyname[15] = {
93 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
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',
101 static char const tmodel_valuename[] = "ThreadingModel";
103 /***********************************************************************
104 * static helper functions
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,
109 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
111 static LONG register_progid(WCHAR const *clsid,
112 char const *progid, char const *curver_progid,
113 char const *name, char const *extra);
115 /***********************************************************************
116 * register_interfaces
118 static HRESULT register_interfaces(struct regsvr_interface const *list)
120 LONG res = ERROR_SUCCESS;
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;
127 for (; res == ERROR_SUCCESS && list->iid; ++list) {
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;
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;
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;
148 if (0 <= list->num_methods) {
149 static WCHAR const fmt[3] = { '%', 'd', 0 };
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;
156 sprintfW(buf, fmt, list->num_methods);
157 res = RegSetValueExW(key, NULL, 0, REG_SZ,
159 (lstrlenW(buf) + 1) * sizeof(WCHAR));
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
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;
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;
176 RegCloseKey(iid_key);
179 error_close_interface_key:
180 RegCloseKey(interface_key);
182 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
185 /***********************************************************************
186 * unregister_interfaces
188 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
190 LONG res = ERROR_SUCCESS;
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;
198 for (; res == ERROR_SUCCESS && list->iid; ++list) {
201 StringFromGUID2(list->iid, buf, 39);
202 res = RegDeleteTreeW(interface_key, buf);
203 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
206 RegCloseKey(interface_key);
208 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
211 /***********************************************************************
214 static HRESULT register_coclasses(struct regsvr_coclass const *list)
216 LONG res = ERROR_SUCCESS;
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;
223 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
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;
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;
240 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
241 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
247 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
248 KEY_READ | KEY_WRITE, NULL,
250 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
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;
264 res = register_key_defvalueA(clsid_key, progid_keyname,
266 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
268 res = register_progid(buf, list->progid, NULL,
269 list->name, list->progid_extra);
270 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
273 if (list->viprogid) {
274 res = register_key_defvalueA(clsid_key, viprogid_keyname,
276 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
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;
283 error_close_clsid_key:
284 RegCloseKey(clsid_key);
287 error_close_coclass_key:
288 RegCloseKey(coclass_key);
290 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
293 /***********************************************************************
294 * unregister_coclasses
296 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
298 LONG res = ERROR_SUCCESS;
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;
306 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
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;
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;
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;
327 error_close_coclass_key:
328 RegCloseKey(coclass_key);
330 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
333 /***********************************************************************
336 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
340 StringFromGUID2(guid, buf, 39);
341 return register_key_defvalueW(base, name, buf);
344 /***********************************************************************
345 * regsvr_key_defvalueW
347 static LONG register_key_defvalueW(
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));
364 /***********************************************************************
365 * regsvr_key_defvalueA
367 static LONG register_key_defvalueA(
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);
384 /***********************************************************************
387 static LONG register_progid(
390 char const *curver_progid,
397 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
398 NULL, 0, KEY_READ | KEY_WRITE, NULL,
400 if (res != ERROR_SUCCESS) return res;
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;
409 res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
410 if (res != ERROR_SUCCESS) goto error_close_progid_key;
414 res = register_key_defvalueA(progid_key, curver_keyname,
416 if (res != ERROR_SUCCESS) goto error_close_progid_key;
422 res = RegCreateKeyExA(progid_key, extra, 0,
423 NULL, 0, KEY_READ | KEY_WRITE, NULL,
425 if (res == ERROR_SUCCESS)
426 RegCloseKey(extra_key);
429 error_close_progid_key:
430 RegCloseKey(progid_key);
434 /***********************************************************************
437 static struct regsvr_coclass const coclass_list[] = {
438 { &CLSID_StdURLMoniker,
444 { &CLSID_CdlProtocol,
445 "CDL: Asynchronous Pluggable Protocol Handler",
450 { &CLSID_FileProtocol,
451 "file:, local: Asynchronous Pluggable Protocol Handler",
456 { &CLSID_FtpProtocol,
457 "ftp: Asynchronous Pluggable Protocol Handler",
462 { &CLSID_GopherProtocol,
463 "gopher: Asynchronous Pluggable Protocol Handler",
468 { &CLSID_HttpProtocol,
469 "http: Asynchronous Pluggable Protocol Handler",
474 { &CLSID_HttpSProtocol,
475 "https: Asynchronous Pluggable Protocol Handler",
481 "mk: Asynchronous Pluggable Protocol Handler",
486 { &CLSID_InternetSecurityManager,
492 { &CLSID_InternetZoneManager,
498 { NULL } /* list terminator */
501 /***********************************************************************
505 static struct regsvr_interface const interface_list[] = {
506 { NULL } /* list terminator */
509 /***********************************************************************
513 #define INF_SET_CLSID(clsid) \
516 static CHAR name[] = "CLSID_" #clsid; \
518 pse[i].pszName = name; \
519 clsids[i++] = &CLSID_ ## clsid; \
522 static HRESULT register_inf(BOOL doregister)
526 HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
529 static CLSID const *clsids[34];
532 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
534 INF_SET_CLSID(CdlProtocol);
535 INF_SET_CLSID(FileProtocol);
536 INF_SET_CLSID(FtpProtocol);
537 INF_SET_CLSID(GopherProtocol);
538 INF_SET_CLSID(HttpProtocol);
539 INF_SET_CLSID(HttpSProtocol);
540 INF_SET_CLSID(MkProtocol);
542 for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
543 pse[i].pszValue = heap_alloc(39);
544 sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
545 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
546 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
547 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
550 strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
553 hAdvpack = LoadLibraryW(wszAdvpack);
554 pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
556 hres = pRegInstall(URLMON_hInstance, doregister ? "RegisterDll" : "UnregisterDll", &strtable);
558 for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
559 heap_free(pse[i].pszValue);
566 /***********************************************************************
567 * DllRegisterServer (URLMON.@)
569 HRESULT WINAPI DllRegisterServer(void)
575 hr = register_coclasses(coclass_list);
577 hr = register_interfaces(interface_list);
580 return register_inf(TRUE);
583 /***********************************************************************
584 * DllUnregisterServer (URLMON.@)
586 HRESULT WINAPI DllUnregisterServer(void)
592 hr = unregister_coclasses(coclass_list);
594 hr = unregister_interfaces(interface_list);
597 return register_inf(FALSE);