2 * self-registerable dll functions for shell32.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "shell32_main.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);
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 DWORD dwCallForAttributes;
69 LPCSTR clsid_str; /* can be NULL to omit */
70 LPCSTR progid; /* can be NULL to omit */
73 /* flags for regsvr_coclass.flags */
74 #define SHELLEX_MAYCHANGEDEFAULTMENU 0x00000001
75 #define SHELLFOLDER_WANTSFORPARSING 0x00000002
76 #define SHELLFOLDER_ATTRIBUTES 0x00000004
77 #define SHELLFOLDER_CALLFORATTRIBUTES 0x00000008
79 static HRESULT register_coclasses(struct regsvr_coclass const *list);
80 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
82 /***********************************************************************
83 * static string constants
85 static WCHAR const interface_keyname[10] = {
86 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
87 static WCHAR const base_ifa_keyname[14] = {
88 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
90 static WCHAR const num_methods_keyname[11] = {
91 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
92 static WCHAR const ps_clsid_keyname[15] = {
93 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
95 static WCHAR const ps_clsid32_keyname[17] = {
96 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
97 'i', 'd', '3', '2', 0 };
98 static WCHAR const clsid_keyname[6] = {
99 'C', 'L', 'S', 'I', 'D', 0 };
100 static WCHAR const ips_keyname[13] = {
101 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
103 static WCHAR const ips32_keyname[15] = {
104 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
106 static WCHAR const progid_keyname[7] = {
107 'P', 'r', 'o', 'g', 'I', 'D', 0 };
108 static WCHAR const shellex_keyname[8] = {
109 's', 'h', 'e', 'l', 'l', 'e', 'x', 0 };
110 static WCHAR const shellfolder_keyname[12] = {
111 'S', 'h', 'e', 'l', 'l', 'F', 'o', 'l', 'd', 'e', 'r', 0 };
112 static WCHAR const mcdm_keyname[21] = {
113 'M', 'a', 'y', 'C', 'h', 'a', 'n', 'g', 'e', 'D', 'e', 'f',
114 'a', 'u', 'l', 't', 'M', 'e', 'n', 'u', 0 };
115 static char const tmodel_valuename[] = "ThreadingModel";
116 static char const wfparsing_valuename[] = "WantsFORPARSING";
117 static char const attributes_valuename[] = "Attributes";
118 static char const cfattributes_valuename[] = "CallForAttributes";
120 /***********************************************************************
121 * static helper functions
123 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
124 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
126 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
128 static LONG recursive_delete_key(HKEY key);
129 static LONG recursive_delete_keyA(HKEY base, char const *name);
130 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
132 /***********************************************************************
133 * register_interfaces
135 static HRESULT register_interfaces(struct regsvr_interface const *list)
137 LONG res = ERROR_SUCCESS;
140 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
141 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
142 if (res != ERROR_SUCCESS) goto error_return;
144 for (; res == ERROR_SUCCESS && list->iid; ++list) {
148 StringFromGUID2(list->iid, buf, 39);
149 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
150 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
151 if (res != ERROR_SUCCESS) goto error_close_interface_key;
154 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
155 (CONST BYTE*)(list->name),
156 strlen(list->name) + 1);
157 if (res != ERROR_SUCCESS) goto error_close_iid_key;
160 if (list->base_iid) {
161 register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
165 if (0 <= list->num_methods) {
166 static WCHAR const fmt[3] = { '%', 'd', 0 };
169 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
170 KEY_READ | KEY_WRITE, NULL, &key, NULL);
171 if (res != ERROR_SUCCESS) goto error_close_iid_key;
173 wsprintfW(buf, fmt, list->num_methods);
174 res = RegSetValueExW(key, NULL, 0, REG_SZ,
176 (lstrlenW(buf) + 1) * sizeof(WCHAR));
179 if (res != ERROR_SUCCESS) goto error_close_iid_key;
182 if (list->ps_clsid) {
183 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
184 if (res != ERROR_SUCCESS) goto error_close_iid_key;
187 if (list->ps_clsid32) {
188 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
189 if (res != ERROR_SUCCESS) goto error_close_iid_key;
193 RegCloseKey(iid_key);
196 error_close_interface_key:
197 RegCloseKey(interface_key);
199 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
202 /***********************************************************************
203 * unregister_interfaces
205 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
207 LONG res = ERROR_SUCCESS;
210 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
211 KEY_READ | KEY_WRITE, &interface_key);
212 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
213 if (res != ERROR_SUCCESS) goto error_return;
215 for (; res == ERROR_SUCCESS && list->iid; ++list) {
218 StringFromGUID2(list->iid, buf, 39);
219 res = recursive_delete_keyW(interface_key, buf);
222 RegCloseKey(interface_key);
224 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
227 /***********************************************************************
230 static HRESULT register_coclasses(struct regsvr_coclass const *list)
232 LONG res = ERROR_SUCCESS;
235 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
236 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
237 if (res != ERROR_SUCCESS) goto error_return;
239 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
243 StringFromGUID2(list->clsid, buf, 39);
244 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
245 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
246 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
249 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
250 (CONST BYTE*)(list->name),
251 strlen(list->name) + 1);
252 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
256 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
257 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
263 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
264 KEY_READ | KEY_WRITE, NULL,
266 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
268 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
269 (CONST BYTE*)list->ips32,
270 lstrlenA(list->ips32) + 1);
271 if (res == ERROR_SUCCESS && list->ips32_tmodel)
272 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
273 (CONST BYTE*)list->ips32_tmodel,
274 strlen(list->ips32_tmodel) + 1);
275 RegCloseKey(ips32_key);
276 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
279 if (list->flags & SHELLEX_MAYCHANGEDEFAULTMENU) {
280 HKEY shellex_key, mcdm_key;
282 res = RegCreateKeyExW(clsid_key, shellex_keyname, 0, NULL, 0,
283 KEY_READ | KEY_WRITE, NULL,
285 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
286 res = RegCreateKeyExW(shellex_key, mcdm_keyname, 0, NULL, 0,
287 KEY_READ | KEY_WRITE, NULL,
289 RegCloseKey(shellex_key);
290 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
291 RegCloseKey(mcdm_key);
295 (SHELLFOLDER_WANTSFORPARSING|SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES))
297 HKEY shellfolder_key;
299 res = RegCreateKeyExW(clsid_key, shellfolder_keyname, 0, NULL, 0,
300 KEY_READ | KEY_WRITE, NULL,
301 &shellfolder_key, NULL);
302 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
303 if (list->flags & SHELLFOLDER_WANTSFORPARSING)
304 res = RegSetValueExA(shellfolder_key, wfparsing_valuename, 0, REG_SZ, "", 1);
305 if (list->flags & SHELLFOLDER_ATTRIBUTES)
306 res = RegSetValueExA(shellfolder_key, attributes_valuename, 0, REG_DWORD,
307 (LPBYTE)&list->dwAttributes, sizeof(DWORD));
308 if (list->flags & SHELLFOLDER_CALLFORATTRIBUTES)
309 res = RegSetValueExA(shellfolder_key, cfattributes_valuename, 0, REG_DWORD,
310 (LPBYTE)&list->dwCallForAttributes, sizeof(DWORD));
311 RegCloseKey(shellfolder_key);
312 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
315 if (list->clsid_str) {
316 res = register_key_defvalueA(clsid_key, clsid_keyname,
318 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
324 res = register_key_defvalueA(clsid_key, progid_keyname,
326 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
328 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
329 NULL, 0, KEY_READ | KEY_WRITE, NULL,
331 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
333 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
334 RegCloseKey(progid_key);
335 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
338 error_close_clsid_key:
339 RegCloseKey(clsid_key);
342 error_close_coclass_key:
343 RegCloseKey(coclass_key);
345 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
348 /***********************************************************************
349 * unregister_coclasses
351 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
353 LONG res = ERROR_SUCCESS;
356 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
357 KEY_READ | KEY_WRITE, &coclass_key);
358 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
359 if (res != ERROR_SUCCESS) goto error_return;
361 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
364 StringFromGUID2(list->clsid, buf, 39);
365 res = recursive_delete_keyW(coclass_key, buf);
366 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
369 res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
370 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
374 error_close_coclass_key:
375 RegCloseKey(coclass_key);
377 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
380 /***********************************************************************
383 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
387 StringFromGUID2(guid, buf, 39);
388 return register_key_defvalueW(base, name, buf);
391 /***********************************************************************
392 * regsvr_key_defvalueW
394 static LONG register_key_defvalueW(
402 res = RegCreateKeyExW(base, name, 0, NULL, 0,
403 KEY_READ | KEY_WRITE, NULL, &key, NULL);
404 if (res != ERROR_SUCCESS) return res;
405 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
406 (lstrlenW(value) + 1) * sizeof(WCHAR));
411 /***********************************************************************
412 * regsvr_key_defvalueA
414 static LONG register_key_defvalueA(
422 res = RegCreateKeyExW(base, name, 0, NULL, 0,
423 KEY_READ | KEY_WRITE, NULL, &key, NULL);
424 if (res != ERROR_SUCCESS) return res;
425 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
426 lstrlenA(value) + 1);
431 /***********************************************************************
432 * recursive_delete_key
434 static LONG recursive_delete_key(HKEY key)
437 WCHAR subkey_name[MAX_PATH];
442 cName = sizeof(subkey_name) / sizeof(WCHAR);
443 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
444 NULL, NULL, NULL, NULL);
445 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
446 res = ERROR_SUCCESS; /* presumably we're done enumerating */
449 res = RegOpenKeyExW(key, subkey_name, 0,
450 KEY_READ | KEY_WRITE, &subkey);
451 if (res == ERROR_FILE_NOT_FOUND) continue;
452 if (res != ERROR_SUCCESS) break;
454 res = recursive_delete_key(subkey);
456 if (res != ERROR_SUCCESS) break;
459 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
463 /***********************************************************************
464 * recursive_delete_keyA
466 static LONG recursive_delete_keyA(HKEY base, char const *name)
471 res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
472 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
473 if (res != ERROR_SUCCESS) return res;
474 res = recursive_delete_key(key);
479 /***********************************************************************
480 * recursive_delete_keyW
482 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
487 res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
488 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
489 if (res != ERROR_SUCCESS) return res;
490 res = recursive_delete_key(key);
495 /***********************************************************************
498 static GUID const CLSID_Desktop = {
499 0x00021400, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
501 static GUID const CLSID_Shortcut = {
502 0x00021401, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
504 static struct regsvr_coclass const coclass_list[] = {
511 { &CLSID_DragDropHelper,
512 "Shell Drag and Drop Helper",
528 SHELLEX_MAYCHANGEDEFAULTMENU
530 { &CLSID_AutoComplete,
541 SHELLFOLDER_WANTSFORPARSING
543 { &CLSID_UnixDosFolder,
548 SHELLFOLDER_WANTSFORPARSING|SHELLFOLDER_ATTRIBUTES|SHELLFOLDER_CALLFORATTRIBUTES,
549 SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER,
552 { NULL } /* list terminator */
555 /***********************************************************************
559 static struct regsvr_interface const interface_list[] = {
560 { NULL } /* list terminator */
563 /***********************************************************************
564 * DllRegisterServer (SHELL32.@)
566 HRESULT WINAPI SHELL32_DllRegisterServer()
572 hr = register_coclasses(coclass_list);
574 hr = register_interfaces(interface_list);
576 hr = SHELL_RegisterShellFolders();
580 /***********************************************************************
581 * DllUnregisterServer (SHELL32.@)
583 HRESULT WINAPI SHELL32_DllUnregisterServer()
589 hr = unregister_coclasses(coclass_list);
591 hr = unregister_interfaces(interface_list);